This ProBuilder code snippet demonstrates how to partially close a trading position when a specified profit percentage is achieved. The example provided will sell 10% of the position after a 10% increase in price from the entry point.
ONCE partialclose = 1
ONCE PerCent = 0.1 //10% of position to close
ONCE PerCentGain = 0.1 //10% increase
ONCE MinLotSize = 0.5 //0.5 lots minimum
ExitQuantity = abs(CountOfPosition) * PerCent
LeftQty = max(MinLotSize, abs(CountOfPosition) - ExitQuantity)
CloseQuantity = abs(CountOfPosition) - LeftQty
IF Not OnMarket THEN
Flag = 1
ENDIF
IF partialclose AND LongOnMarket and close >= (PositionPrice * (1 + PerCentGain)) AND Flag THEN
SELL CloseQuantity Contracts AT Market
Flag = 0
endif
This code snippet is structured to handle the partial closure of a trading position based on a set profit target. Here’s a step-by-step breakdown:
ONCE keyword, ensuring they are set only once and retain their values throughout the execution.ExitQuantity is calculated as 10% of the current position size (CountOfPosition).LeftQty is calculated to ensure that the remaining quantity does not fall below the minimum lot size. It uses the max function to compare the minimum lot size with the remaining position after the partial closure.CloseQuantity is the actual amount to be closed, calculated by subtracting LeftQty from the absolute value of the current position size.SELL operation is triggered if all conditions are met: the strategy is set to allow partial closure, the position is long, the current close price is at least 10% higher than the position price, and the flag is set. After selling, the flag is reset to prevent repeated execution.This example is useful for understanding how to manage trading positions dynamically based on profit targets in ProBuilder language.
Check out this related content for more information:
https://www.prorealcode.com/topic/partial-close-with-porcentage/#post-167934
Visit Link