namespace QuantConnect.Algorithm.CSharp
{
public class TickVolumeConsolidatorTest : QCAlgorithm
{
public Symbol TestSymbol;
public long AccumulatedTicks;
public IDataConsolidator MinuteTickConsolidator; //Consolidate Tick data into Minute bars
public Resolution SymbolResolution;
public override void Initialize()
{
SetStartDate(2020, 1, 1); //Set Start Date
SetEndDate(2020, 12, 31);
SetCash(100000); //Set Strategy Cash
//Store the Resolution so it is a simple flag to switch between Tick and Minute for benchmarking
SymbolResolution = Resolution.Tick; //Switch between Resolution.Tick and Resolution.Minute
Symbol EURUSD = QuantConnect.Symbol.Create("EURUSD", SecurityType.Forex, Market.Oanda);
TestSymbol = AddSecurity(EURUSD, SymbolResolution).Symbol;
AccumulatedTicks = 0;
if (SymbolResolution == Resolution.Tick)
{ //Only create the Tick Consolidator if using Tick Resolution
MinuteTickConsolidator = Consolidate<QuoteBar>(TestSymbol, TimeSpan.FromMinutes(1), EventQuoteBar => {
PrintQuoteBarAndTickVolume(this, EventQuoteBar, AccumulatedTicks);
AccumulatedTicks = 0; //Reset AccumulatedTicks after each consolidated bar
});
}
} //End of Initialize()
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// Slice object keyed by symbol containing the stock data
public override void OnData(Slice data)
{
if (SymbolResolution == Resolution.Tick)
{
if (data.Ticks != null)
{
foreach (var DataSymbol in data.Ticks.Keys)
{
if (DataSymbol != TestSymbol)
{
continue;
}
AccumulatedTicks += data.Ticks[TestSymbol].Count;
}
}
} //End of Tick Resolution
else
{
if (data.QuoteBars != null)
{
foreach (var DataSymbol in data.QuoteBars.Keys)
{
if (DataSymbol != TestSymbol)
{
continue;
}
PrintQuoteBarAndTickVolume(this, data.QuoteBars[DataSymbol], AccumulatedTicks); //AccumulatedTicks will always be 0 with non-Tick Resolution
}
}
} //End of non-Tick Resolution
} //End of OnData()
public bool PrintQuoteBarAndTickVolume(QCAlgorithm algorithm, QuoteBar InQuoteBar, long InTickVolume)
{
if (InQuoteBar.Time.Hour == 0 && InQuoteBar.Time.Minute == 0 && InQuoteBar.Time.DayOfWeek == DayOfWeek.Monday)
{ //Only print once per week to avoid excessive logs
algorithm.Debug(InQuoteBar.Symbol + " " + InQuoteBar.Time + ": O: " + InQuoteBar.Open + "; H: " + InQuoteBar.High + "; L: " + InQuoteBar.Low + "; C: " + InQuoteBar.Close + "; TV: " + InTickVolume);
return true;
}
return false;
} //End of PrintQuoteBarAndTickVolume()
} //End of class TickVolumeConsolidatorTest
} //End of namespace