Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using HighestBar method for an entry

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

    Using HighestBar method for an entry

    Hi,

    I tried to incorrectly use the following code to generate an entry signal when the closing price of a bar was greater than the highest high for the previous 20 bars. Instead of triggering a signal when a new 20 bar high was seen, the signal generated occurred when the close of a bar was greater than the high of the 20th bar back. This signal did generate 76 trades though over a 6 year period.

    protected override void OnBarUpdate()
    {
    int lowestLowBar = LowestBar(Low,10);
    int highestHighBar = HighestBar (High,20);
    // Setting stop loss to lowest low of last 10 bars
    SetStopLoss(CalculationMode.Price, Low[lowestLowBar]);
    // Condition set 1
    if (Close[0] > High[20])
    {
    EnterLong(1, "");
    }
    }

    When I changed the code to the following, no trades were taken when backtesting over the same period in the same instrument. Looking back at the chart over the given time, there were plenty of instances when a bar closed higher than the highs of the previous 20 bars. The only change I made in the code was in the if statement where I changed the value "20" to the variable "highestHighBar". Am I using the HighestBar method correctly? I tried to utilize it in a similar manner as the LowestBar method was used within the same code.

    protected override void OnBarUpdate()
    {
    int lowestLowBar = LowestBar(Low,10);
    int highestHighBar = HighestBar (High,20);
    // Setting stop loss to lowest low of last 10 bars
    SetStopLoss(CalculationMode.Price, Low[lowestLowBar]);
    // Condition set 1
    if (Close[0] > High[highestHighBar])
    {
    EnterLong(1, "");
    }
    }

    Thanks in advance.

    joe

    #2
    jperales,

    Yes, you are using the HighestBar correctly.

    Essentially, Close[0] > High(HighestBar(High,20)) may never be true because if a Close[0] is indeed higher than the previous highest bar, it just created a new high.

    I would suggest keeping track of "PreviousHighestBar" and compare to this, or set the Close[0] > High(HighestBar(High,20)) to a Close[0] >= High(HighestBar(High,20));

    I would need to see more of your code here to comment further. Such as CalculateOnBarClose, etc.

    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by jperales View Post
      Hi,

      I tried to incorrectly use the following code to generate an entry signal when the closing price of a bar was greater than the highest high for the previous 20 bars. Instead of triggering a signal when a new 20 bar high was seen, the signal generated occurred when the close of a bar was greater than the high of the 20th bar back. This signal did generate 76 trades though over a 6 year period.

      protected override void OnBarUpdate()
      {
      int lowestLowBar = LowestBar(Low,10);
      int highestHighBar = HighestBar (High,20);
      // Setting stop loss to lowest low of last 10 bars
      SetStopLoss(CalculationMode.Price, Low[lowestLowBar]);
      // Condition set 1
      if (Close[0] > High[20])
      {
      EnterLong(1, "");
      }
      }

      When I changed the code to the following, no trades were taken when backtesting over the same period in the same instrument. Looking back at the chart over the given time, there were plenty of instances when a bar closed higher than the highs of the previous 20 bars. The only change I made in the code was in the if statement where I changed the value "20" to the variable "highestHighBar". Am I using the HighestBar method correctly? I tried to utilize it in a similar manner as the LowestBar method was used within the same code.

      protected override void OnBarUpdate()
      {
      int lowestLowBar = LowestBar(Low,10);
      int highestHighBar = HighestBar (High,20);
      // Setting stop loss to lowest low of last 10 bars
      SetStopLoss(CalculationMode.Price, Low[lowestLowBar]);
      // Condition set 1
      if (Close[0] > High[highestHighBar])
      {
      EnterLong(1, "");
      }
      }

      Thanks in advance.

      joe
      As HighestBar() necessarily includes the CurrentBar, your condition:

      if (Close[0] > High[highestHighBar])

      is never true, as by definition, the Close is always equal to the High or lower. Try using MAX() and MIN() instead, as they can be separately indexed to the previous bar, or indeed any other.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      656 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      371 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      109 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      574 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      579 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X