Dynamic Position Size Adjustment Based on Performance Metrics

03 Jun 2020
0 comment
0 attachment

This code snippet demonstrates how to dynamically adjust the position size in a trading strategy based on performance metrics such as consecutive wins and periodic profit reviews. The code is written in ProBuilder language and is structured to increase or decrease the risk level according to the recent performance of the strategy.

#### Code for Average Consecutive Winning Streak based risk increment:


once maxPositionSize = 10
once positionSize = 1
// Average Consecutive Wins
once countWins = 0
once aveConsecWins = 0
once countWinStreaks = 0
tradeJustClosed = (longOnMarket[1] and not longOnMarket) or (shortOnMarket[1] and not shortOnMarket)
lastTradeWon = positionPerf(1) > 0
if tradeJustClosed and lastTradeWon then
    countWins = countWins + 1 // counting the number of wins in this streak
elsif tradeJustClosed and not lastTradeWon then
    if countWins >= 2 then // less than 2 is not a streak
        countWinStreaks = countWinStreaks + 1 // increase count of winning streaks
        totalWins = totalWins + countWins // total number of wins so far
        aveConsecWins = totalWins / countWinStreaks // average consecutive wins
    endif
    if countWins > aveConsecWins then
        positionSize = min(maxPositionSize, positionSize + 1) // increase risk
    endif
    countWins = 0 // we lost - reset number of wins to zero
endif
graph aveConsecWins coloured (10, 200, 10) as "ave consec wins"
graph countWins

#### Explanation:

  • Initialization: Sets up initial values for maximum position size, current position size, and counters for wins and winning streaks.
  • Trade Closure Detection: Checks if a trade has just closed, either a long or a short position.
  • Win Counting: If the last trade was a win, it increments the win counter for the current streak.
  • Streak Calculation: If a trade loss occurs, it checks if the current streak qualifies as a streak (2 or more wins), updates the average wins per streak, and resets the win counter.
  • Risk Adjustment: If the current win streak exceeds the average, it increases the position size, capped at the maximum specified.
  • Graphing: Displays the average consecutive wins and the current win count on a graph for visual analysis.

#### Code for Daily, Weekly, or Monthly Performance Review:


// Position size management
// -- Settings --
once posReviewMode = 2 // 1: weekly | 2: monthly
once maxPositionSize = 10
if usePosManagement then
    once plAtLastMonth = 0
    newWeek = DayOfWeek <> DayOfWeek[1] and DayOfWeek=1
    newMonth = month <> month[1]
    doPosReview = (posReviewMode = 1 and newWeek) or (posReviewMode = 2 and newMonth)
    if doPosReview then
        if strategyProfit > plAtLastMonth + 1 then
            positionSize = min(maxPositionSize, positionSize + 1) // increase risk
        else
            positionSize = max(1, positionSize-1) // decrease risk
        endif
        plAtLastMonth = strategyProfit
    endif
endif

#### Explanation:

  • Settings: Configures the review mode (weekly or monthly) and sets the maximum position size.
  • Performance Review Trigger: Determines if it’s time for a performance review based on the current date compared to the last review date.
  • Risk Adjustment: Adjusts the position size based on the comparison of current strategy profit to the profit at the last review period.
  • Profit Tracking: Updates the profit tracking variable to the current strategy profit after each review.

These snippets provide a practical approach to managing risk in automated trading systems by adjusting position sizes based on the strategy’s performance, promoting a dynamic and responsive trading system.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/position-size-management-performance-based-increases/#post-35351

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.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
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...