Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why does this code work vs this code

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

    Why does this code work vs this code

    Example 1 This plots one rectangle and extend its until parameters are met

    private bool DoItOnce = true, keepDrawing = true;
    private int startBar;
    private double rect_high, rect_low;


    if (State != State.Realtime) return; // just use realtime data for this example

    if (DoItOnce)
    {
    startBar = CurrentBar - 30; // pick a startpoint
    rect_high = High[30]; // pick a high level
    rect_low = Low[30]; // pick a low level
    Draw.Rectangle (this, "test", (CurrentBar - startBar), rect_high, 0, rect_low, Brushes.Cornsilk, true); //draw first rect.
    DoItOnce = false; // set bool false so we only draw initial once
    }
    else if (keepDrawing)
    {
    Draw.Rectangle (this, "test", (CurrentBar - startBar), rect_high, 0, rect_low, Brushes.Cornsilk, true); // redraw (extend) rectangle to current bar
    }

    if (CrossAbove(High, rect_low, 1) || CrossBelow(Low, rect_high, 1)) // test to see if boundaries crossed and if so stop drawing.
    keepDrawing = false;





    Example 2 This plots multiple rectangles


    private bool DoItOnce = true, keepDrawing = true,swingStart = true;
    private int mystartBar;
    private double rect_high, rect_low,rect_close1,rect_close2;



    int myCounter = 1;
    string myRect = "myRect";
    string myRect2 = "myRect";
    int mystartBarCounter = 4;
    //int myHighLowCounter = 8;
    bool swingHigh = High[6] <= High[3] && High[5] <= High[3] && High[4] < High[3] && High[2] <= High[3] && High[1] <= High[3] && High[0] <= High[3];


    //if 3rd bar in 7 has 3 lower highs to the right and left, draw a rectangle from swing highest high to swing highest close


    if(swingHigh)
    {

    mystartBar = CurrentBar - 4; // pick a startpoint
    rect_high = MAX(High,8)[0]; // pick a high level
    rect_low = MIN(Low,8)[0]; // pick a low level
    rect_close1 = MAX(Close,8)[0]; // pick a close for highest bar level
    rect_close2 = MIN(Close,8)[0]; // pick a close for lowest bar level

    if(DoItOnce)
    {
    Draw.Rectangle(this, CurrentBar.ToString() + "myRect", false, 3, rect_close1, 0, rect_high, Brushes.DarkRed, Brushes.Red, 5, false);
    DoItOnce = false;
    }

    }
    else if(keepDrawing)
    {
    Draw.Rectangle(this, CurrentBar.ToString() + "myRect", false, 3, rect_close1, 0, rect_high, Brushes.DarkRed, Brushes.Red, 5, false);
    }
    else if(HighestBar(Close, 1) > HighestBar(High, mystartBar))
    {
    keepDrawing = false;
    }

    #2
    Hello jamestrader21x,

    Thanks for your post.

    In taking a quick look at your code one obvious difference is that you are creating unique tag names in example 2 so for each new tag name a new rectangle is added and the old ones remain (nothing is removed). In the first example, the tag name is "test" so it is not unique and this means that when Draw.tectangle() is executed it will first remove the existing "test" and replace it with the newest"test".

    Try removing the "CurrentBar.ToString() +" from the tag name. as in this case you only want to rectangle to change, not to add more rectangles

    Comment


      #3
      Changing it from CurrentBar.ToString() + "myRect" to just "myRect" only plotted one rectangle. I want to plot multiple rectangles yet perform like Example 1. I'm having a tough time figuring out how to get the code right.

      Comment


        #4
        Hello jamestrader21x,

        Thanks for your reply.

        Something you will need to understand is that for each instance of a rectangle that you create and you want it to continue to extend the rectangle you will need to have the same tag name as demonstrated in the first example which is one rectangle redrawn until the price hits it

        So each new instance of a rectangle will need to use a different tag name but to keep intending it you will need to save and reuse that tag name each time you draw the same rectangle.

        To provide another example, assume you have found 6 different swing highs that you want to draw the rectangles from. You would then need 6 differently named rectangles and when you update per bar you would have to use those same 6 tag names to redraw the 6 rectangles from their starting bar to the current bar.

        So you will need to be able to deal with a varying number of variables to keep tracking of the varying number of rectangles. In the case of example 6, that means 6 highs and lows, 6 start bars and 6 tag names, 6 bools.

        As you are finding out, this can get complicated in a hurry. If you would like this created for you, an option is to use the services of a 3rd party programmer in the NinjaTrader Ecosystem. If this is of interest, we can provide a link to such programmers.

        Comment


          #5
          Hello jamestrader21x,

          I meant to post a link to another indicator that may be similar to what you are doing and may provide some further ideas you can use or perhaps modify to meet your needs if it does not.

          This indicator is publicly available on our NinjaTrader Ecosystem website:
          Here is a basic guideline of how to Import NinjaScripts.

          To import NinjaScripts you will need the original .zip file.

          To Import
          1. Download the NinjaScripts to your desktop, keep them in the compressed .zip file.
          2. From the Control Center window select the menu Tools>Import>Ninjascript add-on..
          3. Select the downloaded .zip file
          4. NinjaTrader will then confirm if the import has been successful.

          Critical - Specifically for some NinjaScripts, it will prompt that you are running newer versions of @SMA, @EMA, etc. and ask if you want to replace, press 'No'

          The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
          Last edited by NinjaTrader_PaulH; 08-21-2020, 11:22 AM. Reason: Changed step 2 for NinjaTrader8 import.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          607 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          353 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          560 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          561 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X