- ago
Hello guys.

I had a hard time breaking my head for this one and couldn't get anywhere.

I have a strategy that uses the code below - which you guys here on the forum have helped me to create (Eugene and Glitch):
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Linq; using System.Collections.Generic; namespace WealthLab_Strategies.Indicadores { public class RelativeStrenght : UserStrategyBase { private static List<BarHistory> _buys = new List<BarHistory>(); private static List<BarHistory> _RScalc = new List<BarHistory>(); private TimeSeries RSseries = new TimeSeries(); private int _candidates = 5; private int _candlesPeriod; private int qtySymbols; private double RSfinalPos; private double RS; public RelativeStrenght() { AddParameter("Candidates", ParameterType.Int32, 5, 1, 20, 1); AddParameter("Candles_Period", ParameterType.Int32, 21, 2, 100, 1); } public override void Initialize(BarHistory bars) { _candidates = Parameters.FindName("Candidates").AsInt; _candlesPeriod = Parameters.FindName("Candles_Period").AsInt; RSseries = (bars.Close / (bars.Close >> _candlesPeriod) + (bars.Close / (bars.Close >> (_candlesPeriod * 2)) ) + (bars.Close / (bars.Close >> (_candlesPeriod * 3)) )); bars.Cache[RSseries.Description] = RSseries; StartIndex = _candlesPeriod * 5; } public override void PreExecute(DateTime dt, List<BarHistory> participants) { if (participants.Count < 2) return; //Loop through the BarHistories of the participants, find the index that corresponds to the DateTime dt, //and save the indicator value to the bars.UserData for sorting foreach (BarHistory bars in participants) { int idx = GetCurrentIndex(bars); if (idx < _candlesPeriod * 5) { bars.UserData = -1.0e10; // change this to a negative value if you change the sort from higher to lower below continue; } //RS ranked top down bars.UserData = (bars.Cache[RSseries.Description] as TimeSeries)[idx]; } //this Sorts from lower to higher. participants.Sort((b, a) => a.UserDataAsDouble.CompareTo(b.UserDataAsDouble)); //Add the participants to calculate percentiles _RScalc.Clear(); _RScalc.AddRange(participants.GetRange(0, participants.Count)); //Add the participants with the highest formula score to the _buys list _buys.Clear(); _buys.AddRange(participants.GetRange(0, _candidates)); } public override void Execute(BarHistory bars, int idx) { qtySymbols = _RScalc.Count; RSfinalPos = _RScalc.FindIndex(x => x.UserDataAsDouble == RSseries[idx]); RS = (qtySymbols - RSfinalPos) * 100 / qtySymbols; { //some code here } } } }

I will try to explain my intention:

As seen in the code, I create two lists, one to rank the tokens ("_RScalc") and another to add the top ranked to my ranking ("_buys").

However, within the Execute structure, I do a simple calculation to calculate the percentile to which the symbol belongs.

I would like to use this code to plot the value of this percentile ("RS") for each of the symbols in the _buys list, all in the same panel.

I understand that some things probably need to be changed in the code to accomplish this.

How would it be possible to do this?

Thank you very much in advance.
0
387
0 Replies

Reply

Bookmark

Sort
Currently there are no replies yet. Please check back later.