Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Trading Range Breakout. Long position not triggered...

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

    Trading Range Breakout. Long position not triggered...

    I have been struggling for the whole day already with this piece of code.

    So what I want to accomplish is not very complex I think.
    I would like it to find the Highest price in a certain range of bars. Then if the price closes above the Highest price it should open a long position.
    I prefer to have it triggered as soons as the current price crosses above the Highest price.

    This is my code:

    Code:
    private int maxLookbackPeriod = 300; // Define a maximum lookback period to avoid excessively long periods
            private double highestHigh;
            private int forwardBars = 10;​
    
    
    Calculate = Calculate.OnEachTick;
    
    
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    
    
    protected override void OnBarUpdate()
            {
                int lookbackPeriod = Math.Min(CurrentBar, maxLookbackPeriod);
    
                 if (CurrentBar < lookbackPeriod || CurrentBars[0] < lookbackPeriod)
                {
                    return;
                }
    
                  int highestBarIndex = HighestBar(Highs[0], lookbackPeriod);
                double newHighestHigh = Highs[0][highestBarIndex];
                bool isNewHigh = newHighestHigh != highestHigh;
    
                if (isNewHigh)
                {
                    highestHigh = newHighestHigh;
                    DateTime startTime = Times[0][highestBarIndex];
                     DateTime endTime = Times[0][Math.Min(highestBarIndex + forwardBars, Count - 1)];
                    
                    Draw.Line(this, "HighLine" + CurrentBar, false, startTime, highestHigh, endTime, highestHigh, Brushes.LimeGreen, DashStyleHelper.Dot, 2);
                    Print("Highest High: " + highestHigh);
                }
                        
                if (Close[0] > highestHigh)
                    {
                        EnterLong();
                    }
            }​
    It is so simple but I can't get it working. The long is not being triggered. Hope someone can help me out with this.

    #2
    Hello wuannetraam,

    I have an example of finding the highest bar in a range you may find helpful.


    To investigate your code, add debugging prints for each value before and after a calculation to see where the unexpected behavior is arising.
    Include the time of the bar Time[0] and labels for each value being printed.
    Enable TraceOrders as well so that we can see if the order is being submitted, ignored, cancelled, or rejected.

    Below is a link to a support article that demonstrates using prints to understand behavior.


    Save the output to a text file (right-click the output window, select Save as) and attach the output to your reply.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello wuannetraam,

      I have an example of finding the highest bar in a range you may find helpful.


      To investigate your code, add debugging prints for each value before and after a calculation to see where the unexpected behavior is arising.
      Include the time of the bar Time[0] and labels for each value being printed.
      Enable TraceOrders as well so that we can see if the order is being submitted, ignored, cancelled, or rejected.

      Below is a link to a support article that demonstrates using prints to understand behavior.


      Save the output to a text file (right-click the output window, select Save as) and attach the output to your reply.
      thank you for the response. It is not that i need to find the Highest bar in the range. I am looking for the Highest price.
      If I look at my chart the lines are correctly drawn at the highest prices.
      But when the prices crosses above that point it does not trigger a Long position.

      This is a part from the output. But the output is to big to upload (20mb).
      "Highest High: 5334
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25
      Current Price5332,25​"

      My current code:
      "
      Code:
      protected override void OnBarUpdate()
              {
                  int lookbackPeriod = Math.Min(CurrentBar, maxLookbackPeriod);
      
                   if (CurrentBar < lookbackPeriod || CurrentBars[0] < lookbackPeriod)
                  {
                      return;
                  }
      
                    int highestBarIndex = HighestBar(Highs[0], lookbackPeriod);
                  double newHighestHigh = Highs[0][highestBarIndex];
                  bool isNewHigh = newHighestHigh != highestHigh;
      
                  if (isNewHigh)
                  {
                      Print("Highest High: " + highestHigh);
                      highestHigh = newHighestHigh;
                      DateTime startTime = Times[0][highestBarIndex];
                       DateTime endTime = Times[0][Math.Min(highestBarIndex + forwardBars, Count - 1)];
                      
                      Draw.Line(this, "HighLine" + CurrentBar, false, startTime, highestHigh, endTime, highestHigh, Brushes.LimeGreen, DashStyleHelper.Dot, 2);
                      
                  }
                          
                  if (Close[0] > highestHigh)
                      {
                          EnterLong();
                          Print("Enter Long Position");
                      }
                      else
                      {
                          Print("Current Price" + Close[0]);
                      }
              
              }
              
          }​
      Also I am wondering why the Backtest strategy does not work when I untick "Tick Replay".
      I also tried to Enable TraceOrders but that gave 0 output.

      I hope someone can help me out.

      ​​
      if I change

      if (Close[0] > highestHigh)
      to
      if (Close[0] < highestHigh)
      then it opens a lot of long positions. So it is not an Trading problem I guess.
      Attached Files

      Comment


        #4
        Hello wuannetraam,

        Print the values in the entry condition and this may help to identify the issue.

        Likely, as you are setting the newHighestHigh variable on each new bar before the entry condition, this is always going to be greater than Close[0]. The high will always be equal to or greater than the close price.

        Are you wanting to compare the Close[0] of the current bar to be greater than the previous bars High[1]?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post

          Are you wanting to compare the Close[0] of the current bar to be greater than the previous bars High[1]?
          I think that this is what I want. Let me draw it out.

          Attached Files

          Comment


            #6
            Hello wuannetraam,

            I still recommend that you add prints to fully understand the issue before attempting to apply a fix.

            Try updating the newHighestHigh variable after checking the close is greater than the previous high.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I was hoping someone on this forum could help me out with this piece of code as I am struggilng with it for 2 days now.

              Comment


                #8
                Hello wuannetraam,

                May I confirm you have tried my suggestion and this did not work?

                This thread will remain open for any community members that would like to debug your custom logic as a convenience to you.

                You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  it looks like the problem indeed was in the fact that the latesthigh was constantly updated and there by always bigger then the current price. I created a PreviousHigh varianle outside the method and it seens to be fixed.

                  thank you

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by greenberet, Today, 05:07 AM
                  0 responses
                  12 views
                  0 likes
                  Last Post greenberet  
                  Started by nicbizz, 07-14-2024, 09:02 AM
                  3 responses
                  27 views
                  0 likes
                  Last Post nicbizz
                  by nicbizz
                   
                  Started by Rudmax, 07-02-2024, 05:40 AM
                  12 responses
                  285 views
                  1 like
                  Last Post benghabili  
                  Started by samish18, Today, 03:50 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post samish18  
                  Started by Rotdabbel, 07-17-2024, 10:00 AM
                  2 responses
                  11 views
                  0 likes
                  Last Post Rotdabbel  
                  Working...
                  X