The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
var consolidator = ResolveConsolidator("CustomSymbol", Resolution.Hour);
var consolidator = ResolveConsolidator("CustomSymbol", Resolution.Daily );
public class TestingCustomDataAlgo : QCAlgorithm
{
///
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
///
public override void Initialize()
{
SetStartDate(new DateTime(2010, 04, 02, 00, 00, 00));
SetEndDate(2010, 05, 04);
AddData("EURUSD");
//wire up an event to handle the bars
var consolidator = ResolveConsolidator("EURUSD", Resolution.Hour);
//var consolidator = ResolveConsolidator("EURUSD", Resolution.Daily);
consolidator.DataConsolidated += HourBarHandler;
SubscriptionManager.AddConsolidator("EURUSD", consolidator);
}
///
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
///
/// TradeBars IDictionary object with your stock data
public void OnData(EURUSD data)
{
//Console.WriteLine("Time: " + data.Time);
}
private void HourBarHandler(object sender, BaseData consolidated)
{
Console.WriteLine("Consolidated Time: " + consolidated.Time);
}
}
///
/// EURUSD is a custom data type we create for this algorithm
///
public class EURUSD : BaseData
{
///
/// Opening Price
///
public decimal Open = 0;
///
/// High Price
///
public decimal High = 0;
///
/// Low Price
///
public decimal Low = 0;
///
/// Closing Price
///
public decimal Close = 0;
///
/// Default initializer for EURUSD.
///
public EURUSD()
{
Symbol = "EURUSD";
}
///
/// Return the URL string source of the file. This will be converted to a stream
///
public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed)
{
return @"..\..\..\Data\custom\EURUSD_1M_20100401_20141111.csv";
}
///
/// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
/// each time it is called.
///
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed)
{
EURUSD index = new EURUSD();
try
{
string[] data = line.Split(',');
string timeValue = (Convert.ToInt32(data[2])).ToString("00:00:00");
string dateValue = (Convert.ToInt32(data[1])).ToString("0000/00/00");
index.Symbol = data[0];
index.Time = DateTime.Parse(dateValue + " " + timeValue);
index.Open = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture);
index.High = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture);
index.Low = Convert.ToDecimal(data[5], CultureInfo.InvariantCulture);
index.Close = Convert.ToDecimal(data[6], CultureInfo.InvariantCulture);
index.Value = index.Close;
}
catch (Exception ex)
{
ex.ToString();
}
return index;
}
}
var dtbConsolidator = new DailyTradeBarConsolidator();
dtbConsolidator.DataConsolidated += DayBarHandler;
SubscriptionManager.AddConsolidator("EURUSD", dtbConsolidator);
using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Data.Consolidators;
namespace QuantConnect.Algorithm.Examples
{
///
/// Testing algorithm simply initializes the date range and cash
///
public class TestingCustomDataAlgo : QCAlgorithm
{
///
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
///
public override void Initialize()
{
SetStartDate(new DateTime(2010, 05, 03, 00, 00, 00));
SetEndDate(2010, 06, 01);
AddData("EURUSD");
//create an event to handle the hourly bars
var consolidator = ResolveConsolidator("EURUSD", Resolution.Daily);
consolidator.DataConsolidated += HourBarHandler;
SubscriptionManager.AddConsolidator("EURUSD", consolidator);
var dtbConsolidator = new DailyTradeBarConsolidator();
dtbConsolidator.DataConsolidated += DayBarHandler;
SubscriptionManager.AddConsolidator("EURUSD", dtbConsolidator);
}
private void DayBarHandler(object sender, BaseData consolidated)
{
Console.WriteLine("\nDBH Consolidated Time: " + consolidated.Time.DayOfWeek + " at " + consolidated.Time);
}
///
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
///
/// TradeBars IDictionary object with your stock data
public void OnData(EURUSD data)
{
//Console.WriteLine("Time: " + data.Time);
}
private void HourBarHandler(object sender, BaseData consolidated)
{
Console.WriteLine("\nConsolidated Time: " + consolidated.Time.DayOfWeek + " at " + consolidated.Time);
//Debug("\nConsolidated Daily Time: " + consolidated.Time);
}
}
///
/// ad hoc fix for daily close issue
///
public class DailyTradeBarConsolidator : DataConsolidator
{
private TradeBar _working;
///
/// OutputType returns a Type?
///
public override Type OutputType
{
get { return typeof(TradeBar); }
}
///
/// Update method
///
/// Tradebar type
public override void Update(TradeBar data)
{
if (_working != null && _working.Time.Date != data.Time.Date)
{
OnDataConsolidated(_working);
_working = null;
}
AggregateBar(ref _working, data);
}
///
/// Aggregates the new 'data' into the 'workingBar'. The 'workingBar' will be
/// null following the event firing
///
/// The bar we're building, null if the event was just fired and we're starting a new trade bar
/// The new data
protected void AggregateBar(ref TradeBar workingBar, TradeBar data)
{
if (workingBar == null)
{
workingBar = new TradeBar
{
Time = data.Time,
Symbol = data.Symbol,
Open = data.Open,
High = data.High,
Low = data.Low,
Close = data.Close,
Volume = data.Volume,
DataType = MarketDataType.TradeBar,
Period = data.Period
};
}
else
{
//Aggregate the working bar
workingBar.Close = data.Close;
workingBar.Volume += data.Volume;
workingBar.Period += data.Period;
if (data.Low < workingBar.Low) workingBar.Low = data.Low;
if (data.High > workingBar.High) workingBar.High = data.High;
}
}
}
///
/// EURUSD is a custom data type we create for this algorithm
///
public class EURUSD : BaseData
{
///
/// Opening Price
///
public decimal Open = 0;
///
/// High Price
///
public decimal High = 0;
///
/// Low Price
///
public decimal Low = 0;
///
/// Closing Price
///
public decimal Close = 0;
///
/// Default initializer for EURUSD.
///
public EURUSD()
{
Symbol = "EURUSD";
}
///
/// Return the URL string source of the file. This will be converted to a stream
///
public override string GetSource(SubscriptionDataConfig config, DateTime date, DataFeedEndpoint datafeed)
{
return @"..\..\..\Data\custom\EURUSD_1M_20100401_20141111.csv";
}
///
/// Reader converts each line of the data source into BaseData objects. Each data type creates its own factory method, and returns a new instance of the object
/// each time it is called.
///
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed)
{
EURUSD index = new EURUSD();
try
{
string[] data = line.Split(',');
string timeValue = (Convert.ToInt32(data[2])).ToString("00:00:00");
string dateValue = (Convert.ToInt32(data[1])).ToString("0000/00/00");
index.Symbol = data[0];
index.Time = DateTime.Parse(dateValue + " " + timeValue);
index.Open = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture);
index.High = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture);
index.Low = Convert.ToDecimal(data[5], CultureInfo.InvariantCulture);
index.Close = Convert.ToDecimal(data[6], CultureInfo.InvariantCulture);
index.Value = index.Close;
}
catch (Exception ex)
{
ex.ToString();
}
return index;
}
}
}
DailyTradeBarConsolidator()
in a new release of LEAN? ResolveConsolidator("MSFT", Resolution.Daily);
or is it better to use the custom DailyTradeBarConsolidator() ?
var consolidator = new TradeBarConsolidator(TimeSpan.FromDays(1));
That being said, using the following is always safe:var consolidator = ResolveConsolidator("SPY", Resolution.Daily);
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can
continue your Boot Camp training progress from the terminal. We
hope to see you in the community soon!