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

using loops

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

    using loops

    Hi,

    I'm trying to rewrite some code but it doesn't seem to be working.
    From:

    if (Low[1]<Low[0] && Low[1]<Low[2])
    {
    DemandPoint.Set(Low[1]);
    DrawSquare("DP1"+CurrentBar, true, 1, Low[1], Color.Red);
    }

    To:

    for (int x = 0; x < level; x++)
    {
    if ( Low[level] < Low[level - x] &&
    Low[level] < Low[level + x])
    {
    DemandPoint.Set(Low[level]);
    DrawSquare("DP1" + CurrentBar, true, 1, Low[level], Color.Red);
    }
    }

    where "level" = 1. Nothing is drawing but there is no error when compiling.

    Could you please tell me what I'm doing wrong?

    Regards
    ​​​​​​​Kay Wai

    #2
    Hello kaywai,

    Thanks for your post.

    If level is always 1 then your statement Low[level] < Low[level - x], when x = 0, translates to Low[1] < Low[1] . As x must be less than level it will only run once meaning that the condition Low[1] < Low[1] will never be true.

    You can validate your functionality by using Print statements inside your for loop to print out the values.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thx PaulH!

      I've changed the for loop to:-

      for (int x = 1; x <= level; x++)

      but i run into problems when level = 2.

      when level = 2, the statement should be:-

      if (Low[2]<Low[0] && Low[2]<Low[1] && Low[2]<Low[3] && Low[2]<Low[4])

      and the loop statement

      if ( Low[level] < Low[level - x] &&
      Low[level] < Low[level + x])

      is not calculating the way i thought it would. Is there something missing in the loop statement? I thought the loop statement would compare all 4 equations and if all 4 were true, then it would draw a square at the relevant point. And since we are at this point, my intention is for level to go to a max of 21.

      Would appreciate if you could tell me what I'm missing.

      Regards

      Kay Wai

      Comment


        #4
        Hello kaywai,

        Thanks for your reply.

        The draw statement would be drawn on the first occurrence where the two conditions are true. If you want to draw only when all 4 are true then you would need additional logic such as if the condition is false to then break from the loop. Reference: https://www.dotnetperls.com/break

        You can validate your functionality by using Print statements inside your for loop to print out the values.

        As you change the number of bars to look back, check your "Log" tab for any errors concerning the indicator and bar access. You will likely need to add a check at the top of OnBarUpDate(0 so that your code is not processed until the script has processed that many bars first. Please see: https://ninjatrader.com/support/help...currentbar.htm
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi PaulH,

          Thank you for your explanation. Through the link you provided as well as some help from websites on the internet, I managed to solve it.
          Thanks again.



          Regards
          Kay Wai
          Last edited by kaywai; 08-04-2020, 12:38 AM.

          Comment


            #6
            Hi Paul,

            I've noticed in the dataseries created above, there are points which carry a zero value. I'm trying to draw a line using the points that have been added to the dataseries when true and exclude the zeroes. The other criteria is the most recent low is higher than the previous lows. Would the dataseries be the right "list" to use?

            regards
            Kay Wai

            Comment


              #7
              Hello kaywai,

              Thanks for your reply.

              When you create a data series, it will create as many slots to store information as there are bars in your chart's data series. When you are conditionally (meaning not on every bar) writing values to the data series, it would be expected that you will have gaps between the values you have written.

              Depending on what you need to do, you may need to loop back through your data series to find the last values written. You can test for valid values with DataSeries.ContainsValue(int barsAgo)
              Returns a true or false value. Reference: https://ninjatrader.com/support/help...ries_class.htm

              Do you need to use a data series or do you just need to save the values into a variable until they are needed?



              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hi Paul,
                assuming the code in the initial post is correct, the dataseries will have data added to it at every bar. It's a case of whether It's zero or a number. Am I correct?

                what I'm trying to achieve, in a basic manner, is to use the 5 most recent data points and compare each of them against 5 other earlier data points where the more recent data point is higher than the earlier data points. Once that is done, I'd like DrawRays thru each of those data points. This means i would have a 25 rays drawn.

                Is dataseries the correct thing to use?

                regards
                Kay Wai

                Comment


                  #9
                  Hello kaywai,

                  Thanks for your reply.

                  If you are writing zero to the data series then when you look for the last 5 values you could loop back more than 5 places and use a simple int counter to count when you find an actual value or if zero to keep looping until you hit your lookback limit or your counter =5 and at that point break from the loop.

                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi PaulH,

                    I went to the output window to check on the data. I added a Time to the output. From the output, i've noticed:-


                    1) the dates are wrong and are indicating some different to what is printed on the chart.
                    2) there are random values which don't mean a thing from a chart perspective.
                    3) the chart points are similar to the result i have from coding the long way instead of the "for" loop

                    Would you mind taking a look to see what's wrong? Code is attached.

                    Thanks and regards
                    Kay Wai
                    Attached Files
                    Last edited by kaywai; 08-05-2020, 04:07 AM.

                    Comment


                      #11
                      Hello kaywai,

                      Thanks for your reply.

                      You have the condition if (count == level) and level is fixed at 1.

                      The print statement will print the time of the bar that is currently being processed and you are printing the value of Low at a bars ago of level which is set to 1.

                      When the conditions of Low[level] < Low[level - x] && Low[level] < Low[level + x] and count == level were true. The print statements are executed.

                      Look at the output window, start at the bottom, find the last time printed and then on the chart find that bar and note that the bar before that will show the red square at the price value printed in the output window below the time.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for the explanation PaulH! Appreciate it! Will continue and see how we go.

                        Regards
                        Kay Wai

                        Comment


                          #13
                          Hi PaulH,

                          I tried calling up the data from the DataSeries in my code and the only correct points are all at [0]. I am trying to lock in the 5 points at right of the chart and to look for other points in my DataSeries which are at or before those 5 points AND are lower than those 5 points to DrawRays. And I had rays being drawn from everywhere. Some looked correct but most of them seemed random. I've attached the code for your easy reference.

                          Appreciate all the help you can provide for this next step of my indicator building.
                          Attached Files

                          Comment


                            #14
                            Hello kaywai,

                            Thanks for your reply.

                            I'm not following from your description.

                            Can you create a chart and add your indicator (without the rays but showing the squares) and then manually draw the rays that show what you are trying to do and post a screenshot?

                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi PaulH,

                              Please see attached.

                              Regards
                              Kay Wai

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GussJ, 03-04-2020, 03:11 PM
                              11 responses
                              3,221 views
                              0 likes
                              Last Post xiinteractive  
                              Started by andrewtrades, Today, 04:57 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by chbruno, Today, 04:10 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by josh18955, 03-25-2023, 11:16 AM
                              6 responses
                              436 views
                              0 likes
                              Last Post Delerium  
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post FAQtrader  
                              Working...
                              X