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/

    Getting updated price with each trade update

    General Code/Help
    1
    2
    11
    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.
    • V
      Vypy1 last edited by

      Hi, how do I get the updated price from trade history when adding to an existing position?

      When I open the trade I can get price as trade.price, but if I code it this way:

      if trade.isopen:
         self.avgprice = trade.price #variable that keeps ref of the avg price
      

      It only acts when a new trade is opened, not when an existing trade is updated.

      How do I record the avg price in such a way that self.avgprice gets the value 276.8 when the trade is opened, value 302 when a new buy in that existing trade is made and finally value of 289.475 when adding 3rd time to the same position.

      [TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 734653.0), ('barlen', 0), ('size', 41), ('price', 276.8), ('value', 11348.800000000001), ('pnl', 0.0), ('pnlcomm', 0.0), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f8a5ca8ba90>), ('size', 41), ('price', 276.8), ('commission', 0.0)]))]), TradeHistory([('status', AutoOrderedDict([('status', 1), ('dt', 734696.0), ('barlen', 31), ('size', 82), ('price', 289.475), ('value', 23736.95), ('pnl', 0.0), ('pnlcomm', 0.0), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.BuyOrder object at 0x7f8a5c91f640>), ('size', 41), ('price', 302.15), ('commission', 0.0)]))]), TradeHistory([('status', AutoOrderedDict([('status', 2), ('dt', 734708.0), ('barlen', 39), ('size', 0), ('price', 289.475), ('value', 0.0), ('pnl', -1053.7000000000019), ('pnlcomm', -1053.7000000000019), ('tz', None)])), ('event', AutoOrderedDict([('order', <backtrader.order.SellOrder object at 0x7f8a5c66ae50>), ('size', -82), ('price', 276.625), ('commission', 0.0)]))])]
      
      V 1 Reply Last reply Reply Quote 0
      • V
        Vypy1 @Vypy1 last edited by

        @vypy1 This is solved for me.

        Future reference for anyone facing a similar issue, the average price can be fetched from position.price.

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