| Overall Statistics |
|
Total Trades 17 Average Win 3.11% Average Loss -1.29% Compounding Annual Return 22.269% Drawdown 5.400% Expectancy 1.979 Net Profit 22.314% Sharpe Ratio 2.227 Loss Rate 12% Win Rate 88% Profit-Loss Ratio 2.41 Alpha 0.367 Beta -8.139 Annual Standard Deviation 0.092 Annual Variance 0.008 Information Ratio 2.01 Tracking Error 0.092 Treynor Ratio -0.025 Total Fees $61.64 |
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Algorithm demonstrating custom charting support in QuantConnect.
/// The entire charting system of quantconnect is adaptable. You can adjust it to draw whatever you'd like.
/// Charts can be stacked, or overlayed on each other. Series can be candles, lines or scatter plots.
/// Even the default behaviours of QuantConnect can be overridden.
/// </summary>
public class CustomChartingAlgorithm : QCAlgorithm
{
private decimal _fastMa;
private decimal _slowMa;
private decimal _lastPrice;
private DateTime _resample;
private TimeSpan _resamplePeriod;
private readonly DateTime _startDate = new DateTime(2013, 3, 3);
private readonly DateTime _endDate = new DateTime(2014, 3, 3);
/// <summary>
/// Called at the start of your algorithm to setup your requirements:
/// </summary>
public override void Initialize()
{
//Set the date range you want to run your algorithm:
SetStartDate(_startDate);
SetEndDate(_endDate);
//Set the starting cash for your strategy:
SetCash(100000);
//Add any stocks you'd like to analyse, and set the resolution:
// Find more symbols here: http://quantconnect.com/data
AddSecurity(SecurityType.Equity, "SPY", Resolution.Hour);
//Chart - Master Container for the Chart:
var stockPlot = new Chart("Trade Plot");
//On the Trade Plotter Chart we want 3 series: trades and price:
//var buyOrders = new Series("Buy", SeriesType.Scatter, 0);
//var sellOrders = new Series("Sell", SeriesType.Scatter, 0);
var assetPrice = new Series("Price", SeriesType.Candle, 0);
//stockPlot.AddSeries(buyOrders);
//stockPlot.AddSeries(sellOrders);
stockPlot.AddSeries(assetPrice);
AddChart(stockPlot);
var avgCross = new Chart("Strategy Equity");
var fastMa = new Series("FastMA", SeriesType.Line, 1);
var slowMa = new Series("SlowMA", SeriesType.Line, 1);
avgCross.AddSeries(fastMa);
avgCross.AddSeries(slowMa);
AddChart(avgCross);
_resamplePeriod = TimeSpan.FromMinutes((_endDate - _startDate).TotalMinutes / 2000);
}
/// <summary>
/// OnEndOfDay Event Handler - At the end of each trading day we fire this code.
/// To avoid flooding, we recommend running your plotting at the end of each day.
/// </summary>
public override void OnEndOfDay()
{
//Log the end of day prices:
//Plot("Trade Plot", "Price", _lastPrice);
}
/// <summary>
/// On receiving new tradebar data it will be passed into this function. The general pattern is:
/// "public void OnData( CustomType name ) {...s"
/// </summary>
/// <param name="data">TradeBars data type synchronized and pushed into this function. The tradebars are grouped in a dictionary.</param>
public void OnData(TradeBars data)
{
_lastPrice = data["SPY"].Close;
Plot("Trade Plot", "Price", _lastPrice);
Log(data["SPY"].Time.ToString());
if (_fastMa == 0) _fastMa = _lastPrice;
if (_slowMa == 0) _slowMa = _lastPrice;
_fastMa = (0.01m * _lastPrice) + (0.99m * _fastMa);
_slowMa = (0.001m * _lastPrice) + (0.999m * _slowMa);
if (Time > _resample)
{
_resample = Time.Add(_resamplePeriod);
//Plot("Strategy Equity", "FastMA", _fastMa);
//Plot("Strategy Equity", "SlowMA", _slowMa);
}
//On the 5th days when not invested buy:
if (!Portfolio.Invested && Time.Day % 13 == 0)
{
Order("SPY", (int)(Portfolio.MarginRemaining / data["SPY"].Close));
//Plot("Trade Plot", "Buy", _lastPrice);
}
else if (Time.Day % 21 == 0 && Portfolio.Invested)
{
//Plot("Trade Plot", "Sell", _lastPrice);
Liquidate();
}
}
}
}