DataSeries[] correlations = new DataSeries[31]; Initialize() { // There are 30 symbols for (i = 0; i < symbols.Length; i++) { Add(symbols[i], BarsPeriods[0].BasePeriodType, BarsPeriods[0].BasePeriodValue); } for (i = 1; i < Instruments.Length; i++) { // Correlations are kept historically for averaging correlations[i] = new DataSeries(this, MaximumBarsLookBack.TwoHundredFiftySix); }
As things go on, I calculate my own values. But we wish to smooth these values and not use the latest. To do this, I've been using the SMA indicator with my own data series:
OnBarUpdate() { correlation = CMIFunctions.TradestationCorrelation(Closes[0], Close, length); correlations[BarsInProgress].Set(correlation); correlation = SMA(correlations[BarsInProgress], smooth)[0]; }
string corrList = ""; for (int i = 0; i < smooth + 1; i++) { corrList += "; " + Math.Round(correlations[BarsInProgress][i],3); } Print(BarsInProgress + ": Correlation: " + correlation + "; List" + corrList); SMA testing = SMA(correlations[BarsInProgress], smooth); string prevval = "Previous input values"; int j; for (j = 0; j < smooth + 10 && j < CurrentBar; j++) { prevval += "; " + Math.Round(testing.Input[j],3); } prevval += "; Gave average values"; for (j = 0; j < smooth + 10 && j < CurrentBar; j++) { prevval += "; " + Math.Round(testing[j],3); } Print("Cached SMA" + prevval);
Cached SMAPrevious input values; 0.133; -0.133; -0.067; -0.067; 0.067; -0.267; 0.067; -0.067; -0.067; 0.133; 0.267; -0.067; Gave average values; 0.133; 0.167; 0.2; 0.267; 0.1; 0.267; 0.233; 0.233; 0.333; 0.4; 0.267; 0.433
1: Correlation Average: 0.133333333333333
2: Bar: 441; Correlation List; 0; 0; -0.133
Cached SMAPrevious input values; 0; 0; -0.133; 0.133; 0; -0.2; 0.067; 0; -0.067; 0.067; -0.067; 0; Gave average values; -0.067; -0.133; 0; -0.1; -0.233; -0.1; -0.167; -0.233; -0.167; -0.267; -0.2; -0.167
2: Correlation Average: -0.0666666666666665
3: Bar: 439; Correlation List; 0.067; -0.267; 0.067
Cached SMAPrevious input values; 0.067; -0.267; 0.067; 0.067; -0.2; -0.267; -0.067; -0.133; 0; 0.067; 0; 0.2; Gave average values; 0.033; 0.167; 0.2; 0.033; 0; 0.133; 0.133; 0.167; 0.2; 0.2; 0.3; 0.167
3: Correlation Average: 0.0333333333333334
4: Bar: 441; Correlation List; -0.067; -0.2; 0.067
Cached SMAPrevious input values; -0.067; -0.2; 0.067; 0.067; 0.2; -0.267; 0; -0.2; 0.2; 0; 0.067; 0.4; Gave average values; -0.033; 0.1; 0.133; 0.233; 0; 0.133; 0.033; 0.2; 0.1; 0.1; 0.267; -0.033
4: Correlation Average: -0.0333333333333335
<next bar>
1: Bar: 442; Correlation List; 0; -0.133; -0.067
Cached SMAPrevious input values; 0; -0.133; -0.067; -0.067; 0.067; -0.267; 0.067; -0.067; -0.067; 0.133; 0.267; -0.067; Gave average values; 0.133; 0.167; 0.2; 0.267; 0.1; 0.267; 0.233; 0.233; 0.333; 0.4; 0.267; 0.433
1: Correlation Average: 0.133333333333333
2: Bar: 442; Correlation List; -0.2; 0; -0.133
Cached SMAPrevious input values; -0.2; 0; -0.133; 0.133; 0; -0.2; 0.067; 0; -0.067; 0.067; -0.067; 0; Gave average values; -0.067; -0.133; 0; -0.1; -0.233; -0.1; -0.167; -0.233; -0.167; -0.267; -0.2; -0.167
2: Correlation Average: -0.0666666666666665
3: Bar: 440; Correlation List; 0; -0.267; 0.067
Cached SMAPrevious input values; 0; -0.267; 0.067; 0.067; -0.2; -0.267; -0.067; -0.133; 0; 0.067; 0; 0.2; Gave average values; 0.033; 0.167; 0.2; 0.033; 0; 0.133; 0.133; 0.167; 0.2; 0.2; 0.3; 0.167
3: Correlation Average: 0.0333333333333334
4: Bar: 442; Correlation List; 0.133; -0.2; 0.067
Cached SMAPrevious input values; 0.133; -0.2; 0.067; 0.067; 0.2; -0.267; 0; -0.2; 0.2; 0; 0.067; 0.4; Gave average values; -0.033; 0.1; 0.133; 0.233; 0; 0.133; 0.033; 0.2; 0.1; 0.1; 0.267; -0.033
4: Correlation Average: -0.0333333333333335
<next bar>
1: Bar: 443; Correlation List; 0.067; 0; -0.133
Cached SMAPrevious input values; 0.067; 0; -0.133; -0.067; -0.067; 0.067; -0.267; 0.067; -0.067; -0.067; 0.133; 0.267; Gave average values; 0.233; 0.133; 0.167; 0.2; 0.267; 0.1; 0.267; 0.233; 0.233; 0.333; 0.4; 0.267
1: Correlation Average: 0.233333333333333
2: Bar: 443; Correlation List; -0.133; -0.2; 0
Cached SMAPrevious input values; -0.133; -0.2; 0; -0.133; 0.133; 0; -0.2; 0.067; 0; -0.067; 0.067; -0.067; Gave average values; -0.133; -0.067; -0.133; 0; -0.1; -0.233; -0.1; -0.167; -0.233; -0.167; -0.267; -0.2
2: Correlation Average: -0.133333333333333
3: Bar: 441; Correlation List; -0.067; 0; -0.267
Cached SMAPrevious input values; -0.067; 0; -0.267; 0.067; 0.067; -0.2; -0.267; -0.067; -0.133; 0; 0.067; 0; Gave average values; 0.133; 0.033; 0.167; 0.2; 0.033; 0; 0.133; 0.133; 0.167; 0.2; 0.2; 0.3
3: Correlation Average: 0.133333333333333
4: Bar: 443; Correlation List; 0.067; 0.133; -0.2
Cached SMAPrevious input values; 0.067; 0.133; -0.2; 0.067; 0.067; 0.2; -0.267; 0; -0.2; 0.2; 0; 0.067; Gave average values; 0.1; -0.033; 0.1; 0.133; 0.233; 0; 0.133; 0.033; 0.2; 0.1; 0.1; 0.267
4: Correlation Average: 0.0999999999999998
Etc. Obviously.. these values are incorrect. (For example bar 1 remember to use _two_ bars values, and the data should be: stock 1 should be 0 not 0.133.., stock 2 should be 0, not -0.066, stock 3 should be .0.1, not 0.033, and stock 4 should be -0.133, not -0.033.) I've looked through the SMA indicator code itself, and it seems straight-forward and obvious. I can't imagine what the issue is. Help?
Could this be a "missed bar" problem? If I don't set a value for one bar, and the next bar tell it to average over two bars... what does it use for that missed bar? How can I tell? Still, it should subtract that bar off the end when it recalculates the average.
This output is direct from the output window, no edits, no mixing.
Optimizations:
The SMA indicator _could_ take a couple optimizations... In if (CurrentBar >= Period), there's a usage of math.min() for the divisor in each case. Math.min() is unnecessary -- if currentBar >= Period, divide by Period; else, divide by CurrentBar + 1. The determination of double last could be done in the same way, if moved inside the if. The difference for last would be a couple extra bytes of code vs an extra if on every barUpdate. Anyway.. it just seemed like the SMA code was being pedantically efficient, so I thought I'd chip in ;-)
Comment