Following on from an idea that I had in a discussion elsewhere I have coded this modification of one of my (I think it was mine!) money management systems. This version now automates the switch from one money management type to another after a certain number of trades has occurred and the strategy has reached a certain level of profit. This means for example that you can start trading without money management and then once 100 trades has happened and the strategy has reached 5% profit then money management can be turned on and position size increased based on your equity.
If you don’t want to switch then set both money management types to the same value.
I have just written this and have not tested it as time is short today. I think it should work but if someone else wants to test it for me that would be great as I rarely code anything correct first time! I just had to get this idea started while I ate my breakfast!
Once MoneyManagement = 0 //Set to 0 for level stakes. Set to 1 for increasing stake size as profits increase and decreasing stake size as profits decrease. Set to 2 for increasing stake size as profits increase with stake size never being decreased.
Capital = 10000
MinSize = 1 //The minimum position size allowed for the instrument.
MM2ndType = 1 //Type of money management to switch to after TradesQtyForSwitch number of trades has occurred
TradesQtyForSwitch = 100 //Quantity of trades required before switching to second money management choice.
ProfitNeededForSwitch = 5 //% profit needed before allowing a money management type change.
Equity = Capital + StrategyProfit
if not EnoughTrades then
if abs(countofposition) > abs(countofposition[1]) then
tradecount = tradecount + 1
endif
if tradecount > TradesQtyForSwitch and Equity >= 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
How about a further mod … turn the MM back off if the last * Trades made * per cent Loss?
See here for a > £3k difference due to your code above Vonasi!
Nice one, thank you!
https://www.prorealcode.com/topic/dow-2h-trend-chaser-v4-mm/#post-116508
How about a further mod … turn the MM back off if the last * Trades made * per cent Loss?
Keeping a track of the last x trades would really require arrays to store the trade data. Without them we would really only be able to code for a very limited number of trades and it would all be a bit clunky as coding goes!
Another option is to record the highest equity and switch off money management if equity then falls x% below this. So if draw down is higher than expected then we can return to minimum stake size. What do you think?
Here is the idea in my last post.
It works the same as the first version for switching money management types after x trades and x% profit has been reached. It also records the highest equity and if draw down reaches a set level then it switched the money management back to your chosen type 1. At this point the trade count is reset to zero. Then if the strategy starts performing well again then once there has been enough trades and profit has risen the desired level above the highest equity then the money management type is changed back to your chosen type 2.
I also added a function to stop the strategy if a maximum draw down level has occurred. This is draw down from the maximum equity achieved. Only closed positions are considered.
(not tested much!)
Capital = 10000
MinSize = 1 //The minimum position size allowed for the instrument.
MM1stType = 0 //Starting type of moneymanagement. Set to 0 for level stakes. Set to 1 for increasing stake size as profits increase and decreasing stake size as profits decrease. Set to 2 for increasing stake size as profits increase with stake size never being decreased.
MM2ndType = 1 //Type of money management to switch to after TradesQtyForSwitch number of trades and ProfitNeededForSwitch profit has occurred
TradesQtyForSwitch = 100 //Quantity of trades required before switching to second money management choice.
ProfitNeededForSwitch = 5 //% profit needed before allowing a money management type change to MM2ndType.
DrawdownNeededToSwitch = 10 //% draw down from max equity needed before money management type is changed back to MM1stType.
DrawdownNeededToQuit = 50 //% draw down from max equity needed to stop strategy
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
Above added as Log 194 here …
Snippet Link Library
After a little testing I spotted a cut and paste error that stopped the money management working with position sizes less than 1. I’ve corrected the code in the previous posts.
I’ve had the idea that switching the money management on or back on would be best done after a rise from a lowest equity. So for example we have a 5% draw down and so money management is switched off and we trade just level stakes at minimum position size but the strategy continues to perform badly and equity continues to drop. Eventually it rises from a low point by 10% and so we switch money management back on.
It’s too late now to code it so I’ll try to find some time tomorrow or add it to my list!
In the first code.
As i do so that the máximum position Is 10€ pip And continúe trading with the figure.
And if someOne can explain The “switches”, or i dont know, ir they dont work.
Thanks!!!