Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue with SL not moving to BE

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

    #16
    Originally posted by rockmanx00 View Post
    I think I got it to work. I tried to use stopOrder = execution.Order but that didn't seem to work for me. I took a short and didn't close until the end of day. So I did this instead...

    I added a targetOrder variable and replaced the code within OnExecutionUpdate with
    if (execution.Order != null && execution.Order.Name == "Long")
    {
    stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Price - 50 * TickSize, "buyStop", "Long");
    targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Price + 100 * TickSize, "buyTP", "Long");
    }​

    Same logic for Shorts.

    This allowed the breakeven portions to trigger properly. I turned on traceOrders to true and it does confirm that the stoploss seems to be moving, at least historically. The execution markers confirm this as well.
    Hmmm.., sounds awesome if you got it to work! Would you mind providing the new code that you got? So I can try and implement that with the WaveTrend strategy and time filter?

    Comment


      #17
      I didn't really add too much so I think it would be less confusing for you to just add the parts I did, which was
      private Order targetOrder = null; next to your stopOrder declaration.

      for your Long and Short Entries I added && Position.MarketPosition == MarketPosition.Flat, because it seemed like it kept checking that more than I thought it would. Might be unnecessary.

      My OnExecutionUpdate portion is as follows:
      protected override void OnExecutionUpdate(Execution execution, string executionID, double price, int quantity, MarketPosition marketPosition, string orderID, DateTime time)
      {

      if (execution.Order != null && execution.Order.Name == "Long")
      {
      stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Price - 50 * TickSize, "buyStop", "Long");
      targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Price + 100 * TickSize, "buyTP", "Long");
      }
      if (execution.Order != null && execution.Order.Name == "Short")
      {
      stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Price + 50 * TickSize, "sellStop", "Short");
      targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Price - 100 * TickSize, "sellTP", "Short");
      }

      }​

      give that a shot and let me know if that works for you.

      Comment


        #18
        Hello rockmanx00,

        Variables holding orders should only be assigned from the order parameter provided from OnOrderUpdate().

        From the Desktop SDK documentation:
        "OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated.​"
        Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.

        The ProfitCasestopTrailExitOrdersExample I provided a link to in post # 3 provides sample code of how this should look. This also demonstrates implementing 1-tick intra-bar granularity so you can intra-bar fills in historical as well.


        Is this historical or real-time?

        Please add the State property to the print output.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #19
          I will look into this. Thank you chelsea

          Comment


            #20
            Originally posted by rockmanx00 View Post
            I didn't really add too much so I think it would be less confusing for you to just add the parts I did, which was
            private Order targetOrder = null; next to your stopOrder declaration.

            for your Long and Short Entries I added && Position.MarketPosition == MarketPosition.Flat, because it seemed like it kept checking that more than I thought it would. Might be unnecessary.

            My OnExecutionUpdate portion is as follows:
            protected override void OnExecutionUpdate(Execution execution, string executionID, double price, int quantity, MarketPosition marketPosition, string orderID, DateTime time)
            {

            if (execution.Order != null && execution.Order.Name == "Long")
            {
            stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Price - 50 * TickSize, "buyStop", "Long");
            targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Price + 100 * TickSize, "buyTP", "Long");
            }
            if (execution.Order != null && execution.Order.Name == "Short")
            {
            stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Price + 50 * TickSize, "sellStop", "Short");
            targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Price - 100 * TickSize, "sellTP", "Short");
            }

            }​

            give that a shot and let me know if that works for you.
            Thanks for the feedback, you've helped a lot! I got it working mostly now. Now the only issue is, it keeps re-entering trades if it hits the BE stop loss.

            Comment


              #21
              No problem. I'm a fellow C#/ Ninjascript learner so this was a nice project that I learned a lot from. I think it'll help me understand my strategy that I'm working on as well. Glad you got it mostly working.

              Comment

              Latest Posts

              Collapse

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