Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Trading only on Candle Stick Pattern.

    General Code/Help
    3
    7
    777
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Nikhil Morye
      Nikhil Morye last edited by

      def init(self):
      self.marubozu=bt.talib.CDLMORNINGSTAR(self.data.open,self.data.high,
      self.data.low, self.data.close, penetration=0.3)
      
      def next(self):
      if self.marubozu[0] == 100:
      self.buy()
      
      elif self.marubozu[0] == -100:
      self.sell()
      

      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.

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

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

        RunBacktest.com

        Nikhil Morye 1 Reply Last reply Reply Quote 0
        • Nikhil Morye
          Nikhil Morye @run-out last edited by

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

          1 Reply Last reply Reply Quote 0
          • vladisld
            vladisld last edited by

            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(self.data.open, self.data.high,
                                                            self.data.low, self.data.close, penetration=0.3)
            
                def next(self):
                    if self.marubozu[0] == 100:
                        self.buy()
                    elif self.marubozu[0] == -100:
                        self.sell()
            
            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)
                cerebro.adddata(data)
                cerebro.addstrategy(TestStrategy)
                cerebro.run(runonce=True)
                cerebro.plot()
            
            

            test_marubozy.png

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

            Nikhil Morye 1 Reply Last reply Reply Quote 0
            • vladisld
              vladisld last edited by

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

              1 Reply Last reply Reply Quote 0
              • Nikhil Morye
                Nikhil Morye @vladisld last edited by

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

                1 Reply Last reply Reply Quote 0
                • Nikhil Morye
                  Nikhil Morye last edited by

                  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].datetime.date(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]:
                        return
                      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')
                        pass
                      self.order = None
                    
                    def notify_trade(self, trade):
                      if not trade.isclosed:
                        return
                      
                      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:
                        return
                      # 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()
                  cerebro.addstrategy(TestStrategy)
                  data = bt.feeds.YahooFinanceData(dataname='SBIN.BO',fromdate=datetime(2015, 1, 1),todate=datetime(2020, 6, 30),reversed=True)
                  cerebro.adddata(data)
                  cerebro.broker.setcash(1000.0)
                  cerebro.broker.setcommission(0.001)
                  print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
                  cerebro.run()
                  print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
                  #cerebro.plot();
                  
                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors