Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position.AveragePrice in Strategy not always updated

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Position.AveragePrice in Strategy not always updated

    Hello everyone,

    I'm using Position.AveragePrice for serveral actions in a strategy.

    Now I realised some stange behavior I cannot find an answer for.

    So in the following chart I visualised the current Position.AveragePrice automatically as a thin white dotted line.

    There are several position updates that should lead into Position.AveragePrice updates.

    One initial short with quantity 4 (average price is updating, fine)
    Then a BuyToCover with quantity 3 (average price is not updating, why?)
    Then an additional short (average price is updating, fine)
    Then a BuyToCover with quantity 1 (average price is updating, fine)
    Then a BuyToCover with quantity 1 (average price is not updating, why?)

    Can you help me to identify the reason or the issue?

    Thanks in advance


    Click image for larger version  Name:	2021-09-03 18_03_40-Chart - BBA_BTCUSD.png Views:	4 Size:	285.2 KB ID:	1170054

    And out of my log in the OnBarUpdate with the executions in OnExecutionUpdate and an optional AveragePrice print out if the AveragePrice changes:

    2021-09-03 18:25:55:805 02.09.2021 04:45:00 OnExecutionUpdate NT-00187-160 Short@2021-09-02-04:45
    2021-09-03 18:25:55:805 AveragePrice 02.09.2021 04:46:00 49742,5

    2021-09-03 18:25:55:811 02.09.2021 06:12:00 OnExecutionUpdate NT-00188-160 TP 0/1 | PosBefore: 4
    <<<<<<<Missing>>>>>>>>

    2021-09-03 18:25:55:821 02.09.2021 09:15:00 OnExecutionUpdate NT-00189-160 Short@2021-09-02-04:45
    2021-09-03 18:25:55:821 AveragePrice 02.09.2021 09:16:00 50088,5

    2021-09-03 18:25:55:874 03.09.2021 01:26:00 OnExecutionUpdate NT-00190-160 TP 1/1 | PosBefore: 3
    2021-09-03 18:25:55:874 AveragePrice 03.09.2021 01:27:00 50261,5

    2021-09-03 18:25:55:879 03.09.2021 02:43:00 OnExecutionUpdate NT-00191-160 TP 1/2 | PosBefore: 2
    <<<<<<<Missing>>>>>>>>

    2021-09-03 18:25:55:903 03.09.2021 11:33:00 OnExecutionUpdate NT-00192-160 SL 1/1 | PosBefore: 1 0
    2021-09-03 18:25:55:903 AveragePrice 03.09.2021 11:34:00 0
    Last edited by Airwave; 09-03-2021, 10:37 AM.

    #2
    Hello Airwave,

    Thank you for your post.

    Is this occurring on a sim or live account? Who is the broker/data provider?

    If you have a short script demonstrating the issue in a way that may be replicated, that would be helpful as well.

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      that is happening with Coinbase and a SIM account with BTCUSD.
      But it is happening with every broker.

      My Script is very complex but in general this is happening with very simple
      EnterShort or ExitShort commands that create this issue used in OnBarUpdate();
      Also Position.AveragePrice is just the command I'm using here...

      Like:

      int contracts = <value>;
      currentShortOrderName = (currentShortOrderName == null ? "Short@" + masterStrategy.Times[2][0].ToString("yyyy-MM-dd-HH:mm") : currentShortOrderName);
      masterStrategy.EnterShort(contracts, currentShortOrderName);


      int volumeToReduce = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(mast erStrategy.Position.Quantity) / 100 * Math.Ceiling(percentage)));
      string orderName = "TP " + infoText + " | PosBefore: " + masterStrategy.Position.Quantity.ToString();
      masterStrategy.ExitShort(volumeToReduce, orderName, currentShortOrderName);
      Last edited by Airwave; 09-03-2021, 11:52 AM.

      Comment


        #4
        Hello Airwave,

        Thank you for your reply.

        I've made a simple example strategy that replicates this and here is what I see occurring:

        Click image for larger version

Name:	2021-09-03_12-52-23.png
Views:	238
Size:	72.1 KB
ID:	1170075

        Here's the associated prints I see:

        Position.AveragePrice in OBU at 9/3/2021 12:49:00 PM: 0
        Initial Entry processing in OEU
        Position.AveragePrice in OnExecutionUpdate: 4535.75
        Position.AveragePrice in OnPositionUpdate: 4535.75
        position.AveragePrice in OnPositionUpdate: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:10 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:20 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:20 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:20 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:30 PM: 4535.75
        First Exit processing in OEU
        Position.AveragePrice in OnExecutionUpdate: 4535.75
        Position.AveragePrice in OnPositionUpdate: 4535.75
        position.AveragePrice in OnPositionUpdate: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:30 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:30 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:30 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:30 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:40 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:40 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:40 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:49:50 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:50:00 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:50:00 PM: 4535.75
        Position.AveragePrice in OBU at 9/3/2021 12:50:10 PM: 4535.75
        Second Entry processing in OEU
        Position.AveragePrice in OnExecutionUpdate: 4536
        Position.AveragePrice in OnPositionUpdate: 4536
        position.AveragePrice in OnPositionUpdate: 4536
        Position.AveragePrice in OBU at 9/3/2021 12:50:10 PM: 4536
        Position.AveragePrice in OBU at 9/3/2021 12:50:10 PM: 4536
        Position.AveragePrice in OBU at 9/3/2021 12:50:20 PM: 4536
        Position.AveragePrice in OBU at 9/3/2021 12:50:20 PM: 4536
        Position.AveragePrice in OBU at 9/3/2021 12:50:30 PM: 4536
        Position.AveragePrice in OBU at 9/3/2021 12:50:40 PM: 4536
        Second Exit processing in OEU
        Position.AveragePrice in OnExecutionUpdate: 4536.25
        Position.AveragePrice in OnPositionUpdate: 4536.25
        position.AveragePrice in OnPositionUpdate: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:50:40 PM: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:50:40 PM: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:50:40 PM: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:50:50 PM: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:50:50 PM: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:50:50 PM: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:50:50 PM: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:50:50 PM: 4536.25
        Position.AveragePrice in OBU at 9/3/2021 12:51:00 PM: 4536.25
        Third Exit processing in OEU
        Position.AveragePrice in OnExecutionUpdate: 0
        Position.AveragePrice in OnPositionUpdate: 0
        position.AveragePrice in OnPositionUpdate: 0
        Position.AveragePrice in OBU at 9/3/2021 12:51:00 PM: 0

        So what I'm seeing here is that our first entry updates the average position price to its entry price. When part of that position is exited with the first exit, the average position price will stay the same since all 4 contracts were at the same price. After that first exit, we then have another entry at 4536.25, bringing the average position price to 4536. The next exit would be matched with the remaining 1 contract from the first entry, so the average entry price would then change to the price of the second entry. The third exit then brings the position back to flat and the average entry price to 0 since there is not a position.

        This is exactly what I would expect to see. I'm attaching the example strategy below (note it will only run in real time and will repeat the sequence after returning to a flat position).

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hi Kate,

          thank you very much.

          I updated your example to my problem statement and I could recreate it.

          Could you please check my example.

          Use for instance Coinbase with BTCUSD in a 36minutes chart 180days back.

          Thanks in advance.


          Then you get:

          ================================================== ================================================== ================================================== ===============
          ************************************************** ****************
          Position.AveragePrice in OBU at 02.09.2021 04:45:00: 0
          **********************
          02.09.2021 04:45:00 Strategy 'ScalingPositionAveragePriceTest/231494169': Entered internal SubmitOrderManaged() method at 02.09.2021 04:45:00: BarsInProgress=1 Action=SellShort OrderType=Market Quantity=4 LimitPrice=0 StopPrice=0 SignalName='Short@2021-09-02-04:45' FromEntrySignal=''
          Short@2021-09-02-04:45 processing in OEU
          Position.AveragePrice in OnExecutionUpdate: 49666,53
          Position.AveragePrice in OnPositionUpdate: 49666,53
          position.AveragePrice in OnPositionUpdate: 49666,53
          ************************************************** ****************
          Position.AveragePrice in OBU at 02.09.2021 06:12:00: 49666,53

          **********************
          02.09.2021 06:12:00 Strategy 'ScalingPositionAveragePriceTest/231494169': Entered internal SubmitOrderManaged() method at 02.09.2021 06:12:00: BarsInProgress=0 Action=BuyToCover OrderType=Market Quantity=3 LimitPrice=0 StopPrice=0 SignalName='First Exit' FromEntrySignal='Short@2021-09-02-04:45'
          First Exit processing in OEU
          Position.AveragePrice in OnExecutionUpdate: 49666,53
          Position.AveragePrice in OnPositionUpdate: 49666,53
          position.AveragePrice in OnPositionUpdate: 49666,53
          ************************************************** ****************
          Position.AveragePrice in OBU at 02.09.2021 09:15:00: 49666,53

          **********************
          02.09.2021 09:15:00 Strategy 'ScalingPositionAveragePriceTest/231494169': Entered internal SubmitOrderManaged() method at 02.09.2021 09:15:00: BarsInProgress=1 Action=SellShort OrderType=Market Quantity=2 LimitPrice=0 StopPrice=0 SignalName='Short@2021-09-02-04:45' FromEntrySignal=''
          Short@2021-09-02-04:45 processing in OEU
          Position.AveragePrice in OnExecutionUpdate: 50041,1633333333
          Position.AveragePrice in OnPositionUpdate: 50041,1633333333
          position.AveragePrice in OnPositionUpdate: 50041,1633333333
          ************************************************** ****************
          Position.AveragePrice in OBU at 03.09.2021 01:26:00: 50041,1633333333

          **********************
          03.09.2021 01:26:00 Strategy 'ScalingPositionAveragePriceTest/231494169': Entered internal SubmitOrderManaged() method at 03.09.2021 01:26:00: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Second Exit' FromEntrySignal='Short@2021-09-02-04:45'
          Second Exit processing in OEU
          Position.AveragePrice in OnExecutionUpdate: 50228,48
          Position.AveragePrice in OnPositionUpdate: 50228,48
          position.AveragePrice in OnPositionUpdate: 50228,48
          ************************************************** ****************
          Position.AveragePrice in OBU at 03.09.2021 02:43:00: 50228,48

          **********************
          03.09.2021 02:43:00 Strategy 'ScalingPositionAveragePriceTest/231494169': Entered internal SubmitOrderManaged() method at 03.09.2021 02:43:00: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Third Exit' FromEntrySignal=''
          Third Exit processing in OEU
          Position.AveragePrice in OnExecutionUpdate: 50228,48
          Position.AveragePrice in OnPositionUpdate: 50228,48
          position.AveragePrice in OnPositionUpdate: 50228,48
          ************************************************** ****************
          Position.AveragePrice in OBU at 03.09.2021 11:33:00: 50228,48

          **********************
          03.09.2021 11:33:00 Strategy 'ScalingPositionAveragePriceTest/231494169': Entered internal SubmitOrderManaged() method at 03.09.2021 11:33:00: BarsInProgress=2 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Forth Exit' FromEntrySignal=''
          Forth Exit processing in OEU
          Position.AveragePrice in OnExecutionUpdate: 0
          Position.AveragePrice in OnPositionUpdate: 0
          position.AveragePrice in OnPositionUpdate: 0

          Enabling NinjaScript strategy 'ScalingPositionAveragePriceTest/231494169' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=Unique entries EntriesPerDirection=1000 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes

          Click image for larger version

Name:	2021-09-03 23_30_40-Clipboard.png
Views:	272
Size:	31.9 KB
ID:	1170092
          Last edited by Airwave; 09-03-2021, 03:31 PM.

          Comment


            #6
            Hello Airwave,

            Thank you for your reply.

            I'm not seeing an issue with the results you're getting. This appears to be as would be expected, with the average position price changing after the second entry as you've still got one from the previous entry, then changing to match the entry price of the second entry after the entire original position gets exited at Second Exit.

            Can you clarify what you're seeing that you don't feel would be expected?

            Thanks in advance; I look forward to assisting you further.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Hello Kate,


              So I was expecting, that either (1) the exits have no influence on the AveragePrice
              or that (2) ALL exits are accordingly changing the AveragePrice as they are BuyToCover / Takeprofits.

              According to the behavior of ninjatrader I think the second (2) is that implemented one in Ninjatrader.

              But then I was wondering, that the average price is not changing after "First Exit" but after "Second Exit" but then again not after "Third Exit".
              So strange mixed behaviour.

              Here I have visualised the current AveragePrice behaviour in the chart as (blue/yellow) lines:

              Click image for larger version

Name:	2021-09-08 22_43_45-Chart - BTCUSD.png
Views:	209
Size:	53.0 KB
ID:	1170523


              And that's exactly what I would have expected (see yellow lines):

              Click image for larger version

Name:	2021-09-08 22_44_23-Chart - BTCUSD.png
Views:	202
Size:	53.5 KB
ID:	1170524


              Thanks in advance.

              Comment


                #8
                Hello Airwave,

                Thank you for your reply.

                I'm not sure why you think the average entry price would change after the first exit.

                At that point, all entries were made at the same entry price, 49666,53. (49666,53 * 4) / 4 = 49666,53. If three of those are exited, (49666,53 * 1) / 1 still equals 49666,53. It's only when the second entry is taken at 50228,48 that this would change, because then we have 49666,53 + 50228,48 +50228,48 = 50041,16333333.

                NinjaTrader pairs trades FIFO - first in first out. So the next exit would be paired with your remaining contract at 49666,53. Once that is exited, the average price would be 50228,48 as that is the entry price of both of the remaining two contracts.

                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Kate,

                  okay, think then I had a general misunderstanding of the order system.

                  Thought the entries are never stand alone and always considered as a whole.


                  Would it be possible to configure it in a way like (1) or in a way, that the FIFO has no influence on the average price?

                  Means, like

                  (49666,53 * 4) / 4 = 49666,53 -->
                  49666,53 + 50228,48 +50228,48 = 50041,16333333.

                  but no changed with the exists? As for instance exchanges with perpetual contracts are working that way?

                  Thanks in advance.

                  Comment


                    #10
                    If you would like the exits to have no effect on Position.AveragePrice, then I
                    recommend you put logic in your strategy to accomplish that yourself.

                    For example, try this idea:

                    Setup a global variable,
                    private double AvgEntryPrice = 0;

                    On every Filled or PartFilled in OnExecutionUpdate,
                    AvgEntryPrice = Position.AveragePrice;

                    Then everywhere you use Position.AveragePrice, you would instead use AvgEntryPrice.
                    By using AvgEntryPrice, you are guaranteed that exits don't affect your calculations.

                    Would that help you achieve what you want?

                    Comment


                      #11
                      Thank you bltdavid for that idea.
                      Think I will then do it this way if there is no configurable way for Ninja.

                      Cheers
                      Airwave

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by bortz, 11-06-2023, 08:04 AM
                      47 responses
                      1,611 views
                      0 likes
                      Last Post aligator  
                      Started by jaybedreamin, Today, 05:56 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post jaybedreamin  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      6 responses
                      19 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by Jon17, Today, 04:33 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post Jon17
                      by Jon17
                       
                      Started by Javierw.ok, Today, 04:12 PM
                      0 responses
                      22 views
                      0 likes
                      Last Post Javierw.ok  
                      Working...
                      X