This question refers to this Strategy Builder Screen's trading logic:
When using the Strategy Builder, I noticed that when an indicator time series, like SMA(Close, 14), or an item of an indicator time series, like EMA(10)[5], is selected as part of the condition logic or is used to set a variable as shown in the example above, that the code output always automatically defines an indicator variable like "private SMA SMA1;" and sets the indicator variable in the "else if (State == State.DataLoaded)" branch of the "OnStateChange()" method with code like "SMA1 = SMA(Close, 14);", and then uses that variable's automatically created variable name at the location in the code where the "SMA(Close, 14)" or "EMA(10)[5]" was selected in the Strategy Builder.
Here is the relevant code output in the OnBarUpdate section for the strategy whose Strategy Builder screen is shown above:
if ((RSI1.Avg[0] > 80) && (CrossAbove(EMA1, SMA1, 1))) { TestDoubleVar = (EMA2[5] + (SMA2[7])) ; }
QUESTION #2: For those users who want to program their own NinjaScript strategies without using the Strategy Builder, is it important to always define indicator variables before using them, or is it safe to write something like this in OnBarUpdate?
if ((RSI(Close, 14, 3).Avg[0] > 80) && (CrossAbove(EMA(Close, 14), SMA(Close, 14), 1))) { TestDoubleVar = (EMA(Close, 10)[5] + (SMA(Close, 10)[7])) ; }
QUESTION #3: If indicators should always be defined before using them instead of using them directly, then how can a NinjaScript programmer safely call an SMA or EMA in a loop, where the "Period" input parameter of the EMA or SMA isn't a literal numeric value but is a variable that changes on each iteration of the loop, like this?
(Its the same code as above, but I added a loop and I use indicators inside of the loop that are not pre-defined).
if ((RSI1.Avg[0] > 80) && (CrossAbove(EMA1, SMA1, 1))) { TestDoubleVar = (EMA2[5] + (SMA2[7])) ; double threshold; double tempThreshold = 0; int countOfValuesForAverage = 0; for (int i = 10; i < 50; i+=2) { tempThreshold += (EMA(Close, i)[5] + SMA(Close, i)[7]) / 2.0; countOfValuesForAverage++; } threshold = tempThreshold / countOfValuesForAverage; }
Thanks in advance!
EquityTrader
Comment