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:
#### 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:
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.
Check out this related content for more information:
https://www.prorealcode.com/topic/position-size-management-performance-based-increases/#post-35351
Visit Link