Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Continue Drawing Line Until Indicator Crosses Above
Collapse
X
-
Continue Drawing Line Until Indicator Crosses Above
The lines are drawing fine except I can't figure out how to make them continue to draw . I attached a picture showing what the indicator is currently doing and what I would like it to do. Any help would be greatly appreciated.Last edited by jhowinvest; 06-13-2012, 05:36 PM.Tags: None
-
You will have to change your mind set, and draw the past instead of into the future. You will keep drawing until your condition is met. Unfortunately, you cannot do that in the wizard.Originally posted by jhowinvest View PostI have the following code which draws a line for 5 bars but I want the line to continue drawing until the bollinger band crosses the current line or a new line starts. The lines are drawing fine except I can't figure out how to make them continue to draw until the line is crossed by the bollinger or until a new line starts instead of just n bars into the future(in my current code it's 5 bars into the future). I attached a picture showing what the indicator is currently doing and what I would like it to do. Any help would be greatly appreciated. Current code below:
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if(CurrentBar < 10)
return;
// Condition set 1
if (Bollinger(2.5, 14).Lower[0] > Bollinger(2.5, 14).Lower[1]
&& Bollinger(2.5, 14).Lower[2] > Bollinger(2.5, 14).Lower[1])
{
DrawLine("LowLine" + CurrentBar, true, 1, Bollinger(2.5, 14).Lower[1], -5, Bollinger(2.5, 14).Lower[1], Color.Magenta, DashStyle.Dash, 2);
}
// Condition set 2
if (Bollinger(2.5, 14).Upper[0] < Bollinger(2.5, 14).Upper[1]
&& Bollinger(2.5, 14).Upper[2] < Bollinger(2.5, 14).Upper[1])
{
DrawLine("HiLine" + CurrentBar, true, 1, Bollinger(2.5, 14).Upper[1], -5, Bollinger(2.5, 14).Upper[1], Color.Magenta, DashStyle.Dash, 2);
}
}
The key is when you want the line to start. At that time you name the line, then continue redrawing the same line with a length that is monotonically increasing, so that the line always starts in the same place, and stop drawing that line and start drawing a new one when a new one starts. Rinse, repeat.
-
Thank you Koganum. My issue is I don't know how to start and stop the line except with "n" bars in the past or the future. Currently the line starts as soon as my condition is met (1 bar ago) and continues 5 bars into the future. Can you or anyone point me in the right direction on starting and stopping the line another way?
Comment
-
Hi jhowinvest,
Koganam is right here in that you should look to draw in the past rather than the future. I'm not entirely sure what the lines are trying to convey. Maybe you could draw on a chart manually what it's ultimately supposed to look like so we can have a better idea?Ryan M.NinjaTrader Customer Service
Comment
-
Chart Mark Up
I posted a chart here with the lines manually drawn the way I want them. Stopping the line from drawing is the coding question.Last edited by jhowinvest; 06-13-2012, 05:35 PM.
Comment
-
I see, thanks for posting the screenshot. Given the requirements this can be a bit challenging to code. As koganam mentioned this will take a bit of rethinking your current implementation.
You'll likely need to store the bar number of the condition and it will also help to have this act as the tag of the line. Here's an example structure that may help you get started. This will draw a line pretty much always, so additional tweaking is needed so that it's only drawn exactly when you want.
Code:#region Variables private bool doOnce; private int conditionBar; private bool drawLines; #endregion OnBarUpdate() if (conditionsForDrawNewLine) //this is your main draw condition. { doOnce = true; drawLines = true; } if (doOnce) { conditionBar = CurrentBar; doOnce = false; } if (crossOver) //this will disable drawing after a crossover. drawLines = false; if (drawLines) DrawLine("myLine" + ConditionBar, true, 0, High[0], CurrentBar - ConditionBar, High[0], Color.Blue);Last edited by NinjaTrader_RyanM1; 06-11-2012, 10:19 AM.Ryan M.NinjaTrader Customer Service
Comment
-
Thanks Ryan. I will keep working on this. Your code example gives me a great place to start. Thanks again.
Comment
-
I've been trying to achieve a similar result as jhowinvest so I've used the sample structure Ryan provided below to make lines continue extending to the right until
a certain set of conditions is met.
"CurrentBar - ConditionBar" in his sample made the lines extend all the way to the left so I used instead ConditionBar - CurrentBar, which did make the lines continue extending to the right, which is almost exactly what I've been trying to achieve except that the lines do not stop after the "crossover" event, but keep extending to the right.
I'm not a programmer and I have little experience with Ninjascript.
How can I make the lines stop extending once the "crossover" occurs? I guess this is
what Ryan referred to as "additional tweaking"?
Could any programmers out there offer some help?
I would really appreciate it.
Thanks.
Comment
-
Hello 2fr33d0m,
The if (crossOver) is a substitute for the crossover condition that would cause the script to stop drawing the line.
For example:
if (CrossAbove(SMA(4), SMA(14), 1))
drawLines = false;
This would cause the line to stop drawing when the SMA with a period of 4 crosses above the SMA with a period of 14.
What do you currently have as the condition to set drawLines to false?Chelsea B.NinjaTrader Customer Service
Comment
-
Thank you for replying so soon.
Right now, I just have this:
if (High[0] > Low[conditionBar])
drawLines = false;
I meant to write something that stops the line from extending when any bar after the conditionBar closes and its high is higher than the low of the condition bar.
I set CalculateOnBarClose to true.
What am I missing?
Thanks.
Comment
-
Hi ,
The code you have is almost correct.
The conditionBar is going to be the bar number saved when the line starts.
The Low[] is looking for a bars ago value. To find this bars ago value use CurrentBar-conditionBar.
For example:
if (High[0] > Low[CurrentBar-conditionBar])
drawLines = false;
This code will stop drawing the line if the high of the current bar is greater than the low of the conditionBar.Chelsea B.NinjaTrader Customer Service
Comment
-
Hi,
I tried using Low[CurrentBar-conditionBar] instead of Low[conditionBar], and now
most of the lines do not appear at all, and the few that do still extend all the way
to the right past the bars that meet the condition for stopping them.
How should I go about solving this?
Thanks.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
580 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
336 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
554 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
552 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment