close one particular position if its reach certain net profit
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
Replies
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
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
@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
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
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