Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple Trailingstop on low/high of previous bar

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

    Simple Trailingstop on low/high of previous bar

    Hi guys,
    I just coded a nice strategy for Ninjatrader but I have problems with the necessary trailingstop.

    (case long)

    All the trailingstop is supposed to do is to close a long position if the current price falls below the low of the previous bar.

    I tried things like

    if (Low[0] < Low[-1])
    ExitLong();

    Is there any method which I can call to pass the low of the previous bar? Or do I have to create such a method myself? Maybe it as simple as creating an array which saves all the lows? But where to implement? In OnBarUpdate() it would override the value everytime.

    I hope you can understand my problem and help me to find a solution.
    Thanks!

    #2
    Hello,

    Thank you for the question.

    What you have now is almost correct, in NinjaTrader you can request bars in the past by using positve BarsAgo values.

    For example here would be 1 bar in the past:

    Code:
    if (Low[0] < Low[1])
    ExitLong();
    Also when using bars ago its always good practice to make sure there are at least that many bars on the chart when this is being checked, in the top of OnBarUpdate you might add a statement like the following:
    Code:
    if(CurrentBar < 1) return;
    This is just saying, if the current bar being processed is less than the 1st bar, do nothing.
    the code below this statement would be ignored on bar 0. This prevents index errors, if you check 1 bar ago on bar 0 you would be checking index -1 which is impossible.

    I look forward to being of further assistance.

    Comment


      #3
      Hello Jesse,

      thank you so much for the fast response.

      It works! And already improves my strategy a lot.

      Only problem now is that the stops are too late. Because it compares the lows and this is of course only possible when the bars are finished.

      Is there a posibility to close the position within a bar?

      Something like:

      if (currrentPrice < Low[1])
      ExitLong();

      Thanks

      Comment


        #4
        Hello,

        The Current Price would be the same as saying Close[0], but in NinjaTrader there is two modes in which a NinjaScript item can be run. That is the CalculateOnBarClose setting.

        When set to True, the script will execute OnBatUpdate once for each bar close, on a 1 minute chart that would be once a minute. on a 10 tick that is once every 10 ticks.

        With this set to false instead, OnBarUpdate happends once every tick so the Close[0] is the current price.

        This will effect your logic depending on what you are trying to do.

        Other ways around this would be to use OnMarketData override


        If you are manually coding this you have access to other ways to get live prices such as OnMarketData.

        You could do logic on the tick in this method while the rest of the script is set to calculate on the bar close.

        I look forward to being of further assistance.

        Comment


          #5
          Thank you so much, that's perfect. Can't wait to test it on live data tomorow =)

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          633 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          364 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          567 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          568 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X