QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.89k stars 3.27k forks source link

Map files first date not respected in Lower resolutions #2459

Closed Jay-Jay-D closed 5 years ago

Jay-Jay-D commented 6 years ago

Expected Behavior

The stock period in the stock market as described in the map files must be respected independently of the resolution.

Actual Behavior

The starting date for stocks whose ticker contains previous data is not respected. If the resolution is hourly or daily the price data will be pumped since the first observation available. However, if there is a last date, it is respected and the equity is delisted.

Potential Solution

Investigate why the SubscriptionDataReader is failing at this lines with lower resolutions:

// if factor file has minimum date, update start period if before minimum date
if (!_isLiveMode && _factorFile != null && _factorFile.FactorFileMinimumDate.HasValue)
{
    if (_periodStart < _factorFile.FactorFileMinimumDate.Value)
    {
        _periodStart = _factorFile.FactorFileMinimumDate.Value;

        _resultHandler.DebugMessage(
            string.Format("Data for symbol {0} has been limited due to numerical precision issues in the factor file. The starting date has been set to {1}.",
            config.Symbol.Value,
            _factorFile.FactorFileMinimumDate.Value.ToShortDateString()));
    }
}

Reproducing the Problem

GM went into bankruptcy in January 1st, 2009 and was out of the stock markets until November 1st, 2010 when it came back under the same symbol

The algorithm attached below will work perfectly for higher resolutions:

  1. If the ticker is GM.1 the security will be held until January 1st, 2009.
  2. If the ticker is GM the algorithm will start to receive the security data since November 1st, 2010.

Now, if we set lower resolutions:

  1. If the ticker is GM.1 it’ll work fine.
  2. If the ticker is GM the algorithm will start to receive the security data since January 2nd, 1998.
public class BasicTemplateAlgorithm : QCAlgorithm
{
    string _ticker = "GM"; 
    private Symbol _security;

    public override void Initialize()
    {
        SetStartDate(1998, 01, 01);  //Set Start Date
        SetEndDate(DateTime.Today);    //Set End Date
        SetCash(100000);             //Set Strategy Cash

        _security = AddEquity(_ticker, Resolution.Hour).Symbol;
    }

    public override void OnData(Slice data)
    {
        if (!Portfolio.Invested)
        {
            MarketOrder(_security, 1);
            Log($"Purchased Security {_security}");
        }
    }
}

System Information

QC Cloud.

Checklist

AlexCatarino commented 5 years ago

Cannot reproduce the issue anymore (double checked with @Jay-Jay-D).