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

Min and Max for indicator based range

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

    Min and Max for indicator based range

    Hi

    I have an indicator, and I want to get the MIN and MAX price values for each interval when the indicator is < 0.
    Something like that:
    If (my indicator[1] > 0 AND my indicator[0] < 0)
    take this bar as StartDate

    If (my indicator[1] < 0 AND my indicator[0] > 0)
    take this bar as EndDate

    Min = MIN(StartDate, EndDate)
    Max = MAX(StartDate, EndDate)

    Can you please help me with the code?
    I'm not sure how to take the StartDate and EndDate bars.

    Thank you

    #2
    Code:
    if (myIndicator[1] > 0 && myIndicator[0] < 0)
         startBar = CurrentBar;
    if (myIndicator[1] < 0 && myIndicator[0] > 0)
         endBar = CurrentBar;
    
    range = endBar - startBar;
    MinRange = MIN(Low, range)[0];
    MaxRange = MAX(High, range)[0];
    Not tested, but it should give you an idea of how to proceed.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      I did the following, it compiles but it doesn't work.
      Any idea why or how I could debug it?

      Thank you

      privatedouble MinRange;
      privatedouble MaxRange;
      privateint startBar;
      privateint endBar;
      privateint Range;

      protectedoverridevoid OnBarUpdate()

      if (MyIndicator[1] > 0 && MyIndicator[0] < 0)
      startBar = CurrentBar;


      if (MyIndicator[1] < 0 && MyIndicator[0] > 0)
      endBar = CurrentBar;


      Range = endBar - startBar;


      MinRange = MIN(Low, Range)[0];
      MaxRange = MAX(High, Range)[0];



      if (Close[0] > MaxRange)

      {

      EnterLong("Long");

      }





      if (Close[0] < MinRange)

      {
      EnterShort("Short");
      }

      Comment


        #4
        Stefy,

        I would guess that this doesn't work probably because you are trying to reference the previous bar (i.e. MyIndicator[1]) without first checking for the condition when there is no previous bar. If you try to reference MyIndicator[1] on the first bar, your indicator or strategy will fail and not execute.

        On the first bar, there isn't any [1]. So you will need a statement at the beginning of your OnBarUpdate() method to wait until the first bar has passed:

        if (CurrentBar == 0) return;

        This will cause your code to be skipped on the first bar (CurrentBar=0).

        -Alex

        Comment


          #5
          Hi anachronist,

          Thank you for the suggestion, but it doesn't help.

          Comment


            #6
            The problem might also be an unallowed zero or negative parameter in MAX or MIN. Here's your code above, with my changes in red:

            Code:
            protected override void OnBarUpdate()
            {
               [COLOR=Red]if (CurrentBar == 0) return;[/COLOR]
               if (MyIndicator[1] > 0 && MyIndicator[0] < 0)
                  startBar = CurrentBar;
               if (MyIndicator[1] < 0 && MyIndicator[0] > 0)
                  endBar = CurrentBar;
            
               Range = endBar - startBar;
               [COLOR=Red]if (Range > 0) {[/COLOR]
                  MinRange = MIN(Low, Range)[0];
                  MaxRange = MAX(High, Range)[0];
                  if (Close[0] > MaxRange) {
                     EnterLong("Long");
                  }
                  if (Close[0] < MinRange) {
                     EnterShort("Short");
                  }
               [COLOR=Red]}[/COLOR]
            -Alex

            Comment


              #7
              Please see the Control Center for any errors. Here is a tip on how to debug your NinjaScript: http://www.ninjatrader-support.com/v...ead.php?t=3418
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                I printed the MinRange and MaxRange values and they look fine.
                I used the TraceOrders function and got no orders, so the problem is in the order generation.
                Since I don't see how the problem can be in the syntax
                if (Close[0] > MaxRange)
                {
                EnterLong("Long");
                }

                I guess the problem must be in the MaxRange/MinRange. Maybe if I created a DataSeries instead of a double for MaxRange/MinRange it would work?
                I understand somehow the software doesn't "see" when the price crosses one of those two thresholds.

                Thank you to everyone

                Comment


                  #9
                  The MaxRange and MinRange variables should have no problem being "seen". The question is does it ever evaluate to being less than the Close[0]. Print out the Close[0] value along side the MaxRange/MinRange value and you will know when it should have traded.

                  Also, there have been numerous suggested code changes that it will be helpful to see what the exact code you have in play looks like now.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    I printed the following:

                    Print(MinRange +
                    " " + MaxRange + " " + Close[0] + " " + MyIndicator(5)[0]);

                    and plotted the results in Excel. When the price moves, the upper and lower boundaries (MaxRange and MinRange) move as well, and hence the price never breaks each of them. I think I need to "freeze" the upper and lower boundaries when MyIndicator is positive.

                    Do you agree? How can I do that?

                    Thank you

                    Here is the code so far:

                    if (CurrentBar == 0)
                    return;


                    if(MyIndicator[1] > 0 && MyIndicator [0] < 0)
                    startBar = CurrentBar;


                    if(MyIndicator[1] < 0 && MyIndicator [0] > 0)
                    endBar = CurrentBar;

                    Range = endBar - startBar;


                    if (Range > 0)
                    {
                    MinRange = MIN(Low, Range)[0];
                    MaxRange = MAX(High, Range)[0];


                    if (Close[0] > MaxRange)

                    {

                    EnterLong("Long");

                    }


                    if (Close[0] < MinRange)

                    {
                    EnterShort("Short");
                    }

                    Comment


                      #11
                      As you have coded, the range values can change anytime a new high/low is made for the existing bar. What youy have to do as you suggest is hold on to the range values and only reassign them when a new value should be there.
                      RayNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by MGHORBEL, Today, 06:25 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post MGHORBEL  
                      Started by knowmad, Yesterday, 03:52 AM
                      4 responses
                      44 views
                      0 likes
                      Last Post knowmad
                      by knowmad
                       
                      Started by Bogdan097, Yesterday, 03:25 PM
                      1 response
                      12 views
                      0 likes
                      Last Post NinjaTrader_Gaby  
                      Started by MSerag, 05-06-2024, 11:52 PM
                      5 responses
                      30 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by Aviram Y, Today, 06:03 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post Aviram Y  
                      Working...
                      X