Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I get this value?

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

    How do I get this value?

    I am trying to develop a strategy that will average into a position multiple times and then the target needs to be calculated from the entire position price average as fills occur. I cannot figure out an easy way to get the entire position average. The computer tracks it and shows it on the screen, but all that seems available to me is the position average of each order entry - not the average of all entries.

    Please see the attached image to see exactly which number I am looking for.
    Attached Files

    #2
    Hello RichWaters,

    There are available properties
    Position.AvgPrice
    Position.Quantity

    These are only available programatically from positions opened by the strategy. Similar properties are not available for positions opened outside of the strategy.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      I am aware of those values, but here is the problem

      I have order 1 (entryOrderLong1) and order 2 (entryOrderLong2) in one strategy. Each one seems to need its own profit target and stop. Unless I am doing something wrong, this seems to result in Position.AvgPrice just returning the average price for entryOrderLong1 and a different average price for entryOrderLong2. So when I try to set the profit targets, I can set them each correctly based on its own price average, but it is not correct for the entire strategy.

      Example, entryOrderLong1 is filled for 2 contracts at 90.85 and entryOrderLong2 gets filled for 4 contracts at 90.75. 90.85 is the Position.AvgPrice for entryOrderLong1 and 90.75 is the Position.AvgPrice for entryOrderLong2. The real average for the 6 contracts is 90.78. I want my profit target to be 10 ticks above 90.78 for all contracts. Now, if the strategy averages down again at 90.65 for 4 more contracts, the Position.AvgPrice for entryOrderLong3 is 90.65, but now my price average for all open contracts is 90.73. I would now want my profit target for all open contracts in the 3 orders to be 90.83 (10 ticks above 90.73).

      How would I handle this? If there some kind of variable like Position.AvgPrice that applies to all open contracts, then it should be fairly simple.

      Is there one Position.AvgPrice that applies to all open contracts in the strategy?

      Comment


        #4
        Yes, Position.AvgPrice should return average price for all positions, but the positions must all be currently open. Are you seeing something different? How are you checking its value? You can use Print(Position.AvgPrice); and view in Tools > Output Window.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          This is what the code looks like that sets the target orders

          Code:
                  protected override void OnExecution(IExecution execution)
                  {
                      /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since
                      OnExecution() is called after OnOrderUpdate() which ensures your strategy has received the execution which is used
                      for internal signal tracking. */
                      if (entryOrderL1 != null && entryOrderL1.Token == execution.Order.Token) 
                      {
                          if (execution.Order.OrderState == OrderState.Filled || 
                              execution.Order.OrderState == OrderState.PartFilled || 
                              (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                          {
                              if (execution.MarketPosition == MarketPosition.Long)
                              {
                                  stopPriceLong = Position.AvgPrice - (20 * TickSize);
                                  entryOrderStopL1 = ExitLongStop(0, true, execution.Order.Filled, stopPriceLong, "StopLong-L1", "L1");
                                  entryOrderTargetL1 = ExitLongLimit(0, true, execution.Order.Filled, Position.AvgPrice + (10 * TickSize), "TargetLongL1", "L1");
                                  DrawArrowUp("stop"+CurrentBar,0,stopPriceLong,Color.Red);
                              }
                              
                              // Resets the entryOrder object to null after the order has been filled or partially filled
                              if (execution.Order.OrderState != OrderState.PartFilled)
                              {
                                  entryOrderL1     = null;
                              }
                          }
                      }
                      if (entryOrderL2 != null && entryOrderL2.Token == execution.Order.Token) 
                      {
                          if (execution.Order.OrderState == OrderState.Filled || 
                              execution.Order.OrderState == OrderState.PartFilled || 
                              (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                          {
                              if (execution.MarketPosition == MarketPosition.Long)
                              {
                                  stopPriceLong = Position.AvgPrice - (20 * TickSize);
                                  entryOrderStopL2 = ExitLongStop(0, true, execution.Order.Filled, stopPriceLong, "StopLong-L2", "L2");
                                  entryOrderTargetL2 = ExitLongLimit(0, true, execution.Order.Filled, Position.AvgPrice + (10 * TickSize), "TargetLongL2", "L2");
                                  DrawArrowUp("stop"+CurrentBar,0,stopPriceLong,Color.Red);
                              }
                              
                              // Resets the entryOrder object to null after the order has been filled or partially filled
                              if (execution.Order.OrderState != OrderState.PartFilled)
                              {
                                  entryOrderL2     = null;
                              }
                          }
                      }
          This sets a target order that is at the average price of that particular order so I get 2 target orders, one at 10 ticks above order 1 and the other 10 ticks above order 2. The same if there are 3 orders, but I need all open contracts to be at x number of ticks above the position average. What do I need to do to make this work the way I need it to work?

          Comment


            #6
            Wait a minute...

            I am seeing now that what I need to do is somehow refresh the target orders as fills happen so that they continue to adjust with the average price of the entire position. Is there an example of how to do this correctly?

            Comment


              #7
              What version are you using? In version 7 you no longer reference the .Token for orders. Making equality comparisons is done with the IOrder directly.

              Position.AvgPrice will provide the average of all open positions. I would simplify your strategy until you can verify it's working as you expect. Create one without using those advanced methods like OnExecution() and OnOrderUpdate(). Only add this layer of complexity once the basic stuff works the way you like. Use Print() statements to check values. Additional debugging help is available here:
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                I am using version 7 but used a code sample I found as the basis

                The problem I had until using this code was that I would end up with the target orders being at the right price but in a long position the target sells were for more contracts than the bought contracts which would then cause me to be in an unwanted short position. It seems that for some reason, the default behavior of NT without getting into advanced order management places a target order for the number of contracts specified in the original order even if it was a partial fill. Then, when that order fills from additional fills to the same order, I get more target orders each time for the original specified number of contracts in the original order.

                Now, I have orders for the correct number of contracts, but I need the target orders to stay X number of ticks away from Position.AvgPrice.
                Last edited by richwaters; 02-24-2011, 04:40 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                637 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                366 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                107 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                569 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                571 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X