Overall Statistics |
Total Trades 1518 Average Win 1.51% Average Loss -0.57% Compounding Annual Return 72.046% Drawdown 15.200% Expectancy 0.331 Net Profit 1408.893% Sharpe Ratio 2.884 Probabilistic Sharpe Ratio 99.465% Loss Rate 63% Win Rate 37% Profit-Loss Ratio 2.65 Alpha 0.782 Beta 0.03 Annual Standard Deviation 0.273 Annual Variance 0.075 Information Ratio 1.811 Tracking Error 0.328 Treynor Ratio 26.532 Total Fees $3340.39 Estimated Strategy Capacity $45000000.00 Lowest Capacity Asset AMZN R735QTJ8XC9X |
//Copyright HardingSoftware.com, 2021. Released to the public domain. //Use entirely at your own risk. //This algorithm contains open source code from other sources, //no claim is being made to such code. //Do not remove this copyright notice. using System; using System.Collections.Generic; using System.Linq; using QuantConnect.Data.Market; using QuantConnect.Data.UniverseSelection; namespace QuantConnect.Algorithm.CSharp { public class TrendSingle : QCAlgorithm { string ticker = "AMZN"; Symbol symbol; Resolution resolution=Resolution.Hour; int period = 10; decimal exponent = 1.5m; List<decimal> prices = new List<decimal>(); decimal trend = 0; public override void Initialize() { UniverseSettings.Resolution = resolution; SetStartDate(2016, 6, 10); //SetEndDate(2017, 8, 30); SetCash(100000); symbol = AddEquity(ticker, resolution).Symbol; } public void OnData(TradeBars data) { if (data.ContainsKey(symbol)) { TradeBar bar=data[symbol]; prices.Add(bar.Close); if (prices.Count > period) { prices.RemoveAt(0); } if (prices.Count == period) { trend = Trend(prices.ToArray(), exponent); } } if (trend > 0 && Portfolio[symbol].Quantity <= 0) { SetHoldings(symbol, 0.99m); } else if (trend < 0 && Portfolio[symbol].Quantity >= 0) { SetHoldings(symbol, -0.99m); } } public static decimal Trend(decimal[] values, decimal exponent) { decimal[] changes = ChangesPercent(values); return ExponentialMovingAverage(changes, exponent); } public static decimal[] ChangesPercent(decimal[] values) { List<decimal> lOut = new List<decimal>(); for (int i = 0; i < values.Length - 1; i++) { lOut.Add((values[i + 1] - values[i]) / values[i]); } return lOut.ToArray(); } public static decimal[] ExponentialWeights(int length, decimal exponent) { List<decimal> weights = new List<decimal>(); double e = Convert.ToDouble(exponent); for (int i=0;i<length;i++) { double w = (double)(i + 1); weights.Add(Convert.ToDecimal(Math.Pow(w, e))); } return weights.ToArray(); } public static decimal ExponentialMovingAverage(decimal[] values, decimal exponent) { return WeightedAverage(values, ExponentialWeights(values.Length, exponent)); } public static decimal WeightedAverage(decimal[] values, decimal[] weights) { return values.Zip(weights, (x, y) => x * y).Sum() / weights.Sum(); } } }