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

Having script theoretically add infinite lines if certain conditions are met.

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

    Having script theoretically add infinite lines if certain conditions are met.

    Hello,

    I would like to know if there are any examples you can point me to where a script can theoretically keep adding/drawing lines on a chart if certain conditions are met. Say for instance, if 3 bars back to back have Higher Highs and Lows, draw a line form Low[3] to High[1]. Then if 3 more bars pass and they are consecutive Lower Highs and Lows, draw a line from High[3] to Low[1]. The drawings will keep being added onto the screen every time those conditions are met.

    #2
    Hello Don22Trader1,

    Thank you for your post.

    I would like to start out by warning that a script that adds and accumulates drawing objects would become more resource-intensive over time and could lead to performance issues. Depending on the scenario, rendering lines with custom graphics in OnRender() may be a more performance-friendly option. For more information, please see the section "Using DrawObjects vs custom graphics in OnRender()" on the best practices page here:
    https://ninjatrader.com/support/help...s.htm#Performa nce

    In order to create new draw objects of the same type, a unique tag is needed in the Draw() method. If you use the same tag, the script will just modify the same draw object. The following snippet uses a unique tag each time so that each time the condition is true, a new line is drawn rather than modifying an existing line:
    Code:
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 3)
                    return;
    
                // if the last two completed bars have higher highs and lows than the preceding bar, draw a line from Low[3] to High[1]
                if (High[1] > High[2] && Low[1] > Low[2] && High[2] > High[3] && Low[2] > Low[3])
                    Draw.Line(this, "higherLine" + CurrentBar.ToString(), 3, Low[3], 1, High[1], Brushes.Blue);
    
                // if the last two completed bars have lower highs and lows than the preceding bar, draw a line from High[3] to Low[1]
                if (High[1] < High[2] && Low[1] < Low[2] && High[2] < High[3] && Low[2] < Low[3])
                    Draw.Line(this, "lowerLine" + CurrentBar.ToString(), 3, High[3], 1, Low[1], Brushes.Blue);
            }​
    For more information on the Draw.Line() method:


    This example could also be achieved in the Strategy Builder, and then you could click the "View Code" button to see what it would look like if it were coded manually in an unlocked script. You would set up a condition that makes price data comparisons, then an action to draw the line. For examples of those types of conditions and actions (and more) in the Strategy Builder:



    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by KonAdams, 05-07-2024, 10:53 PM
    9 responses
    25 views
    0 likes
    Last Post KonAdams  
    Started by haas88, 03-21-2024, 02:22 AM
    16 responses
    193 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by JimB17, 01-10-2020, 10:27 AM
    75 responses
    2,793 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by JGriff5646, 05-07-2024, 10:02 PM
    3 responses
    29 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by dp8282, Today, 07:35 AM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Erick  
    Working...
    X