Initially I thought that maybe each plot added in State.SetDefaults would be assigned a value starting with 0 and incrementing with each new "AddPlot()", then this index would be used to determine which value in OnBarUpdate() was associated with each plot. To test this I changed the order around in both locations, however the plotted indicator remained unchanged even after removing and reapplying to the chart.
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
How does NT know which plot goes with which values?
Collapse
X
-
How does NT know which plot goes with which values?
I'm trying to figure out how NinjaTrader knows to associate each plot with each value that is defined in OnBarUpdate().
Initially I thought that maybe each plot added in State.SetDefaults would be assigned a value starting with 0 and incrementing with each new "AddPlot()", then this index would be used to determine which value in OnBarUpdate() was associated with each plot. To test this I changed the order around in both locations, however the plotted indicator remained unchanged even after removing and reapplying to the chart.
Tags: None
-
The "Values" Array.
Value[0] ( note the absence of the 's' ) seen in most indicators is just a reference to Values[0][0].
Current value (E.g. [0] ) of the second plot can be found in Values[1][0].
https://ninjatrader.com/support/help...t8/?values.htm
HedgePlay
-
Originally posted by butt_toast View PostTo test this I changed the order around in both locations, however the plotted indicator remained unchanged even after removing and reapplying to the chart.
Your testing sounds inconclusive to me.
Without seeing the code, I would chalk that up as: you made no changes at all.
Comment
-
Originally posted by hedgeplay View PostThe "Values" Array.
Value[0] ( note the absence of the 's' ) seen in most indicators is just a reference to Values[0][0].
Current value (E.g. [0] ) of the second plot can be found in Values[1][0].
https://ninjatrader.com/support/help...t8/?values.htm
HedgePlay
Am I even in the ballpark here?
That is a head scratcher.
Once I get this to plot anything I feel like I can mess with it and get the ball rolling but I'm just spinning my wheels here.
I bought the NinjaScript Programmer's Launch Pad ebook and the first tutorial skips right over this topic and I feel like just getting something to plot would be the natural first step.
Code:public class anIndi : Indicator { private Series<double> aPleasePlotThis; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @""; Name = "anIndi"; Calculate = Calculate.OnBarClose; IsOverlay = false; DisplayInDataBox = true; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = true; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; //Disable this property if your indicator requires custom values that cumulate with each new market data event. //See Help Guide for additional information. IsSuspendedWhileInactive = true; AddLine(Brushes.Black, 1, "please just plot something, anything"); } else if (State == State.Configure) { aPleasePlotThis = new Series<double>(this); } } protected override void OnBarUpdate() { double aPleasePlotThis = 1.0; //please plot anything Value[0] = aPleasePlotThis; } }//end class
Last edited by butt_toast; 07-25-2021, 10:47 AM.
Comment
-
Originally posted by bltdavid View Post
Both locations?
Your testing sounds inconclusive to me.
Without seeing the code, I would chalk that up as: you made no changes at all.
I removed the code from the tutorial because I didn't want to share the author's IP and edited my question to be more generic.
I have since posted some code that I created instead of the tutorial code.
Comment
-
Originally posted by hedgeplay View PostEasiest question I will address all day.
Change this line: Value[0] = aPleasePlotThis;
To this: Value[0] = aPleasePlotThis[0]; /// Add [0] at the end
HedgePlay
It's now telling me: Cannot apply indexing with [] to an expression of type 'double'
Comment
-
Swap your OBU() for this one..
Code:protected override void OnBarUpdate() { double aPleasePlotThisLocal = 1.0; /// [B]changed the name of this double .. should not match/override the data series name. [/B] aPleasePlotThis[0] = aPleasePlotThisLocal; //please plot anything Value[0] = aPleasePlotThis[0]; }
If that does not do it attach a *.cs file that compiles with but still does not plot.
If it does not compile then as David said we will need to see the full real code you are trying to get to compile ( maybe a stripped down version of the real one will all basic/solvable compile errors resolved.) and the first few compiles errors left.
HedgePlayLast edited by hedgeplay; 07-25-2021, 11:02 AM.
Comment
-
Originally posted by butt_toast View PostSorry I had initially included code from a tutorial with a question more specific to that tutorial.
I removed the code from the tutorial because I didn't want to share the author's IP and edited my question to be more generic.
I have since posted some code that I created instead of the tutorial code.
In fact, the issue is with my explanation.
I should have elaborated more.
I presume, you did these two edits:
1. Swap the AddPlot lines.
2. Swap the indexes for the plot assignments.
If you make the changes at both locations, you effectively cancelled out
the changes and I'd expect the indicator to show the same visual plots.
When you made changes at both locations, the edit at the 2nd location will
re-align the plots back to the existing assignments -- that is, the edit at the
2nd location effectively reverts the intended effect of the 1st edit.
- Likes 1
Comment
-
Originally posted by hedgeplay View PostSwap your OBU() for this one..
Code:protected override void OnBarUpdate() { double aPleasePlotThisLocal = 1.0; /// [B]changed the name of this double .. should not match/override the data series name. [/B] aPleasePlotThis[0] = aPleasePlotThisLocal; //please plot anything Value[0] = aPleasePlotThis[0]; }
If that does not do it attach a *.cs file that compiles with but still does not plot.
If it does not compile then as David said we will need to see the full real code you are trying to get to compile ( maybe a stripped down version of the real one will all basic/solvable compile errors resolved.) and the first few compiles errors left.
HedgePlay
Googled that, found a potential solution on this forum:
Code:if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5) return;
I've attached the .cs file.Attached Files
Comment
-
Originally posted by butt_toast View Post
That got it to compile, now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array."
And now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 5: Index was outside the bounds of the array."
I've attached the .cs file.
Oh, by the way.. Hi David. Happy Sunday to you!
HedgePlay
Last edited by hedgeplay; 07-25-2021, 11:24 AM.
Comment
-
-
Originally posted by butt_toast View Post
That got it to compile, now the output window prints: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array."
Googled that, found a potential solution on this forum:
Code:if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5) return;
Good homework and very important code to know use >>> "if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5)"
In this case you only had one data series loaded ( CurrentBars[0] ) so the conditional tests to both CurrentBars[1] and CurrentBars[2] will drive that OBU() indexing error because those bar arrays do not exist until you load more data series in OSC().Configure above.
So in this case "if( CurrentBar < 0 )" or "if( CurrentBar < 30 )" might be all you need.
But as soon as you add more data series to an indi you will want a version of the example you found.
HedgePlay
Comment
-
Originally posted by hedgeplay View Post
Ok. First, "good on ya"
Good homework and very important code to know use >>> "if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5)"
In this case you only had one data series loaded ( CurrentBars[0] ) so the conditional tests to both CurrentBars[1] and CurrentBars[2] will drive that OBU() indexing error because those bar arrays do not exist until you load more data series in OSC().Configure above.
So in this case "if( CurrentBar < 0 )" or "if( CurrentBar < 30 )" might be all you need.
But as soon as you add more data series to an indi you will want a version of the example you found.
HedgePlay
Still no dice.
I swapped out the line you mentioned with the code you mentioned, and the error in the output window has changed to: "Indicator 'anIndi': Error on calling 'OnBarUpdate' method on bar 30: Index was outside the bounds of the array."
The number of hours I've sunk into getting a simple plot to plot has taken half of my Sunday so before I throw my computer out of the window I am going to have to step away.
I was able to make a strategy with WPF buttons and multiple data series but I can't even get a line to plot in an indicator after several hours, there has to be something I'm missing here. Maybe my frustration levels are too high at the "wasted" hours.
Time to play with the dog and forget about my inability to get one line to plot in an indicator.
Comment
-
Second, you might have confused AddLine() which is great as the center line but often no need to reference in OUB() ..
Will AddPlot() which you really do want as the first Addxx() object in OSC().SetDefaults so that Value[0] (Values[0][0] ) will Plot the value you are assigning it in OBU().
So OSC() SetDefaults.
Code:. { if (State == State.SetDefaults) { Description = @""; Name = "anIndi"; Calculate = Calculate.OnBarClose; IsOverlay = false; DisplayInDataBox = true; PaintPriceMarkers = true; [B]/// Many parms deleted.. Do not add this complexity unless you really need it .. /// will often trip you up when you overlook a needed change in parms. [/B] IsSuspendedWhileInactive = true; AddPlot(Brushes.Blue, "MyPlot"); [B]/// I added the plot line you really wanted.. [/B] AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZe roLine); [B] /// was "please just plot .." , use the NT default example. Also, as the LAST in the list[/B] } .
In OBU()
Code:. protected override void OnBarUpdate() { // /if (CurrentBars[0] < 5 || CurrentBars[1] < 5 || CurrentBars[2] < 5) return;[B] // Delete, too many arrays referenced when only one dataSeries was added [/B] if(CurrentBar < 0) return; if(CurrentBar > Count-50) // Can remove this conditional .. limits output to only last 50 bars { double aPleasePlotThisLocal = -0.2; //please plot anything aPleasePlotThis[0] = aPleasePlotThisLocal; Value[0] = aPleasePlotThis[0]; } } .
Updated example indi attached
HedgePlayAttached FilesLast edited by hedgeplay; 07-25-2021, 12:20 PM.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by forgpwp123, 09-05-2021, 12:24 AM
|
3 responses
233 views
1 like
|
Last Post
|
||
Started by tanas.eduard, Today, 07:10 AM
|
0 responses
1 view
0 likes
|
Last Post
![]()
by tanas.eduard
Today, 07:10 AM
|
||
Started by MicroTrends, 03-15-2023, 08:08 AM
|
18 responses
89 views
0 likes
|
Last Post
|
||
Started by Uhumm, 03-24-2023, 04:10 PM
|
5 responses
66 views
0 likes
|
Last Post
|
||
Started by Bliksem, 03-24-2023, 03:59 PM
|
3 responses
70 views
0 likes
|
Last Post
![]()
by Bliksem
Today, 06:48 AM
|
Comment