Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help with this

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

    Need help with this

    Hi,

    The codes connect pivot lows to pivot lows. Once it comes across a new pivot low, it will lookback for the most recent pivot low which is less than the current pivot low and draw a ray between the two points. That was working fine till the clutter of rays made it impossible to even see the candles!

    So I tried to limit the number of rays to 10 (DemandLineMaxCount) as there are usually around 8 pivot lows a day on a 10 min chart. The intention is to have it remove the oldest ray as and when new rays are being drawn. Unfortunately it looked like it was keeping the old rays as new rays are removed. I increased the limit to 100 before I could get the chart to populate (see attached chart) and still the newer pivot lows didn't populate or got removed! The red dots in the charts are where there should be rays drawn

    I'm not sure where it went wrong. Could someone please take a look? I've attached the add-on ninjascript.

    Update:I changed the Lines 128 and 129 from DemandLines[DemandLines.Count-1] to DemandLines[0] and DemandLines.RemoveAt(DemandLines.Count-1) to DemandLines.RemoveAt(0).

    At least the newer lines are there and the older lines are removed. What I can't understand is when DemandLineMaxCount =10, I only see 3 rays when I should have 9.

    Regards
    Kay Wai
    Attached Files
    Last edited by kaywai; 05-30-2022, 10:32 PM.

    #2
    Hello kaywai,

    I'm not going to debug your code but I can suggest a simpler approach to limiting draw objects.

    If I understand correctly, you want to limit to see the last x number of rays.

    When you draw an object if the tag name is NOT unique, then the older drawing (with the same tag name) is automatically removed when the latest one is drawn. You do not need to remove it yourself! So you want to create a limited number of tagnames.

    You can create an integer counter that you increment when you draw a ray. You then add the counter to the drawing tag name. When the counter is reset (after x objects drawn) then previous ray with that same tag name will be removed and the new one will be drawn.

    if (your condition to draw a ray)
    {
    Draw.Ray(this, "myRay"+myCounter, .....);
    myCounter ++; // increment counter (you could need to create as private int myCounter =0;

    if (myCounter >= 10)
    {
    myCounter == 0; // reset counter
    }
    }

    You could also create a public int input so that you can change the number of rays to be visible when ever you apply the indicator.
    Last edited by Tasker-182; 05-30-2022, 05:59 AM.

    Comment


      #3
      Hello Kay Wai,

      To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

      In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

      Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

      I am happy to assist you with analyzing the output from the output window.

      Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output from both computers to your reply.

      Below is a link to a forum post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

      https://ninjatrader.com/support/foru...121#post791121

      Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hi ChelseaB,

        I did have print statements originally which is how I got to this stage. But I'm not sure how to generate print statements to understand how list works. Could you perhaps provide an example?

        The problem only occurred after I needed to reduce or cap the number of rays on a chart. Hence, when I added list. The other possible issue is with the foreach loop. I was thinking it would remove 1 ray at a time, so I would have 9 or 10 rays on the chart. I did not expect to see only 3! The positive is that it is the 3 most recent lines! How do you print statements for foreach loops?

        This is the first time I've used foreach loops and list. So I hope you understand when at times I seem confused.

        Another thing is how do you do a Print statement for a DrawRay? I tried printing the tags (but I could be printing it incorrectly) because I'm getting a tag at every bar. Technically though, if every ray is printing correctly, there shouldn't be an issue, right?

        I look forward to your assistance.

        Regards
        Kay Wai
        Last edited by kaywai; 05-30-2022, 03:46 AM.

        Comment


          #5
          Tasker-182 I did try a code earlier, similar to what you suggested (please see attached) but to me, the code was randomly removing rays.

          In any case, I did try your suggestion. It would seem that I am getting a similar result to what I'm getting with list - when the counter hits limit, in this case 10, i'm left with 3 lines. Good thing is it is the 3 most recent lines.

          Perhaps there is some other logic at work that I do not understand.

          Regards

          Kay Wai
          Attached Files
          Last edited by kaywai; 05-30-2022, 03:47 AM.

          Comment


            #6
            Hello Kaiwai,

            I attached a simple example for you to test.

            It will draw a dot every 10 bars (after the first 20 bars) over the duration of the data series. By using a counter in the tag name it will limit the dots to just the latest 10 (or any number you enter as an input, default is 10).

            To "prove" that it is drawing and removing the dots, I added a print statement that prints the time of the bar and the tag name of the dot being drawn. This will indeed show that the code was executed on every 10 bars (by default) yet you only see the last 10 bars.

            I hope this helps.


            Click image for larger version  Name:	ExampleDrtawObjectLimit.png Views:	0 Size:	179.8 KB ID:	1203365


            ExampleDrawObjectLimit.zip

            Last edited by Tasker-182; 05-31-2022, 06:07 AM.

            Comment


              #7
              Hi Tasker-182,

              I can only see the chart. Could you please re-share your example? Thanks.

              Regards
              Kay Wai

              Comment


                #8
                I think it shows now.

                Comment


                  #9
                  Hi Tasker-182,

                  I added your example to my code. Here is the result. (Please attached screenshot).

                  The tags are similar to yours but as you can see, there are only 2 lines for today's chart! There should be 6 new Rays drawn today plus the remaining from the previous trading day. Could my "for" loop to determine when the rays are drawn be causing the problem? They seem to work fine without the cap.

                  I've attached my code if you'd like to take a look. No bells and whistles in it. Very small code.

                  Regards

                  Kay Wai


                  Attached Files
                  Last edited by kaywai; 05-31-2022, 12:50 PM.

                  Comment


                    #10
                    Hello kaywai,

                    I would suggest double clicking on the Rays that you see on the chart and look at the Drawing objects data. You will likely see the 10 drawing objects listed and if you click on each one and look at the data section for each ray you will likely see that several have the exact same characteristics meaning there are 10 rays drawn but they have the same data points as the three that you see. So I think you are drawing the ray, then on the next bar drawing the next ray right on top.

                    So the issue then becomes your code discerning when you need to actually draw a new ray as opposed to drawing rays with the same data.





                    Comment


                      #11
                      Hi Tasker-182,

                      You are correct. I just picked a random ray near the right side of the chart, and true enough, there were 5 rays there!!!

                      I only need to draw 1 ray each time, not repeating it 5 times!

                      So the "for" loop is the issue. Any suggestions what I can do?

                      Once the latest pivot low is determined, the code needs to look back for a pivot low which is lower than the latest pivot low. And a ray is drawn through the 2 points.

                      I thought a "for" loop would be most appropriate. I didn't realise it would draw over itself. I did wonder why the tags kept drawing on every bar but never thought they were multiple rays being drawn over each other.

                      Regards

                      Kay Wai

                      Comment


                        #12
                        Hello kaywai,

                        All I could suggest is checking to see if the newly determined ray X and Y points match already in used ray X and Y points and if so to skip drawing the ray and counter. I'm not sure if in the (for loop) look back you are encountering more than one ray so you may end up needing to save more than 1 set of values.

                        You probably want to use plenty of print statements to look at all the data points being used and see if there is a way to navigate or trigger the action so you are only drawing rays once.

                        I don't know if this will help but if you have just drawn a ray, on the next bar do you really need to look back past the bar the ray was last drawn on? (Maybe you do but if not then you can limit the look back to the last bar drawn to?).

                        Comment


                          #13
                          Something to ponder on I guess. Thanks for the pointers Tasker-182.

                          Comment


                            #14
                            Hi Tasker-182,

                            I got the desired result, I think! It seems to be a case of whether you put a block of code within another block of code or to have it as a separate block.

                            Anyway, thanks for your patience and guidance! Appreciate it!

                            Regards
                            Kay Wai
                            Attached Files

                            Comment


                              #15
                              kaywai your concept seems interesting, can you share your final code?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              637 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              366 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              569 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              571 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X