Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

setStopLoss and setProfitTarget orders being cancelled

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

    setStopLoss and setProfitTarget orders being cancelled

    Hi,

    GOAL:
    My goal is to build pyramid trade entry logic such that the more a trade goes in my favor, as certain prices cross, a new entry order is executed.


    APPROACH:
    I have an original order I used a simple EnterLongLimit and use the SetStoppLoss and SetProfitTarget as you'll see in the attached code snippet.

    Using a long as an example:

    From reading this forum, I've understood that using a EnterLongStopLimit and setting my stop price and limit price is a way to achieve my entry logic, when prices crosses the stop price, submit a limit order for my entry. This appears to be working well in the code provided for the "Buy SLM" part. If price crosses, it will indeed execute the Buy SLM order.

    For each of those EnterStopLongLimit orders I have associated setStopLoss and setProfitTarget orders with the appropriate signal name.

    ISSUE:
    The issue I have is those associated setStopLoss and SetProfitTarget orders are being cancelled for some reason, so that when a pyramid entry executes we get the new position but no new target/stop orders.

    I've tried looking into isLiveUntilCancelled, but not sure how to use it, or if even possible for these order types.

    Can you please advise how I might achieve my requirements? Thanks!


    Attached is a sample log of a single trade setup that executes, places the orders, but then shows the cancellations.

    Code Snippet for pyramid logic:

    // Pyramid Logic Function
    private void ExecutePyramidLong(double entryPrice, double stopPrice, int contractsToTrade, double targetPrice, double rr)
    {
    double stopSize = Math.Abs(entryPrice - stopPrice); // Calculate the stop size
    Print("Target RR: " + rr); //Debug statement

    for (int i = 1; i <= rr; i++) // Loop from 1x to target RR
    {
    // Calculate the appropriate Entry and stop prices for the pyramid order
    double pyramidEntryPrice = entryPrice + (i * stopSize); // Entry price for the pyramid trade
    double pyramidStopPrice = pyramidEntryPrice - stopSize ; // Stop price for the pyramid trade (new entry - stop size)
    // Generate a unique signal name for each pyramid trade
    string pyramidSignalName = "PyramidEntry_" + i;

    // Set stop loss and profit target for the pyramid trade
    SetStopLoss(pyramidSignalName, CalculationMode.Price, pyramidStopPrice, false);
    SetProfitTarget(pyramidSignalName, CalculationMode.Price, targetPrice);

    // Enter the pyramid long trade using EnterLongStopLimit
    EnterLongStopLimit(contractsToTrade, pyramidEntryPrice, pyramidEntryPrice, pyramidSignalName);

    // Debug print statements
    Print("pyramidSignalName: " + pyramidSignalName + ", Entry Price = " + pyramidEntryPrice + ", Stop Price = " + pyramidStopPrice);
    }

    }​
    Attached Files
    Last edited by farawayz; 10-13-2024, 10:12 AM.

    #2
    Hello farawayz,

    TraceOrders should state why the order is being cancelled.
    Printing the order.ToString() in OnOrderUpdate() will give information about what order states all orders are entering.

    Please enable TraceOrders and print the order.ToString() object in OnOrderUpdate().

    Below is a link to a support article on using TraceOrders and adding debugging prints to understand behavior.


    Save the output to a text file (right-click the output window > select Save as) then attach the text file to your next post.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chelsea.

      I do have TraceOrders turned on and I believe the attached file of logs is the result of that, for example here is what we get for the cancelled messages:
      Order='81c2eba7f3854529bb84919da2ba690c/Playback101' Name='PyramidEntry_8' New state='Cancelled' Instrument='ES 12-24' Action='Buy' Limit price=5784.75 Stop price=5784.75 Quantity=3 Type='Stop Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      Order='81c2eba7f3854529bb84919da2ba690c/Playback101' Name='PyramidEntry_8' New state='Cancel submitted' Instrument='ES 12-24' Action='Buy' Limit price=5784.75 Stop price=5784.75 Quantity=3 Type='Stop Limit' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      I will try the additional steps of adding a "OnOrderUpdate" function and the order.toString option as you have advised and report back.

      Thanks

      Comment


        #4
        Hello farawayz,

        Once the debugging print is added, be sure to attach the saved text file so that we can see what is happening in context.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chelsea I will do that. Will be a little later in the day, appreciate your help.

          If you happen to know of any generic pyramid code logic to compare against, please do let me know. Thanks!

          Comment


            #6
            Hi Chelsea, just FYI I'm still working on this and will come back when I have enough understanding to bring you something.

            Comment


              #7
              HI Chelsea, thanks to your help I figured this out. Appreciate it.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              41 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              124 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              64 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              41 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X