Overall Statistics |
Total Trades 10001 Average Win 0.03% Average Loss -0.01% Compounding Annual Return 55.470% Drawdown 1.200% Expectancy 0.677 Net Profit 36.842% Sharpe Ratio 9.172 Loss Rate 62% Win Rate 38% Profit-Loss Ratio 3.38 Alpha 0.36 Beta 0.002 Annual Standard Deviation 0.039 Annual Variance 0.002 Information Ratio 1.488 Tracking Error 0.124 Treynor Ratio 145.1 Total Fees $10001.00 |
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();} } } } }