Overall Statistics |
Total Trades 277 Average Win 7.42% Average Loss -10.02% Compounding Annual Return 512.224% Drawdown 67.100% Expectancy 0.237 Net Profit 773.631% Sharpe Ratio 1.342 Loss Rate 29% Win Rate 71% Profit-Loss Ratio 0.74 Alpha 2.402 Beta 0.214 Annual Standard Deviation 1.801 Annual Variance 3.243 Information Ratio 1.301 Tracking Error 1.803 Treynor Ratio 11.302 Total Fees $0.00 |
namespace QuantConnect { public class BasicTemplateAlgorithm : QCAlgorithm { //public string pair1 = "EURAUD"; //2015 200window public string pair1 = "NZDJPY"; RollingWindow<double> window; RollingWindow<double> diffWindow; const int WL = 50; bool IsLong = false; BollingerBands bb; List<OrderTicket> pendingOrders; public override void Initialize() { SetStartDate(2016,01,01); SetEndDate(2016, 12, 30); //SetBrokerageModel(BrokerageName.FxcmBrokerage); SetBrokerageModel(BrokerageName.OandaBrokerage); SetCash(10000); var res = Resolution.Hour; AddForex(pair1, res, Market.Oanda); //AddForex(pair1, res, Market.FXCM); bb = BB(pair1, WL, 1.0M, MovingAverageType.Simple, res); window = new RollingWindow<double>(WL); diffWindow = new RollingWindow<double>(WL); pendingOrders = new List<OrderTicket>(); SetWarmup(WL); } private double getAverage(RollingWindow<double> window){ var doubleList = new List<double>(); for (int i = 0; i < WL; i++) doubleList.Add(window[i]); return doubleList.Average(); } private double getStandardDeviation(RollingWindow<double> window) { var doubleList = new List<double>(); for (int i = 0; i < WL; i++) doubleList.Add(window[i]); double average = doubleList.Average(); double sumOfDerivation = 0; foreach (double value in doubleList) { sumOfDerivation += (value) * (value); } double sumOfDerivationAverage = sumOfDerivation / (doubleList.Count - 1); return Math.Sqrt(sumOfDerivationAverage - (average*average)); } public void OnData(TradeBars data) { window.Add((double)(decimal)data[pair1].Close); if(!window.IsReady) return; if(!bb.IsReady) return; var sm = getAverage(window); var diff = (double)(data[pair1].Close) - sm; diffWindow.Add(diff); if(!diffWindow.IsReady) return; if (IsWarmingUp) return; var std = getStandardDeviation(diffWindow); Log("diff: " + diff.ToString()); Log("std: " + std.ToString()); var close = (double)data[pair1].Close; if(!Portfolio.Invested){ if(close < bb.LowerBand){ SetHoldings(pair1, 10); IsLong = true; } else if (close > bb.UpperBand ){ SetHoldings(pair1, -10); IsLong = false; } } else{ // Average criss cross if(IsLong && close > sm + 0.5* std) Liquidate(); else if (!IsLong && close < sm - 0.5*std) Liquidate(); } } } }