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

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

    basic for loop

    I need a for loop to determine the highest price close when an indicator is above a specific level. Can somebody please provide the correct code for making this happen. I just want the loop to start when the fs plot d is above 80 and capture the highest close while it's above 80 and break out of the loop once the fs plot d is less than 80. For example.


    double s = StochasticsFast(StochD, StochK).D[0];
    double highestcloseabove80 = 0;


    if( s > 80)
    {
    for(int i = 1; i < 100; i++)
    {
    if(s > 80 && Close[0] > Close[1])
    {
    highestcloseabove80 = Close[i]
    }
    if(s < 80)
    {
    break;
    }

    }

    This could also be done using highest close x bars back when the fs plot d has crossed below 80 but i need to make sure its 100% accurate.
    Last edited by gordongekko; 01-22-2018, 12:54 PM.

    #2
    Hello gordongekko,

    Thanks for your post.

    Perhaps I am not understanding correctly. I can read your description to say the highest close ever or in the last 100 bars or the highest close only during the period that S>80

    If you want to capture the highest close during a period while S>80 it seems like you have most of what you need with just:

    if (S>80)
    {
    if (Close[0] > highestcloseabove80)
    {
    highestcloseabove80 = Close[0];
    }
    }

    Once the condition S>80 is no longer true then you have your value as this will check each bar close when S>80.

    If this does not answer your question, please feel free to post a marked chart that shows what you are looking to find.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      So in other words for this type of calculation a loop is not necessary. When would somebody want to use a loop? I'm making some divergence indicators and I've seen people use loops for these but it seems that for most of these situations a loop isn't needed. Let's say i wanted to reference the values of both macd, price and the fast stochastic every time plot D was above 80. Would this require a loop?

      Comment


        #4
        Hello gordongekko,

        Thank for your reply.

        The use of a loop depends on the circumstances, generally, you would use a loop when you want to repeat a block of code X number of times. It is always a coding goal not to repeat code unless absolutely necessary.

        The thing to keep in mind is that your code is executed every time OnBarUpdate() is triggered (which depends on the calculate mode used). The other thing is that when the code is first applied, it processes from the very beginning of loaded data up to the current bar and that would be the basis of the code I referenced in the previous post.

        For the other indicators you mentioned, if all you need is their value at the same time as the highestcloseabove80 then you would access those at the same moment. On the other hand if you needed to know, for example, their peak value over the duration of S>80 when S>80 transitions to S<80, you certainly could do a loop based on the bar number of the first instance of S>80 to the last instance of S>80. Alternatively, you could capture the number of bars that S>80 and then use that in a method such as

        if (Crossbelow(StochasticsFast(StochD, StochK).D, 80, 1))

        {
        MAX(MACD(12, 26,9), CuurentBar - firstS80bar)[0]; // lookback is the period that S>80
        MIN(MACD(12, 26, 9), CurrentBar - firstS80bar)[0]; // Look back is the period that S>80
        }
        Note the above example has not been tested and is provide for illustrative purposes only.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          " On the other hand if you needed to know, for example, their peak value over the duration of S>80 when S>80 transitions to S<80, you certainly could do a loop based on the bar number of the first instance of S>80 to the last instance of S>80. "

          This is exactly what I need. I need to capture the highest close for the duration of time that s >80 until s < 80 and then reference this value in another method. If a loop is the only way to ensure you get the exact highest close in that time period then I need the code for this. Please assist.

          Comment


            #6
            Hello gordongekko,

            Thanks for your reply.

            For the highest close, please see the example provided in post #2.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              If i want to compare the highest close the last time the fs plot d was >80 to the high the current time the fs is greater than 80 do I need a for loop to get this?
              So I would be comparing the highest close for the last 2 time periods/cycles the fast stochastic plot D was above > 80 and between both cycles it went below 80 and then back above to start cycle 2 and cycle 2 is complete once it has gone below 80 again.
              Last edited by gordongekko; 01-22-2018, 02:16 PM.

              Comment


                #8
                Hello gordongekko,

                Thanks for your reply.

                You could if you are determined to do a loop.

                A more efficient way is to save either the bar number where the highest value was previously found or save the highest value itself into a variable. Saving the bar number allows you to dynamically return to that bar at any time, for example to get the high of that bar High[CurrentBar - savedBar] so you can always use the barsago.

                What may be a better solution for you is to plan out what information you need and when it is needed and then decide if it can only be done by looping backwards all the time or if you can save the values as they are determined/discovered by going over the historical data once.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  So you're saying that the most efficient way to get these values is with this type of method:

                  if (Crossbelow(StochasticsFast(StochD, StochK).D, 80, 1))

                  {
                  MAX(MACD(12, 26,9), CuurentBar - firstS80bar)[0]; // lookback is the period that S>80
                  MIN(MACD(12, 26, 9), CurrentBar - firstS80bar)[0]; // Look back is the period that S>80
                  }

                  If so, can you clarify. It looks like I need to make a seperate double for the first close above stochastic 80 and then each subsequent bar would be assingned to the double highestcloseabove80 if close[0] > firsts80bar && s > 80. Then i can use the above method to obtain the highest close within that range?

                  Comment


                    #10
                    Hello gordongekko,

                    Thanks for your reply.

                    In that example, I was suggesting how you can find other indicator values such as the high of the MACD and low of the MACD during the period where S>80. To accomplish that, you would need to save the bar where S>80 the first time (perhaps using the crossabove() method).

                    To find the Highest close value, please see post #2.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      I've attached an image to show you exactly the type of thing I need to learn how to do. I don't see how this can be done using example #2 because i need to compare 2 different instances of the same thing.
                      Attached Files
                      Last edited by gordongekko; 01-22-2018, 03:08 PM.

                      Comment


                        #12
                        Hello gordongekko,

                        Thanks for your reply.

                        You could create a couple of double variables, one to hold the previous value of the high and one to hold the current value of the high. You would create and use a bool to control the program flow and you would use the crossabove and crossbelow methods as the start/stop triggers for the data checks.

                        if (CrossAbove(StochasticsFast(StochD, StochK).D, 80, 1))
                        {
                        prevHigh = highestcloseabove80; // save the to previous highest for comparing
                        HBcheck = true; // set a bool true so we know to keep checking while S>80
                        }

                        if (Close[0] > highestcloseabove80 && HBCheck)
                        {
                        highestcloseabove80 = Close[0]; // save for this instance
                        }

                        if (CrossBelow(StochasticsFast(StochD, StochK).D, 80, 1)) // looking for end of S>80
                        {
                        HBCheck = false; // stop checking cause we are less than 80
                        if (prevHigh > highestcloseabove80)
                        {
                        // do something if previous high is higher
                        }
                        }


                        In general, this would do what you are asking about, this code has not been tested so you would need to review, implement and test.

                        Please note that we do not provide coding services and if you need that we can refer you to 3rd party coders.
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          ok i get it now just assign whatever value you want to obtain from that range to another double and then compare that to the new one. Thanks for your assistance.

                          Comment


                            #14
                            Originally posted by NinjaTrader_PaulH View Post
                            Hello gordongekko,

                            Thanks for your reply.

                            You could if you are determined to do a loop.

                            A more efficient way is to save either the bar number where the highest value was previously found or save the highest value itself into a variable. Saving the bar number allows you to dynamically return to that bar at any time, for example to get the high of that bar High[CurrentBar - savedBar] so you can always use the barsago.

                            What may be a better solution for you is to plan out what information you need and when it is needed and then decide if it can only be done by looping backwards all the time or if you can save the values as they are determined/discovered by going over the historical data once.
                            Hello Paul and thanks for your solution. I'm facing a similar issue but with a Secondary DataSeries (EntryBar below) for your savedBar.
                            What I'm trying to do is store and track the Bid values fluctuation from the moment I enter a long trade until I get flat again.

                            I have this Secondary series on a 1min chart (primary series)
                            Code:
                            AddDataSeries(BarsPeriodType.Tick, 1);
                            
                            AddDataSeries(BarsPeriodType.Tick, 1, MarketDataType.Bid);
                            And I have an EntryBar variable in OnBarUpdate following your savedBar method
                            Code:
                            protected override void OnBarUpdate()
                            {
                                 //Print(BarsInProgress);
                                 //Print("2" + " " + Time[0] + " " + maxBid);
                            
                                 if(CurrentBar<2) return;
                            
                                 if(BarsInProgress == 1 || BarsInProgress == 2)
                                      return;
                            
                            [B]if ( PositionAccount.MarketPosition == MarketPosition.Long )[/B]
                                 {
                                      {
                                           Print("3" + " " + Time[0] + " " + maxBid);
                                           Print("A" + " " + Time[0] + " " + CurrentBars[1]);
                                           Print("B" + " " + Time[0] + " " + EntryBar);
                                           int period = CurrentBars[1] - EntryBar;
                                           Print("C" + " " + period);
                            [B]EntryBar = CurrentBars[1];[/B]
                                           maxBid = MAX(Closes[1], period)[0];
                                           maxbidBool = true;
                                           Print("4" + " " + Time[0] + " " + maxBid);
                                      }
                            
                                      Print("5" + " " + Time[0] + " " + maxBid);
                            As you can see above, I'm trying to get the single int value of the CurrentBars[1] at the moment of entry long. But for some reason I can't get the EntryBar/savedBar int to give the single Bar ago int value. Instead it gives a series of values changing over time.

                            Please see prints labelled B (the EntryBar prints)
                            Code:
                            Enabling NinjaScript strategy 'BidAskIncDecr/238743953' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=True MaxRestarts=4 in 5 minutes
                            3 11/01/2022 19:24:00 0
                            A 11/01/2022 19:24:00 6
                            [B]B 11/01/2022 19:24:00 0[/B]
                            C 6
                            4 11/01/2022 19:24:00 81.25
                            5 11/01/2022 19:24:00 81.25
                            3 11/01/2022 19:24:00 81.25
                            A 11/01/2022 19:24:00 10
                            [B]B 11/01/2022 19:24:00 6[/B]
                            C 4
                            4 11/01/2022 19:24:00 81.25
                            5 11/01/2022 19:24:00 81.25
                            3 11/01/2022 19:24:00 81.25
                            A 11/01/2022 19:24:00 12
                            [B]B 11/01/2022 19:24:00 10[/B]
                            C 2
                            4 11/01/2022 19:24:00 81.24
                            5 11/01/2022 19:24:00 81.24
                            3 11/01/2022 19:24:00 81.24
                            A 11/01/2022 19:24:00 14
                            [B]B 11/01/2022 19:24:00 12[/B]
                            C 2
                            4 11/01/2022 19:24:00 81.24
                            5 11/01/2022 19:24:00 81.24
                            3 11/01/2022 19:24:00 81.24
                            A 11/01/2022 19:24:00 15
                            [B]B 11/01/2022 19:24:00 14[/B]
                            C 1
                            4 11/01/2022 19:24:00 81.24
                            5 11/01/2022 19:24:00 81.24
                            3 11/01/2022 19:25:00 81.24
                            A 11/01/2022 19:25:00 17
                            [B]B 11/01/2022 19:25:00 15[/B]
                            C 2
                            4 11/01/2022 19:25:00 81.24
                            5 11/01/2022 19:25:00 81.24
                            3 11/01/2022 19:25:00 81.24
                            A 11/01/2022 19:25:00 18
                            [B]B 11/01/2022 19:25:00 17[/B]
                            C 1
                            4 11/01/2022 19:25:00 81.25
                            5 11/01/2022 19:25:00 81.25
                            3 11/01/2022 19:25:00 81.25
                            A 11/01/2022 19:25:00 19
                            [B]B 11/01/2022 19:25:00 18[/B]
                            C 1
                            4 11/01/2022 19:25:00 81.25
                            5 11/01/2022 19:25:00 81.25
                            3 11/01/2022 19:25:00 81.25
                            A 11/01/2022 19:25:00 22
                            [B]B 11/01/2022 19:25:00 19[/B]
                            C 3
                            4 11/01/2022 19:25:00 81.26
                            5 11/01/2022 19:25:00 81.26
                            3 11/01/2022 19:25:00 81.26
                            [B]A 11/01/2022 19:25:00 22
                            B 11/01/2022 19:25:00 22[/B]
                            C 0
                            Value of property 'Period' of NinjaScript 'MAX' is 0 and not in valid range between 1 and 2147483647.
                            Disabling NinjaScript strategy 'BidAskIncDecr/238743953'
                            As you can see in the prints above the B labelled prints value changes over time. That is my issue. I need the 1st value only (B 11/01/2022 19:24:00 6).

                            Please see the details and prints in my other post

                            https://ninjatrader.com/support/foru...83#post1185283

                            https://ninjatrader.com/support/foru...47#post1185247

                            I've tested adding the Bar index as EntryBar = CurrentBars[1][0]; but that's not working (Cannot apply indexing with [] to an expression of type 'int' CS0021 109 17).

                            How to apply your method to my DataSeries problem and get the single int value of the savedBar/EntryBar?
                            Last edited by PaulMohn; 01-12-2022, 03:06 AM.

                            Comment


                              #15
                              You should have started a new thread for this question.

                              CurrentBars[1] is the bar number for the 1-Tick secondary
                              data series and, by definition, is changing every tick.

                              Is that the number you want to use?
                              Last edited by bltdavid; 01-12-2022, 04:39 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Rapine Heihei, Today, 08:19 PM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by Rapine Heihei, Today, 08:25 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post Rapine Heihei  
                              Started by f.saeidi, Today, 08:01 PM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by Rapine Heihei, Today, 07:51 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Rapine Heihei  
                              Started by frslvr, 04-11-2024, 07:26 AM
                              5 responses
                              96 views
                              1 like
                              Last Post caryc123  
                              Working...
                              X