COUNTOFSHORTSHARES is a function in ProBuilder language used to count the number of open short shares or lots in the trading system. This function is particularly useful for strategies that involve multiple short entries and need to track the number of active short positions to manage risk and comply with strategy constraints.
COUNTOFSHORTSHARES
// Define a MACD indicator
myMACD = MACD[12,26,9](close)
// Condition to enter a short position
short = myMACD crosses under 0
// Enter the first short position if no short positions are currently open
IF NOT ShortOnMarket AND short THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
// Check if the number of short shares is less than 5
MAXSHARES = COUNTOFSHORTSHARES < 5
// Add another short order if certain conditions are met
IF TRADEINDEX(1) > 5 AND TRADEPRICE(1) - Close > 10 AND ShortOnMarket AND MAXSHARES THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
// Set a trailing stop
SET STOP TRAILING 50
This example demonstrates how to use the COUNTOFSHORTSHARES function within a trading strategy that involves short selling based on the MACD indicator. The function helps to ensure that the number of short positions does not exceed a specified limit, in this case, 5 positions. It is used to conditionally execute further trades based on the current market position and the strategy’s rules.
This function is essential for managing the scale of exposure in short trading scenarios and can help in automating trading strategies while adhering to predefined risk management rules.