For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Trading only on Candle Stick Pattern.

  • def init(self):
    self.marubozu=bt.talib.CDLMORNINGSTAR(,,,, penetration=0.3)
    def next(self):
    if self.marubozu[0] == 100:
    elif self.marubozu[0] == -100:

    I will be updating the code to add in stop loss and take profit limit, but that is not the issue.
    orders are not being logged at close price with this.
    Help will be appreciated.

  • Can you print out the marubozu values at the beginning of next?

  • @run-out yes its showing values in form of 0,100,-100

  • I've run your code with minimal changes and it seems that both buy and sell orders are executed:

    import os
    import backtrader as bt
    class TestStrategy(bt.Strategy):
        def __init__(self):
            self.marubozu = bt.talib.CDLMORNINGSTAR(,,
                                          ,, penetration=0.3)
        def next(self):
            if self.marubozu[0] == 100:
            elif self.marubozu[0] == -100:
    if __name__ == '__main__':
        cerebro = bt.Cerebro()
        data_path = os.path.join(bt.__file__, '../../datas/yhoo-1996-2014.txt')
        data = bt.feeds.YahooFinanceCSVData(dataname=data_path)


    You may try to take a look at your data and see if it is ok.

  • Sorry, my mistake - only buy orders are executed in the previous run. Will try to understand what has happened with sell orders.

  • Got it my way around it with bracket order. :)

  • Got my way around this peculiar problem with bracket order,

    putting the code out if anyone needs it,

    the code needs to tweaked around its too big for no reason but hopefully it helps who faced a similar issue like mine.

    class TestStrategy(bt.Strategy):
      params = dict (buyperiod=1.15,sellperiod=0.95)
      def log(self, txt, dt=None):
        ''' Logging function fot this strategy'''
        dt = dt or self.datas[0]
        print('%s, %s' % (dt.isoformat(), txt))
      def __init__(self):
        self.dataclose = self.datas[0].close
        self.dataopen = self.datas[0].open
        self.datahigh = self.datas[0].high
        self.datalow = self.datas[0].low
        self.marubozu = bt.talib.CDLMARUBOZU(self.dataopen,self.datahigh,self.datalow,self.dataclose) 
        self.order = None
        self.buyprice = None
        self.buycomm = None
        self.sellprice = None
        self.sellcomm = None
      def notify_order(self, order):
        if order.status in [order.Submitted, order.Accepted]:
        if order.status in [order.Completed]:
          if order.isbuy():
            #self.log('BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %(order.executed.price,order.executed.value,order.executed.comm))
            self.buyprice = order.executed.price
            self.buycomm = order.executed.comm
          elif order.issell():
            #self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %(order.executed.price,order.executed.value,order.executed.comm))
            self.sellprice = order.executed.price
            self.sellcomm = order.executed.comm
          self.bar_executed = len(self)
        elif order.status in [order.Canceled, order.Margin, order.Rejected]:
          self.log('Order Canceled/Margin/Rejected')
        self.order = None
      def notify_trade(self, trade):
        if not trade.isclosed:
        self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %(trade.pnl, trade.pnlcomm))
      def next(self):
        # Simply log the closing price of the series from the reference
        self.log('Close, %.2f' % self.dataclose[0])
        # Check if an order is pending ... if yes, we cannot send a 2nd one
        if self.order:
        # Check if we are in the market
        if not self.position:
          if self.marubozu[0] == 100:
            self.log('BUY CREATE, limitprice %.2f, price %.2f, stopprice %.2f' %(self.dataopen*1.35,self.dataclose[0],self.datalow[0]))
            self.order = self.buy_bracket(limitprice=self.dataopen*self.p.buyperiod, price=self.dataclose, stopprice=self.datalow)
          elif self.marubozu[0] == -100:
            self.log('Sell CREATE, limitprice %.2f, price %.2f, stopprice %.2f' %(self.datalow*0.65,self.dataclose[0],self.datahigh[0]))
            self.order = self.sell_bracket(limitprice=self.datalow*self.p.sellperiod, price=self.dataclose, stopprice=self.datahigh)
    cerebro = bt.Cerebro()
    data = bt.feeds.YahooFinanceData(dataname='SBIN.BO',fromdate=datetime(2015, 1, 1),todate=datetime(2020, 6, 30),reversed=True)
    print('Starting Portfolio Value: %.2f' %
    print('Final Portfolio Value: %.2f' %

Log in to reply