/*
 #region UC-1
 *              class MyIndicator : FunctionBase<double,double>
 *              {
 *                      Input  open, close;
 *                      Output result;
 *
 *                      public override void Pre()
 *                      {
 *                              open   = MapTo( "Open" );
 *                              close  = MapTo( "Close" );
 *                              result = MapTo( "Result" );
 *                      }
 *
 *                      public override void Iter()
 *                      {
 *                              if( open > close.Prev )
 *                                      result.Data = (open + close.Prev)/2.0;
 *    �         else
 *                                      result.Data = result.Prev;
 *                      }
 *              }
 *
 *              public static void UC1()
 *              {
 *                      // Get Microsoft market data.
 *                      Timeseries ts1 = Timeseries.RandomTimeseriesOHLC(20);
 *
 *                      // Use ts1 as input and ts2 will refer to the output.
 *                      MyIndicator indicator = new MyIndicator();
 *                      Timeseries ts2 = indicator.Exec(ts1);
 *
 *                      // Display the output.
 *                      for( int i=0; i < ts2.Length; i++ )
 *                      {
 *                              Console.WriteLine( ts2["TheOutputName"][i] );
 *                      }
 *              }
 #endregion
 */
        public static void UC2()
        {
            Timeseries ibm  = Timeseries.RandomTimeseriesOHLC(20);             // Market data for IBM.
            Timeseries msft = Timeseries.RandomTimeseriesOHLC(20);             // 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();

            // Display : Approach 1
            foreach (Index idx in new Iter(ts1, ibm, msft))
            {
                Console.WriteLine("{0} {1} {2} {3} {4}",
                                  ibm.Timestamps[idx],
                                  ibm.Close[idx],
                                  msft.Close[idx],
                                  ts1["Price1"][idx],
                                  ts1["Price1"][idx]);
            }

            // Display : Approach 2
            // 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 synchronzation purpose.
            Variable var1 = ibm.Close;
            Variable var2 = msft.Close;
            Variable var3 = ts1["Price1"];
            Variable var4 = ts1["Price2"];

            foreach (Index idx in new Iter(var1, var2, var3, var4))
            {
                Console.WriteLine("{0} {1} {2} {3} {4}",
                                  var1.Timestamps[idx],
                                  var1[idx], var2[idx], var3[idx], var4[idx]);
            }

            // Display : Approach 3
            // A different way of creating new time series
            // by adding at once ALL the variables of a
            // 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);
            foreach (Index idx in new Iter(ts4))
            {
                Console.WriteLine("{0} {1} {2} {3} {4}",
                                  ts4.Timestamps[idx],
                                  ts4["IBM.Close"][idx],
                                  ts4["MSFT.Close"][idx],
                                  ts4["_Price1"][idx],
                                  ts4["_Price2"][idx]);
            }
        }
        /*
        #region UC-1
        class MyIndicator : FunctionBase<double,double>
        {
            Input  open, close;
            Output result;

            public override void Pre()
            {
                open   = MapTo( "Open" );
                close  = MapTo( "Close" );
                result = MapTo( "Result" );
            }

            public override void Iter()
            {
                if( open > close.Prev )
                    result.Data = (open + close.Prev)/2.0;
          �         else
                    result.Data = result.Prev;
            }
        }

        public static void UC1()
        {
            // Get Microsoft market data.
            Timeseries ts1 = Timeseries.RandomTimeseriesOHLC(20);

            // Use ts1 as input and ts2 will refer to the output.
            MyIndicator indicator = new MyIndicator();
            Timeseries ts2 = indicator.Exec(ts1);

            // Display the output.
            for( int i=0; i < ts2.Length; i++ )
            {
                Console.WriteLine( ts2["TheOutputName"][i] );
            }
        }
        #endregion
        */
        public static void UC2()
        {
            Timeseries ibm  = Timeseries.RandomTimeseriesOHLC(20); // Market data for IBM.
            Timeseries msft = Timeseries.RandomTimeseriesOHLC(20); // 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();

            // Display : Approach 1
            foreach( Index idx in new Iter(ts1,ibm,msft) )
            {
                Console.WriteLine( "{0} {1} {2} {3} {4}",
                    ibm.Timestamps[idx],
                    ibm.Close[idx],
                    msft.Close[idx],
                    ts1["Price1"][idx],
                    ts1["Price1"][idx]);
            }

            // Display : Approach 2
            // 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 synchronzation purpose.
            Variable var1 = ibm.Close;
            Variable var2 = msft.Close;
            Variable var3 = ts1["Price1"];
            Variable var4 = ts1["Price2"];
            foreach (Index idx in new Iter(var1,var2,var3,var4))
            {
                Console.WriteLine("{0} {1} {2} {3} {4}",
                    var1.Timestamps[idx],
                    var1[idx], var2[idx], var3[idx], var4[idx] );
            }

            // Display : Approach 3
            // A different way of creating new time series
            // by adding at once ALL the variables of a
            // 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 );
            foreach( Index idx in new Iter(ts4) )
            {
                Console.WriteLine( "{0} {1} {2} {3} {4}",
                    ts4.Timestamps[idx],
                    ts4["IBM.Close"][idx],
                    ts4["MSFT.Close"][idx],
                    ts4["_Price1"][idx],
                    ts4["_Price2"][idx] );
            }
        }