Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Avg.Fill.Price

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

    Avg.Fill.Price

    I just downloaded SampleOnOrderUpdate and I am having difficulties with the changing Position.AveragePrice to entryOrder.Avg.FillPrice. I want the breakeven to use the average price of the specific IOrder in this case entryOrder. When I replace Position.AveragePrice with entryOrder.Avg.FillPrice then backtesting is not godd.




    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Submit an entry limit order if we currently don't have an entry order open
    if (entryOrder == null && CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorHigh[0] && ToTime(Time[0]) > ToTime(8, 30, 0) && ToTime(Time[0]) < ToTime(8, 46, 0))
    {
    /* The entryOrder object will take on a unique ID from our EnterLong()
    that we can use later for order identification purposes in the OnOrderUpdate() method. */
    entryOrder = EnterLong(1, "MyEntry");
    }

    /* If we have a long position and the current price is 4 ticks in profit, raise the stop-loss order to breakeven.
    We use (7 * (TickSize / 2)) to denote 4 ticks because of potential precision issues with doubles. Under certain
    conditions (4 * TickSize) could end up being 3.9999 instead of 4 if the TickSize was 1. Using our method of determining
    4 ticks helps cope with the precision issue if it does arise. */
    if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (7 * (TickSize / 2)))
    {
    // Checks to see if our Stop Order has been submitted already
    if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
    {
    // Modifies stop-loss to breakeven
    stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice, "MyStop", "MyEntry");
    }
    }

    #2
    Hi flexi,

    The code you have posted is still using Position.AvgPrice.

    What code are you currently using to use the entryOrder.AvgFillPrice?

    Are you checking that the entryOrder is not null?

    If you print the entryOrder.AvgFillPrice just before calling it, what is the value that prints?
    http://www.ninjatrader.com/support/h.../nt7/print.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      That seems the problem. Can you give me example of the code that I should implement in order to
      use the AvgFillPrice ?

      Comment


        #4
        Hi flexi,

        In the conditions you would use the entryOrder.AvgFillPrice.

        For example:
        if (Position.MarketPosition == MarketPosition.Long && entryOrder != null)
        Print(entryOrder.AvgFillPrice.ToString());

        if (Position.MarketPosition == MarketPosition.Long && entryOrder != null && Close[0] >= entryOrder.AvgFillPrice + (7 * (TickSize / 2)))
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Now when I put that code back-testing is working but it is not moving the stop to breakeven.
          So it is ignoring completely this commands. It is only taking profit and make loses, no breakeven. Can you tell me if everything is ok now? I put my whole code in the attachment.


          {
          if (Position.MarketPosition == MarketPosition.Long && entryOrder != null)
          {
          Print(entryOrder.AvgFillPrice.ToString());
          }

          if (Position.MarketPosition == MarketPosition.Long && entryOrder !=null && Close[0] >= entryOrder.AvgFillPrice + (160 * (TickSize / 2)))
          {
          // Checks to see if our Stop Order has been submitted already
          if (stopOrder != null && stopOrder.StopPrice < entryOrder.AvgFillPrice)
          {
          // Modifies stop-loss to breakeven
          stopOrder = ExitLongStop(0, true, stopOrder.Quantity, entryOrder.AvgFillPrice, "MyStop", "MyEntry");
          }
          Attached Files
          Last edited by flexi; 09-03-2014, 10:49 AM.

          Comment


            #6
            Hi flexi,

            What is the printed entryOrder.AvgFillPrice that is showing in the Output window?

            Are there any errors in the log?

            What price is the stop being initially set to?
            What is showing as the price the stop is being modified to?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              entryOrder.AvgFillPrice was not printing on the output window. But I moved the whole condition in the OnExecution part and no is printing fine. The only problem now is the following condition is not working in the OnExecution part:

              Close[0] >= execution.Order.AvgFillPrice

              Is there some alternative for the closing price in the OnExecution part?

              Comment


                #8
                Hi flexi,

                The entryOrder.AvgFillPrice will print as long as there is an order that was saved to the entryOrder IOrder handle.

                This is not in the code of post #5 so I am assuming that the code that places the order is the same. The price should print 1 bar after the order is entered.

                What code is currently being used to place the order?

                Is entryOrder still null when you use it?


                Close[0] is available in OnExecution(). If you print close what prints to the output window?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Ok Close is now fine when I addes brackets. But when I return the entryOrder.AvgFillPrice in the
                  protected override void OnBarUpdate() part there is no print in the output windows and no errors as well. Bellow is the whole context so please tell me if I miss something

                  protected override void OnBarUpdate()
                  {
                  // Submit an entry limit order if we currently don't have an entry order open
                  if (entryOrder == null && CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorHigh[0] && ToTime(Time[0]) > ToTime(8, 30, 0) && ToTime(Time[0]) < ToTime(8, 46, 0))
                  {
                  /* The entryOrder object will take on a unique ID from our EnterLong()
                  that we can use later for order identification purposes in the OnOrderUpdate() method. */
                  entryOrder = EnterLong(1, "MyEntry");
                  }

                  /* If we have a long position and the current price is 4 ticks in profit, raise the stop-loss order to breakeven.
                  We use (7 * (TickSize / 2)) to denote 4 ticks because of potential precision issues with doubles. Under certain
                  conditions (4 * TickSize) could end up being 3.9999 instead of 4 if the TickSize was 1. Using our method of determining
                  4 ticks helps cope with the precision issue if it does arise. */

                  {
                  if (Position.MarketPosition == MarketPosition.Long && entryOrder != null)
                  Print(entryOrder.AvgFillPrice.ToString());



                  if (Position.MarketPosition == MarketPosition.Long && entryOrder !=null && (Close[0] >= entryOrder.AvgFillPrice + (80 * (TickSize / 2))))
                  {
                  // Checks to see if our Stop Order has been submitted already
                  if (stopOrder != null && stopOrder.StopPrice < entryOrder.AvgFillPrice)
                  {
                  // Modifies stop-loss to breakeven
                  stopOrder = ExitLongStop(0, true, stopOrder.Quantity, entryOrder.AvgFillPrice, "MyStop", "MyEntry");
                  }

                  Comment


                    #10
                    Hi flexi,

                    With your curly braces cannot tell what is going on.

                    {
                    if (Position.MarketPosition == MarketPosition.Long && entryOrder != null)
                    Print(entryOrder.AvgFillPrice.ToString());

                    Where is the if statement proceeding this?
                    If there is no if statement, why is this curly brace here?

                    If this is the complete code, then there is a missing closing curly brace.

                    Perhaps it would help if you export your code and post the export.

                    To export your script do the following:
                    1. Click File -> Utilities -> Export NinjaScript
                    2. Enter a unique name for the file in the value for 'File name:'
                    3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
                    4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


                    By default your exported file will be in the following location:
                    • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


                    Below is a link to the help guide on Exporting NinjaScripts.
                    http://www.ninjatrader.com/support/h...nt7/export.htm
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I attached my code. I am reall not sure why

                      Print(entryOrder.AvgFillPrice.ToString()); is not returning anything in the output window

                      Thanks for your time
                      Attached Files

                      Comment


                        #12
                        Hi flexi,

                        Thanks for the export.

                        (edited)
                        The issue is that you are setting entryOrder to null when it fills and not when the trade is exited in OnOrderUpdate and OnExecution.

                        This means nothing will print in OnBarUpdate because you have basically erased that variable by setting it to null.
                        Last edited by NinjaTrader_ChelseaB; 09-04-2014, 07:04 AM.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks for the quick replay.

                          So is there a way that I can disable setting the entryOrder to null when it fills?

                          Comment


                            #14
                            Hi flexi,

                            It would be preferable to set the entryOrder to null once the exit order has filled.

                            Also, if you are asking how to not set entryOrder to null once it fills, just remove the code. Its on line 113. (The one on line 88 is ok, I see now this is for when the entryOrder is cancelled.)
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            633 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            364 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            105 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            567 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            568 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X