I am trying to add subsequent coding so that I only have 3 participants and have that working correctly. However I am getting multiple positions and I only want single positions in the same symbol. How do I modify the following code so that there are no duplicates in the Buy List?
CODE:
public override void PreExecute(DateTime dt, List<BarHistory> participants) { // rank the candidates by rsi foreach (BarHistory bh in participants) { IndicatorBase symbolRsi = (IndicatorBase)bh.Cache[seriesKey]; int idx = GetCurrentIndex(bh); //this returns the index of the BarHistory for the bar currently being processed double rsiVal = symbolRsi[idx]; //if the indicator isn't valid set a high value to avoid selection in rotation if (idx < symbolRsi.FirstValidIndex) rsiVal = 1.0e6; bh.UserData = rsiVal; //save the current Rsi value along with the BarHistory instance } //sort the participants by Rsi value (lowest to highest) participants.Sort((a, b) => a.UserDataAsDouble.CompareTo(b.UserDataAsDouble)); //keep the top 3 symbols buys.Clear(); for (int n = 0; n < 3; n++) { if (n >= participants.Count) break; buys.Add(participants[n]); } } //execute the strategy rules here, this is executed once for each bar in the backtest history
Rename
Try setting "Max Open Pos Per Symbol" to 1 in Strategy Settings.
Don't add the symbol's BarHistory to the buy list if it's already an Open Position.
CODE:
buys.Clear(); for (int n = 0; n < 3; n++) { if (n >= participants.Count) break; bool addit = true; foreach (Position p in OpenPositionsAllSymbols) { if (p.Symbol == participants[n].Symbol) { addit = false; break; } } if (addit) buys.Add(participants[n]); }
Your Response
Post
Edit Post
Login is required