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

basic for loop assistance

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

    basic for loop assistance

    Can somebody make a basic for loop to capture the highest high that occurred the last time an indicator was above/below a specified range. For example, how do I find the highest price high that occurred the last time the macd average line was above zero? I realize this can be done using a simple for loop but I don't have experience making these.

    I have attached an image showing the exact range I need to scan. Please assist.

    This is not correct obviously but what I need is for somebody to make a working version.

    if(CrossBelow(MACD(12, 26, 9).Avg, 0, 1))
    {

    double highestPriceHighAboveZero = 0;
    for(int i = 0; i < CurrentBar; i++)
    {
    double macd = MACD(12, 26, 9).Avg[i];
    if( something )
    {

    Find the highest price high the last tiem macd was greater than zero
    by looping through historical data

    }
    }


    }
    Attached Files
    Last edited by gordongekko; 02-16-2018, 03:39 PM.

    #2
    Hello gordongekko,

    Thank you for your post.

    There is no need to loop through the historical data. You only need to set up a condition to check the MACD average plot and then assign a value if the High is greater than the prior highest High.

    For example:
    Code:
    		private double highestHigh = double.MinValue;
    		protected override void OnBarUpdate()
    		{
    			if (MACD(12,26,9).Avg[0] > 0)
    			{
    				if (High[0] > highestHigh)
    					highestHigh = High[0];
    			}
    		}
    Please let me know if you have any questions.

    Comment


      #3
      Even if there is no need in this particular situation. Can you make an example of a for loop that would obtain that value by scanning historcal data.

      Comment


        #4
        I forgot to mention an important detail in my previous post. The reason I assumed I would need a for loop to get the highest high the last time the macd avg line was above 0 is because I need to get this value for the previous time that occured when the macd has gone above 0 again after coming back below 0. If this is possible without a for loop I'm assuming I would need to transfer the value of the current high to another variable after each reverse cross. Will this work?

        Comment


          #5
          Hello gordongekko,

          Thank you for your response.

          I have created a video to explain this a bit further and I have attached the updated code to show you how to do this in code.

          You can find the video at the following link: https://www.dropbox.com/s/5e19kehpgi...47-21.mp4?dl=0

          Please let me know if you have any questions.
          Attached Files

          Comment


            #6
            Thanks. Those video responses are much more useful than text. I understand how to do this without a loop now. So the reason for the aversion to using toops unless there is no other option is because they use up additional computer resources and thus should only be only be used when necessary.

            Comment


              #7
              Hello gordongekko,

              Thank you for your response.

              In some circumstances a For Loop could still be considered but in the case of your idea OnBarUpdate() serves the needed purpose.

              Please let me know if I may be of further assistance.

              Comment


                #8
                For some reason the the lowest low version of this indicator isn't working. I created basically a verbatim mirror image of of the above zero version and it's not assigning any value to the lowest lows when the macd average line is below zero.


                This is the code:

                ************************





                the variables are declared in the class before onstatechange is called

                private double highestHigh = 0;
                private double priorHighestHigh = 0;
                private double lowestLow = 0;
                private double priorLowestLow = 0;



                this is run during the onbarupdate method


                // PREVIOUS MACD HIGH//




                if (MACD(12,26,9).Avg[0] > 0)
                {
                if (High[0] > highestHigh)
                highestHigh = High[0];

                Print("");
                Print(Time[0]);
                Print(highestHigh);
                Print(priorHighestHigh);

                }
                else if(MACD(12,26,9).Avg[0] <= 0 && MACD(12,26,9).Avg[1] > 0)
                {
                priorHighestHigh = highestHigh;
                highestHigh = 0;
                }



                // PREVIOUS MACD LOW//



                if (MACD(12,26,9).Avg[0] < 0)
                {
                if (Low[0] < lowestLow)
                lowestLow = Low[0];

                Print("");
                Print(Time[0]);
                Print(lowestLow);
                Print(priorLowestLow);

                }
                else if(MACD(12,26,9).Avg[0] >= 0 && MACD(12,26,9).Avg[1] < 0)
                {
                priorLowestLow = lowestLow;
                lowestLow = 0;
                }



                *********************
                Last edited by gordongekko; 02-22-2018, 08:31 AM.

                Comment


                  #9
                  Please disregard my previous post. I figured out how to do it.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Kaledus, Today, 01:29 PM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by frankthearm, Yesterday, 09:08 AM
                  13 responses
                  45 views
                  0 likes
                  Last Post frankthearm  
                  Started by PaulMohn, Today, 12:36 PM
                  2 responses
                  16 views
                  0 likes
                  Last Post PaulMohn  
                  Started by Conceptzx, 10-11-2022, 06:38 AM
                  2 responses
                  55 views
                  0 likes
                  Last Post PhillT
                  by PhillT
                   
                  Started by yertle, Yesterday, 08:38 AM
                  8 responses
                  37 views
                  0 likes
                  Last Post ryjoga
                  by ryjoga
                   
                  Working...
                  X