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!
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
NinjaTrader
drawing a horizontal line on the stop level
Collapse
X
-
Originally posted by Pete77Re my RemoveDrawObject effort, for some strange reason I am getting price instead of bar number for DrawnAtBar. I entered "private int DrawnAtBar = 0" under public class, otherwise entered your very helpfull code suggestion..
Otherwise, congrats, sounds like you're making good progress!
Comment
-
Hi bitDavid. I will try to upload my little program. Hopefully you can see why the RemoveDrawObject is not working EntryLines.cs,thanks.
Comment
-
Hello Pete77,
I believe bltdavid is suggesting this be a separate condition set.
private int drawnOnBar;
if (/* conditions to draw object */)
{
DrawDot("tag1", true, 0, Low[0] - TickSize, Color.Red);
drawnOnBar = CurrentBar;
}
// conditions to remove drawing object
if (CurrentBar - drawnOnBar >= 3)
{
RemoveDrawObject("tag1");
}Last edited by NinjaTrader_ChelseaB; 03-27-2019, 02:05 PM.Chelsea B.NinjaTrader Customer Service
Comment
-
I made the corrections (Capitalization errors etc) but I believe there is something wrong with the logic.When I Print "Current bar - drawnOnBar" I get big numbers, in the hundreds,so the line is immediately removed instead of three bars later. EntryLines.cs WhenI comment "RemoveDrawObject " the line shows up in the correct place. I will try to post again.
Attached FilesLast edited by Pete77; 03-26-2019, 03:03 PM.
Comment
-
Hello Pete77,
In the current file you have provided, you are no longer saving the bar number the object was drawn on to a variable.
Also, the code to remove the drawing object is not in a separate condition set. Instead, this is nested inside of the 'if (Close[1] > Open[1] && (Close[0] < Open[0]))' condition set.
A separate condition would not be within the action block of the other condition set. Instead, this would be outside of the curly braces of the condition set it is in.
Currently, your object will only be removed if it is drawn on the same bar because the condition to check if its 3 bars later is in the same action block as the condition set that draws the object. It is only able to draw and remove the object at the same time. Since it will never the drawing bar and 3 bars later at the same time, this nested condition will never evaluate as true.Chelsea B.NinjaTrader Customer Service
Comment
-
Hello Pete77,
CurrentBar is the bar number that is processing.
If you save the bar number that is processing to a variable, on a later bar you can subtract that bar number from the current bars number to find out how many bars ago that occurred.
The condition set where you are checking the variable should be in a different condition set than where the variable is being set.
If you Print(CurrentBar); outside of any conditions in OnBarUpdate(), this will print the bar number that is processing in OnBarUpdate().
Attached is an example.Attached FilesChelsea B.NinjaTrader Customer Service
Comment
-
Thanks for the explanation. I separated the condition sets as suggested.Still not working. When I Print "CurrentBar - drawnOnBar", On Output I get a bunch of zeros then EntryLines.cs numbers like 309.I will post current code.About to give up on this.
Comment
-
Hello Pete77,
Attached is an example that draws an object on the 10th to last historical bar.
Then on each new bar gives the number of bars ago that object was drawn.
You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.Attached FilesChelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by Pete77 View PostWhen I Print "CurrentBar - drawnOnBar", On Output I get a bunch of zeros
You don't want your OnBarUpdate to run that often, do you?
Running on each bar close is probably more what you want.
Make sure you understand this page,
https://ninjatrader.com/support/help...?calculate.htm
So, change your code to this,
Code:Calculate = Calculate.OnBarClose;
You could wrap your entire contents of OnBarUpdate inside IsFirstTickOfBar.
That would make your code impervious to whatever Calculate happens to be set to.
To make it impervious to the whims of the Calculate property,
change your current OnBarUpdate code from this,
Code:protected override void OnBarUpdate() { if (Close[1] > Open[1] && (Close[0] < Open[0])) { Draw.HorizontalLine(this, "Tag1", Low[1] - TickSize, Brushes.Red); drawnOnBar = CurrentBar; } Print(CurrentBar - drawnOnBar); if ((CurrentBar - drawnOnBar) >= 3) RemoveDrawObject("Tag1"); }
Code:protected override void OnBarUpdate() { if (IsFirstTickOfBar) { if (Close[1] > Open[1] && (Close[0] < Open[0])) { Draw.HorizontalLine(this, "Tag1", Low[1] - TickSize, Brushes.Red); drawnOnBar = CurrentBar; } Print(CurrentBar - drawnOnBar); if ((CurrentBar - drawnOnBar) >= 3) RemoveDrawObject("Tag1"); } }
Code:protected override void OnBarUpdate() { if (!IsFirstTickOfBar) return; if (Close[1] > Open[1] && (Close[0] < Open[0])) { Draw.HorizontalLine(this, "Tag1", Low[1] - TickSize, Brushes.Red); drawnOnBar = CurrentBar; } Print(CurrentBar - drawnOnBar); if ((CurrentBar - drawnOnBar) >= 3) RemoveDrawObject("Tag1"); }
your OnBarUpdate repeatedly for every price change during the life of that bar, which is a
lot of calls. Thus, the calculation "CurrentBar - drawnAtBar" produces zero at every price change,
until the bar finally closes and CurrentBar is incremented by 1.
CurrentBar does not increment just because OnBarUpdate is called. Because of the "flexibility"
of what the Calculate property is able to achieve, your OnBarUpdate could be called hundreds
or even thousands of times and CurrentBar hasn't changed.
Remember, CurrentBar is only incremented by 1 when the bar closes.
Again, I can't emphasize this enough, make sure you completely understand this page,
https://ninjatrader.com/support/help...?calculate.htm
This may help, too,
https://ninjatrader.com/support/help...ttickofbar.htm
Last edited by bltdavid; 03-27-2019, 10:15 PM.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by ZeroKuhl, Today, 04:31 PM
|
0 responses
7 views
0 likes
|
Last Post
by ZeroKuhl
Today, 04:31 PM
|
||
Started by Vulgoth_t_Destroyer, 05-09-2022, 04:45 PM
|
54 responses
5,336 views
0 likes
|
Last Post
by Gaterz
Today, 04:29 PM
|
||
Started by ETFVoyageur, 05-07-2024, 07:05 PM
|
11 responses
73 views
0 likes
|
Last Post
by -=Edge=-
Today, 04:27 PM
|
||
Started by _Zero_, 04-10-2020, 03:21 PM
|
145 responses
7,899 views
6 likes
|
Last Post
by johng2
Today, 04:19 PM
|
||
Started by TheTradingMantis, 01-19-2023, 02:05 AM
|
43 responses
917 views
0 likes
|
Last Post
by jmagaia
Today, 04:13 PM
|
Comment