QuantConnect / Lean

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

QCAlgorithm.WarmUpIndicator #3861

Closed Martin-Molinero closed 4 years ago

Martin-Molinero commented 4 years ago

Expected Behavior

public class BasicTemplateAlgorithm : QCAlgorithm
{
    private SimpleMovingAverage _fast;
    private SimpleMovingAverage _slow;

    private Symbol _symbol;

    public override void Initialize()
    {
        EnableAutomaticIndicatorWarmUp = true;

        SetTimeZone("Europe/Madrid");

        SetStartDate(2016, 01, 01);
        SetEndDate(DateTime.Now);

        SetAccountCurrency("USD");
        SetCash(100000);             //Set Strategy Cash

        SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);

        // Find more symbols here: http://quantconnect.com/data
        _symbol = AddCrypto("BTCUSD", Resolution.Daily).Symbol;

        // create one moving average
        _fast = SMA(_symbol, 2, Resolution.Daily);
        _slow = SMA(_symbol, 22, Resolution.Daily);
    }

    /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
    /// Slice object keyed by symbol containing the stock data
    public override void OnData(Slice data)
    {
        if (data.Time.Year < 2017) return;

        if (_fast > _slow)
        {
            if (Portfolio.CashBook["BTC"].Amount == 0)
            {
                Buy(_symbol, 10);
            }
        }
        else
        {
            if (Portfolio.CashBook["BTC"].Amount > 0)
            {
                Liquidate(_symbol);
            }
        }
    }
}

System Information

N/A

Checklist

Martin-Molinero commented 4 years ago

note: using WarmUpIndicator during universe selection on newly listed symbols will not warm up the indicator, since there is no data, and will log a message of the format: The starting date for symbol KRP, 2016-06-15, has been adjusted to match map file first date 2017-02-03. since the history request start date will be adjusted.