Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Move SL to BE when Calculate.OnBarClose ?

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

    Move SL to BE when Calculate.OnBarClose ?

    Hello,
    I know how to set my SL at BE when using Calculate.OnEachTick but I can't find a way to do it when Calculate.OnBarClose.
    I set my trigger conditions in OnBarUpdate, it triggers a limit order(short or long), when the order is triggered I initialy set my SL and TP in OnOrderUpdate but when I want to set any condition to move my SL to BE in OnBarUpdate nothing works.

    I know the code is getting in my {} because I debugged it using prints but SetStopLoss(CalculationMode.Price, Position.AveragePrice) does not move the existing SL.
    Would much appreciate any help
    Regards,
    Mikael

    #2
    Hello Mikael,

    Below is a link to an example of a breakeven created with the Strategy Builder.


    As well as a link to an unlocked script that implements 1-tick intra-bar granularity and does trailing action similar to breakeven action.



    The stop loss if called with a new price or tick distance would update after the bar closes.

    To understand the behavior, add debugging prints and enable TraceOrders.


    Enable TraceOrders.

    Print the order.ToString() object in OnOrderUpdate().

    Print the bar time and stop price being supplied to the Exit method or Set method one line above where the method is called for the breakeven action.

    Save the output (right-click the output window and select Save as) and attach this to your next post.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply Chelsea !
      I tried to Enable trace orders and so on but I don't have much result :/ (I am relatively new to coding you guessed it).
      I joined two files :
      1/Debug1 : you see the way I trigger my SL and TP. They always work, I have no problem
      2/Debug2 : You see the way I tried to set my BE. I placed it in OnBarUpdate. With prints I see we get in the brackets when we're supposed to but my SL is not modified.
      Also, I tried to used ExitLongStopMarket like you provided in your example (or ExistShortStopMarket) but it doesn't work either. Also tried to place all the conditions (trigger SL, TP and BE) in OnOrderUpdate but doesn't work either .
      I would be glad if you could help me with the two files I provide, otherwise I will try my best to try again with the "enable trace orders" and so on.
      I can also post the full code...
      Thank you Chelsea !
      Attached Files

      Comment


        #4
        Hello MikaISO,

        Be sure you have watched the 'Debugging with the Strategy Builder' and 'NinjaScript Editor 401' videos on using prints.

        The Time[0] should be included with the print, so that we may investigate the specific bar where you are expecting the action to occur.

        If these prints are not appearing, this would suggest the condition is not evaluating as true.

        Add a print one line above the parent and the nested branching commands (if statements).

        Print(string.Format("{0} | isBE: {1} == false", Time[0], isBE));
        if (isBE == false)

        Print(string.Format("{0} | Position.MarketPosition: {1} == MarketPosition.Long && Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]: {2}): {3} >= PercentTptoBE: {4} * (TP: {5} - Position.AveragePrice: {6}): {7} / TickSize: {8} = {9}", Time[0], Position.MarketPosition, Close[0], Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]), PercentTptoBE, TP, Position.AveragePrice, PercentTptoBE * (TP - Position.AveragePrice), PercentTptoBE * (TP - Position.AveragePrice) / TickSize));
        if ((Position.MarketPosition == MarketPosition.Long)
        && (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) >= PercentTptoBE * (TP - Position.AveragePrice) / TickSize))​



        Also, add the OnOrderUpdate() override and print the order.ToString().
        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.


        protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
        {​
        Print(order.ToString());
        }

        Save the output (right-click the output window and select Save as) and attach this to your next post.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I tried my best, hope the print I'm sending is enough
          Attached Files

          Comment


            #6
            Hello MikaISO,

            Please also enable TraceOrders.

            Then remove the instance from the chart Strategies window configured area, or from the Strategies tab of the Control Center, and add a new instance from the Available list.

            Also, please add one additional print on a new line directly above the call to SetStopLoss() that prints the time of the bar and Position.AveragePrice so we can see what price the method is being called with.

            Re-run the script and provide the new output.


            One note, as you are using Set methods, these cannot be unset. It is important to call these with CalculationMode.Ticks before submitting a new entry.
            This forum post discusses.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I found the solution .
              I set my initial SL in "OnExecutionUpdate" and I set my new BE SL in "OnBarUpdate". I each time use SetStopLoss and it works

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              56 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              132 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              73 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              45 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