├── .gitattributes ├── emaSlope.py └── theWork.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /emaSlope.py: -------------------------------------------------------------------------------- 1 | import vectorbt as vbt 2 | import pandas as pd 3 | import numpy as np 4 | from datetime import datetime 5 | from numba import njit 6 | import talib 7 | 8 | 9 | data = vbt.BinanceData.download( 10 | ['BTCUSDT'], 11 | start='2022-03-01', 12 | end='2022-03-21', 13 | interval='1h' 14 | ) 15 | 16 | high = data.get('High') 17 | low = data.get('Low') 18 | close = data.get('Close') 19 | 20 | #average = input.string(title="Source MA Type", defval="EMA",options=["EMA","SMA"]) 21 | len = 27 # title="Source MA Length") 22 | slopeFlen = 5 23 | slopeSlen= 10 24 | 25 | trendfilter=True # title="Trend Filter") 26 | trendfilterperiod= 200 # title="Trend Filter MA Period") 27 | #trendfiltertype=input.string(title="Trend Filter MA Type", defval="EMA",options=["EMA","SMA"]) 28 | 29 | volatilityfilter=False # title="Volatility Filter") 30 | volatilitystdevlength= 20 # title="Vol Filter STDev Length") 31 | volatilitystdevmalength= 30 #title="Vol Filter STDev MA Length") 32 | 33 | # //variabili 34 | 35 | # if average == "EMA": 36 | def emaSlope(high, low, close, len = 27, 37 | slopeFlen = 5, 38 | slopeSlen= 10, 39 | trendfilter=True, 40 | trendfilterperiod= 200, 41 | volatilityfilter=False, 42 | volatilitystdevlength= 20, 43 | volatilitystdevmalength= 30): 44 | 45 | out = vbt.MA.run(close,len, ewm=True) 46 | out = out.ma 47 | 48 | out.columns.names = (None,None) 49 | # else: 50 | # out = vbt.MA.run(close,len) 51 | 52 | slp = out.shift()/out 53 | 54 | emaslopeF = vbt.MA.run(slp,slopeFlen, ewm=True) 55 | emaslopeS = vbt.MA.run(slp,slopeSlen, ewm=True) 56 | 57 | TrendConditionL = close>(vbt.MA.run(close,trendfilterperiod, ewm=True)).ma 58 | 59 | TrendConditionS = close<(vbt.MA.run(close,trendfilterperiod, ewm=True)).ma 60 | 61 | # VolatilityCondition=ta.stdev(close,volatilitystdevlength)>ta.sma(ta.stdev(close,volatilitystdevlength),volatilitystdevmalength) 62 | VolatilityCondition = talib.STDDEV(close.flatten(),volatilitystdevlength)>talib.SMA(talib.STDDEV(close.flatten(),volatilitystdevlength),volatilitystdevmalength) 63 | 64 | 65 | if trendfilter == True: 66 | if volatilityfilter == True: 67 | ConditionEntryL= (np.array(emaslopeF.ma.values.tolist()))>np.array(emaslopeS.ma.values.tolist()).tolist() and np.array(TrendConditionL.values.tolist()).tolist() and np.array(VolatilityCondition.values.tolist()).tolist() 68 | else: 69 | ConditionEntryL= (np.array(emaslopeF.ma.values.tolist())>np.array(emaslopeS.ma.values.tolist())).tolist() and np.array(TrendConditionL.values.tolist()) 70 | else: 71 | if volatilityfilter == True: 72 | ConditionEntryL= (np.array(emaslopeF.ma.values.tolist())>np.array(emaslopeS.ma.values.tolist())).tolist() and np.array(VolatilityCondition.values.tolist()).tolist() 73 | else: 74 | ConditionEntryL= np.array(emaslopeF.ma.values.tolist())>np.array(emaslopeS.ma.values.tolist()) 75 | 76 | # Condizioni di entrata short 77 | if trendfilter == True: 78 | if volatilityfilter == True: 79 | ConditionEntryS= (np.array(emaslopeF.ma.values.tolist()) upper[i - 1]: 36 | dir_[i] = 1 37 | elif close[i] < lower[i - 1]: 38 | dir_[i] = -1 39 | else: 40 | dir_[i] = dir_[i - 1] 41 | if dir_[i] > 0 and lower[i] < lower[i - 1]: 42 | lower[i] = lower[i - 1] 43 | if dir_[i] < 0 and upper[i] > upper[i - 1]: 44 | upper[i] = upper[i - 1] 45 | 46 | if dir_[i] > 0: 47 | trend[i] = long[i] = lower[i] 48 | else: 49 | trend[i] = short[i] = upper[i] 50 | 51 | return trend, dir_, long, short 52 | 53 | def faster_supertrend_talib(high, low, close, period=7, multiplier=3): 54 | high = high.flatten() 55 | low = low.flatten() 56 | close = close.flatten() 57 | avg_price = talib.MEDPRICE(high, low) 58 | atr = talib.ATR(high, low, close, period) 59 | upper, lower = get_basic_bands(avg_price, atr, multiplier) 60 | return get_final_bands_nb(close, upper, lower) 61 | 62 | def theWorks(high, low, close, period, period1, period2, multiplier, multiplier1, multiplier2, ema_len, rsi_len): 63 | high = high.flatten() 64 | low = low.flatten() 65 | close = close.flatten() 66 | supert, superd, superl, supers = faster_supertrend_talib(high, low, close, period, multiplier) 67 | supert1, superd1, superl1, supers1 = faster_supertrend_talib(high, low, close, period1, multiplier1) 68 | supert2, superd2, superl2, supers2 = faster_supertrend_talib(high, low, close, period2, multiplier2) 69 | ema = vbt.MA.run(close, window=ema_len, ewm = True, short_name='ema') 70 | rsi = vbt.RSI.run(close, window=rsi_len) 71 | superSupl = pd.DataFrame(np.sort(((np.array([superl, superl1, superl2])).transpose()))[:,1:2]) 72 | superSups = pd.DataFrame(np.sort(((np.array([supers, supers1, supers2])).transpose()))[:,1:2]) 73 | long = pd.DataFrame(close) > superSupl 74 | short = pd.DataFrame(close) < superSups 75 | longema = close > ema.ma 76 | shortema = close > ema.ma 77 | longrsi = rsi.rsi < 20 78 | shortrsi = rsi.rsi > 70 79 | entries = long.iloc[:, 0].values.tolist() and longema.tolist() and longrsi.tolist() 80 | exits = short.iloc[:, 0].values.tolist() and shortema.tolist() and shortrsi.tolist() 81 | return entries, exits 82 | 83 | the_Works = vbt.IndicatorFactory( 84 | class_name='theWorks', 85 | short_name='tw', 86 | input_names=['high', 'low', 'close'], 87 | param_names=['period', 'period1', 'period2', 'multiplier', 'multiplier1', 'multiplier2', 'ema_len', 'rsi_len'], 88 | output_names=['entries', 'exits'] 89 | ).from_apply_func( 90 | theWorks, 91 | period=7, 92 | period1=8, 93 | period2=9, 94 | multiplier=3, 95 | multiplier1=4, 96 | multiplier2=5, 97 | ema_len=200, 98 | rsi_len=14, 99 | param_product=True #all combination, False no combination 100 | ) 101 | 102 | periods = np.arange(50, 200, 50) 103 | ema_lenght = np.arange(20,250,30) 104 | rsi_lenght = np.arange(10, 20, 2) 105 | tw = the_Works.run(high, low, close, period=periods, period1=periods, period2=periods, ema_len=ema_lenght, rsi_len=rsi_lenght) 106 | 107 | pf = vbt.Portfolio.from_signals( 108 | close=close, 109 | entries=tw.entries, 110 | short_entries=tw.exits, 111 | fees=0.001, # commission of 0.1% 112 | sl_stop=0.005, tp_stop = 0.01, 113 | freq='1h' 114 | ) 115 | pf.stats() 116 | metric = 'total_return' 117 | perf = pf.deep_getattr(metric) 118 | print(perf.idxmax()) 119 | results = pf.total_profit() 120 | pf[results.idxmax()].trades.plot().show('browser') --------------------------------------------------------------------------------