Overall Statistics Total Trades56Average Win1.86%Average Loss-3.51%Compounding Annual Return-6.462%Drawdown34.300%Expectancy-0.071Net Profit-15.01%Sharpe Ratio-0.307Loss Rate39%Win Rate61%Profit-Loss Ratio0.53Alpha-0.031Beta-0.119Annual Standard Deviation0.171Annual Variance0.029Information Ratio-1.101Tracking Error0.212Treynor Ratio0.441Total Fees\$105.26
```namespace QuantConnect
{
/*
*   QuantConnect University: Full Basic Template:
*
*   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
*   We have explained some of these here, but the full algorithm can be found at:
*   https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
*/
public class BasicTemplateAlgorithm : QCAlgorithm
{
SimpleMovingAverage sma;
ExponentialMovingAverage ema;
SecurityHolding SPY;

//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{

//Start and End Date range for the backtest:
SetStartDate(2013, 1, 1);

//Cash allocation
SetCash(25000);

// register for hourly SPY data

// define our 6 period SMA, we'll pump 4-hour data into this guy
sma = new SimpleMovingAverage("SMA6", 6);
// define out 9 period EMA, we'll pump 4-hour data into this guy as well
ema = new ExponentialMovingAverage("EMA19", 19);

// define a 4 hour consolidator, each consolidator can only be bound to
// a single symbol, so if we wanted to also do AAPL data, we would need
// another consolidator for AAPL
var fourHourSpy = ResolveConsolidator("SPY", TimeSpan.FromHours(4));

// register our sma to receive data from our fourHourSpy consolidator, making our
// sma a 6 period 4-hour SMA
RegisterIndicator("SPY", sma, fourHourSpy);

// register our ema to receive data from our fourHourSpy consolidator, making our
// ema a 6 period 4-hour SMA
RegisterIndicator("SPY", ema, fourHourSpy);

// Plot our indicators on each new update
PlotIndicator("SPY", sma, ema);

SPY = Portfolio["SPY"];
}

//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
{

const decimal threshold = 0.000075m;
if (SPY.Quantity <= 0 && ema > sma*(1+threshold))
{
SetHoldings("SPY", .75m);
}
else if (SPY.Quantity >= 0 && ema < sma*(1-threshold))
{
SetHoldings("SPY", -.75m);
}
}
}
}```