namespace QuantConnect
{
/*
* QuantConnect University: 50-10 EMA - Exponential Moving Average Cross
*
* The classic exponential moving average cross implementation using a custom class.
* The algorithm uses allows for a safety margin so you can reduce the "bounciness"
* of the trading to confirm the crossing.
*/
public class QCUMovingAverageCross : QCAlgorithm
{
//Define required variables:
int quantity = 0;
decimal price = 0;
decimal tolerance = 0m; //0.1% safety margin in prices to avoid bouncing.
string symbol = "SPY";
DateTime sampledToday = DateTime.Now;
//Set up the EMA Class:
ExponentialMovingAverage emaShort;
ExponentialMovingAverage emaLong;
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
SetStartDate(2014, 01, 01);
SetEndDate(DateTime.Now);
SetCash(25000);
AddSecurity(SecurityType.Equity, symbol, Resolution.Second);
emaShort = EMA(symbol, 10, Resolution.Daily);
emaLong = EMA(symbol, 50, Resolution.Daily);
}
//Handle TradeBar Events: a TradeBar occurs on every time-interval
public void OnData(TradeBars data) {
//One data point per day:
if (sampledToday.Date == data[symbol].Time.Date) return;
//Only take one data point per day (opening price)
price = Securities[symbol].Close;
sampledToday = data[symbol].Time;
//Wait until EMA's are ready:
if (!emaShort.IsReady || !emaLong.IsReady) return;
//Get fresh cash balance: Set purchase quantity to equivalent 10% of portfolio.
decimal cash = Portfolio.Cash;
int holdings = Portfolio[symbol].Quantity;
quantity = Convert.ToInt32((cash * 0.5m) / price);
if (holdings > 0 || holdings == 0) {
//If we're long, or flat: check if EMA crossed negative: and crossed outside our safety margin:
if ((emaShort * (1+tolerance)) < emaLong)
{
//Now go short: Short-EMA signals a negative turn: reverse holdings
Order(symbol, -(holdings + quantity));
Log(Time.ToShortDateString() + " > Go Short > Holdings: " + holdings.ToString() + " Quantity:" + quantity.ToString() + " Samples: " + emaShort.Samples);
}
} else if (holdings < 0 || holdings == 0) {
//If we're short, or flat: check if EMA crossed positive: and crossed outside our safety margin:
if ((emaShort * (1 - tolerance)) > emaLong)
{
//Now go long: Short-EMA crossed above long-EMA by sufficient margin
Order(symbol, Math.Abs(holdings) + quantity);
Log(Time.ToShortDateString() + "> Go Long > Holdings: " + holdings.ToString() + " Quantity:" + quantity.ToString() + " Samples: " + emaShort.Samples);
}
}
}
}
}