Calculating Position Size Based on Volatility and Risk Management

22 Jan 2019
0 comment
0 attachment

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.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/dynamic-position-size/#post-53949

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.
Yannick Veteran
Martigny (before Vevey)
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...