| Overall Statistics |
|
Total Trades 88 Average Win 11.07% Average Loss -2.47% Compounding Annual Return 769.654% Drawdown 21.100% Expectancy 2.237 Net Profit 769.654% Sharpe Ratio 2.975 Loss Rate 41% Win Rate 59% Profit-Loss Ratio 4.48 Alpha -0.004 Beta 119.349 Annual Standard Deviation 0.551 Annual Variance 0.303 Information Ratio 2.95 Tracking Error 0.551 Treynor Ratio 0.014 Total Fees $6257.43 |
using System.Drawing;
namespace QuantConnect.Algorithm.CSharp
{
public class RSI_MA : QCAlgorithm
{
private ExponentialMovingAverage _fastEMA;
private ExponentialMovingAverage _slowEMA;
private RelativeStrengthIndex _RSI;
string security = "BTCEUR";
/// <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(2017, 01, 01); //Set Start Date
SetEndDate(2017, 12, 31); //Set End Date
SetCash(10000); //Set Strategy Cash
SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);
/// NOTE set benchmark
AddCrypto(security, Resolution.Hour);
/// Create consolidator
var consolidator = new TradeBarConsolidator(6);
consolidator.DataConsolidated += ConsolidatedHandler;
SubscriptionManager.AddConsolidator(security, consolidator);
/// Register Indicators
_RSI = RSI(security, 14);
_fastEMA = EMA(security, 50);
_slowEMA = EMA(security, 200);
/// Register indicators to receive consolidated data
RegisterIndicator(security, _RSI, consolidator);
RegisterIndicator(security, _fastEMA, consolidator);
RegisterIndicator(security, _slowEMA, consolidator);
/// Add chart
Chart stockPlot = new Chart("Trade Plot");
stockPlot.AddSeries(new Series("Buy", SeriesType.Scatter, "", Color.Green, ScatterMarkerSymbol.Triangle));
stockPlot.AddSeries(new Series("Sell", SeriesType.Scatter, "", Color.Red, ScatterMarkerSymbol.Triangle));
stockPlot.AddSeries(new Series("Price", SeriesType.Line, "", Color.Blue));
stockPlot.AddSeries(new Series("RSI", SeriesType.Line, 1));
stockPlot.AddSeries(new Series("Slow EMA", SeriesType.Line, 2));
stockPlot.AddSeries(new Series("Fast EMA", SeriesType.Line, 2));
AddChart(stockPlot);
}
/// Event handler
private void ConsolidatedHandler(object sender, TradeBar bar)
{
/// check if all indicators are ready
if (!_RSI.IsReady || !_fastEMA.IsReady || !_slowEMA.IsReady) return;
var holdings = Portfolio[security].Quantity;
/// Plot price and indicators
Plot("Trade Plot", "Price", bar.Close);
Plot("Trade Plot", "RSI", _RSI);
Plot("Trade Plot", "Fast EMA", _fastEMA);
Plot("Trade Plot", "Slow EMA", _slowEMA);
/// we only want to go long if we're currently short or flat
if (holdings <= 0)
{
/// check if uptrend: fast over slow EMA & RSI buy signal
if (_RSI > 70 && _fastEMA > _slowEMA)
{
SetHoldings(security, 1);
Log("--->BUY<--- AT: " + bar.Close);
Plot("Trade Plot", "Buy", bar.Close);
}
}
/// if invested: sell if fast < slow EMA
if (holdings > 0)
{
if (_RSI < 30)
{
Liquidate(security);
Log("--->SELL<--- AT: " + bar.Close);
Plot("Trade Plot", "Sell", bar.Close);
}
}
}
}
}