Hi, I have written an Indicator (Indicator 1) that calls another Indicator (Indicator 2) to complete a calculation and draw some arrows and lines on a chart. This works great when invoked as an Indicator on a Chart. However, I now want to instantiate Indicator 1 from within a Strategy and access the Values[0] Series of Indicator 1 (the arrows calculated and drawn by the indicator). When I make my call to the Indicator 1 Property for Values[0] to test if is IsValidDataPoint(0), I receive an error that I am referencing an invalid Bar (-1). Hereās a breakdown of the code in question:
public class Indicator1Strategy : Strategy
{
Indicator 1 jed = null;
ā¦
else if (State == State.DataLoaded)
{
jed = new Indicator1();
}
ā¦
protected override void OnBarUpdate()
{
if( CurrentBar < 126 )
return;
Print("CurrentBar: "+CurrentBar);
// If Indicator 1 is primed, set a Stop Entry Order
if( jed.Property1.IsValidDataPoint(0) &&
( Position.MarketPosition == MarketPosition.Flat ||
Position.MarketPosition == MarketPosition.Short) )
{
ā¦
This refence to the current element in the Series Property1 produces the following error:
āCurrentBar: 126
Strategy āIndicator1Strategyā: Error on calling āOnBarUpdateā method on bar 126: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.ā
This is perplexing to me, since Indicator1 works fine when applied as an indicator to the chart. When calling an Indicator from a Strategy, do I need to have the Indicator create a new Bar Series to operate correctly? Any suggestions would be greatly appreciated.
Youāre getting that Invalid Bar (-1) error because youāre not adding the indicator right, You canāt just new Indicator1() inside State.DataLoaded. doesnāt hook into the strategyās OnBarUpdate loop, so the indicator never calculates its Values[0] series. The data is just empty.
You need to use ājed = AddIndicator(new Indicator1());ā inside the āState.Configureā Thatās the only way the indicator gets processed on every single bar block instead.
In my experience, these errors are caused by either: not checking the BarsInProgress, or not keeping the indicator up to date. It is necessary to call Update() for each indicator during the OnBarUpdate() call.
Since the OPās indicator doesnāt appear to use a different data series, itās likely an update issue. I would suggest changing the OnBarUpdate() code to add a call to Update() on every iteration.
Thanks, noisefilter. Your and Markās replies got my Strategy up and running! Iāve written numerous successful Indicators using the approach I first tried for this Strategy, but it sure didnāt work as I expanded into Strategy development. I do wish the NinjaScript documentation included better threads on how to do common things, like calling Indicators from within Strategies. Thanks again!
Thanks, Mark. Your and noisefilterās replies got my Strategy up and running! Iāve written numerous successful Indicators using the approach I first tried for this Strategy, but it sure didnāt work as I expanded into Strategy development. I do wish the NinjaScript documentation included better threads on how to do common things, like calling Indicators from within Strategies. Thanks again!