Search code examples
pythonbotstradingbinance

MFI indicator from talib python library doesn`t return any value


def on_message(ws, message):
    global in_position
    global closes
    json_message = json.loads(message)

    candle = json_message['k']
    is_candle_closed = candle['x']
    close = candle['c']
    high = candle['h']
    low = candle['l']
    volume = candle['V']

    if is_candle_closed:
        print("Candle closed at {}".format(close))
        highs.append(float(high))
        lows.append(float(low))
        closes.append(float(close))
        volumes.append(float(volume))
        print(':')

        if len(closes) > MFI_PERIOD:
            np_closes = numpy.array(closes)
            mfi = talib.MFI(highs, lows, np_closes, volumes, MFI_PERIOD)
            last_mfi = mfi[-1]
            print('The current (MIDPOINT) is {}'.format(last_mfi))

-im using binance websocket stream to get kline data -im trying to get data or values from MFI(money flow index) to automate some trades +i tried other indicators such as RSI and it worked

  • but i dont know why this one isnt

Solution

  • it's a simple solution, you have to convert the values to numpy number to use as parameter for talib.MFI

    ...

     if len(closes) > MFI_PERIOD:
            np_closes = numpy.array(closes)
            np_highs = numpy.array(highs)
            np_lows = numpy.array(lows)
            np_volumes = numpy.array(volumes)
            mfi = talib.MFI(np_highs, np_lows, np_closes, np_volumes, MFI_PERIOD)
            last_mfi = mfi[-1]
            print('The current (MIDPOINT) is {}'.format(last_mfi))
    

    I'm work on the same project and it works for me.