Стратегия, которая склеивает фьючерсы
//Calculate futures prices a-la russian volatility index
//We assume that data series in data set are ordered by expiration time
//We also assume that new futures contract starts 8 days before previous expires
Bars newbars = new Bars(@"",BarScale.Minute, Bars.BarInterval);
int n_fut = 8;
int n_sm = 4;
int N = 24*60*60;
int bar1 =0;
string folder = @"E:\newBars\";
string file = @"newbars.txt";
for(int ds =0;ds < DataSetSymbols.Count - 1; ds++)
{
bool first_time = true;
SetContext( DataSetSymbols[ds], true );
DateTime start_next = Bars.Date[Bars.Count - 1].AddDays(-n_fut);
for(int bar=bar1; bar < Bars.Count;bar++)
{
double N_exp = (Bars.Date[Bars.Count - 1] - Bars.Date[bar]).TotalSeconds;
double T = N_exp/N;
if(T <= n_fut - n_sm)
{
SetContext( DataSetSymbols[ds+1], true );
newbars.Add(Bars.Date[bar1], Open[bar1],High[bar1], Low[bar1], Close[bar1],Volume[bar1]);
RestoreContext();
bar1++;
}
if(n_fut - n_sm < T && T < n_fut )
{
double tmpClose = Close[bar]*Close[bar]*(T - n_fut + n_sm)/n_sm;
double tmpOpen = Close[bar]*Close[bar]*(T - n_fut + n_sm)/n_sm;
double tmpHigh = High[bar]*High[bar]*(T - n_fut + n_sm)/n_sm;
double tmpLow = Low[bar]*Low[bar]*(T - n_fut + n_sm)/n_sm;
double tmpVolume = Volume[bar]*Volume[bar]*(T - n_fut + n_sm)/n_sm;
SetContext( DataSetSymbols[ds+1], true );
if(first_time)
{
bar1 = Bars.ConvertDateToBar(start_next, false);
first_time= false;
}
tmpClose += Close[bar1]*Close[bar1]*(n_fut-T)/n_sm;
tmpOpen += Open[bar1]*Open[bar1]*(n_fut-T)/n_sm;
tmpHigh += High[bar1]*High[bar1]*(n_fut-T)/n_sm;
tmpLow += Low[bar1]*Low[bar1]*(n_fut-T)/n_sm;
tmpVolume += Volume[bar1]*Volume[bar1]*(n_fut-T)/n_sm;
bar1++;
RestoreContext();
newbars.Add(Bars.Date[bar], Math.Sqrt(tmpOpen),Math.Sqrt(tmpHigh),Math.Sqrt(tmpLow),Math.Sqrt(tmpClose),Math.Sqrt(tmpVolume));
}
if(T >= n_fut)
newbars.Add(Bars.Date[bar], Open[bar],High[bar], Low[bar], Close[bar],Volume[bar]);
}
RestoreContext();
}
SetContext( DataSetSymbols[DataSetSymbols.Count - 1], false );
for(int bar = bar1; bar < Bars.Count;bar++)
newbars.Add(Bars.Date[bar], Open[bar],High[bar], Low[bar], Close[bar],Volume[bar]);
RestoreContext();
Write2File(folder, file ,newbars);
}
private void Write2File(string folder, string file, Bars bars)
{
if (!System.IO.Directory.Exists(folder))
System.IO.Directory.CreateDirectory(folder);
System.IO.StreamWriter SW = new System.IO.StreamWriter(folder + file);
SW.WriteLine(@",,,,,");
for (int i = 0; i < bars.Count; i++)
{
SW.WriteLine(bars.Date[i] + @"," +
bars.Open[i].ToString().Replace(',', '.') + @"," +
bars.High[i].ToString().Replace(',', '.') + @"," +
bars.Low[i].ToString().Replace(',', '.') + @"," +
bars.Close[i].ToString().Replace(',', '.') + @"," +
bars.Volume[i].ToString().Replace(',', '.'));
}
SW.Close();
}
}