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 Mindset, 04-21-2026, 06:46 AM
          0 responses
          93 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by M4ndoo, 04-20-2026, 05:21 PM
          0 responses
          138 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by M4ndoo, 04-19-2026, 05:54 PM
          0 responses
          68 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by cmoran13, 04-16-2026, 01:02 PM
          0 responses
          123 views
          0 likes
          Last Post cmoran13  
          Started by PaulMohn, 04-10-2026, 11:11 AM
          0 responses
          73 views
          0 likes
          Last Post PaulMohn  
          Working...
          X