| Overall Statistics |
|
Total Trades 8 Average Win 15.19% Average Loss -2.17% Compounding Annual Return 12.929% Drawdown 9.800% Expectancy 1.002 Net Profit 7.818% Sharpe Ratio 0.837 Loss Rate 75% Win Rate 25% Profit-Loss Ratio 7.01 Alpha -0.055 Beta 9.47 Annual Standard Deviation 0.159 Annual Variance 0.025 Information Ratio 0.713 Tracking Error 0.159 Treynor Ratio 0.014 Total Fees $37.35 |
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Simple indicator demonstration algorithm of MACD
/// </summary>
public class MACDTrendAlgorithm : QCAlgorithm
{
private DateTime _previous;
private MovingAverageConvergenceDivergence _macd;
private readonly string _symbol = "AAPL";
/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetStartDate(2015, 01, 01);
SetEndDate(2015, 08, 15);
AddSecurity(SecurityType.Equity, _symbol, Resolution.Daily);
// define our daily macd(12,26) with a 9 day signal
_macd = MACD(_symbol, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily);
}
/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">TradeBars IDictionary object with your stock data</param>
public void OnData(TradeBars data)
{
// only once per day
if (_previous.Date == Time.Date) return;
if (!_macd.IsReady) return;
var holding = Portfolio[_symbol];
var signalDeltaPercent = (_macd - _macd.Signal)/_macd.Slow;
var tolerance = 0.001m;
// if our macd is greater than our signal, then let's go long
if (holding.Quantity <= 0 && signalDeltaPercent > tolerance) // 0.01%
{
// longterm says buy as well
SetHoldings(_symbol, 1.0);
}
// of our macd is less than our signal, then let's go short
else if (holding.Quantity >= 0 && signalDeltaPercent < -tolerance)
{
Liquidate(_symbol);
}
// plot both lines
Plot("MACD", _macd, _macd.Signal);
Plot(_symbol, "Close", data[_symbol].Close);
Plot(_symbol, _macd.Fast, _macd.Slow);
_previous = Time;
}
}
}