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(2014, 01, 01);

SetEndDate(2018, 02, 03);

AddEquity(Symbol, Resolution.Daily);

fast = DEMA(Symbol, 30, Resolution.Daily);

slow = DEMA(Symbol, 150, Resolution.Daily);

}

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;

}

}

}