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

Muliptle TP levels are accumulating instead of assigning each value from the array

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

    Muliptle TP levels are accumulating instead of assigning each value from the array

    The TakeProfitLevels array stores the TP values for EntriesPerTrade

    Assuming there are 4 entries, the TP values might be 40, 60, 80, 100 ticks.

    It should TP each entry to the corresponding value. However it is adding the next TP value to the prior one each time for each entry. So then the TP levels are 40, 100, 180, 280 in this example. Can someone tell me what is causing this?

    The print of the short levels are all correct values.

    The Stoplimit seems to be having a problem too as it doesnt work, but i assume its the same issue

    for (int i = 0; i < EntriesPerTrade; i++)
    {

    takeProfitShortLevels = execution.Order.AverageFillPrice - TakeProfitLevels[i] * TickSize;
    takeProfitLongLevels = execution.Order.AverageFillPrice + TakeProfitLevels[i] * TickSize;

    ExitShortLimit(0, true, execution.Order.Filled, takeProfitShortLevels, "Short TP " + i, "Short " + i);
    ExitShortStopLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + InitialSLAmount, execution.Order.AverageFillPrice, "Initial Short SL " + i, "Short " + i);
    ExitLongLimit(0, true, execution.Order.Filled, takeProfitLongLevels, "Long TP " + i, "Long " + i);
    ExitLongStopLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - InitialSLAmount, execution.Order.AverageFillPrice, "Initial Long SL " + i, "Long " + i);
    PrintTo = PrintTo.OutputTab2;
    Print(DateTime.Now + " " + execution.Order.AverageFillPrice + " short levels: " + i + " " + takeProfitShortLevels);

    }​
    Last edited by sofortune; 08-09-2023, 01:01 PM.

    #2
    Hello sofortune,

    Thank you for your post.

    In your print statement, I do not see that you are printing the value of TakeProfitLevels[i]. This would also be helpful information to understand the behavior as well. I suggest adding prints such as the following:

    Code:
    if (execution.Order.OrderState == OrderState.Filled)
    {
    for (int i = 0; i < EntriesPerTrade; i++)
    {​
    
    double takeProfitShortLevels = execution.Order.AverageFillPrice - TakeProfitLevels[i] * TickSize;
    Print(string.Format("{0} AverageFillPrice: {1} - TakeProfitLevels[i]: {2} * TIckSize: {3} takeProfitShortLevels: {4}", Time[0], execution.Order.AverageFillPrice, TakeProfitLevels[i], TickSize));
    double takeProfitLongLevels = execution.Order.AverageFillPrice + TakeProfitLevels[i] * TickSize;
    Print(string.Format("{0} AverageFillPrice: {1} + TakeProfitLevels[i]: {2} * TIckSize: {3} takeProfitShortLevels: {4}", Time[0], execution.Order.AverageFillPrice, TakeProfitLevels[i], TickSize));
    
    ExitShortLimit(0, true, execution.Order.Filled, takeProfitShortLevels, "Short TP " + i, "Short " + i);
    Print(Time[0] + "ExitShortLimit at limit price: " + takeProfitShortLevels);
    ExitShortStopLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + InitialSLAmount, execution.Order.AverageFillPrice, "Initial Short SL " + i, "Short " + i);
    ExitLongLimit(0, true, execution.Order.Filled, takeProfitLongLevels, "Long TP " + i, "Long " + i);
    Print(Time[0] + "ExitLongLimit at limit price: " + takeProfitLongLevels);
    ExitLongStopLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - InitialSLAmount, execution.Order.AverageFillPrice, "Initial Long SL " + i, "Long " + i);​​
    PrintTo = PrintTo.OutputTab2;
    Print(DateTime.Now + " " + execution.Order.AverageFillPrice + " short levels: " + i + " " + takeProfitShortLevels);
    
    }
    }​
    You could also enable Trace Orders to get more information about orders when they are submitted, changed, rejected, or ignored. This may be done by setting TraceOrders to True in State.SetDefaults or State.Configure. For more details on working with TraceOrders:


    If you are still unsure about the output vs. the prices of your TP orders, please provide the output by right-clicking the NinjaScript Output window and saving as a text file. The text file may then be attached to your reply in the forum.

    Please let me know if I may be of further assistance.​
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily

      I have added print statements to check all the calculations along the way. All of the calculations appear to be correct for the values, but the actual execution is still using the accumulated values for each iteration. I've attached the output and also a screenshot of the trades, it should TP every 5 dollars based on 20 ticks (as the print output also shows) but it is not.





      if (execution.Order.OrderState == OrderState.Filled)
      {
      double takeProfitShortLevels;
      double takeProfitLongLevels;

      for (int i = 0; i < EntriesPerTrade; i++)
      {
      PrintTo = PrintTo.OutputTab2;
      takeProfitShortLevels = execution.Order.AverageFillPrice - TakeProfitLevels[i] * TickSize;
      Print(string.Format("{0} AverageFillPrice: {1} - TakeProfitLevels[i]: {2} * TickSize: {3} takeProfitShortLevels: {4}" + " " + i, Time[0], execution.Order.AverageFillPrice, TakeProfitLevels[i], TickSize, takeProfitShortLevels));
      takeProfitLongLevels = execution.Order.AverageFillPrice + TakeProfitLevels[i] * TickSize;
      Print(string.Format("{0} AverageFillPrice: {1} + TakeProfitLevels[i]: {2} * TickSize: {3} takeProfitLongLevels: {4}" + " " + i, Time[0], execution.Order.AverageFillPrice, TakeProfitLevels[i], TickSize, takeProfitLongLevels));

      // Handle short position TP and SL
      ExitShortLimit(0, true, execution.Order.Filled, takeProfitShortLevels, "Short TP " + i, "Short " + i);
      Print(Time[0] + "ExitShortLimit at limit price: " + takeProfitShortLevels + " " + i);
      ExitShortStopLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + InitialSLAmount, execution.Order.AverageFillPrice, "Initial Short SL " + i, "Short " + i);

      // Handle long position TP and SL
      ExitLongLimit(0, true, execution.Order.Filled, takeProfitLongLevels, "Long TP " + i, "Long " + i);
      Print(Time[0] + "ExitLongLimit at limit price: " + takeProfitLongLevels + " " + i);
      ExitLongStopLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - InitialSLAmount, execution.Order.AverageFillPrice, "Initial Long SL " + i, "Long " + i);


      Print(Time[0] + " " + execution.Order.AverageFillPrice + " short levels: " + i + " " + takeProfitShortLevels);
      Print(Time[0] + " " + execution.Order.AverageFillPrice + " short levels: " + i + " " + takeProfitLongLevels);
      }
      }
      else if (execution.Order.OrderState == OrderState.PartFilled)
      {
      // Handle partial fills if needed
      }
      if (marketPosition == MarketPosition.Flat)
      {
      exitingPosition = false;
      }​

      Click image for larger version

Name:	image.png
Views:	88
Size:	18.6 KB
ID:	1263919
      Attached Files

      Comment


        #4
        Hello sofortune,

        Thank you for your reply.

        Please advise; what is the output result with TraceOrders enabled? Here is more info about Trace Orders from my previous reply:
        You could also enable Trace Orders to get more information about orders when they are submitted, changed, rejected, or ignored. This may be done by setting TraceOrders to True in State.SetDefaults or State.Configure. For more details on working with TraceOrders:
        https://ninjatrader.com/support/help...aceorders2.htm
        I appreciate your patience and look forward to your reply.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Here are the TraceOrders. What i don't really get either is that there are 128 trades taken according to the trades page on the analyzer. But traceorder only shows those few. Not sure if i fully understand how it works.
          Attached Files

          Comment


            #6
            Hello sofortune,

            Thank you for your patience.

            What I recommend is simplifying your strategy to start with only 1 or 2 entries and perform the logic manually outside of the loop to begin with. Next, add a third or fourth entry outside of the loop as well. If this is behaving as expected, then you can comment out (or create a copy of the strategy) the manual logic and test out the loop with 1 entry, then 2, then so on to see if the behavior repeats itself. You could also see if you yield different results using the Playback connection rather than the Strategy Builder, as the Playback connection will build bars tick-by-tick as if the data were being fed in real-time vs. the Strategy Analyzer which uses a historical fill algorithm for order fills.

            For more details on discrepancies between real-time and backtest results:The historical fill processing used by the Strategy Analyzer is described in more detail here:Thank you for your time and patience.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Thanks - i have been trying to debug and I have gotten a bit closer now.This is my current block of code, it is now setting the correct values for TP and SL, but it seems to be setting way too many limit and stoplimit orders (refer to the photo). It is setting EntriesPerTrade qty of stop order at each target price, with the exception for the last one. Seems like it is looping 4 (the value of EntriesPerTrade) times at each price with exception for the last one.... but i really cannot find the issue in the logic.Anything here obviously wrong?

              Click image for larger version

Name:	image.png
Views:	78
Size:	19.2 KB
ID:	1264261
              if (execution.Order.OrderState == OrderState.Filled)
              {
              double takeProfitShortLevels;
              double takeProfitLongLevels;

              for (int i = 0; i < EntriesPerTrade; i++)
              {
              Print("InitialSLAmount is: " + InitialSLAmount + " Loop" + i);
              takeProfitShortLevels = Position.AveragePrice - TakeProfitLevels[i] * TickSize;
              Print(string.Format("{0} AverageFillPrice: {1} - TakeProfitLevels[i]: {2} * TickSize: {3} takeProfitShortLevels: {4}" + " " + i, Time[0], Position.AveragePrice, TakeProfitLevels[i], TickSize, takeProfitShortLevels));
              takeProfitLongLevels = Position.AveragePrice + TakeProfitLevels[i] * TickSize;
              Print(string.Format("{0} AverageFillPrice: {1} + TakeProfitLevels[i]: {2} * TickSize: {3} takeProfitLongLevels: {4}" + " " + i, Time[0], Position.AveragePrice, TakeProfitLevels[i], TickSize, takeProfitLongLevels));

              // Handle short position TP and SL
              if (Position.MarketPosition == MarketPosition.Short)
              {
              ExitShortLimit(0, true, Position.Quantity, takeProfitShortLevels, "Short TP " + i, "Short " + i);
              Print(Time[0] + " " + Position.AveragePrice + " ExitShortLimit at limit price: " + takeProfitShortLevels + " " + i);
              ExitShortStopLimit(0, true, Position.Quantity, Position.AveragePrice + InitialSLAmount, Position.AveragePrice, "Initial Short SL " + i, "Short " + i);
              Print(Time[0] + " " + Position.AveragePrice + " ExitShortStopLimit at limit price: " + (Position.AveragePrice + InitialSLAmount) + " " + i);
              Print(Time[0] + " " + Position.AveragePrice + " short levels: " + i + " " + takeProfitShortLevels);

              }
              // Handle long position TP and SL
              else if (Position.MarketPosition == MarketPosition.Long)
              {
              ExitLongLimit(0, true, Position.Quantity, takeProfitLongLevels, "Long TP " + i, "Long " + i);
              Print(Time[0] + " " + Position.AveragePrice + " ExitLongLimit at limit price: " + takeProfitLongLevels + " " + i);
              ExitLongStopLimit(0, true, Position.Quantity, Position.AveragePrice - InitialSLAmount, Position.AveragePrice - InitialSLAmount, "Initial Long SL " + i, "Long " + i);
              Print(Time[0] + " " + Position.AveragePrice + " ExitLongStopLimit at limit price: " + (Position.AveragePrice - InitialSLAmount) + " " + i);
              Print(Time[0] + " " + Position.AveragePrice + " long levels: " + i + " " + takeProfitLongLevels);
              }

              }
              initialLimitsSet = true;
              }​
              Attached Files

              Comment


                #8
                Hello sofortune,

                Thank you for your reply.

                You are using the value of Position.Quantity to determine the quantity in your exit methods. Since you are using fromEntrySignal, it should tie the exit to the entry and use the quantity represented by the entry that matches fromEntrySignal. You could enable Trace Orders to see if any of the orders are being submitted more than once and better understand the behavior:


                I see you are setting isLiveUntilCancelled to true; you should also consider using Order object variables to retain a reference to the order object for your entries and exits, then assign the order objects to the appropriate variables. You could add a check to only submit the exit order if the exit order object is null. order object assignment is mentioned in the description of the order class here:


                We also have a reference sample that keeps track of order objects and live until cancelled orders here:


                Thank you for your time and patience.
                Emily C.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Segwin, 05-07-2018, 02:15 PM
                14 responses
                1,789 views
                0 likes
                Last Post aligator  
                Started by Jimmyk, 01-26-2018, 05:19 AM
                6 responses
                837 views
                0 likes
                Last Post emuns
                by emuns
                 
                Started by jxs_xrj, 01-12-2020, 09:49 AM
                6 responses
                3,294 views
                1 like
                Last Post jgualdronc  
                Started by Touch-Ups, Today, 10:36 AM
                0 responses
                13 views
                0 likes
                Last Post Touch-Ups  
                Started by geddyisodin, 04-25-2024, 05:20 AM
                11 responses
                63 views
                0 likes
                Last Post halgo_boulder  
                Working...
                X