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



  • 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

    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 Ok solved it.

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


Log in to reply
 

});