Calculate profit in account dollars instead of pips

Created at 11 Nov 2023, 06:02
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

Calculate profit in account dollars instead of pips
11 Nov 2023, 06:02


Hi,

I was wondering if somebody could help me make adjustments to my code. Currently it is set to close all positions when there is x amount of profit in pips. The problem is that the profit in dollars wildly differs from symbol to symbol so I'm hoping to make some small tweaks to change it to close all positions when profit, for that symbol only (not total account profit), reaches x amount in dollars.

See code below:

 if (Positions.Where(x => x.SymbolName == SymbolName && x.Label == ThiscBotLabel).Average(x => x.NetProfit) >= FirstVolume * AverageTakeProfit * Symbol.PipSize)
                {
                    foreach (var position in Positions)
                    {
                        if (position.SymbolName == SymbolName && position.Label == ThiscBotLabel)
                            ClosePositionAsync(position);

 

Many thanks


@Mr4x
Replies

PanagiotisCharalampous
11 Nov 2023, 07:09

Hi there

Try this

 if (Positions.Where(x => x.SymbolName == SymbolName && x.Label == ThiscBotLabel).Average(x => x.NetProfit) >= AmountInProfit)

 


@PanagiotisCharalampous

Mr4x
11 Nov 2023, 07:42

RE: Calculate profit in account dollars instead of pips

PanagiotisCharalampous said: 

Hi there

Try this

 if (Positions.Where(x => x.SymbolName == SymbolName && x.Label == ThiscBotLabel).Average(x => x.NetProfit) >= AmountInProfit)

 

Thank you for your quick reply Panagiotis. Unfortunately when I put that in, all positions close at random dollar figures. For example if I put in “2” as a whole number - then positions are closed anywhere from $2 to $22 in profit in my base currency.

Is there any function to normalise and make sure that all positions are closed at (or close enough to) the dollar figure that is put as the “AmountInProfit”?

Thanks 


@Mr4x

PanagiotisCharalampous
12 Nov 2023, 06:17

Hi there,

It's because it takes the average. If you want to close the position whenever the specific position reaches above that amount, the use this

 if (Positions.Where(x => x.SymbolName == SymbolName && x.Label == ThiscBotLabel && x.NetProfit > AmountInProfit))

@PanagiotisCharalampous

Mr4x
12 Nov 2023, 07:04

RE: Calculate profit in account dollars instead of pips

PanagiotisCharalampous said: 

Hi there,

It's because it takes the average. If you want to close the position whenever the specific position reaches above that amount, the use this

 if (Positions.Where(x => x.SymbolName == SymbolName && x.Label == ThiscBotLabel && x.NetProfit > AmountInProfit))

Thank you again for your reply. I tried that exact code but I get the following build failed error:

Error CS0029: Cannot implicitly convert type ‘System.Collections.Generic.IEnumerable<cAlgo.API.Position>’ to 'bool'


@Mr4x

PanagiotisCharalampous
13 Nov 2023, 06:28 ( Updated at: 13 Nov 2023, 06:30 )

RE: Calculate profit in account dollars instead of pips

PanagiotisCharalampous said: 

Hi there,

It's because it takes the average. If you want to close the position whenever the specific position reaches above that amount, the use this

 if (Positions.Where(x => x.SymbolName == SymbolName && x.Label == ThiscBotLabel && x.NetProfit > AmountInProfit))

Hi there,

Remove the if statement and use the below loop instead

                    foreach (var position in Positions.Where(x => x.SymbolName == SymbolName && x.Label == ThiscBotLabel && x.NetProfit > AmountInProfit))
                                   				       	                {
                                   				       	                ClosePositionAsync(position);
                                   				       	                }     

@PanagiotisCharalampous