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 price strategy

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

    Average price strategy

    good morning ,
    I would like to develop a strategy that uses up to 3 entries at different and distant prices and that manages the position as the only average price that will be closed to a take profit set on the average price.
    example :
    FDax Future, (trend short):
    - 1st entry long: pmc 10125, 1 quantity
    - 2nd entry long: pmc 10050, 1 quantity
    - 3rd entry long: pmc 10000, 1 quantity

    for a total of :
    quantity = 3 pcs
    Pmc = ((10000 + 10050 + 10125) / 3) = 10058

    and I set the take profit to 10 pbs (point)

    so what I want is that all positions are closed at 10058 + 10 = 10068

    In the strategies that I have built up to now I have not succeeded in fact each position is managed as a single and taking the example of the 3 previous inputs at the time of market return (change from short to long) is closed one position at a time, and the first will be the one open at 10000 which is closed at 10010.
    Then the second one will be closed at 10060 and finally the third closed at 10135.

    thanks for a tip

    #2
    Hello marubozu79,

    Thank you for the post.

    If you need to place a stop relative to your average entry I would recommend using Position.AveragePrice and ExitLongStopMarket to create the stop. Position.AveragePrice will give you your average entry price, so you can place an exit order using that price. If you then enter an additional contract, you can use ChangeOrder() to update the ExitLong order to proper values.

    Please let me know if you have any questions.
    Last edited by NinjaTrader_ChrisL; 01-03-2019, 10:24 AM. Reason: Fixed link
    Chris L.NinjaTrader Customer Service

    Comment


      #3

      I tried the "ChangeOrder" but it does not work properly and therefore does not enter the second order.
      Is there a strategy to take as an example on your portal?
      thank you

      I paste the code:

      protected override void Initialize()
      {
      CalculateOnBarClose = true;
      DefaultQuantity = quantityEntry;
      TraceOrders = true;
      Unmanaged = true;
      }
      protected override void OnBarUpdate()
      {

      // Condition set 1 - entry long if flat
      if ( CurrentBar>1
      && ....
      && ToTime(Time[0]) < ToTime(21, 20, 0)
      )
      {
      buyOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, Low[0] + 4 * TickSize, 0, "", "Buy Below");

      }

      // Condition set 2 - entry long if long
      if ( CurrentBar>1
      && ....
      && ToTime(Time[0]) < ToTime(21, 20, 0)
      ​​​​​​​)
      {
      ChangeOrder(buyOrder, 1, Low[0] + 4 * TickSize,0);
      }
      }

      Comment


        #4
        Hello marubozu79,

        Thank you for your patience.

        This concept turned out to be a little more complex for the managed approach than I originally thought. I am working on an example strategy now.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hello marubozu79,

          Thank you for your patience.

          I have attached a strategy that demonstrates how to change your stop loss when another entry appears. The sample is contrived, it will enter one contract for each bar up to three bars. Every time the average entry price changes, the stop loss changes it's quantity and price relative to average entry. To test it I recommend running the simulated data feed and turn it all the way up. This strategy uses the OnPosistionUpdate and OnExecutionUpdate event methods. OnExecutionUpdate is used to make sure the first entry order is filled before setting the initial stop. OnPosistionUpdate is used to detect changes in the average entry price and make the order change.

          To import the example go to Tools>Import>NinjaScript addon.

          Please let me know if I can assist further.
          Attached Files
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            first of all, thank you, you are very helpful,
            NT7 does not compile your code, it gives me a lot of errors. Do you have any idea why?
            thank you

            Comment


              #7
              Hello marubozu79,

              Thank you for the reply.

              My apologies, I did not notice we were in the NinjaTrader 7 section of the forum. I will work on converting the example above.

              Thanks in advance for your patience.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                thanks, I'm waiting
                Good weekend

                Comment


                  #9
                  Hello marubozu79,

                  Thank you for your patience.

                  I have attached the NT7 version of this sample. Note that the unmanaged approach must be used in order to use ChangeOrder() in NinjaTrader 7.

                  Please let me know if you have any questions.
                  Attached Files
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    ok, thanks now the stop loss management and the change order is very clear.

                    All I have left is a problem related to placing the order like OrderType.Limit.
                    Indeed, it happens that by setting up for example:

                    SubmitOrder (0, OrderAction.Buy, OrderType.Limit, 1, Close [1], 0, "", "EntryOrder" + counter);

                    Several pending, Accepted and Working orders are generated that enter simultaneously when the Price == Close [1] condition occurs.
                    I should instead distance these orders from N Tick as indicated in the first post.

                    How can I check the entry Pending orders; the counter "++ counter;" you indicated for the OrderType.Market order is now no longer working.

                    thank you very much.

                    Comment


                      #11
                      Hello marubozu79,

                      Thank you for the reply.

                      It is expected for anyone that follows the example that I posted to tailor the entry logic to enter the market based on real trade conditions. The example simply enters one contract after the other on each consecutive bar and moves the stop as a proof of concept. I would need to see how you are interweaving the logic into your own strategy. Could you please export the code and post the .zip file here?

                      I look forward to your reply.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello Chris
                        ok,
                        I attach what I have done so far.
                        There is a problem with multiple input and non-efficient Limit output.
                        I recommend market raplay on Tf 1 min.
                        Thanks again
                        Attached Files

                        Comment


                          #13
                          Hello marubozu79,

                          Thank you for the reply.

                          I fixed up the strategy a bit here. initialStopLossSet was never getting set back to true so the next time you go from flat to long it would not set the initial stop. As for the additional entry logic that is something you would need to debug as I can not design entry/exit logic for client's strategies.

                          Please let me know if you have any questions.
                          Attached Files
                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            Thanks for the stoploss correction.
                            The questions that arise are these: what does it mean and how should I do to debug? what does client logic mean?
                            Is there a simple way of not generating pending orders if one already exists, or on the contrary when I generate a pending order, how can I delete / stop the pending order already present?
                            Thanks for the reply

                            Comment


                              #15
                              Hello marubozu79,

                              Thanks for the reply. By custom logic I mean the code that our customers write. If you don't want another order to go through while you already have a resting order, just set up a boolean variable and use it as a flag in OnOrderUpdate. Set the flag when your pending order is accepted, then the strategy will know to not enter another pending order if the flag is set.

                              Please let me know if I can assist further.
                              Chris L.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kempotrader, Today, 08:56 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post kempotrader  
                              Started by kempotrader, Today, 08:54 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post kempotrader  
                              Started by mmenigma, Today, 08:54 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post mmenigma  
                              Started by halgo_boulder, Today, 08:44 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post halgo_boulder  
                              Started by drewski1980, Today, 08:24 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post drewski1980  
                              Working...
                              X