public override void Initialize() { var res = TimeSpan.FromHours(1); if (VOL_PROTECT) { AddEquity("VIXM", Resolution.Hour); } foreach (var s in symbols) { AddEquity(s, Resolution.Minute).SetLeverage(2); pfe[s] = new PolarisedFractalEfficiency(); Algo.RegisterIndicator(s, pfe[s], res); ppfe[s] = 0; eit[s] = new EhlerInstantaneousTrend(); Algo.RegisterIndicator(s, eit[s], res); pvalue[s] = 0; activate[s] = false; // if (riskProtectSymbols.Contains(s)) // { // prevPerf[s] = 1; // reference[s] = 1; // _pfeReference[s] = new PolarisedFractalEfficiency(); // } } }
public static bool Activate(string strategy, PolarisedFractalEfficiency indicator) { var day = indicator.Current.EndTime.Day; if (_pday != day) { _pday = day; if (!_values.ContainsKey(strategy)) { _values[strategy] = indicator.Current.Value; //_previousValues[strategy] = indicator; if (indicator > 0) { _activated.Add(strategy); return(true); } return(false); } else { var pv = _values[strategy]; var cv = indicator.Current.Value; if (cv > pv && cv > -50) { _activated.Add(strategy); return(true); } else if (pv > 75 && cv < 75 || cv < -50) { _activated.Remove(strategy); return(false); } _values[strategy] = cv; } } return(_activated.Contains(strategy)); }
public override void Initialize() { /*SetStartDate(2020, 1, 1); * if (LATEST_DEPLOY) SetStartDate(2021, 2, 6); */ //SetStartDate(2016, 1, 1); //SetEndDate(2021, 3, 15); //Set Start Date // SetStartDate(2011, 2, 1); // SetEndDate(2020, 1, 15); POS_COUNT = symbols.Count(); POS_COUNT -= (symbols.Contains("VXX")) ? 1: 0; POS_COUNT -= (symbols.Contains("VXX.1")) ? 1: 0; //CASH = Math.Max(10000*POS_COUNT, 20000);//Max for when testing VXX only //CASH = 145886; if (LATEST_DEPLOY) { CASH = 179860; } //SetCash(CASH); //Set Strategy Cash //SetStartDate(2018, 3, 1); //Set Start Date // if (symbols.Count() > 5) // { // //symbols.UnionWith(Universes.SP500); // symbols.UnionWith(Universes.NDX); // symbols.UnionWith(Universes.NDX99); // } //SetWarmup(TimeSpan.FromDays(stockPickingPeriod*4)); var res = TimeSpan.FromHours(1); if (VOL_PROTECT) { AddEquity("VIXM", Resolution.Hour); } foreach (var s in symbols) { AddEquity(s, Resolution.Minute).SetLeverage(2); pfe[s] = new PolarisedFractalEfficiency(); Algo.RegisterIndicator(s, pfe[s], res); ppfe[s] = 0; eit[s] = new EhlerInstantaneousTrend(); Algo.RegisterIndicator(s, eit[s], res); pvalue[s] = 0; activate[s] = false; // if (riskProtectSymbols.Contains(s)) // { // prevPerf[s] = 1; // reference[s] = 1; // _pfeReference[s] = new PolarisedFractalEfficiency(); // } } }
public void Update(QCAlgorithm algo) { if (algo.Time.Day == _previousDay) { return; } foreach (var a in _algos) { var perf = a.Algo.Portfolio.TotalPortfolioValue / _previousPnl[a.Algo.Name]; _previousPnl[a.Algo.Name] = a.Algo.Portfolio.TotalPortfolioValue; _dailyReturns[a.Algo.Name].Add(perf); } _previousDay = algo.Time.Day; //then we compute new weights valid for the period/day //we compute relative weights 2 by 2 and then take the average var pfeScores = new Dictionary <string, decimal>(); var processed = new HashSet <string>(); foreach (var a1 in _algos) { var name1 = a1.Algo.Name; pfeScores[name1] = 0; foreach (var a2 in _algos) { var name2 = a2.Algo.Name; if (name1 == name2 || processed.Contains(name2)) { continue; } //computes pfe for a1/a2 //sanity check var ret1 = _dailyReturns[name1]; var ret2 = _dailyReturns[name2]; if (ret2 != null && ret1 != null && (!ret1.IsReady || !ret2.IsReady || ret1.Count != ret2.Count)) { throw new Exception("why are we here?"); continue; } var pfe = new PolarisedFractalEfficiency(); DateTime dt = DateTime.Now; var p1 = 1m; var p2 = 1m; pfe.Update(new IndicatorDataPoint(dt, p1 / p2)); for (int i = 0; i < ret1.Count; i++) { p1 *= ret1.ElementAt(0); p2 *= ret2.ElementAt(0); pfe.Update(new IndicatorDataPoint(dt.AddDays(i + 1), p1 / p2)); } pfeScores[name1] += pfe + 100; pfeScores[name2] += -pfe + 100; } processed.Add(name1); _weights[name1] = Math.Max(pfeScores[name1], 0); } //now rescale the weights // note than when there is a subset if there are ties, they will all be included // but scaled properly in the weights so we should not trigger leverage breach from here var scalingFactor = (_subset == 0) ? _weights.Values.Sum() : _weights.Values.GroupBy(x => x) .OrderByDescending(x => x) .Take(_subset) .SelectMany(x => x) .Sum();; var threshold = -100m; if (_subset != 0) // then we set the threshold to be @ the subset level { threshold = _weights.Values.GroupBy(x => x) .OrderByDescending(x => x) .Skip(_subset) .Take(1) .SelectMany(x => x).Average(); } foreach (var key in _weights.Keys.ToList()) { var w = _weights[key]; if (w > threshold) { _weights[key] = w / scalingFactor; } else { _weights[key] = 0; } } }