Overall Statistics |
Total Trades 65382 Average Win 0.03% Average Loss -0.01% Compounding Annual Return 31.283% Drawdown 1.500% Expectancy 0.271 Net Profit 204.147% Sharpe Ratio 8.789 Loss Rate 58% Win Rate 42% Profit-Loss Ratio 2.06 Alpha 0 Beta 0 Annual Standard Deviation 0.031 Annual Variance 0.001 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $67670.17 |
using System; using System.Linq; using QuantConnect.Indicators; using QuantConnect.Models; using MathNet.Numerics; namespace QuantConnect.Algorithm.Examples { public class QCUMovingAverageCross : QCAlgorithm { private const string Symbol1 = "VXX"; private const string Symbol2 = "TVIX"; public int myTimeFrame = 60; public decimal mySlippageValue = 0.0002m; public StandardDeviation stdDev; public SimpleMovingAverage mySMA; public List<double> myDataList1 = new List<double>(); public List<double> myDataList2 = new List<double>(); public override void Initialize() { SetStartDate(2012, 06, 01); SetEndDate(2016, 07, 01); SetWarmup(TimeSpan.FromMinutes(myTimeFrame)); AddSecurity(SecurityType.Equity, Symbol1, Resolution.Minute); AddSecurity(SecurityType.Equity, Symbol2, Resolution.Minute); Securities[Symbol1].SlippageModel = new ConstantSlippageModel((decimal) mySlippageValue); Securities[Symbol2].SlippageModel = new ConstantSlippageModel((decimal) mySlippageValue); stdDev = new StandardDeviation(myTimeFrame); mySMA = new SimpleMovingAverage(myTimeFrame); } DateTime myDate; decimal myOpenPrice1; decimal myOpenPrice2; public void OnData(TradeBars data) { if(this.Time != myDate) { myDate = this.Time; myOpenPrice1 = data[Symbol1].Open; myOpenPrice2 = data[Symbol2].Open; } var myDifference = Math.Abs(((myOpenPrice1 - data[Symbol1].Close) / myOpenPrice1) - (.5m *((myOpenPrice2 - data[Symbol2].Close) / myOpenPrice2))); stdDev.Update(this.Time, myDifference); mySMA.Update(this.Time, myDifference); if (!stdDev.IsReady || !mySMA.IsReady) return; if (!Portfolio.Invested) { if (Math.Abs(myDifference) > ((2 * stdDev) + Math.Abs(mySMA))) { if(((myOpenPrice1 - data[Symbol1].Close) / myOpenPrice1) > (.5m *((myOpenPrice2 - data[Symbol2].Close) / myOpenPrice2))) { SetHoldings(Symbol1, .05); // + SetHoldings(Symbol2, -.1); } else { SetHoldings(Symbol1, -.05); SetHoldings(Symbol2, .1); // + } } } else { if (Math.Abs(myDifference) < ((0.5m * stdDev) + Math.Abs(mySMA))){Liquidate();} } } } }