Use Case 7

Goal

This is an example of logic using multiple time frame.

User wish to trig a buy when a 1 minute moving average cross over occurs (period 3 and 8 will be used). Furthermore, a buy can occur only if the Dow Jones Relative Strength Index over a period of 5 price bar is greater than 70. The time frame of the RSI is 3 minutes. Users data source has only price bar of 1 minutes. The user will check for a new buy signal every minute during trading hours by calling a function.

Solution

C#:

...
public DateTime CheckForLatestBuySignal()
{
   // Get intraday 1 minute data for the security to be traded.
   Timeseries data_1min = ...;

   // Get intraday 1 minute Dow Jones data.
   Timeseries dj_1min = ...;

   // Create a dow jones time series with 3 minutes price bars.
   Timeseries dj_3min = dj_1min.ConvertBarPeriod( Per3Min );

   // Do the calculation...
   data_1min["Rsi"] = dj_3min.Rsi(5);
   Variable a = data_1min.Sma(3);
   Variable b = data_1min.Sma(8);
   data_1min["CrossAbove"] = a.CrossAbove( b );

   // Take note that the RSI is automatically expanded and
   // synchronized when added to data_1min.

   // Find the latest buy signal, starting from the end
   // of the timeseries.
   for( int i=data_1min.Length-1; i >= 0 ; i-- )
   {
      if( (data_1min["Rsi"][i] > 70) &&
          (data_1min["CrossAbove"][i] == 1) )
      {
         return data_1min.Timestamps[i];
      }
   }

   return null; // No buy signal trig yet.
}
 

WARNING: Work in progress.