Thanks
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Accessing an Indicator dataseries from a strategy
Collapse
X
-
-
Hello kenb2004,
As koganam said, it’s a scope error. Please make the PctUpRet a private field like
@ koganam - thanks for your input.Code:[B][COLOR="Green"]#region Variable double PctUpRet = 0; #endregion[/COLOR][/B] protected override void OnBarUpdate() { SwingDataSeries(); Buy_1(); } #region SwingDataSeries //******************************************* private void SwingDataSeries() { if (RedDn){ // this bool is true when up move ends and starts drawing down swing double Pct_Up_Ret = Swing.Pct_up_ret[0]; // Swing Dataseries for Percent Up Retracement [B][COLOR="SeaGreen"]PctUpRet[/COLOR][/B] = Math.Round(Pct_Up_Ret*100); Print(Time[0] + " - New Strategy Up Move Retracement = "+PctUpRet+"%"); } } #endregion #region Buy_1 //******************************************* private void Buy_1() { if (PctUpRet >= 50) // this line generates the error { CancelSell1_Entry(); // Cancel Sell1 Entry Order CancelSell1_PTST(); // Cancel Sell1 Profit and Stop CancelSell1_Exit(); // Cancel Sell_1 Exit Order // CancelSell1_BE(); // Cancel Sell_1 Break Even Order // buy1entry = EnterLongLimit(0, true, 1, Close[0] + EntryOffset * TickSize, "Buy_1"); DrawArrowUp("ArrowUp" + CurrentBar, false, 0, Low[0] + -2 * TickSize, Color.Lime); DrawDot("My Up dot" + CurrentBar, false, 0, Close[0], Color.Lime); } } #endregion
Please let me know if I can assist you any further.JoydeepNinjaTrader Customer Service
Comment
-
Hello kenb2004,
Use the print function extensively in the code to get the values and use the same appropriately.
Please refer to this post on how to debug your code http://www.ninjatrader.com/support/f...ead.php?t=3418
If you have any specific NinjaScript questions would be happy to assist you further.JoydeepNinjaTrader Customer Service
Comment
-
In order to calculate the percent retrace, you must have used values for the SwingHigh and SwingLow, so you know those values a priori. The 50% retrace is (SwingHigh + SwingLow)/2.Originally posted by kenb2004 View PostThanks, that solved the error problem. The other issue is being able to assign buy1entry to the PRICE of the 50% retracement. Any thoughts?
Thanks
Comment
-
Where can I put the following code so I can use the double "UpSwingPts" in ANY method, ANYWHERE in my strategy?
Thanks
double UpSwingPts = Swing.Upswing_pts[0]; // Up Swing Points from Indicator DataSeries
Print(Time[0] + " - Strategy Up Swing Points = "+UpSwingPts+" Pts");
Comment
-
Hello kenb2004,
Thanks for writing in and I am happy to assist you.
Please make the scope of the variable to private. For example
//call the same in OnBarUpdateCode:#region Variables private double UpSwingPts = 0; // other variables #endregion
Code:protected override void OnBarUpdate() { UpSwingPts = Swing.Upswing_pts[0]; // Up Swing Points from Indicator DataSeries
Please let me know if I can assist you any further.JoydeepNinjaTrader Customer Service
Comment
-
In order to access a variable from anywhere in the code, you need to declare the variable as a class variable, You do this by putting the declaration, along with others in the "Variables" region.Originally posted by kenb2004 View PostWhere can I put the following code so I can use the double "UpSwingPts" in ANY method, ANYWHERE in my strategy?
Thanks
double UpSwingPts = Swing.Upswing_pts[0]; // Up Swing Points from Indicator DataSeries
Print(Time[0] + " - Strategy Up Swing Points = "+UpSwingPts+" Pts");
You then access the code from within any eventhandler or method by simply assigning or accessing it directly.Code:#region Variables private double UpSwingPts = 0; // other variables #endregion
Code:AnyEventHandlerOrMethod() { UpSwingPts = SomeCalculation; // assigns value, or double SomeLocalVariable = UpSwingPts (operator) SomethingElse; //access the value to use in another calculation. }
Comment
-
Hello kenb2004,
It is more sort of a C# query. Please refer to this post http://stackoverflow.com/questions/6...ed-and-nothing
Please let me know if I can assist you any further.JoydeepNinjaTrader Customer Service
Comment
-
It is again a matter of scope. Best practice is to declare variables as narrowly as possible, but no narrower. That avoids unintended side effects. By declaring it private, it is not accessible outside the class, and the processor does not use the overhead necessary to expose the variable where it is not needed; apart from which, no code outside the control of the class can alter the value at all.Originally posted by kenb2004 View PostThank you, that was very helpful. Could you tell me why you chose private double instead of double or public double?
Thanks
Comment
-
hi! I just want to check with you, I am trying to use the data series,
I follow the procedure in declaring data series.
publicclass MACDCross : Strategy
{
#region Variables
private DataSeries myDataSeries;
#endregion
protectedoverridevoid Initialize()
{
CalculateOnBarClose = true;
myDataSeries = new DataSeries (this);
}
but when I run it gives me an error "myDataSeries" does not exist.
how can I solve this problem?
Comment
-
Hello xedrick12,
Thanks for your post and welcome to the forums!
I copied your code and it compiles without error. It is possible that in the code you compiled that there may be a typo or hidden character in the names. Please copy what you posted into a new file and try again.
Also, when you compile, NinjaTrader compiles ALL scripts at the same time so it is also possible that the error could be from another source file. If you continue to get the error, take note of the file name listed on the left column of the error. If it is different than the file you are working in you will need to open that file to correct the error (or delete the file) and recompile.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
105 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
53 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
35 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
38 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
74 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment