User needs to calculate the correlation between the typical price of the IBM and MSFT market data. The user wish also to display on the console the timestamp, the typical price and the closes of both stocks. Each line will represent one price bar.
The following perform the calculation in C#
| TimeSeries ibm = ...; // Market data for IBM. TimeSeries msft = ...; // Market data for Microsoft. // Static method to synchronize the time series // with each other. Timeseries.Sync( ibm, msft ); // Calculate the typical price. Timeseries ts1 = new Timeseries(); ts1["Price1"] = ibm.TypPrice(); ts1["Price2"] = msft.TypPrice(); // Calculate correlations between the two typical prices. double correl = ts1.CorrelValue(); |
The following present 3 different approach to display the variables:
| // Display : Approach 1 // Must put all the variables being iterated // into a single time series. Timeseries ts2 = new Timeseries(); ts2["Var1"] = ibm.Close; ts2["Var2"] = msft.Close; ts2["Var3"] = ts1["Price1"]; ts2["Var4"] = ts1["Price2"]; for( int i=0; i < ts2.Length; i++ ) { Console.WriteLine( "{0} {1} {2} {3} {4}", ts2.Timestamps[i], ts2["Var1"][i], ts2["Var2"][i], ts2["Var3"][i], ts2["Var4"][i] ); } // Display : Approach 2 // Get a better speed execution time by creating // a reference to the often access variables // once prior to the loop. Still need to // put all the variable in the same time series // for synchronization purpose. Timeseries ts3 = new Timeseries(); Variable var1 = ts3["Var1"] = ibm.Close; Variable var2 = ts3["Var2"] = msft.Close; Variable var3 = ts3["Var3"] = ts1["Price1"]; Variable var4 = ts3["Var4"] = ts1["Price2"]; for( int i=0; i < ts3.Length; i++ ) { Console.WriteLine( "{0} {1} {2} {3} {4}", ts3.Timestamps[i], var1[i], var2[i], var3[i], var4[i] ); } // Display : Approach 3 // A different way of creating new time series // by adding at once all the variables of another // time series. The prefix allows to create // namespaces to avoid variable name collision // e.g. "Close" exist both in the ibm and msft // time series. Timeseries ts4 = new Timeseries(); ts4.SetAllVar( "IBM.", ibm ); ts4.SetAllVar( "MSFT.", msft ); ts4.SetAllVar( "_", ts1 ); for( int i=0; i < ts4.Length; i++ ) { Console.WriteLine( "{0} {1} {2} {3} {4}", ts4.Timestamps[i], ts4["IBM.Close"][i], ts4["MSFT.Close"][i], ts4["_Price1"][i], ts4["_Price2"][i] ); } |
WARNING: Work in progress.