| Overall Statistics |
|
Total Trades 7 Average Win 44.89% Average Loss -11.18% Compounding Annual Return 17.757% Drawdown 32.100% Expectancy 0.672 Net Profit 87.187% Sharpe Ratio 0.607 Loss Rate 67% Win Rate 33% Profit-Loss Ratio 4.02 Alpha 0.18 Beta -0.051 Annual Standard Deviation 0.289 Annual Variance 0.083 Information Ratio 0.265 Tracking Error 0.314 Treynor Ratio -3.463 Total Fees $49.99 |
using QuantConnect.Indicators;
using System.Drawing; // for Color
namespace QuantConnect
{
public class BasicTemplateAlgorithm : QCAlgorithm
{
private string Symbol = "CNX";
private decimal Symbol_price = 0;
ExponentialMovingAverage emaFast;
ExponentialMovingAverage emaSlow = null;
//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);
SetEndDate(DateTime.Now.Date.AddDays(-1));
//Cash allocation
SetCash(25000);
//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, Symbol, Resolution.Minute);
emaFast = EMA(Symbol, 20, Resolution.Daily);//new ExponentialMovingAverage(Symbol,15, Resolution.Daily);
emaSlow = EMA(Symbol, 100, Resolution.Daily);
var emaPlot = new Chart("EMA");
emaPlot.AddSeries(new Series("EMA-100", SeriesType.Line, 0));
emaPlot.AddSeries(new Series("EMA-20", SeriesType.Line, 0));
emaPlot.AddSeries(new Series("Price", SeriesType.Line,0));
emaPlot.AddSeries(new Series("Buy", SeriesType.Scatter, 0));
emaPlot.AddSeries(new Series("Sell", SeriesType.Scatter, 0));
AddChart(emaPlot);
}
//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
public void OnData(TradeBars data)
{
if(!emaSlow.IsReady) return;
var price = data[Symbol].Close;
var qnty = Portfolio[Symbol].Quantity;
Symbol_price = price;
ManagePositionsEMA(price);
BuyOnEMA(data);
}
private void BuyOnEMA(TradeBars data)
{
var price = data[Symbol].Close;
var qnty = Portfolio[Symbol].Quantity;
// Enter long position
if( qnty <= 0 && price > emaFast && emaFast> emaSlow) // qnty == 0 &&
{
SetHoldings(Symbol, 1m);
Debug("Long "+Symbol+" on " + Time.ToShortDateString()+" at "+price.ToString());
Plot("EMA", "Buy", price);
}
// Enter short position
if( qnty >= 0 && price < emaFast && emaFast < emaSlow) //qnty == 0 &&
{
SetHoldings(Symbol, -1m);
Debug("Short "+Symbol+" on " + Time.ToShortDateString()+" at "+price.ToString());
Plot("EMA", "Sell", price);
}
}
private void ManagePositionsEMA(decimal price)
{
if (Portfolio[Symbol].Quantity > 0)
{
if (emaFast < emaSlow || price < emaSlow)
{
// Debug("Close "+Symbol+" on " + Time.ToShortDateString());
// SetHoldings(Symbol, 0);
// Plot("EMA", "Sell", price);
}
}
else if (Portfolio[Symbol].Quantity < 0)
{
}
}
public override void OnEndOfDay()
{
Plot("EMA","IsLong", (Portfolio[Symbol].Quantity > 0 ? 5:0));
Plot("EMA","IsShort", (Portfolio[Symbol].Quantity < 0 ? 5:0));
Plot("EMA", "EMA-20", emaFast);
Plot("EMA", "EMA-100", emaSlow);
Plot("EMA", "Price", Symbol_price);
}
}
}