You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I am trying to back-test a strategy using k's envelopes. The 800 period moving average of the highs and lows is taken. These averages act as resistance and support levels respectively..
I am following the article at the below link.
Here is my code. Basically, have I implemented this strategy as the article describes? What got me confused is the indexing of the variables so I want to be sure that I am not making a basic programming error.
The signal function from the article is below.
def signal(Data, close, ma_high, ma_low, buy, sell):
for i in range(len(Data)):
if Data[i, close] < Data[i, ma_high] and Data[i, close] > Data[i, ma_low] and Data[i - 1, close] > Data[i, ma_high] and Data[i - 2, close] > Data[i, ma_high]:
Data[i, buy] = 1
if Data[i, close] < Data[i, ma_high] and Data[i, close] > Data[i, ma_low] and Data[i - 1, close] < Data[i, ma_low] and Data[i - 2, close] < Data[i, ma_low]:
Data[i, sell] = -1
My code follows.
stop_loss_factor=2
take_profit_factor=3
l_ema_lookback=800
def init(self):
super().init()
self.high_ma=self.I(talib.MA,self.data.df["High"].to_numpy(),timeperiod=self.l_ema_lookback)
self.low_ma=self.I(talib.MA,self.data.df["Low"].to_numpy(),timeperiod=self.l_ema_lookback)
self.set_trailing_sl(self.stop_loss_factor)
def next(self):
super().next()
limit_order_target=(self.data.High-self.data.Low)+self.data.High
stop_loss=self.data.Close-self.stop_loss_factor*self.atr
take_profit=self.data.Close+self.take_profit_factor*self.atr
if self.data.Close>self.low_ma and self.data.Close<self.high_ma and self.data.Close[-2]>self.high_ma[-2] and self.data.Close[-3]>self.high_ma[-3]:
self.buy(sl=stop_loss,tp=take_profit)
elif ((self.data.Close>self.low_ma and self.data.Close<self.high_ma and self.data.Close[-2]<self.low_ma[-2] and self.data.Close[-3]<self.low_ma[-3])):
self.position.close()
df=pd.read_csv("./ohlc/hourly/SBIN.csv",parse_dates=['date'], index_col=['date'],date_parser=dateparse)
print("length of data frame=",len(df))
df["Open"]=df["open"]
df["High"]=df["high"]
df["Low"]=df["low"]
df["Close"]=df["close"]
bt = Backtest(df, candlestickAdx, commission=.002,exclusive_orders=True,cash=30000)
opt=bt.optimize(take_profit_factor=range(2,3),stop_loss_factor=range(1,3),l_ema_lookback=range(10,900),maximize='Equity Final [$]',random_state=0,max_tries=300)
o=bt.run()
print(o)
#print(opt._strategy)
#bt.plot(filename="strategy.html")
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I am trying to back-test a strategy using k's envelopes. The 800 period moving average of the highs and lows is taken. These averages act as resistance and support levels respectively..
I am following the article at the below link.
Here is my code. Basically, have I implemented this strategy as the article describes? What got me confused is the indexing of the variables so I want to be sure that I am not making a basic programming error.
The signal function from the article is below.
My code follows.
Beta Was this translation helpful? Give feedback.
All reactions