How to Use NetProfit for all positions in "buy" or "sell" direction only

Created at 25 Nov 2019, 16:20
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!
MR

Mr4x

Joined 12.04.2019

How to Use NetProfit for all positions in "buy" or "sell" direction only
25 Nov 2019, 16:20


Hi team,

I'm looking at building a bot, but I don't have much experience with the NetProfit function. Basically I want something similar to the below code:

----

if (Account.Equity >= balance + Profit)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);

----

But instead of calculating the net profit of the whole account, I want to be able to calculate the netprofit of all positions open in one direction only (eg: only all the "buy" positions which are currently in positive in net profit) even if there are sell positions currently open at the same time.

I then want to be able to close only those positions, whilst leaving the trades in the opposite direction open.

Is this at all possible?

Many thanks for your suggestions,

Mr4x


@Mr4x
Replies

PanagiotisCharalampous
25 Nov 2019, 16:45

Hi Mr4x,

See below an example of how to check for Net Profit on sell positions only

            if (Account.Equity >= Account.Balance + Positions.Where(x => x.TradeType == TradeType.Sell).Sum(p => p.NetProfit))
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }

Best Regards,

Panagiotis


@PanagiotisCharalampous

Mr4x
25 Nov 2019, 17:03

Panagiotis Charalampous said:

Hi Mr4x,

See below an example of how to check for Net Profit on sell positions only

            if (Account.Equity >= Account.Balance + Positions.Where(x => x.TradeType == TradeType.Sell).Sum(p => p.NetProfit))
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }

Best Regards,

Panagiotis

Hi Panagiotis,

Thank you once again for that speedy reply. I just need one more piece to the puzzle and I think I have solved it. In the above code could you please give me an example of what it would look like if I wanted to close all sell positions if Net Profit of all sell positions was 150? I'm trying to see where in the code I would put that but can't for the life of me figure it out.

Many thanks,

Mr4x


@Mr4x

PanagiotisCharalampous
26 Nov 2019, 08:24

Hi Mr4x,

See below

            if (Positions.Where(x => x.TradeType == TradeType.Sell).Sum(p => p.NetProfit) > 150)
            {
                foreach (var position in Positions.Where(x => x.TradeType == TradeType.Sell))
                {
                    ClosePosition(position);
                }
            }

Best Regards,

Panagiotis


@PanagiotisCharalampous

Mr4x
26 Nov 2019, 09:02

Sir, thank you very much for your assistance! 

Mr4x


@Mr4x

... Deleted by UFO ...