Implementing Dynamic Position Sizing Based on Margin and Profit in ProBuilder

18 May 2020
0 comment
0 attachment

This ProBuilder script is designed to dynamically adjust trading position sizes based on the margin cost and accumulated strategy profits. It is particularly set up for trading the DOW index, considering different margin tiers as provided by a broker like IG. The script includes parameters to control the reinvestment of profits into new positions, which helps in managing risk and capital utilization effectively.

//Money Management DOW
MM = 1 // = 0 for optimization
if MM = 0 then
    positionsize=1
ENDIF

if MM = 1 then
    ONCE startpositionsize = .4
    ONCE factor = f // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etc
    ONCE factor2 = 20 // tier 2 factor
    ONCE margin = (close*.005) // tier 1 margin value of 1 contract in instrument currency; change decimal according to available leverage
    ONCE margin2 = (close*.01)// tier 2 margin value of 1 contract in instrument currency; change decimal according to available leverage
    ONCE tier1 = 55 // DOW €1 IG first tier margin limit
    ONCE maxpositionsize = 550 // DOW €1 IG tier 2 margin limit
    ONCE minpositionsize = .2 // enter minimum position allowed

    IF Not OnMarket THEN
        positionsize = startpositionsize + Strategyprofit/(factor*margin)
    ENDIF

    IF Not OnMarket THEN
        IF startpositionsize + Strategyprofit/(factor*margin) > tier1 then
            positionsize = (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor2*margin2)) + tier1 //incorporating tier 2 margin
        ENDIF

        IF startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THEN
            positionsize = minpositionsize //keeps positionsize from going below allowed minimum
        ENDIF

        IF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor2*margin2)) + tier1 > maxpositionsize then
            positionsize = maxpositionsize// keeps positionsize from going above IG tier 2 margin limit
        ENDIF
    ENDIF
ENDIF

The code snippet above manages the position size for trading based on the profit generated by the strategy and the margin requirements for different tiers. Here’s a breakdown of its functionality:

  • Initialization: Variables such as startpositionsize, factor, and margin values are set initially using the ONCE keyword, ensuring they are assigned only once.
  • Dynamic Position Sizing: The position size is adjusted based on the strategy’s profit and the defined margin. This calculation helps in reinvesting a portion of the profits back into new positions.
  • Handling Different Margin Tiers: The script checks if the calculated position size based on profits and initial margin exceeds the first tier limit. If so, it recalculates the position size considering the second tier’s margin requirements.
  • Position Size Limits: It ensures that the position size does not fall below a minimum threshold or exceed the maximum allowed size, thus maintaining compliance with trading limits and avoiding excessive risk.

This approach helps in optimizing the capital usage based on performance and market conditions, making it a practical tool for managing investments dynamically.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/reinvesting-full-value-of-portfolio-after-each-trade/#post-122308

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.
nonetheless Master
Currently debugging life, so my bio is on hold. Check back after the next commit for an update.
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...