Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

average fill price

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

    average fill price

    Hi, I'm building a kinda Martingale system with the unmanaged approach and I'm having trouble with the order.AverageFillPrice

    It will often give me the wrong value (I'm monitoring with ChartTrader) as well as go to 0. I want new orders to open only on the other side i.e. profitable territory

    I'm calculating my average entry price in OnOrderUpdate, is that the best place to do it?

    Thank you

    Best
    Karl

    #2
    Hello kalli44100,

    Thank you for your post.

    What is the orderState of the order when you are seeing the incorrect fill prices?

    Could you also be specific as to where exactly you are seeing the incorrect information? For example, are you seeing this incorrect info in the order fill price as seen in the Orders tab?

    It would be expected that the AverageFillPrice is 0 in realtime at some points. For example, checking right immediately after submitting an order - at this time it is possible the order is not filled yet. Only after it is filled will it have the AvgFillPrice populated.



    I recommend debugging using prints - print out the name and OrderState along with the AverageFillPrice.

    Please let us know if you have any further questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      When I readd your response, I thought the issue might be related to the OCO part where I'm creating a new ordergroup every time and it's giving me the average fill price of each new order instead of the cumulative one. Would that be possible?

      This is how i'm sending the order (it's within a for loop ergo the "i"):
      SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Limit, 1, roundedPrice, 0, "","LongPos"+i);

      To avoid the entryPrice=0 issue, i did this in OnOrderUpdate:
      if(Position.MarketPosition != MarketPosition.Flat){entryPrice=order.AverageFillP rice;Print("entryPrice="+entryPrice);}

      Does the "" in SubmitOrderUnmanaged create a new OCO group every time? should I just stick to one String then?

      Comment


        #4
        Hello,

        Thank you for your response.

        Using an empty string "" for the oco parameter means you are not specifying an OCO id, so no orders are being tied together via OCO.

        Additionally, OnOrderUpdate() is called per order, not cumulatively. If you print out the name, orderState, AverageFillPrice you can see this more clearly.

        I recommend reviewing this sample script for the Unmanaged Approach which demonstrates submitting orders tied via OCO.



        Please let me know if I can assist further.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Thank you for the example Gaby!

          So when I entered a string into the oco slot it gave me the error that it cannot reuse this oco. Does oco only involve the order and its TP and SL? is the adding to the order unrelated to oco? As in I thought that adding all my orders to a single oco would resolve the issue but now I'm just getting errors that cancel the script,, doh:P Do I have to use oco at all for this purpose as I am not using TP and SL orders? I'm just using Limit orders for this particular strategy. I lookeed at the example but couldn't quite applyy it to this scenario.

          Comment


            #6
            Hello,

            This means you are reusing the same OCO ID for a new pair of orders - OCO IDs cannot be reused. The sample script from my previous post demonstrates how to avoid getting this error when using the Unmanaged Approach and tying orders together via OCO using GetAtmStrategyUniqueId().



            If you are not looking to tie orders together via OCO (one cancels the other) like for stop loss and profit target orders then it is not necessary to use an OCO id. You can leave it as the empty string or null.
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Ok great so I don't need to deal with oco in this case, phew

              I went with the onexecutionupdate example seeing that onordereupdate doesn't seem to be the right place to put quantity and entryprice calculations. but i'm getting some errors. I went with the last example on the page: https://ninjatrader.com/support/help...tionupdate.htm
              It's saying the list does not contain a definition for 'IsNullOrEmpty' and 'entryQuantitiy' does not exist.

              Would it be betteer to use the example just above this one where it calculates for any order and not longs and shorts separately?

              I know in your example it uses a variation of those. But would be good to know what the current errors entail.
              Thank you

              Karl

              Comment


                #8
                Hello Karl,

                What is leading you to believe OnOrderUpdate is not updating with the correct quantity and entry price?

                What are the specific errors you are getting? Can you provide a screenshot?
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  The errors are the ones I mentioned above with regard to the linked tutorial.

                  Re onorderupdate, it seems to update the quantity and average price when there is a new limit order but not when there is a new execution, so for this script the updating of quantity and avg fill price is too slow and onorderexectution seems to be the better choice. Sorry, there were no errors on orderupdate, just not the right choice in this context (I think).

                  The errors were regarding 'IsNullOrEmpty' and 'entryQuantitiy1'

                  Comment


                    #10
                    Hello,

                    Who are you connected to for data / who is your broker? If you are using Rithmic or IB, this would be expected and it would be correct to use the Rithmic/IB friendly examples.

                    With these two brokers, the events from OnOrderUpdate(), OnPositionUpdate(), and OnExecutionUpdate() are not guaranteed to arrive in a specific order. The key is to have your strategy set up so that it does not depend on Order, Position, or Execution information to be obtained in any order.

                    We have sample scripts that are Rithmic/IB friendly using the Unmanaged Approach that can be found here:



                    That code is just meant to be a sample snippet - it doesn't have entryQuantity1 defined in the sample. You would need to define a variable called entryQuantity1. Have you defined the LongEntry1Prices list in order to use isNullOrEmpty?

                    The full code for this sample snippet is from ManagedRithmicIBFriendlySimpleExample if you want to take a look at how to properly implement this.

                    I recommend importing the RithmicIBFriendlyExamples code from the linked post above and looking at the code for UnmanagedRithmicIBFriendlyExample, since you are using the Unmanaged Approach.
                    Gaby V.NinjaTrader Customer Service

                    Comment


                      #11
                      The account I'm using for this is a Tradovate account. Does that mean some of the script won't work?

                      Yeah I thought as much re the entryQuantity1 but was confused that I could not see it. With regard to isNullOrEmpty, I just defined the private list variable as seen in the example. Ah you mean the list needs to be defined properly and that isn't in the snipped, ok.

                      Do I need separate long and short tracking or would it be ok to just track the current order regardless of direction (since it doesn't particularly matter in this case for me)

                      Comment


                        #12
                        I'm looking through the examples but I cannot find where the Lists are properly defined so I''m still stuck with this error: 'List<double>' does not contain a definition for 'IsNullOrEmpty' and no accessible extension method....

                        Comment


                          #13
                          Hello,

                          With that connection, my previous response wouldn't apply. OnOrderUpdate(), OnPositionUpdate(), and OnExecutionUpdate() would arrive in a specific order. Without seeing the output from the prints I've previously mentioned, I can't provide much insight into your claims that the quantity/average price is being reported incorrectly. If you would like assistance with this, please provide the output for analysis.

                          It would be advisable to separate tracking of your long and short positions. Have you looked at the UnmanagedTemplate sample script to see how it handles long and short orders separately?



                          I would highly recommend studying this example.
                          Gaby V.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Gaby,

                            I followed the example and will keep long and short separate, thank you for your recommendation.

                            Instead of IsNullOrEmpty I just put: if (LongEntry1Prices==null) and it seems to work. I could not for the life of me find a further definition of the List including the IsNullOrEmpty, are you sure it's in there somewhere? Anyway I took it out.

                            So this is my current OnExecutionUpdate code, taken more or less from the example:
                            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                            {
                            if (execution.Name == "LongPos"){
                            sumFilledLong1 += execution.Quantity;size=sumFilledLong1;Print("Size Long="+sumFilledLong1);
                            if (LongEntry1Prices==null)LongEntry1Prices = new List<double>();
                            //if (LongEntry1Prices.IsNullOrEmpty())LongEntry1Prices = new List<double>();
                            for (int i = 0; i < execution.Quantity; i++)LongEntry1Prices.Add(execution.Price);

                            //entryPrice = 0;
                            for (int i = 0; i < LongEntry1Prices.Count; i++){
                            entryPrice += LongEntry1Prices[i];
                            entryPrice /= LongEntry1Prices.Count;
                            Print("entryPrice Long = "+entryPrice);
                            }

                            if (sumFilledLong1 == entryQuantity1){
                            size=sumFilledLong1;Print("Size Long="+sumFilledLong1);
                            sumFilledLong1 = 0;
                            LongEntry1Prices.Clear();
                            }
                            }



                            So now Size is working nicely
                            EntryPrice was working initiallyy but then now cocmes out like this:
                            Size Short=1
                            entryPrice Short = 2340.2​
                            Size Short=2
                            entryPrice Short = 2340.2
                            entryPrice Short = 2340.2​
                            Size Short=3
                            entryPrice Short = 1560.13333333333
                            entryPrice Short = 1300.11111111111
                            entryPrice Short = 1213.6037037037​
                            Size Short=4
                            entryPrice Short = 888.450925925926
                            entryPrice Short = 807.162731481481
                            entryPrice Short = 786.96568287037
                            entryPrice Short = 782.041420717592​​

                            This is GC/Gold, average fill price is at 2340.6 at 4 contracts short. Looks like too much division going on but should be correct from the example, not sure what's wrong,, any idea?

                            Thank you Gaby

                            Comment


                              #15
                              I think I put the division by ycount for the average inside the for loop, oups:P just testing but hopefully that was the issue
                              corrected ccode snipped:

                              for (int i = 0; i < LongEntry1Prices.Count; i++){entryPrice += LongEntry1Prices[i];}
                              entryPrice /= LongEntry1Prices.Count;
                              Print("entryPrice Long = "+entryPrice);

                              on another note, where would i check if a limit order has been placed? I want to count the existing limit orders (which I'm doing by count++ for every spot in my List of Levels) but then the count goes wrong if the level qualfies but the in itself but doesn't qualify for a limit order to be placed because of proximity to current price etc. Iow I only want to add to the counter if the limit order was really placed. Is that onorderupdate now?​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jaybedreamin, Today, 01:08 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by Rheiverson, 07-18-2024, 04:28 PM
                              2 responses
                              18 views
                              0 likes
                              Last Post Rheiverson  
                              Started by p1kn1t, Today, 11:32 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post p1kn1t
                              by p1kn1t
                               
                              Started by Bob-Habanai, Today, 10:27 AM
                              0 responses
                              10 views
                              0 likes
                              Last Post Bob-Habanai  
                              Started by bortz, 11-06-2023, 08:04 AM
                              57 responses
                              2,762 views
                              0 likes
                              Last Post bortz
                              by bortz
                               
                              Working...
                              X