Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position.AvgPrice coming back 0

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

    Position.AvgPrice coming back 0

    Here's the basic structure:

    OnBarUpdate()
    {
    if (condition)
    GoLong();
    }

    GoLong()
    {
    SetStopLoss("Long1", CalculationMode.Price, (Position.AvgPrice - myVar), false);
    SetProfitTarget("Long1", CalculationMode.Price, myVar2);
    EnterLong(DefaultQuantity, "Long1");
    }

    Now, on the surface this looks fine to me, but it wasn't giving the expected results.
    I stuck a ton of print statements in there, and everything is fine except Position.AvgPrice is coming back 0 at every step. The print function was called from GoLong(). As a result, none of the stops are being set correctly. Is it a problem of the ordering I'm calling things here? Thanks guys.

    *edited to add this is for an FX trade, maybe that has something to do with it
    Last edited by Locke; 01-14-2015, 08:55 PM.

    #2
    Locke, the reason would be there's no meaningfull AvgPrice to work with until the entry has filled. You could check for the price being greater than 0 or for the Position.MarketPosition to be non flat before submitting your stops / targets. A quicker feedback would be given by monitoring the more advanced OnExecution method for the incoming entry fill event and then sending the exits.

    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()

    Comment


      #3
      Ok, followup:

      1. Had I used another value I had for sure instead of Position.AvgPrice (or calced based of ticks), would the managed orders of NT work correctly using this structure? Where SetStopLoss SetProfitTarget, and EnterLong are all called in the same function? Is this ok practice or bad practice?

      2. How about the below structure ? Is this one correct use of OnExecution?

      OnBarUpdate()
      {
      if (condition)
      GoLong();
      }

      GoLong()
      {
      EnterLong(DefaultQuantity, "Long1");
      }

      protected override void OnExecution(IExecution execution)
      {
      if (Position.MarketPosition == MarketPosition.Long)
      {
      SetStopLoss("Long1", CalculationMode.Price, (Position.AvgPrice - myVar), false);
      SetProfitTarget("Long1", CalculationMode.Price, myVar2);

      }
      // end if

      if (Position.MarketPosition == MarketPosition.Short)
      {
      SetStopLoss("Short1", CalculationMode.Price, (Position.AvgPrice + myVar), false);
      SetProfitTarget("Short1", CalculationMode.Price, myVar2);

      }
      // end if
      }

      Comment


        #4
        Locke, if you have a static stop / target based on a set tick amount you would just have those Set calls in the Initialize() method.

        For the OnExecution() you want to assign an IOrder to your Enter() method and then filter your feedback for just that order object, so you know the entry has been reported filled thus the position created.



        Otherwise if you don't filter you just get to 'listen' to feedback from all your executions since the OnExecution() handler would be called for each execution seen by the script.

        Comment


          #5
          Bertrand thanks for the reply.

          When using IOrder, do you have to reset entryOrder = null at some point anywhere?
          So you set entryOrder = EnterLong() first somewhere.

          1. If you just flip from long to short do you need to set entryOrder = null before flipping?
          2. If you have a stop or takeprofit first, when you are flat do you need to set entryOrder = null then?

          Or can you just keep overwriting it?

          Thanks much.

          Comment


            #6
            Hello,

            Thank you for the questions.

            If you are just changing directions, as long as the order that is changing the direction is also assigning your variable there would not be a need to set this to null because you are changing its value immediately. Here is a simple example if you were currently long and have a statement like

            IOrder entryOrder = null;

            protected override OnBarUpdate()
            {
            entryOrder = EnterShort();
            }

            The EnterShort will exit the long position and assign the new order to entryOrder all in one shot. This would be a direct re assignment of the variable entryOrder so there would be no need to set null to the variable first.

            For your second question, if your entry order is closed by a stop loss or profit target this is a different story. These will not directly set the variable to null or re assign a new value so you would need to check if the order was filled and if so set the entryOrder variable to null to keep your logic running correctly. Generally you can check this in the OnOrderUpdate method to see if the order was filled or any other status it may have been in.


            Please let me know if I may be of additional assistance.

            Comment


              #7
              Thanks Jesse, starting to get it.

              I've only been using OnBarUpdate and OnExecution up until now.
              For OnOrderUpdate, what am I going to check for? I don't want to just see if the order was filled and then clear it, because I am trying to store the traded price and use it elsewhere.

              That is, in OnExecution, I am checking if entryOrder != null && entryOrder == execution.Order , and then I am saving down tradedPx = execution.Order.AvgFillPrice. I am then using tradedPx to help SetStopLoss and SetProfitTarget within OnExecution.

              If I set entryOrder = null in OnOrderUpdate, will that clear it before OnExecution can save the variable?

              From the help guide:
              CRITICAL: If you want to drive your strategy logic based on order fills you must use OnExecution() instead of OnOrderUpdate(). OnExecution() is always triggered after OnOrderUpdate(). There is internal strategy logic that is triggered after OnOrderUpdate() is called but before OnExecution() that can adversely affect your strategy if you are relying on tracking fills within OnOrderUpdate().

              Would it make sense to reset entryOrder at the end of OnExecution?
              Last edited by Locke; 01-15-2015, 02:17 PM.

              Comment


                #8
                Hello,

                Thank you for the reply.

                The OnOrderUpdate was just an example of a place in NinjaScript where IOrders are commonly used, I am sorry I should have used OnExecution in the explanation of this instead.

                For what you are doing you would be correct to get the Average fill price from the execution its self. In this case you would only want to assign the entryOrder variable to null once you have placed the orders that are using its value.

                What I mean is the order of your logic would be important in this case because if you set the variable to null before you use it, you will have an error of the price being empty.

                If you use the value it holds for your targets and then set it to null in preparation for the next time this happens that would be fine.

                Mainly you need to ensure you actually have a value for the targets to use. Once they have used this value you would be free to release the variable or set it to null to clean up.

                I look forward to being of further assistance.

                Comment


                  #9
                  Thanks again Jesse. I'll stick it at the end of OnExecution for now and see if it breaks anything else. Appreciate it.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  673 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  379 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  111 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  578 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  582 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X