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

Order Validity



  • Hi!! Is it possible to set the validity of an order until the next bar instead of using dates? If yes, how do I do it?

    Thanks :)



  • You can still used dates to set the validity of every order to exactly one bar. Let's say your candles are 60 minutes long.

    dt = self.datas[0].datetime.datetime()
    self.sell(exectype=bt.Order.Limit,
              size=self.position.size,
              price=self.ema[0],
              valid=dt + datetime.timedelta(minutes=60))
    

    If you want to make it dependent on your data, pass timeframe_in_m to your strategy.


  • administrators

    @fivo said in Order Validity:

    If you want to make it dependent on your data, pass timeframe_in_m to your strategy.

    Data feeds have a parameter named timeframe which carries the original value when the data feed was created. Using it (rather than an extra parameter) together with the chosen compression, you can get it ...

    auto_timer = dict(
        bt.TimeFrame.Seconds=lambda x: datetime.timedelta(seconds=x),
        bt.TimeFrame.Minutes=lambda x: datetime.timedelta(minutes=x),
        bt.TimeFrame.Days=lambda x: datetime.timedelta(days=x),
    )
    
    nextbar = self.data.num2date() + auto_timer[self.data.p.timeframe](self.data.p.compression)
    

    Notice self.data.num2date() which

    • If no numeric value is given uses the current value from the datetime line (i.e.: self.lines.datetime[0])