COUNTOFPOSITION

Category: ProBacktest

COUNTOFPOSITION is a function in ProBuilder language used to determine the number of open shares or lots in the current trading position. It returns a positive value for long positions and a negative value for short positions. This function is particularly useful for strategies that need to monitor and control the size of positions dynamically.

Syntax:

COUNTOFPOSITION

Example:

myMACD = MACD[12,26,9](close)
short = myMACD crosses under 0
IF NOT ShortOnMarket AND short THEN
    SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
MAXSHARES = abs(COUNTOFPOSITION) <= 10
IF TRADEINDEX(1) > 5 AND TRADEPRICE(1) - Close > 10 * pointsize AND ShortOnMarket AND MAXSHARES THEN
    SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
SET STOP TRAILING 50

In this example, the MACD indicator is used to determine the entry point for a short position. The COUNTOFPOSITION function is then used to ensure that the total number of shares shorted does not exceed 10. Additional short orders are placed only if certain conditions are met, including a sufficient drop in price and a minimum number of bars since the last trade.

  • The MACD (Moving Average Convergence Divergence) is a trend-following momentum indicator that shows the relationship between two moving averages of a security’s price.
  • The function abs() is used to get the absolute value of COUNTOFPOSITION, which is necessary when checking the total number of shares regardless of whether the position is long or short.
  • TRADEINDEX(1) and TRADEPRICE(1) are functions used to access properties of the most recent trade, helping to implement conditions based on recent trading activity.

This function is essential for managing risk and ensuring that trading strategies do not exceed predefined exposure limits.

Related Instructions:

  • COUNTOFLONGSHARES probacktest
  • Logo Logo
    Loading...