This ProBuilder code snippet demonstrates how to calculate the position size for trading based on the daily volatility and predefined risk parameters. The calculation adheres to a risk management strategy inspired by Van Tharp’s principles, which focus on controlling risk to preserve capital.
equity=10000+3*STRATEGYPROFIT
risk=0.01
TF=4
DailyRange=max(abs(Dhigh(1)-Dlow(1)),max(abs(Dhigh(1)-Dclose(2)),abs(Dlow(1)-Dclose(2))))
DailyATR=wilderaverage[20*TF](DailyRange)
PositionSize = round((equity*risk)/(DailyATR*pointvalue))
Explanation of the Code:
- Equity Calculation: The variable equity is set to 10000 plus three times the strategy profit. This represents the total capital available for trading, adjusted for any profits or losses from the strategy.
- Risk Setting: The risk variable is set to 0.01, indicating that the trader is willing to risk 1% of their equity on a single trade.
- Time Frame Multiplier: TF is set to 4, which is used to adjust the period for calculating the Average True Range (ATR) to a specific time frame (in this case, possibly representing 4 hours if each period is an hour).
- Daily Range Calculation: DailyRange computes the maximum range of price movement within a day, considering the highest and lowest prices, as well as the previous day’s close.
- Average True Range (ATR): DailyATR calculates the exponential moving average (using Wilder’s method) of the Daily Range over a period adjusted by the time frame multiplier (20*TF).
- Position Size Calculation: Finally, PositionSize is calculated by dividing the product of equity and risk by the product of DailyATR and a point value (not defined in the snippet but typically represents the value of a minimum price movement). The result is then rounded to determine the number of units to trade.
This code snippet is a practical example of implementing risk management techniques in trading algorithms, ensuring that the position size is adjusted according to the current market volatility and the trader’s risk appetite.