For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
I am new with backtrader pls help
-
I am trying to implement a simple strategy based on stochastic oscillator in uptrend market
timeframe of 5 minute
but my code tends to not work, can take a look pleaseimport backtrader as bt import datetime class Stochstrategy(bt.Strategy): params = ( ('per',7), ('size',20), ) def __init__(self): self.pfast = bt.indicators.SmoothedMovingAverage(self.data[0],period=60) self.pslow = bt.indicators.ExponentialMovingAverage(self.data[0],period=1728) self.stoch = bt.ind.Stochastic(self.data) self.crs = bt.ind.CrossOver(self.pfast,self.pslow) def notify_trade(self,trade): if trade.isclosed: dt = self.data.datetime.date() print('---------------------------- TRADE ---------------------------------') print("1: Data Name: {}".format(trade.data._name)) print("2: Bar Num: {}".format(len(trade.data))) print("3: Current date: {}".format(dt)) print('4: Status: Trade Complete') print('5: Ref: {}'.format(trade.ref)) print('6: PnL: {}'.format(round(trade.pnl,2))) print('--------------------------------------------------------------------') def next(self): orders = self.broker.get_orders_open() # Cancel open orders so we can track the median line if orders: for order in orders: self.broker.cancel(order) if not self.position: if self.crs: if self.stoch.l.k<25 and self.stoch.l.k==self.stoch.l.d: self.buy(exectype=bt.Order.Stop, price=self.boll.lines.bot[0], size=self.params.size) elif self.stoch.l.k>75 and self.stoch.l.k==self.stoch.l.d: self.sell(exectype=bt.Order.Stop, price=self.boll.lines.top[0],size= self.p.size)
-
@masla-darkshark said in I am new with backtrader pls help:
def next(self):
orders = self.broker.get_orders_open()# Cancel open orders so we can track the median line if orders: for order in orders: self.broker.cancel(order) if not self.position: if self.crs: if self.stoch.l.k<25 and self.stoch.l.k==self.stoch.l.d: self.buy(exectype=bt.Order.Stop, price=self.boll.lines.bot[0], size=self.params.size) elif self.stoch.l.k>75 and self.stoch.l.k==self.stoch.l.d: self.sell(exectype=bt.Order.Stop, price=self.boll.lines.top[0],size= self.p.size)
You are cancelling your orders every bar. Perhaps you mean to puthem after
if not self.position:
or afterif self.crs:
? -
@run-out Sorry I didn't mean to put in that top block of code... wish we could edit.