Thanks
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Accessing an Indicator dataseries from a strategy
Collapse
X
-
Hello kenb2004,
Thanks for your post and I am happy to assist you.
Yes, you can access the dataseries that is not a Plot from another strategy.
Please refer to this example for further details http://www.ninjatrader.com/support/f...ead.php?t=4991
Please let me know if I can assist you any further.JoydeepNinjaTrader Customer Service
-
I am familiar with the SampleBoolSeries method for accessing a
BullIndication or BearIndication state, but how would that apply to a dataseries value. If you replaced DataSeries for BoolSeries in this Sample Indicator would SampleBoolSeries.myDataSeries[0] in a Strategy give you the value of the dataseries for the current bar?
Comment
-
Hello kenb2004,
Yes, that is the procedure and the basic concept is the same.
A basic code would look like this
In variable
in InitializeCode:private DataSeries myDataSeries;
assign a value in say OnBarUpdateCode:myDataSeries = new DataSeries(this);
in propertyCode:myDataSeries.Set(Close[0]);
Call the data series from the strategy asCode:[Browsable(false)] [XmlIgnore()] public DataSeries MyDataSeries { // We need to call the Update() method to ensure our exposed variable is in up-to-date. get {return myDataSeries; } }
Please let me know if I can assist you any further.Code:double myDataSeriesValue = SampleBoolSeries().MyDataSeries[0];
JoydeepNinjaTrader Customer Service
Comment
-
I have successfully called the new dataseries in the strategy and printed the running %retracement to the Output window by putting the following code in OnBarUpdate
double Pct_Up_Ret = Swing.Pct_up_ret[0]; // Swing Dataseries for Percent Up Retracement
double PctUpRet = Math.Round(Pct_Up_Ret*100);
Print(Time[0] + " - New Strategy Up Move Retracement = "+PctUpRet+"%");
however when I try to use it as a condition:
if (PctUpRet[0] >= 50)
I get an error "The name 'PctUpRet' does not exist in the current context. How can it not exist if it prints the running %retracement accurately?
ThanksLast edited by kenb2004; 02-20-2012, 04:22 PM.
Comment
-
It looks like you have a scope error. Are you sure that you are trying to access the variable within the scope that it was declared?Originally posted by kenb2004 View PostI have successfully called the new dataseries in the strategy and printed the running %retracement to the Output window by putting the following code in OnBarUpdate
double Pct_Up_Ret = Swing.Pct_up_ret[0]; // Swing Dataseries for Percent Up Retracement
double PctUpRet = Math.Round(Pct_Up_Ret*100);
Print(Time[0] + " - New Strategy Up Move Retracement = "+PctUpRet+"%");
however when I try to use it as a condition:
if (PctUpRet[0] >= 50)
I get an error "The name 'PctUpRet' does not exist in the current context. How can it not exist if it prints the running %retracement accurately?
Thanks
Comment
-
Hey koganam
Thanks for your input, but I really don't know what a scope error is. The dataseries is a percent rounded to a whole percent of the previous up swing. When I use the above print command it follows perfectly the running percentage retracement. The error says that it does not exist in the current context. I'm assuming "context" is the operative word. I'm at a loss.
Comment
-
I believe that you are running this possibly in the OnBarUpdate() event handler? If you do not mind publicly posting, at least the whole OnBarUpdate() code, I am sure that we can come to a quick resolution. If the code must remain secret, then I guess we shall just have to wait until you can send the code directly to NT support.Originally posted by kenb2004 View PostHey koganam
Thanks for your input, but I really don't know what a scope error is. The dataseries is a percent rounded to a whole percent of the previous up swing. When I use the above print command it follows perfectly the running percentage retracement. The error says that it does not exist in the current context. I'm assuming "context" is the operative word. I'm at a loss.
Comment
-
The code is part of a multi-purpose template so I'll give you what I believe is pertinent.
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
double PctUpRet = 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
Comment
-
Emphasis mine. PctUpRet is declared in the SwingDataSeries() method/function. That makes it local in scope to that function: it does not exist, as soon as the function closes and returns.Originally posted by kenb2004 View PostThe code is part of a multi-purpose template so I'll give you what I believe is pertinent.
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
double PctUpRet = 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
If you want to use a variable that you will either pass to a function, or modify in a function, and then access elsewhere in the code, you want to declare it as a class variable, and just ensure that when processed, it is not creating or being subjected to unintended side-effects.
A class variable can be declared anywhere outside a function or event handler. Good practice is to declare it in the "Variables" section, so that all declarations are organized in one place.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
80 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
46 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
29 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
32 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
66 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment