freqtrade / technical

Various indicators developed or collected for the Freqtrade
GNU General Public License v3.0
758 stars 218 forks source link

MOST indicator #90

Closed tarantula3535 closed 3 years ago

tarantula3535 commented 4 years ago

i am work to write this indicator....
https://tr.tradingview.com/v/28ZcW4nv/ MOST by Anıl ÖZEKŞİ actually good filtering buy or sell ... her is the code... my problem is so slow the backtesting. where is the wrong or why my codes are so slow in the backtesting?

def MOST(dataframe , percent, length):
    import talib.abstract as ta
    emacol = 'ema_' + str(length) + '_' + str(percent)
    mostcol = 'most_' + str(length) + '_' + str(percent)
    dataframe[emacol] = ta.EMA( dataframe , timeperiod = length)
    dataframe[emacol] = dataframe[emacol].fillna(method="bfill")
    dataframe['tempema'] = 0.000000
    dataframe['trend'] = -1
    dataframe[mostcol] = 0.000000
    for i in range(1,len(dataframe[emacol])):
        if (dataframe['trend'][i-1] == 1) & (dataframe['tempema'][i-1] >= dataframe[emacol][i]):
            dataframe.loc[i,'tempema'] = dataframe.loc[i-1,'tempema']
        elif (dataframe['trend'][i-1] == 1) & (dataframe['tempema'][i-1] < dataframe[emacol][i]):
            dataframe.loc[i,'tempema'] = dataframe.loc[i,emacol]
        elif (dataframe['trend'][i-1] == -1) & (dataframe['tempema'][i-1] <= dataframe[emacol][i]):
            dataframe.loc[i,'tempema'] = dataframe.loc[i-1,'tempema']
        elif (dataframe['trend'][i-1] == -1) & (dataframe['tempema'][i-1] > dataframe[emacol][i]):
            dataframe.loc[i,'tempema'] = dataframe.loc[i,emacol]

        if (dataframe[emacol][i] >= dataframe[mostcol][i-1]) & (dataframe['trend'][i-1] == 1):
            dataframe.loc[i,'trend'] = 1
            dataframe.loc[i,mostcol] = dataframe['tempema'][i]*(1-percent)
        elif (dataframe[emacol][i] <= dataframe[mostcol][i-1]) & (dataframe['trend'][i-1] == -1):
            dataframe.loc[i,'trend'] = -1
            dataframe.loc[i,mostcol] = dataframe['tempema'][i]*(1+percent)
        elif (dataframe[emacol][i] >= dataframe[mostcol][i-1]) & (dataframe['trend'][i-1] == -1):
            dataframe.loc[i,'trend'] = 1
            dataframe.loc[i,mostcol] = dataframe['tempema'][i]*(1-percent)
        elif (dataframe[emacol][i] <= dataframe[mostcol][i-1]) & (dataframe['trend'][i-1] == 1):
            dataframe.loc[i,'trend'] = -1
            dataframe.loc[i,mostcol] = dataframe['tempema'][i]*(1+percent)
    return {
        emacol : dataframe[emacol] ,
        mostcol : dataframe[mostcol]
    }
tarantula3535 commented 4 years ago

i find a bug in this code and recalculate. now same result at the tradingview... and add MAtype

def MOST2(dataframe,  percent=2, length=8, MAtype=1):
    import talib.abstract as ta
    df = dataframe.copy()
    mostvalue = 'MOST_' + str(length) + '_' + str(percent)
    mavalue = 'MA_' + str(length) + '_' + str(percent)
    trend = 'Trend_' + str(length) + '_' + str(percent)
    # Compute basic upper and lower bands
    if MAtype==1:
        df[mavalue]=ta.EMA(df , timeperiod = length)
    elif MAtype==2:
        df[mavalue]=ta.DEMA(df , timeperiod = length)
    elif MAtype==3:
        df[mavalue]=ta.T3(df , timeperiod = length)
    df['basic_ub'] = df[mavalue] * (1+percent/100)
    df['basic_lb'] = df[mavalue] * (1-percent/100)
    # Compute final upper and lower bands
    df['final_ub'] = 0.00
    df['final_lb'] = 0.00
    for i in range(length, len(df)):
        df['final_ub'].iat[i] = df['basic_ub'].iat[i] if df['basic_ub'].iat[i] < df['final_ub'].iat[i - 1] or df[mavalue].iat[i - 1] > df['final_ub'].iat[i - 1] else df['final_ub'].iat[i - 1]
        df['final_lb'].iat[i] = df['basic_lb'].iat[i] if df['basic_lb'].iat[i] > df['final_lb'].iat[i - 1] or df[mavalue].iat[i - 1] < df['final_lb'].iat[i - 1] else df['final_lb'].iat[i - 1]
    # Set the MOST value
    df[mostvalue] = 0.00
    for i in range(length, len(df)):
        df[mostvalue].iat[i] = df['final_ub'].iat[i] if df[mostvalue].iat[i - 1] == df['final_ub'].iat[i - 1] and df[mavalue].iat[i] <= df['final_ub'].iat[i] else \
                        df['final_lb'].iat[i] if df[mostvalue].iat[i - 1] == df['final_ub'].iat[i - 1] and df[mavalue].iat[i] >  df['final_ub'].iat[i] else \
                        df['final_lb'].iat[i] if df[mostvalue].iat[i - 1] == df['final_lb'].iat[i - 1] and df[mavalue].iat[i] >= df['final_lb'].iat[i] else \
                        df['final_ub'].iat[i] if df[mostvalue].iat[i - 1] == df['final_lb'].iat[i - 1] and df[mavalue].iat[i] <  df['final_lb'].iat[i] else 0.00
    # Mark the trend direction up/down
    df[trend] = np.where((df[mostvalue] > 0.00), np.where((df[mavalue] < df[mostvalue]), 'down',  'up'), np.NaN)
    # Remove basic and final bands from the columns
    df.drop(['basic_ub', 'basic_lb', 'final_ub', 'final_lb'], inplace=True, axis=1)
    df.fillna(0, inplace=True)
    return df
hroff-1902 commented 4 years ago

is it explained anywhere else, beside that page at tradingview?

tarantula3535 commented 4 years ago

yes.. http://teknikanalizsanati.com/pagesorgulama.aspx?bolum=MOST&sayfano=3&kategoriSira=84

hroff-1902 commented 4 years ago

The text there is placed there as a picture, untranslatable with an online translator, to get the description in English...

The code in tradingview is closed non-public, are you sure you do not violate copyright of the author of it rewriting it in python?

tarantula3535 commented 4 years ago

yes i write publisher Anil Özekşi on twitter.. only with the condition that we publish the indicator name is "MOST"

hroff-1902 commented 4 years ago

For the sake of analogy, in Wikipedia terms, I would say it falls into the "original research" (https://en.wikipedia.org/wiki/Wikipedia:No_original_research) category -- an indicator which is only described in one (or a few) article by its creator and not used anywhere else...

equinox794 commented 3 years ago

Traceback (most recent call last): line xx, in MOST2 df = dataframe.copy() AttributeError: 'float' object has no attribute 'copy'

xmatthias commented 3 years ago

well you're not calling the indicator correctly. it should be MOST2(dataframe, <parameters>) - and it seems like you're missing the dataframe parameter.

equinox794 commented 3 years ago

thanks for answer


klines = client.get_klines(symbol=symbol, interval=Client.KLINE_INTERVAL_5MINUTE)
klines = df(klines)
test = MOST2(klines,  percent=2, length=8, MAtype=1)
print(test)

Traceback (most recent call last): File "most2.py", line 64, in test = MOST2(klines, percent=2, length=8, MAtype=1) File "d:\p\most2.py", line 35, in MOST2 df[mavalue]=ta.EMA(df , timeperiod = length) File "_abstract.pxi", line 398, in talib._ta_lib.Function.call File "_abstract.pxi", line 270, in talib._ta_lib.Function.set_function_args File "_abstract.pxi", line 221, in talib._ta_lib.Function.set_input_arrays Exception: input_arrays parameter missing required data key: close

xmatthias commented 3 years ago

The whole technical library is intended to be used with freqtrade - or with a dataframe formatted as such. which means, the following keys are requried: ["date", "open", "high", "low", "close"].

Not all indicators will use all columns - but to be on the safe side, best have all of them available.