Implementing Partial Closure of a Trading Position Based on Profit Percentage

27 Dec 2023
0 comment
0 attachment

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:

  • Initialization: Variables such as partialclose, PerCent, PerCentGain, and MinLotSize are initialized using the ONCE keyword, ensuring they are set only once and retain their values throughout the execution.
  • Calculating Exit Quantity: ExitQuantity is calculated as 10% of the current position size (CountOfPosition).
  • Ensuring Minimum Lot Size: 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.
  • Calculating Close Quantity: CloseQuantity is the actual amount to be closed, calculated by subtracting LeftQty from the absolute value of the current position size.
  • Market Entry Check: A flag is set if the strategy is not currently in the market, ensuring that operations are only performed when the strategy is active.
  • Conditional Selling: The 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.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/partial-close-with-porcentage/#post-167934

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
nonetheless Master
Code artist, my biography is a blank page waiting to be scripted. Imagine a bio so awesome it hasn't been coded yet.
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...