Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving stop to breakeven not working

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

    Moving stop to breakeven not working

    Hi

    I have included stop to breakeven script as per https://ninjatrader.com/support/helpGuides/nt8/modifying_the_price_of_stop_lo.htm​. But I can't have it fire as desired. It never moves it to breakeven after meeting my criteria of 40 ticks above position average. What am I missing from code? Here's code and a snapshot of an example where it should have stopped me at breakeven but continued to my full stop.

    1st half of code (non bold) is the long entry execution. Second part of the code in bold is supposed to the stop to break even component:

    isFirstPullbackSeriesLong[0] = isFirstPullbackLong ? 1 : 0;
    if (isFirstPullbackLong)
    {
    Draw.ArrowUp(this, "FirstPullbackLong" + CurrentBar, false, 0, Low[0] + 2 * TickSize, Brushes.Orange);
    // Place order at the high of the pullback candle
    StoplimitOrder = EnterLongStopLimit(NumberOfContracts, High[0], High[0], "Long Entry");
    SetStopLoss("Long Entry", CalculationMode.Ticks, FixedStopLossTicksLong, false);
    SetProfitTarget("Long Entry", CalculationMode.Ticks, FixedTargetProfitTicksLong);

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, FixedStopLossTicksLong);
    }

    // If a long position is open, allow for stop loss modification to breakeven
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
    if (Close[0] > Position.AveragePrice + 40 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    }
    }


    }

    Example:

    Click image for larger version

Name:	image.png
Views:	147
Size:	88.1 KB
ID:	1313014​​​

    #2
    Hello snoinvest,

    Have you tried using a Print to verify the condition is actually becoming true? That would be the best first step to make sure the method is even being called at all.

    Code:
    if (Close[0] > Position.AveragePrice + 40 * TickSize)
    {
        Print("Stop should break even now");
        SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    }

    Comment


      #3
      I have done as asked. I'm not seeing anything firing on the ouput section: This is what i get:
      Disabling NinjaScript strategy 'TKRealPullbackStrategy/332001075'
      Enabling NinjaScript strategy 'TKRealPullbackStrategy/332001075' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes

      Comment


        #4
        Hello snoinvest,

        That means the condition was not true so that is the reason it was not working.

        To get a better idea of why you can add a print outside the condition like the following:

        Code:
        Print(Time[0] + "   " + Close[0] + " > " + (Position.AveragePrice + 40 * TickSize));
        if (Close[0] > Position.AveragePrice + 40 * TickSize)
        {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
        }​

        Comment


          #5
          I did as requested and get the following output, please advise:
          Disabling NinjaScript strategy 'TKRealPullbackStrategy/332001079'
          7/22/2024 8:16:00 AM 19905 > 19916.75
          7/22/2024 8:23:00 AM 19909.25 > 19916.75
          7/22/2024 8:24:00 AM 19909.5 > 19916.75
          7/22/2024 8:26:00 AM 19914 > 19916.75
          7/22/2024 8:31:00 AM 19922.25 > 19916.75
          7/22/2024 8:41:00 AM 19925.25 > 19938
          7/22/2024 9:15:00 AM 19940.75 > 19947.5
          7/22/2024 12:57:00 PM 19964 > 19973
          7/22/2024 1:01:00 PM 19958.25 > 19973
          7/22/2024 1:03:00 PM 19964.25 > 19973
          7/22/2024 1:05:00 PM 19975 > 19973
          7/23/2024 8:59:00 AM 19972.25 > 19973.75
          7/23/2024 11:20:00 AM 20075.25 > 20081.75
          7/23/2024 11:21:00 AM 20073 > 20081.75
          7/23/2024 11:31:00 AM 20061.75 > 20081.75
          7/23/2024 11:32:00 AM 20060.25 > 20081.75
          Strategy 'TKRealPullbackStrategy/332001079': An order has been ignored since the stop price ‘19217’ near the bar stamped ‘07/26/2024 08:07:00’ is invalid based on the price range of the bar. This is an invalid order and subsequent orders may also be ignored.
          7/26/2024 12:22:00 PM 19219 > 19235.75
          7/26/2024 12:23:00 PM 19216.25 > 19235.75
          7/26/2024 12:24:00 PM 19219.25 > 19235.75
          7/26/2024 12:28:00 PM 19233.25 > 19235.75
          7/26/2024 1:02:00 PM 19281.25 > 19289
          7/26/2024 1:07:00 PM 19281 > 19289
          7/29/2024 8:24:00 AM 19315.25 > 19321.5
          7/29/2024 8:28:00 AM 19323 > 19321.5
          7/29/2024 8:35:00 AM 19348 > 19351
          7/29/2024 8:36:00 AM 19347.75 > 19351
          7/29/2024 12:15:00 PM 19316 > 19312.75
          7/29/2024 12:16:00 PM 19307.5 > 19312.75
          7/30/2024 8:41:00 AM 19267.5 > 19277.75
          7/30/2024 8:46:00 AM 19268 > 19277.75
          7/30/2024 8:56:00 AM 19267.5 > 19277.75
          7/30/2024 8:57:00 AM 19268.25 > 19277.75
          7/30/2024 9:14:00 AM 19271 > 19277.75
          7/30/2024 9:20:00 AM 19277.5 > 19277.75
          7/30/2024 9:21:00 AM 19276.25 > 19277.75
          7/30/2024 9:22:00 AM 19274.75 > 19277.75
          7/30/2024 9:23:00 AM 19275 > 19277.75
          7/31/2024 8:18:00 AM 19271.5 > 19265.75
          7/31/2024 8:35:00 AM 19315 > 19316.25
          7/31/2024 8:36:00 AM 19311.5 > 19316.25
          7/31/2024 8:50:00 AM 19346 > 19344
          7/31/2024 8:51:00 AM 19348.5 > 19344
          7/31/2024 9:53:00 AM 19379.75 > 19376.75
          7/31/2024 11:31:00 AM 19489.25 > 19491.25
          7/31/2024 12:52:00 PM 19452.5 > 19462.75
          7/31/2024 1:09:00 PM 19462 > 19460.5
          7/31/2024 2:27:00 PM 19477 > 19478.5
          7/31/2024 2:28:00 PM 19477.5 > 19478.5
          7/31/2024 2:29:00 PM 19471.75 > 19478.5
          8/1/2024 9:47:00 AM 19635.5 > 19655
          8/1/2024 9:50:00 AM 19649.75 > 19655
          Enabling NinjaScript strategy 'TKRealPullbackStrategy/332001079' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes

          Comment


            #6
            Hello snoinvest,

            While I can see some cases where the value was greater I can't see where the target filled just based on the prints. To use these prints you will need to check what the fill price of the target was and see if it filled before the PnL limit was hit. Your condition is not checking if you are in a position so potentially the target filled before the offset amount was reached. The prints will happen on each bar so that will only be helpful to visually compare on the chart with the bar timestamps to see if the price ever was greater than the offset amount before the target had filled. Assuming the price goes above the offset amount before the target fills then it should be able to reset the target to the new price.

            One other note is that if your entry condition is remaining true then your second stoploss condition wont work. If the code if (isFirstPullbackLong) remains true that means SetStopLoss is being called for each bar, the second SetStopLoss will be ignored if that initial condition remains true. You may want to also check if that condition is remaining true during this time by adding that variabele to the print.

            Comment


              #7
              sorry i'm having difficulty following. what do you mean by this: To use these prints you will need to check what the fill price of the target was and see if it filled before the PnL limit was hit. What do you mean by PnL ( is that the stop loss?). Also "the target filled before the offset amount was reached" what do you mean by this? in the example I gave the target wasn't hit (assuming by target you mean profit target). It was a losing trade, but at the peak of the move, it was certainly above the 40 ticks from my entry point which should have triggered the stop to be moved to breakeven. Please advise.

              Thank you,
              Tedla.

              Comment


                #8
                Hello snoinvest,

                Please excuse me I meant the AveragePrice in place of Pnl.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                116 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                61 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                40 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                43 views
                0 likes
                Last Post TheRealMorford  
                Started by Mindset, 02-28-2026, 06:16 AM
                0 responses
                82 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Working...
                X