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/

    Which datetime to take in notify_order()?

    General Code/Help
    2
    2
    97
    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.
    • NewestTrader
      NewestTrader last edited by

      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!

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

        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
        

        RunBacktest.com

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors