Implementing a Dynamic Money Management and Quit System Based on Portfolio Drawdown

03 Apr 2022
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to implement a dynamic money management strategy that adjusts based on trading performance and a system quit mechanism triggered by a specified percentage drawdown from the maximum equity. The code is designed to switch between different money management types and halt trading if the equity falls significantly.


Capital = 10000
MinSize = 1
MM1stType = 0
MM2ndType = 1
TradesQtyForSwitch = 50
ProfitNeededForSwitch = 3
DrawdownNeededToSwitch = 10
DrawdownNeededToQuit = 35
Once MoneyManagement = MM1stType
Equity = Capital + StrategyProfit
maxequity = max(equity,maxequity)
if equity < maxequity * (1 - (DrawdownNeededToSwitch/100)) then
    enoughtrades = 0
    tradecount = 0
    moneymanagement = MM1stType
endif
if equity < maxequity * (1 - (DrawdownNeededToQuit/100)) then
    quit
endif
if not EnoughTrades then
    if abs(countofposition) > abs(countofposition[1]) then
        tradecount = tradecount + 1
    endif
    if tradecount > TradesQtyForSwitch and maxequity >= Capital * (1 + (ProfitNeededForSwitch/100)) then
        EnoughTrades = 1
        MoneyManagement = MM2ndType
    endif
endif
IF MoneyManagement = 1 THEN
    PositionSize = Max(MinSize, Equity * (MinSize/Capital))
ENDIF
IF MoneyManagement = 2 THEN
    PositionSize = Max(LastSize, Equity * (MinSize/Capital))
    LastSize = PositionSize
ENDIF
IF MoneyManagement <> 1 and MoneyManagement <> 2 THEN
    PositionSize = MinSize
ENDIF
PositionSize = Round(PositionSize*100)
PositionSize = PositionSize/100
PositionSizeLong = 1 * positionsize
PositionSizeShort = 1 * positionsize

The code snippet above is structured to manage trading risk and adjust position sizes based on the performance of the trading strategy. Here’s a step-by-step explanation:

  • Initialization of Variables: Sets initial capital, minimum position size, and types of money management strategies.
  • Dynamic Equity Calculation: Updates the equity based on capital and strategy profits and tracks the maximum equity reached.
  • Money Management Switch: If the equity drops below a certain percentage from the maximum equity, it switches back to the initial money management type.
  • Quit Condition: Stops the trading strategy if the equity falls by a specified percentage from the maximum equity, indicating a significant drawdown.
  • Trade Counting: Counts the number of trades to determine when enough trades have been made to consider switching to a more aggressive money management type.
  • Position Size Adjustment: Adjusts the position size based on the current money management strategy, ensuring it does not fall below the minimum size.

This approach helps in managing trading risk by dynamically adjusting the investment size and provides a mechanism to stop trading to prevent further losses in a significant drawdown scenario.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/code-to-close-all-trades-based-on-equity-or-portfolio-value/#post-191102

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
I usually let my code do the talking, which explains why my bio is as empty as a newly created file. Bio to be initialized...
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...