| Overall Statistics |
|
Total Trades 46 Average Win 0.66% Average Loss -1.19% Compounding Annual Return 0.699% Drawdown 9.000% Expectancy 0.083 Net Profit 2.115% Sharpe Ratio 0.145 Loss Rate 30% Win Rate 70% Profit-Loss Ratio 0.56 Alpha -0.062 Beta 3.547 Annual Standard Deviation 0.061 Annual Variance 0.004 Information Ratio -0.183 Tracking Error 0.061 Treynor Ratio 0.002 Total Fees $1104.50 |
namespace QuantConnect.Algorithm.CSharp
{
public class LearningFear : QCAlgorithm
{
///Set to true when you want to do a 3 year back test up to one year in the past
public bool Inbound = true;
private int LongP = 25;
private int ShortP = 4;
private RelativeStrengthIndex _rsiLong;
private RelativeStrengthIndex _rsiShort;
private SimpleMovingAverage _SMA200;
public override void Initialize()
{
if(Inbound) {
SetStartDate(DateTime.Now.Date.AddDays(-4*365)); //Set Start Date 4 years in the past
SetEndDate(DateTime.Now.Date.AddDays(-1*365)); //Set End Date end date 1 year in the past leaving 1 year for out of bounds forward testing
}
else
{
SetStartDate(DateTime.Now.Date.AddDays(-1*365)); //Set Start Date 4 years in the past
SetEndDate(DateTime.Now); //Set End Date end date 1 year in the past leaving 1 year for out of bounds forward testing
}
SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash);
SetCash(1000000); //Set Strategy Cash
AddEquity("SPY", Resolution.Daily);
SetWarmUp(TimeSpan.FromDays(200+50));
_rsiLong = RSI("SPY",LongP,MovingAverageType.Simple, Resolution.Daily);
_rsiShort = RSI("SPY",ShortP,MovingAverageType.Simple, Resolution.Daily);
_SMA200 = SMA("SPY",200,Resolution.Daily);
//setup chart
var rsiPlot = new Chart("RSI Chart");
var RSIFast = new Series("Fast", SeriesType.Line,0);
var RSISlow = new Series("Slow", SeriesType.Line,0);
var SMASlow = new Series("SMA200", SeriesType.Line,0);
//TODO add go long go short markers
rsiPlot.AddSeries(RSIFast);
rsiPlot.AddSeries(RSISlow);
AddChart(rsiPlot);
}
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// Slice object keyed by symbol containing the stock data
public override void OnData(Slice data)
{
if(!_rsiLong.IsReady) return;
if(!_rsiShort.IsReady) return;
if(!_SMA200.IsReady) return;
Plot("RSI Chart","Fast", _rsiShort.Current);
Plot("RSI Chart","Slow", _rsiLong.Current);
Plot("RSI Chart","SMA200", _SMA200.Current);
//we are in an uptrend
if(data["SPY"].Price > _SMA200.Current)
{
if(_rsiShort.Current < 25 && !Portfolio["SPY"].IsLong)
{
//go long over sold
SetHoldings("SPY",1);
}
}
if(_rsiShort.Current > 65 && Portfolio["SPY"].IsLong)
{
//go long over sold
SetHoldings("SPY",0);
}
}
public override void OnOrderEvent(OrderEvent orderEvent)
{
Log(orderEvent.ToString());
}
}
}