Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AvgFillPrice

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

    AvgFillPrice

    Hi Folks,

    I am a new one and trying to learn the basics. Question: what's the most reliable way of getting the realised fill price of an executed order?

    I am asking this because I have noticed that IOrder.AvgFillPrice may be zero (0), although IOrder.OrderState == OrderState.Filled OnOrderUpdate. Sounds strange?

    I have been bactesting my strategy using the Sim account and historical CL 02-09 data (1 min bars from Dec 4 to Dec 19). The strategy generated in total 1318 orders (buy and sell @ mkt), and exactly in 8 cases the AvgFillPrice was zero as described above. At least in one occasion the corresponding order was shown in the chart as 'Close'. Comments?

    Regards,
    Jari

    #2
    How about Position.AvgPrice ?

    Comment


      #3
      Made some further tests with extensive log writing. It appeared that all the (OrderHandle.OrderState == OrderState.Filled && OrderHandle.AvgFillPrice == 0) cases related to a system-generated Sell or BuyToCover order, i.e. I had an open long or short position, which was closed by an offsetting order NOT TRIGGERED by my own application logic (managed by OnBarUpdate()).

      It's worth noting that although I use SetStopLoss() and SetProfitTarget() methods, which btw seem to be working as expected, the problem does NOT relate to them. In the above-mentioned cases the position was always well away from the specified stop-loss or profit target.

      Specialist advice needed!


      Comment


        #4
        For clarification,

        are you saying that in the OnOrderUpdate() method, you put in someting like:

        if (order.OrderState == OrderState.Filled) Print(order.AvgFillPrice);

        You will sometimes get a zero value?
        RayNinjaTrader Customer Service

        Comment


          #5
          Ray,

          Yes. that's exactly what I am saying, and I have a log file to support my 'case'.

          Made further studies and found out that whenever this happens (i.e. OrderState == OrderState.Filled && AvgFillPrice == 0), also Name == 'Exit on close' and Bars.FirstBarOfSession == True. Could this relate to ExitOnClose = true?!?!

          Example of IOrder.ToString in such a case:
          Order='NT-00584/Sim101' Name='Exit on close' State=Filled Instrument='CL 02-09' Action=Sell Limit price=0 Stop price=0 Quantity=1 Strategy='Algo1' Type=Market Tif=Gtc Oco='' Filled=0 Fill price=0 Token='3a9dc13b29464beaa32e1be07753be0e' Gtd='1.12.2099 0:00:00'.

          Question: I have noticed, too, that
          Performance.AllTrades.TradesPerformance.Currency.C umProfit functions 'well' when this happens (I am running my own PnL count in parallel - since I buy and sell @ mkt, I can use GetCurrentAsk() and GetCurrentBid() when AvgFillPrice = 0). But if AvgFillPrice is zero, then where's the actual close price of the position stored?!

          If you wish, I can send the source...

          Comment


            #6
            jp_kettunen,

            Sorry I do not follow. Please just try a really simple case to see how it works. Just make yourself one IOrder and do EnterLong(). In OnOrderUpdate() when you receive the fill state then just print out order.AvgFillPrice. You should find it returns the fill price.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              My point is that I get the AvgFillPrice right in most of the cases OnOrderUpdate. As I wrote in my first post,

              "I have been backtesting my strategy using the Sim account and historical CL 02-09 data (1 min bars from Dec 4 to Dec 19). The strategy generated in total 1318 orders (buy and sell @ mkt), and exactly in 8 cases the AvgFillPrice was zero".

              I have found out later, using extensive log writing, that when IOrderAvgFill Price is 0, then also

              1) IOrder.Action = 'Sell' or 'BuyToCover',
              2) IOrder.Name = 'Exit on close', AND
              3) Bars.FirstBarOfSession == True.

              Now I wonder,

              1) Does this relate to the fact that I have enabled ExitOnClose?
              2) Is this 'business as usual' or have I found a bug in the software?
              3) Am I just dreaming?!

              Comment


                #8
                jp_kettunen,

                I guess I am still not understanding you. Please strip down your strategy to bare essentials that highlight what you feel is in question then if you could upload that I could take a look at my end and help you evaluate it. Thank you.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  /* Fragments of a strategy that can demonstrate that AvgFillPrice can be zero, when OrderState is filled OnOrderUpdate */

                  private bool LongOpen = false;
                  private bool ShortOpen = false;

                  protected override void Initialize()
                  {
                  CalculateOnBarClose = false;
                  ExitOnClose = true;
                  ExitOnCloseSeconds = ExitOnCloseSecs;
                  SetStopLoss(CalculationMode.Ticks, MaxLoss);
                  SetProfitTarget(CalculationMode.Ticks, ProfitTarget);
                  }

                  protected override void OnBarUpdate()
                  {
                  // Undisclosed trading logic to determine UpSignal and DownSignal!

                  ...

                  // Market orders

                  if (UpSignal)
                  {
                  if (ShortOpen)
                  {
                  ExitShort("", "");
                  }

                  if (!LongOpen && SufficientVolume() && CurrentSpread() <= MaxSpread)
                  {
                  EnterLong(LotsPerTrade, "");
                  }
                  }

                  if (DownSignal)
                  {
                  if (LongOpen)
                  {
                  ExitLong("", "");
                  }

                  if (!ShortOpen && SufficientVolume() && CurrentSpread() <= MaxSpread)
                  {
                  EnterShort(LotsPerTrade, "");
                  }
                  }
                  }

                  protected override void OnPositionUpdate(IPosition PositionHandle)
                  {
                  if (PositionHandle.MarketPosition == MarketPosition.Flat)
                  {
                  LongOpen = false;
                  ShortOpen = false;
                  }

                  if (PositionHandle.MarketPosition == MarketPosition.Long)
                  {
                  LongOpen = true;
                  ShortOpen = false;
                  }

                  if (PositionHandle.MarketPosition == MarketPosition.Short)
                  {
                  LongOpen = false;
                  ShortOpen = true;
                  }
                  }

                  protected override void OnOrderUpdate(IOrder OrderHandle)
                  {
                  if (OrderHandle.OrderState == OrderState.Filled)
                  {
                  if (OrderHandle.Action == Action.Buy || OrderHandle.Action == Action.SellShort)
                  {
                  PosOpenPrice = OrderHandle.AvgFillPrice;
                  }
                  else
                  {
                  PosClosePrice = OrderHandle.AvgFillPrice;

                  // If OrderHandle.Action = 'Sell' or 'BuyToCover', and
                  // if OrderHandle.Name = 'Exit on close', and
                  // if Bars.FirstBarOfSession = True, then also
                  // OrderHandle.AvgFillPrice = 0.
                  }
                  }
                  }

                  Comment


                    #10
                    Thank you. We will look into it and get back to you tomorrow.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      This is a bug which will be fixed with next update. Thanks for reporting.

                      Comment


                        #12
                        OK, that explains... thanks.

                        One related question: If I initiate trades from the Control Center while running a strategy, then I presume that OnOrderUpdate() and OnPositionUpdate() methods are called in a similar way than in case of strategy-initiated orders, right? But how can I differentiate between these two cases? By specifying a distinctive signal name when opening or closing a position and comparing it to the current IOrder.Name? How about inside OnPositionUpdate()?

                        Comment


                          #13
                          Manual orders do not interact with NinjaScript strategy orders. They do not "see" each other. If you do both it is up to you to maintain and track your proper account position.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            And therefore OnOrderUpdate() and OnPositionUpdate() methods are NOT called in case of manual orders?

                            Comment


                              #15
                              No. The strategy and all of its methods do not interface with any manual orders.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              639 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
                              572 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X