This one is with Daily resolution:
using System;
using System.Globalization;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;
namespace QuantConnect.EMA.GDAX
{
public class MovingAverageCross : QCAlgorithm
{
private const string Symbol = "MSFT";
private DoubleExponentialMovingAverage fast;
private DoubleExponentialMovingAverage slow;
public override void Initialize()
{
SetStartDate(2009, 01, 01);
SetEndDate(2018, 02, 03);
AddEquity(Symbol, Resolution.Daily);
fast = DEMA(Symbol, 30, Resolution.Daily);
slow = DEMA(Symbol, 150, Resolution.Daily);
SetWarmup(150);
}
//private DateTime previous;
public void OnData(TradeBars data)
{
if (!slow.IsReady) return;
//if (previous.Minute == Time.Minute) return;
const decimal tolerance = 0.00015m;
var holdings = Portfolio[Symbol].Quantity;
if (holdings <= 0)
{
// if the fast is greater than the slow, go long
if (fast > slow * (1 + tolerance))
{
Log("BUY >> " + Securities[Symbol].Price);
SetHoldings(Symbol, 1);
}
}
// Liquidate if currently holding a position
// if the fast is less than the slow then sell
if (holdings > 0 && fast < slow)
{
Log("SELL >> " + Securities[Symbol].Price);
Liquidate(Symbol);
}
Plot(Symbol, "Price", data[Symbol].Price);
Plot(Symbol, fast, slow);
//previous = Time;
}
}
}
This one is with minute data:
using System;
using System.Globalization;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;
namespace QuantConnect.EMA.GDAX
{
public class MovingAverageCross : QCAlgorithm
{
private const string Symbol = "MSFT";
private DoubleExponentialMovingAverage fast;
private DoubleExponentialMovingAverage slow;
public override void Initialize()
{
SetStartDate(2009, 01, 01);
SetEndDate(2018, 02, 03);
AddEquity(Symbol, Resolution.Minute);
fast = DEMA(Symbol, 30, Resolution.Minute);
slow = DEMA(Symbol, 150, Resolution.Minute);
SetWarmup(150);
}
//private DateTime previous;
public void OnData(TradeBars data)
{
if (!slow.IsReady) return;
//if (previous.Minute == Time.Minute) return;
const decimal tolerance = 0.00015m;
var holdings = Portfolio[Symbol].Quantity;
if (holdings <= 0)
{
// if the fast is greater than the slow, go long
if (fast > slow * (1 + tolerance))
{
Log("BUY >> " + Securities[Symbol].Price);
SetHoldings(Symbol, 1);
}
}
// Liquidate if currently holding a position
// if the fast is less than the slow then sell
if (holdings > 0 && fast < slow)
{
Log("SELL >> " + Securities[Symbol].Price);
Liquidate(Symbol);
}
Plot(Symbol, "Price", data[Symbol].Price);
Plot(Symbol, fast, slow);
//previous = Time;
}
}
}
No idea of those questions:
Also, could you please provide some insights to the non-trading hours and the indicators? How can I ignore the indicators during the non-trading hours, as well as, how to can I leave these data points out of the DEMA calcuations? Or is it ignored automatically?
I guess you only receive data once there is data to consume. Otherwise indicators would show empty for zero or with last price. You can use PlotIndicator(Symbol, fast); to see that.