- Following the example in the SampleBoolSeries file downloaded below, let's say that the double ExposedVariable needs to be accessed from within a Strategy.
How can this be done?
I am getting an error when declaring: private double SampleBoolSeries.ExposedVariable; - I have also changed State==DataLoaded. Can you take a look at the code and amend it so ExposedVariable is accessible? Thanks in advance!
- namespace NinjaTrader.NinjaScript.Strategies
{
public class ExposedVariableTestUnlocked : Strategy
{
private double SampleBoolSeries.ExposedVariable;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "ExposedVariableTestUnlocked";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Day;
TraceOrders = true;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
ExposedVariable = 0;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
double SampleBoolSeries;
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if (Close[0] != Open[0])
{
Print(CurrentBars[0].ToString() + @" Close0 value is = " + Close[0].ToString());
Print(CurrentBars[0].ToString() + @" ExposedVariable value is = " + SampleBoolSeries.ExposedVariable.ToString());
}
}
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Access variables that are not plots within a Strategy
Collapse
X
-
Access variables that are not plots within a Strategy
Official NinjaScript reference code samplesTags: None
-
Hello roblogic,
Thank you for the post.
I am not certain I understand the scope of what you are trying to do here, the sample you linked to does specifically what you asked as the last print in the strategy.
Following the example in the SampleBoolSeries file downloaded below, let's say that the double ExposedVariable needs to be accessed from within a Strategy.This is from the strategies code, it is printing the value of ExposedVariable from the indicator. Is this not what you are trying to do?Code:Print(SampleBoolSeries().ExposedVariable);
The error you are seeing is because private double SampleBoolSeries.ExposedVariable; is not valid, you cannot define an indicator property as a strategy property in this way.
I look forward to being of further assistance.
-
Hello roblogic,
Correct, this is a good point which I can provide some more detail on.
DataLoaded is only letting us know that data has been loaded, however, if you wanted to access values such as price values or indicators, that is all done past the DataLoaded point in OnBarUpdate or the other data event-driven overrides. You can consider DataLoaded more of a one time step for extra configuration.
There is also a difference in the syntax you had used that resulted in an error and the syntax in this sample, part of that is how you call the indicator:
Another part is where you call the indicator.Code:SampleBoolSeries()
Because the value is coming from the indicator and that indciator needs to process to produce a value, it could not be set as a property in the way that you had shown and needs to be called later in logic. You can still create variables which are indicators if you do it correctly. The SampleMACrossOver strategy that comes with NinjaTrader is an easy way to see how this is done. That strategy actually does use DataLoaded to setup the indicators but it shows the correct way to do this. The values are later accessed in OnBarUpdate.
I look forward to being of further assistance.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
54 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
131 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
73 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
44 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment