Close first and last positions

Created at 03 Feb 2020, 13:22
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!
NE

netmstnet

Joined 09.01.2020

Close first and last positions
03 Feb 2020, 13:22


Good day,

How can i find and close the first an last openned positions?

Thank`s!


@netmstnet
Replies

PanagiotisCharalampous
03 Feb 2020, 14:02

Hi netmstnet,

You can use First() and Last() functions. See below an example

Positions.First().Close();

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

netmstnet
03 Feb 2020, 14:06

RE:

PanagiotisCharalampous said:

Hi netmstnet,

You can use First() and Last() functions. See below an example

Positions.First().Close();

Best Regards,

Panagiotis 

Join us on Telegram

 

Thank you, Panagiotis.


@netmstnet

netmstnet
03 Feb 2020, 14:24

RE:

PanagiotisCharalampous said:

Hi netmstnet,

You can use First() and Last() functions. See below an example

Positions.First().Close();

Best Regards,

Panagiotis 

Join us on Telegram

 

hm, i think need more help, here is the code:

// Close all buy positions if all buy positions' target profit is met
            if (Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == ThiscBotLabel) >= 2)
            {
               
                if (Positions.Where(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == ThiscBotLabel).Average(x => x.NetProfit) >= FirstVolume * AverageTakeProfit * Symbol.PipSize)
                {
                    foreach (var position in Positions)
                    {
                        if (position.TradeType == TradeType.Buy && position.SymbolName == SymbolName && position.Label == ThiscBotLabel)
                            ClosePosition(position);
                    }
                }
            }
            // Close all sell positions if all sell positions' target profit is met
            if (Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == ThiscBotLabel) >= 2)
            {
                if (Positions.Where(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == ThiscBotLabel).Average(x => x.NetProfit) >= FirstVolume * AverageTakeProfit * Symbol.PipSize)
                {
                    foreach (var position in Positions)
                    {
                        if (position.TradeType == TradeType.Sell && position.SymbolName == SymbolName && position.Label == ThiscBotLabel)
                            ClosePosition(position);
                    }
                }
            }

 

i need to select first and last positions, then calculate if  proift of this positions NetProfit > AverageTakeProfit, close first and last.

p.s. sorry my english


@netmstnet

PanagiotisCharalampous
03 Feb 2020, 14:29

Hi netmstnet,

You can try this condition

            if (Positions.First().NetProfit > AverageTakeProfit)
                Positions.First().Close();

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

netmstnet
03 Feb 2020, 15:20

RE:

PanagiotisCharalampous said:

Hi netmstnet,

You can try this condition

            if (Positions.First().NetProfit > AverageTakeProfit)
                Positions.First().Close();

Best Regards,

Panagiotis 

Join us on Telegram

You mean like this:

if ((Positions.First().NetProfit+ Positions.Last().NetProfit) > AverageTakeProfit)
                Positions.First().Close();
                Positions.Last().Close();

 


@netmstnet