Here is the code:
At the top of the script I have these declarations defining three custom series that are needed within the method:
private Series<double> CandlePatterns;
private Series<double> CandlePatterns1;
private Series<double> CandlePatterns2;
Then in OnStateChange within the block else if (State==State.DataLoaded) I have the following three statements:
CandlePatterns = new Series<double>(this);
CandlePatterns1 = new Series<double>(this);
CandlePatterns2 = new Series<double>(this);
(Side question: What does the key word “this” mean/do in this context?)
Next, in a separate block, I have the code for the actual method. I placed this code block after OneStateChange and before OnBarUpdate.
private double LagMultiply(int lag, int length, int BarsAgo)
{
// First ensure that enough candles are available to make the calculations:
if (CurrentBars[0] < length + lag + BarsAgo
return -1000.0;
CandlePatterns[0] = Convert.ToDouble( Math.Sign( Close[0] - Open[0] ) ) ;
// A Green Candle (Close [0] > Open[0] will produce +1 , A Red Candle (Close[0] < Open[0] ) will produce -1
CandlePatterns1[0] = CandlePatterns[0] * CandlePatterns[lag];
// Tseries Element multiplication with the corresponding lagged element of the same series for the given lag value.
CandlePatterns2[0] = SUM(CandlePatterns1, length)[0];
// Summing up the multiplied element values for a given input length
return CandlePatterns2[BarsAgo];
// Return the desired result at a the desired "bars ago" value.
}
I then have the following print statement(s) to test the calculated results
Print("LagMultiply(0, 5, 0) = " +LagMultiply(0, 5, 0));
This sets the lag to 0, uses a length of 5 elements, and returns the current result. Since the series CandlePatterns elements are either +1 (green candle) or -1 (red candle) setting the lag to 0 will produce a result of 5 which is also the maximum value it could possibly take since it is just multiplying the corresponding vector elements with themselves ( i.e. magnitude squared).
Using Playback mode with downloaded NQ 12-23 data, while also keeping the time fixed/stationary in order to produce repeatable results on subsequent runs, the NinjaScript output are as follows:
LagMultiply(0, 5, 0) = 5
Setting the lag to 3 produces the following output:
LagMultiply(3, 5, 0) = 1
Setting the lag to 3 and the BarsAgo to 14 produces the following output:
LagMultiply(3, 5, 14) = -3
Setting the lag to 0, the length to 10, and BarsAgo to 0 produces the following output:
LagMultiply(0, 10, 0) = 10
Setting the lag to 1, the length to 10, and BarsAgo to 0 produces the following output:
LagMultiply(1, 10, 0) = 0
Setting the lag to 1, the length to 10, and BarsAgo to 4 produces the following output:
LagMultiply(1, 10, 4) = 2
All of the above results were produced using a SINGLE print statement (i.e. a single call to the method). I simply recompiled the script each time I changed the input parameter values to the method. These results are reasonable, and I believe are also accurate.
The PROBLEM comes about when there are MULTIPLE CALLS to the METHOD with each call using different input parameter values. The results become corrupted and I don’t understand what is going on. Shouldn’t each call be independent of all the other calls? Why should multiple calls to this same method produce different (and incorrect) results? For example, using two print statements instead of just one,
Print("LagMultiply(0, 5, 0) = " +LagMultiply(0, 5, 0));
Print("LagMultiply(3, 5, 0) = " +LagMultiply(3, 5, 0));
now produces different results from the single calls above with obviously incorrect results (compare to the above):
LagMultiply(0, 5, 0) = 1241
LagMultiply(3, 5, 0) = 1241
As does:
LagMultiply(0, 5, 0) = 1308
LagMultiply(1, 10, 0) = 0
Or three calls
LagMultiply(0, 5, 0) = 1241
LagMultiply(3, 5, 0) = 1241
LagMultiply(3, 5, 14) = 1223
Or four calls
LagMultiply(0, 5, 0) = 5
LagMultiply(3, 5, 0) = 5
LagMultiply(3, 5, 14) = 10
LagMultiply(0, 10, 0) = 10
Or five calls
LagMultiply(0, 5, 0) = 1308
LagMultiply(3, 5, 0) = 1308
LagMultiply(3, 5, 14) = 1299
LagMultiply(0, 10, 0) = 1307
LagMultiply(1, 10, 0) = 1307
Or six calls
LagMultiply(0, 5, 0) = 1308
LagMultiply(3, 5, 0) = 1308
LagMultiply(3, 5, 14) = 1299
LagMultiply(0, 10, 0) = 1307
LagMultiply(1, 10, 0) = 1307
LagMultiply(1, 10, 4) = 1305
Strangely, even the very first call to the method, in a sequence of multiple calls to it, produces a different result than the corresponding single call did. How can the very first call to this method, of the sequence of multiple calls to it, already be corrupted even before the subsequent calls were made? Why are the multiple calls impacting the results? I’m confounded. What am I misunderstanding?
Can you please help me resolve this issue? I suspect that the way in which I structured the LagMultiply method and/or the custom series used within the method are somehow incompatible with the way the Tseries are constructed or referenced/indexed and that this is causing the corruption of the results when I place multiple calls to this same method within the strategy.
Thank you for your advice, guidance, and solutions. It will be very much appreciated.
Regards
Comment