private bool oneTime = false;
private string symbol = "SPY";
public override void Initialize()
{
SetStartDate(2020, 3, 25);
SetEndDate(2020, 3, 25);
SetCash(1000);
AddEquity(symbol, Resolution.Tick);
AddEquity(symbol, Resolution.Second);
AddEquity(symbol, Resolution.Minute);
AddEquity(symbol, Resolution.Hour);
AddEquity(symbol, Resolution.Daily);
SetWarmup(0);
}
public override void OnData(Slice data)
{
if (IsWarmingUp) return;
if(!data.ContainsKey(symbol) || data[symbol] == null) { }
else {
if(!oneTime)
{
ShowUsingInt(symbol);
ShowUsingTimeSpan(symbol);
//ShowTickUsingTimeSpan(symbol);
oneTime = true;
}
}
}
private void ShowTickUsingTimeSpan(string symbol)
{
var bars1 = GetHistory<Tick>(symbol, TimeSpan.FromSeconds(1), Resolution.Tick);
var bars2 = GetHistory<Tick>(symbol, TimeSpan.FromMinutes(1), Resolution.Tick);
var bars3 = GetHistory<Tick>(symbol, TimeSpan.FromHours(1), Resolution.Tick);
var bars4 = GetHistory<Tick>(symbol, TimeSpan.FromDays(1), Resolution.Tick);
Debug($"Tick Symbol {symbol} Timespan: Seconds : {bars1.Count()}; Minutes: {bars2.Count()}; Hours: {bars3.Count()}; Days: {bars4.Count()}");
}
private void ShowUsingTimeSpan(string symbol)
{
var bars1 = GetHistory<TradeBar>(symbol, TimeSpan.FromSeconds(1000), Resolution.Second);
var bars2 = GetHistory<TradeBar>(symbol, TimeSpan.FromMinutes(1000), Resolution.Minute);
var bars3 = GetHistory<TradeBar>(symbol, TimeSpan.FromHours(1000), Resolution.Hour);
var bars4 = GetHistory<TradeBar>(symbol, TimeSpan.FromDays(1000), Resolution.Daily);
Debug($"Symbol {symbol} Timespan: Seconds : {bars1.Count()}; Minutes: {bars2.Count()}; Hours: {bars3.Count()}; Days: {bars4.Count()}");
}
private void ShowUsingInt(string symbol)
{
var bars1 = GetHistory<TradeBar>(symbol, 1000, Resolution.Second);
var bars2 = GetHistory<TradeBar>(symbol, 1000, Resolution.Minute);
var bars3 = GetHistory<TradeBar>(symbol, 1000, Resolution.Hour);
var bars4 = GetHistory<TradeBar>(symbol, 1000, Resolution.Daily);
Debug($"Symbol {symbol} int: Seconds : {bars1.Count()}; Minutes: {bars2.Count()}; Hours: {bars3.Count()}; Days: {bars4.Count()}");
}
private IEnumerable<T> GetHistory<T>(string symbol, TimeSpan periods, Resolution resolution) where T : BaseData
{
return History<T>(symbol, periods, resolution);
}
private IEnumerable<T> GetHistory<T>(string symbol, int periods, Resolution resolution) where T : BaseData
{
return History<T>(symbol, (int)periods, resolution);
}
The above results print out:
Symbol SPY int: Seconds : 1000; Minutes: 1000; Hours: 1000; Days: 1000
Symbol SPY Timespan: Seconds : 0; Minutes: 390; Hours: 203; Days: 688
My assumption would be that both of these would have the same results?
Now when I call ShowTickUsingTimeSpan(symbol) too, it only prints out:
Runtime Error: Execution Security Error: Operation timed out - 5 minutes max. Check for recursive loops. (Open Stacktrace
Now if I use a different symbol, for example AMD:
Without the tick being printed out:
Symbol AMD int: Seconds : 1000; Minutes: 1000; Hours: 1000; Days: 1000
Symbol AMD Timespan: Seconds : 0; Minutes: 390; Hours: 203; Days: 688
With the tick being printed out
Symbol AMD int: Seconds : 1000; Minutes: 1000; Hours: 1000; Days: 1000
Symbol AMD Timespan: Seconds : 0; Minutes: 390; Hours: 203; Days: 688
Tick Symbol AMD Timespan: Se...
So my questions are:
- Why passing in TimeSpan into Histotry rather than int gives different answers?
- Why getting History for tick just doesn't work? "SPY" doesn't work, "AMD" doesn't even print out the line?
Alexandre Catarino
Hi Michael A Astrauckas ,
The History method with the integer parameter is the smartest method. It evaluates the beginning of the request period by taking into account whether the market is open to fetch the requested number of bars.
On the other hand, the History method with a TimeSpan parameter will simply subtract the current algorithm Time with that value.
Charles Naccio
Alexandre Catarino in what cirumstances is it appropriate to use TimeSpan? I've personally been using TimeSpans for everything, and then ran into this same issue as Michael.
Also, I didn't quite understand your answer on why TimeSpan isn't working; can you explain with a little more detail why the following doesn't produce 7,800 minute bars of history data as expected? 20 days * 390 minutes per day (6.5hrs * 60)
var history = algo.History("SPY", TimeSpan.FromDays(20), Resolution.Minute);
I'm personally getting history when stocks are added to my universe, and using it to warm up various indicators, of various timeframes. I'm trying to use a single history call for minute level data which I'm then consolidating and then warming up all higher timeframe indicators as opposed to making separate history calls for each timeframe.
Charles Naccio
Actually, Alexandre scratch the above, I just noticed the below snippet from the documentation which made it click for me.
"Keep in mind you TimeSpan history requests are relative to "now" in Algorithm Time. If you requested this data on a Monday morning, it would return an empty array because the market was closed over the weekend."
In my code above, what I didn't realize is that requesting 20 days of minute data using a timespan only returned aroudn 15 days of data after you factor in weekends over a 3 week period which is rougly 20 days (21 to be exact).
Hope this helps make it click for others!
Michael A Astrauckas
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!