Hi i was wondering how to create a code for money managment.
Eksample: you start with 500$ with one lot size. When money doubles it will double the lot size to 2 and then when you double again it will add 4 lot sizes.
It would be nice to have on the autotrading so i dont need to stop it to adjust my position.
Thanks for the help 🙂 Stay green
capital = 500
equity = capital + strategyprofit
once lastchange = capital
once positionsize = 1
if equity > lastchange * 2 then
positionsize = positionsize * 2
lastchange = lastchange * 2
else
if equity < lastchange / 2 then
positionsize = max(1,positionsize / 2)
lastchange = lastchange / 2
endif
endif
if (your conditions) then
buy positionsize contracts at market
endif
Not tested. This code also reduces the size if you lose. Delete lines 9 to 13 if you don’t want to do that.
Thanks i will try it out. Just one quick question. If im adding this to a strategy what do i den add in the (your conditions)?
Im not a good programer 🙁
A slightly better way to do it in my mind is like this:
capital = 500
positionsize = max(1,1 +(round((strategyprofit/capital)-0.5)))
This does not double the position size but increments or decrements it by 1 for every 500 won or lost by a strategy down to a minimum stake size of 1.
If im adding this to a strategy what do i den add in the (your conditions)?
Where ever you like above or below the money management code as long as it is below any DEFPARAM line.
I had a chance to test my first example and there is a minor issue that if your strategy profit jumps from 500 to 1500 then you will have to wait for two bars before the position size is doubled twice. This might not be an issue on a strategy that makes small wins but on a long term buy and hold strategy that takes big profits or big losses then it might be.