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/

    Time Based Strategy

    General Code/Help
    1
    3
    44
    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

      Hey guys, I am trying to implement a strategy which is based on time.

      Something like:

      1. Enter at 11.30 am
      2. No matter what position exit at 12.00 pm

      Is there some module in backtrader which could help me in it.

      Currently, I m slicing the data frame based on time and then feeding it to bt, but I have around 30-40 such stock which I want to screen so it gets difficult.

      Is there a workaround for this

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

        @Nikhil-Morye

        class TestStrategy(bt.Strategy):
         def __init__(self):
           # Keep a reference to the "close" line in the data[0] dataseries
           self.dataclose = self.datas[0].close
           self.dataopen = self.datas[0].open
           self.datahigh = self.datas[0].high
           self.datalow = self.datas[0].low
           mask = pd.date_range('2020/06/10 09:20:00', periods=30, freq='D')
           z = mask[mask.dayofweek < 5]
         
         def next(self):
           if self.datas[0].datetime.datetime(0) in z:
             if self.datahigh == self.dataopen:
               mainside = self.buy(price=self.dataclose[0], exectype=bt.Order.Limit, transmit=False)
               lowside  = self.sell(price=self.dataclose[0]*0.95, size=mainside.size, exectype=bt.Order.Stop,transmit=False, parent=mainside)
               highside = self.sell(price=self.dataclose[0]*1.15, size=mainside.size, exectype=bt.Order.Limit,transmit=True, parent=mainside)
               print(f'On:{self.datas[0].datetime.datetime(0)},Buy at:{self.dataclose[0]},Sell at:{round(self.dataclose*0.95,2)},Stop Loss:{round(self.dataclose*1.05,2)}')
             elif self.datalow == self.dataopen:
               mainside = self.sell(price=self.dataclose[0], exectype=bt.Order.Limit, transmit=False)
               lowside  = self.close(price=self.dataclose[0]*1.05, size=mainside.size, exectype=bt.Order.Stop,transmit=False, parent=mainside)
               highside = self.close(price=self.dataclose[0]*0.85, size=mainside.size, exectype=bt.Order.Limit,transmit=True, parent=mainside)
               print(f'On:{self.datas[0].datetime.datetime(0)},Short at:{self.dataclose[0]},Sell at:{round(self.dataclose*1.05,2)},Stop Loss:{round(self.dataclose*0.95,2)}')
             else:
               pass
           else:
             pass
        

        But the order is not being executed

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

          @Nikhil-Morye Ok solved it.

          the isue was with the fixer comand of mine.
          if any admin please close the tread

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }