The following comes oh-so-close.
Does anyone have any insight why the candles show the data out of phase (details after example)?
public class CustomChartingAlgorithm : QCAlgorithm
{
private readonly DateTime _startDate = new DateTime(2013, 10, 4);
private readonly DateTime _endDate = new DateTime(2013, 10, 11);
private TradeBarConsolidator _dayConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(1));
private Chart _spyChart;
/// <summary>
/// Called at the start of your algorithm to setup your requirements:
/// </summary>
public override void Initialize()
{
//Set the date range you want to run your algorithm:
SetStartDate(_startDate);
SetEndDate(_endDate);
//Set the starting cash for your strategy:
SetCash(100000);
//Add any stocks you'd like to analyse, and set the resolution:
// Find more symbols here: http://quantconnect.com/data
var under = AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
_spyChart = new Chart("SPY");
_spyChart.AddSeries(new Series("Candle", SeriesType.Candle));
AddChart(_spyChart);
_dayConsolidator.DataConsolidated += onDayConsolidator;
SubscriptionManager.AddConsolidator("SPY", _dayConsolidator);
}
private void onDayConsolidator(object sender, TradeBar consolidated)
{
// chart OHLC backdated as if it were today's only data
var t = consolidated.Time; //bar's time begins at midnight
_spyChart.Series["Candle"].AddPoint(t.AddHours(9.5), consolidated.Open);
_spyChart.Series["Candle"].AddPoint(t.AddHours(10), consolidated.High);
_spyChart.Series["Candle"].AddPoint(t.AddHours(11), consolidated.Low);
_spyChart.Series["Candle"].AddPoint(t.AddHours(16), consolidated.Close);
}
}
By out of phase, I mean the chart, ends up showing candles that mix up data from different days. The first 2 candles look OK, but after that...
For example. Oct8 bar comes out (OHLC)
167.62Â 165.36Â 165.48 165.80
Which, in other words, the bar is actually showing as if OHLC were
Oct8High, Oct8Low, Oct8Close, Oct9Open //Yes, Oct9
Yet, I have verified the raw data being emitted by onDayConsolidator is:
Oct 09, 2013Â 165.80 166.20 164.53 165.60
Oct 08, 2013Â 167.40 167.62 165.36 165.48
Oct 07, 2013Â 167.42 168.45 167.25 167.43Â Â