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

Which datetime to take in notify_order()?



  • Hi,

    I am building an environment where I do live trading and backtesting within the same python module. I want to store the values that I get from backtrader in my SQL database for both kinds of trading.

    I am presently thinking how I can store the positions that are created / deleted during trading and that I get informed about in the method self.notify_order(self, order) .

    I would like to take the datetime that I possibly could get from backtrader for an order that is in state order.getstatusname() = 'Completed' and use this as part of a compound key that I will store in my database.

    I presently see the following candidates for this, but I am unsure which one to take.

    These are my possible candidates:

    1. self.data.num2date(order.p.data.l.datetime[0])
    2. order.p.owner.datas[0].datetime.datetime(0)
    3. order.params.data.datetime.datetime(0)
    4. datetime.datetime.utcnow() (at least during livetrading), as maybe the moment when notify_order() gets triggered is independant from any candles I get).

    At least during backtesting, 1.-3. contain the datetime that I will receive with the upcoming next(), but I am not sure whether to take 1.-3. is valid anyhow.

    What would you take in my situation / what makes sense from the internal logics point of view of backtrader?

    Thanks in advance!



  • You may with to look into using trade instead of orders. notify_trade will be notified when trades open and close. Backtrader uses numbers for dates. In notify_trade you could use the following:

    trade.dtopen
    trade.dtclose
    # Giving values like: 
    735906.0
    

Log in to reply
 

});