Calculating Percentage Distance from TakeProfit Price to position.EntryPrice to do A Stop Jump
Calculating Percentage Distance from TakeProfit Price to position.EntryPrice to do A Stop Jump
26 Sep 2022, 09:22
So what I am trying to is to create a Stop Jump that once the price is getting closer to the Take Profit it will removed the take profit and move the Stop Lose closer by percentages in distance.
What do i mean by percentage in distance?
For example:
Parameneters
% Distance Trigger
% Distance Place StopJump
--
Position Entry Price = 20,000
Take Profit Price = 25,000
Distance = (Distance between EntryPrice and TakeProfit)
What I am trying to do is the following:
if the position is winning at "X Distance(in % from the Take Profit)", Remove take profit and place Stop Lose at "X Distance( in % from the Take Profit).
private void OnStopJumpPerPercentage()
{
var sellPositions = Positions.Where(iPosition => iPosition.TradeType == TradeType.Sell);
var buyPositions = Positions.Where(iPosition => iPosition.TradeType == TradeType.Buy);
double PercentageTrigger = StopJumpTrigger / 100;
double percentageDiference = StopJumpPercentage / 100;
double NewStopJumplaceBuy =0;
double NewStopJumplaceSell =0;
foreach (Position position in buyPositions)
{
double DistanceFRomEntryAndMarjetTPBuy = Math.Round((TakeProfitPriceBuy - position.EntryPrice) * Symbol.PipSize);
double CurrentDistanceFrom_TP_Price = Math.Round((TakeProfitPriceBuy - Symbol.Ask) * Symbol.PipSize);
double DistancePipTrigger = Math.Round((DistanceFRomEntryAndMarjetTPBuy * PercentageTrigger) * Symbol.PipSize);
if (CurrentDistanceFrom_TP_Price >= DistancePipTrigger)
ModifyPosition(position, position.StopLoss, NewStopJumplaceBuy);
}
foreach (Position position in sellPositions)
{
double DistanceFRomEntryAndMarjetTPSell = Math.Round((TakeProfitSell - position.EntryPrice) * Symbol.PipSize);
double CurrentDistanceFrom_TP_Price = Math.Round((TakeProfitSell - Symbol.Bid) * Symbol.PipSize);
double DistancePipTrigger = Math.Round((DistanceFRomEntryAndMarjetTPSell * PercentageTrigger) * Symbol.PipSize);
NewStopJumplaceSell = position.EntryPrice + (DistanceFRomEntryAndMarjetTPSell * percentageDiference) * Symbol.PipSize;
if (CurrentDistanceFrom_TP_Price >= DistancePipTrigger)
ModifyPosition(position, NewStopJumplaceSell);
}
}