remove TP & SL from positions

Created at 18 Jul 2018, 11:44
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

remove TP & SL from positions
18 Jul 2018, 11:44


i have and use heavy a cbot that does hedge me if iam allowing a too large drawdown, however the problem is, if a position has a TP set i can't leave as the hedge plus a position with a TP would be a massive expense if both scenarios are met.

 

is there a way to alter ALL positions in the same symbol and just remove all the TP and SL values ? 

the code is simple but i do not know how to work with lists in c# ..

 

maybe someone could give me a hint in how to implement that ?

                   if (positions.Sum(x => x.NetProfit) > -NetLoss)
                    {
                        continue;
                        var PosNet = (positions.Sum(x => x.NetProfit));
                        LogTextPositions = LogTextPositions + PosNet + " | ";
                    }

                    // calculates total buy and sell volume
                    var buyVolume = positions.Where(x => x.TradeType == TradeType.Buy).Sum(x => x.Volume);
                    var sellVolume = positions.Where(x => x.TradeType == TradeType.Sell).Sum(x => x.Volume);

                    // skips rest of the iteration if both volumes are equal
                    if (buyVolume == sellVolume)
                    {
                        continue;
                    }

                    // if buy volume is higher than sell volume
                    if (buyVolume > sellVolume)
                    {
                        // opens sell order equal to the difference of volumes
                        ExecuteMarketOrder(TradeType.Sell, GetSymbol(symbolCode), buyVolume - sellVolume, "Hedge (NetLoss)");
                        Notifications.PlaySound(Flocation + "swingfish-helper-hedge.mp3");
                    }
                    // else sell volume is higher than buy volume
                    else
                    {
                        // opens buy order equal to the difference of volumes
                        ExecuteMarketOrder(TradeType.Buy, GetSymbol(symbolCode), sellVolume - buyVolume, "Hedge (NetLoss)");
                        Notifications.PlaySound(Flocation + "swingfish-helper-hedge.mp3");
                    }

 


@swingfish
Replies

PanagiotisCharalampous
18 Jul 2018, 11:51

Hi swingfish,

You can use something like this

            foreach (var position in Positions)
            {
                if (position.SymbolCode == Symbol.Code)
                {
                    ModifyPosition(position, null, null);
                }
            }

Best Regards,

Panagiotis


@PanagiotisCharalampous

swingfish
18 Jul 2018, 15:09

RE:

Panagiotis Charalampous said:

Hi swingfish,

You can use something like this

            foreach (var position in Positions)
            {
                if (position.SymbolCode == Symbol.Code)
                {
                    ModifyPosition(position, null, null);
                }
            }

Best Regards,

Panagiotis

hey, i tried that lready but could not get it to work .. 

 

i have a init on the function start like this 

            var groups = Positions.GroupBy(x => x.SymbolCode).ToList();

            foreach (var positions in groups)
{
.... all hedge code

 

and to execute the hedge (one side) its like this:

                    if (buyVolume > sellVolume)
                    {
                        // opens sell order equal to the difference of volumes
                        ExecuteMarketOrder(TradeType.Sell, GetSymbol(symbolCode), buyVolume - sellVolume, "Hedge (NetLoss)");
                        Notifications.PlaySound(Flocation + "swingfish-helper-hedge.mp3");
                        foreach (var position in positions)
                        {
                            if (position.SymbolCode == Symbol.Code)
                            {
                                ModifyPosition(position, null, null);
                            }
                        }

                    }
 

i noticed "Positions" is different, but i did try it with Positions and positions .. both have no effect. the order is placed, sound plays but no position is being edited .. 

 


@swingfish

swingfish
18 Jul 2018, 15:24

oh never mind .. i figured it out 

since i use a group there is no need to make the the filter for the selected symbol.

 

thanks for your help ;) 

 

however do you have an idea on how i can get rid of the "nothing to change" error (happens if a position does not have TP or SL set


@swingfish