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/

    Backtrader Broker Value Calculation Always Uses Closing Price

    General Discussion
    1
    2
    3
    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.
    • Andrew Rosca
      Andrew Rosca last edited by

      My strategy uses cheat-on-open and occasionally runs out of margin.

      It turns out that the broker value calculation is hardcoded to closing prices, which leads to a mismatch between the size of the position I am trying to enter (based on opening price) and what the broker thinks I have in equity (based on closing price of the same bar).
      https://github.com/mementum/backtrader/blob/e2674b1690f6366e08646d8cfd44af7bb71b3970/backtrader/brokers/bbroker.py#L437-L442

      if not self.p.shortcash:
      	dvalue = comminfo.getvalue(position, data.close[0])
      else:
      	dvalue = comminfo.getvaluesize(position.size, data.close[0])
      
      dunrealized = comminfo.profitandloss(position.size, position.price, data.close[0])
      

      Is anyone aware of a workaround for this?

      Andrew Rosca 1 Reply Last reply Reply Quote 0
      • Andrew Rosca
        Andrew Rosca @Andrew Rosca last edited by

        I think this has already been answered here actually: https://community.backtrader.com/topic/726/question-on-cheat-on-open

        I am going to try the custom sizer approach.

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