Back

Appear to have issues when passing TimeSpan into History method.

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?
Anyway, this doesn't seem to be a data issue but more a code issue?  Can someone tell me what i'm doing wrong? Thanks,   Michael
Update Backtest







0

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.


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.

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed