Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

use setstoploss method in onbarupdate

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

    use setstoploss method in onbarupdate

    hi,

    i have an issue using setstoploss in onbarupdate method.
    what i am trying to do is if i am in a long position and the current bar closes upwards i want my stoploss be placed or changed to the low of the current bar - vice versa for a short position.
    i am using the following code in onbarupdate:

    if (Position.MarketPosition == MarketPosition.Short)
    {
    Print("Short");
    if (Close[0] < Open[0])
    {
    SetStopLoss(CalculationMode.Price, High[0] + TickSize);
    Print("Close < Open - High[0] " + High[0]);
    }
    }

    if (Position.MarketPosition == MarketPosition.Long)
    {
    Print("Long");
    if (Close[0] > Open[0])
    {
    SetStopLoss(CalculationMode.Price, Low[0] - TickSize);
    Print("Close > Open - Low[0] " + Low[0]);
    }
    }
    P.S. code is called on bar close

    Cannot understand why its not working - the print statements are called correctly

    Thanks in advance


    #2
    Hello idontknow,

    Welcome to the NinjaTrader forums!

    What specifically is not working?

    Are you prints not appearing?

    Do you have TraceOrders enabled? Are there messages with TraceOrders showing the set method was ignored?

    Are you printing the order object in OnOrderUpdate() to see the order progression?

    Please save the output from the output window to a text file and include this with your post.

    Below is a link to a forum post on using Print() and TraceOrders to understand behavior.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      hi chelsea,
      thanks for your reply - i added "TraceOrders = true;" statement to the setdefaults section.
      unfortunately there is no printing to the output window regarding order placements.
      i attached the output window textfile.
      the printstatements are executed but there is no logging of order tracing

      by the way i tried the strategy on replay data is it possible that this causes the issue?
      Attached Files

      Comment


        #4
        Hello idontknow,

        "If TraceOrders is set to true in State.SetDefaults instead of State.Configure, the change will not take effect until a new instance of the script is added. Defaults are only pulled for new instances.
        In the Strategy Builder TraceOrders is set in the Defaults (and requires removing and adding a new instance)."
        https://ninjatrader.com/support/foru...121#post791121

        To confirm, you are calling Print(order.ToString()); in OnOrderUpdate()?

        Prints also work in playback.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chelsea,
          sorry - my bad removing and adding fixed it and by reading the statements i found the issue regarding the stop orders too - I missed the order name in the statement i used further up in the code.
          Thanks for your help anyway - regards - excellent help as always

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          50 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          126 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          69 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 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