I am trying to put a time limit on the code to draw a text but it is not working and I cant figure out what am I doing wrong... all I want is when the time is between the time variables, to draw Trading... and if not write no trading or something like that... here is the code I am using.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
draw a text on the chart based on time
Collapse
X
-
draw a text on the chart based on time
hello
I am trying to put a time limit on the code to draw a text but it is not working and I cant figure out what am I doing wrong... all I want is when the time is between the time variables, to draw Trading... and if not write no trading or something like that... here is the code I am using.
Tags: None
-
I forgot to attach the document.
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 0)
return;
// Set 1
if ((((Times[0][0].TimeOfDay > Start_Time.TimeOfDay)
&& (Times[0][0].TimeOfDay < End_Time.TimeOfDay))
||
((Times[0][0].TimeOfDay > Start_Time2.TimeOfDay)
&& (Times[0][0].TimeOfDay < End_Time2.TimeOfDay)))
&& Position.MarketPosition == MarketPosition.Flat
)
{
IsTradingTime = false;
}
// Set 2
if ((((Times[0][0].TimeOfDay > Start_Time.TimeOfDay)
&& (Times[0][0].TimeOfDay < End_Time.TimeOfDay))
||
((Times[0][0].TimeOfDay > Start_Time2.TimeOfDay)
&& (Times[0][0].TimeOfDay < End_Time2.TimeOfDay)))
&& Position.MarketPosition == MarketPosition.Flat
)
{
IsTradingTime = true;
}
// Set 3
if (IsTradingTime == true)
{
Draw.TextFixed(this, @"Test Fixed text_1", trading, TextPosition.TopRight);
}
// Set 4
if (IsTradingTime == false)
{
Draw.TextFixed(this, @"Test Fixed text_1", @"no trading", TextPosition.TopRight);
}
}
}
}
-
Hello babouin77,
Thank you for your inquiry.
It looks like your conditions in Set 1 and Set 2 are the same, which would cause IsTradingTime to always be set to true if it's between one of those two time periods, so you'd always see it print true unless you're in a trade during one of the two time periods, in which case it would print false. It looks like your strategy was created with the Strategy Builder, is that correct? If you've unlocked that code you could simply do something like this:
if (((Times[0][0].TimeOfDay > Start_Time.TimeOfDay)
&& (Times[0][0].TimeOfDay < End_Time.TimeOfDay))
||
((Times[0][0].TimeOfDay > Start_Time2.TimeOfDay)
&& (Times[0][0].TimeOfDay < End_Time2.TimeOfDay)))
{
IsTradingTime = true;
}
else {
IsTradingTime = false;
}
// Set 3
if (IsTradingTime == true)
{
Draw.TextFixed(this, @"Test Fixed text_1", "trading", TextPosition.TopRight);
}
// Set 4
if (IsTradingTime == false)
{
Draw.TextFixed(this, @"Test Fixed text_1", @"no trading", TextPosition.TopRight);
}
If you're just wanting that to print based on whether it's between those two times, I wouldn't suggest checking the position, otherwise it'll print "no trading" when in a position during that time frame.
Now if it's still locked in the Strategy Builder this gets trickier, because you can't really do an else in the Builder. What you could do is in set 1, don't put any conditions, but have it automatically set the IsTradingTime bool to false, and then leave your conditions in set 2 to set IsTradingTime to true. This way, it's false unless you're within those hours and you get the same effect as above.
Please let us know if we may be of further assistance to you.
- Likes 1
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
558 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
324 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 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
545 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
547 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment