close one particular position if its reach certain net profit

Created at 22 Jul 2020, 15:35
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!
TA

tasr1r1

Joined 25.12.2012

close one particular position if its reach certain net profit
22 Jul 2020, 15:35


I am trying to develop new strategy with code which will close only specific opened position if its reach set net profit (variable volume )

in example, in this code i want to close the first buy position only and the formula to calculate net profit as per below.

 

 (Math.Round((firstbuy - Symbol.Bid) * Symbol.PipSize * FirstVolume)

 

firstbuy value was assigned from a nested if by this code:

  firstbuy = LastResult.Position.EntryPrice;

 

appreciate your help


@tasr1r1
Replies

PanagiotisCharalampous
22 Jul 2020, 15:43

Hi tasr1r1,

What help do you need exactly? Getting the position's net profit?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

tasr1r1
22 Jul 2020, 17:07

RE:

PanagiotisCharalampous said:

Hi tasr1r1,

What help do you need exactly? Getting the position's net profit?

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis

 

Yes, the full line code would be (using OnTick())

  if (Math.Round((firstbuy - Symbol.Bid) * Symbol.PipSize * FirstVolume) <= xvalue)

{

}

 

this is not  the same as to close all position if reach certain amount which i already have the code. This is only  to close one specific position if net profit  value is met.

for closing all position i am using this code

     var positions = this.Positions.FindAll(Label);
            double netProfit = positions.Sum(x => x.NetProfit);

                 if (Math.Round(netProfit) >= Math.Round(TargetMoney))
                 {
                     foreach (var position in positions)
                   {
                         ClosePosition(position);
          
                   }

}

 


@tasr1r1

PanagiotisCharalampous
23 Jul 2020, 08:29

Hi tasr1r1,

I cannot understand why are you doing calculations and not just use the position.NetProfit property.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

tasr1r1
23 Jul 2020, 09:27

RE:

PanagiotisCharalampous said:

Hi tasr1r1,

I cannot understand why are you doing calculations and not just use the position.NetProfit property.

Best Regards,

Panagiotis 

Join us on Telegram

Thats the code that i cant figure out :D

To get position.NetProfit property from specific position

 

When i looked around the forum, it was said it has to be calculated manually


@tasr1r1

GridSurfer
23 Jul 2020, 23:28

I assume this would work.

foreach (var position in positions)
{
	if (position.NetProfit >= TargetMoney)
		ClosePosition(position);         
}

 


@GridSurfer

tasr1r1
24 Jul 2020, 04:06

RE:

Thanks Gridsurfer!! will give it a try

 

GridSurfer said:

I assume this would work.

foreach (var position in positions)
{
	if (position.NetProfit >= TargetMoney)
		ClosePosition(position);         
}

 

 


@tasr1r1