exclude results from first loop

Created at 19 Mar 2024, 14:04
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
swingfish's avatar

swingfish

Joined 25.06.2016

exclude results from first loop
19 Mar 2024, 14:04


i have the following piece of code which works just fine.

how can i exclude catches in the second loop if the symbol is already present in the first one?

basically not show lomit orders if a active position is already open on the same symbol.

                var positions = new Dictionary<string, double>();
                var sb = new StringBuilder();

                foreach (var position in Positions)
                {
                    if (_ignoredSymbols.Contains(position.SymbolName))
                        continue;

                    var symbol = _symbolNameMap.ContainsKey(position.SymbolName) ? _symbolNameMap[position.SymbolName] : position.SymbolName;
                    var volume = position.VolumeInUnits * (position.TradeType == TradeType.Buy ? 1 : -1);
                    double pips = position.Pips;

                    if (positions.ContainsKey(symbol))
                    {
                        positions[symbol] += volume;
                    }
                    else
                    {
                        positions.Add(symbol, volume);
                    }
                }

                foreach (var position in positions)
                {
                    if (position.Value > 0)
                    {
                        sb.AppendLine("Buy " + position.Key);
                    }
                    else if (position.Value < 0)
                    {
//                        sb.AppendLine("Sell " + position.Key + " ("+position.Value+")" );
                        sb.AppendLine("Sell " + position.Key );
                    }
                    else
                    {
                        sb.AppendLine(position.Key + " Hedged");
                    }
                }

                
                // Limit orders
                var Limits = new Dictionary<string, double>();
//                var sb = new StringBuilder();

                foreach (var position in PendingOrders)
                {
                    if (_ignoredSymbols.Contains(position.SymbolName))
                        continue;

                    var symbol = _symbolNameMap.ContainsKey(position.SymbolName) ? _symbolNameMap[position.SymbolName] : position.SymbolName;
                    var volume = position.VolumeInUnits * (position.TradeType == TradeType.Buy ? 1 : -1);
                    //double pips = position.Pips;

                    if (positions.ContainsKey(symbol))
                    {
                        positions[symbol] += volume;
                    }
                    else
                    {
                        positions.Add(symbol, volume);
                    }
                }

                foreach (var position in positions)
                {
                    if (position.Value > 0)
                    {
                        sb.AppendLine("BuyLimit " + position.Key);
                    }
                    else if (position.Value < 0)
                    {
//                        sb.AppendLine("Sell " + position.Key + " ("+position.Value+")" );
                        sb.AppendLine("SellLimit " + position.Key );
                    }
                    else
                    {
                        sb.AppendLine(position.Key + " HedgedLimits");
                    }
  }


@swingfish
Replies

firemyst
23 Mar 2024, 03:24 ( Updated at: 23 Mar 2024, 05:34 )

You need to be more clear in your post.

You say “how can i exclude catches in the second loop if the symbol is already present in the first one?” when you have 4 loops in your example code.

Then you say, “not show lomit orders if a active position is already open on the same symbol.” when your sample code for limit orders is the 3rd loop shown in the code?

If I understand what I think you're trying to do, why not just:

if (Positions.Count(x => x.SymbolName == theSymbolName) == 0)
{
    //didn't find it in current open positions, so add it
    if (!Limits.ContainsKey(theSymbolName)
        Limits.Add(theSymbolName,0);
}

@firemyst