COUNTOFLONGSHARES is a function in ProBuilder language used to count the number of open long positions (shares or lots) in a trading strategy. This function is particularly useful for strategies that need to monitor and control the number of active long trades to manage risk and comply with trading limits.
COUNTOFLONGSHARES
myMACD = MACD[12,26,9](close)
long = myMACD crosses over 0
// Initiate first order
IF NOT LongOnMarket AND long THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Check if the number of long shares is less than or equal to 3
MAXSHARES = COUNTOFLONGSHARES <= 3
// Place another order if conditions are met
IF TRADEINDEX(1) > 5 AND Close - TRADEPRICE(1) > 10 AND LongOnMarket AND MAXSHARES THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
SET STOP TRAILING 50
In this example, the COUNTOFLONGSHARES function is used to ensure that the number of long contracts does not exceed three. The strategy first checks if there is no long position in the market and if the MACD indicator crosses over zero, which is a signal to go long. If these conditions are met, it buys one contract. It then uses the COUNTOFLONGSHARES to check if additional orders can be placed based on the number of existing long positions and other conditions like price movement and time elapsed since the last trade.
This function is straightforward but essential for implementing risk management and position sizing in automated trading strategies.