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:
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.