calculating working capital for money management
Forums › ProRealTime English forum › ProOrder support › calculating working capital for money management
- This topic has 24 replies, 3 voices, and was last updated 1 year ago by ZeroCafeine.
Tagged: capital, drawdown, equity, lotsize, management, margin, nlots, risk, size, strategyprofit
-
-
06/06/2021 at 10:26 AM #171240
I recently changed the Money Management code I had been using, mainly because it couldn’t account for the profit accrued when stopping and restarting an algo.
Looking at the existing snippets I could find, I opted for a minor variation of one by Vonasi:
1234567891011121314151617MM = 0 // 0 for optimizationif MM = 0 thenpositionsize=0.4ENDIFif MM thenCapital = ????MinSize = 0.2 // IG minimum position size allowedMaxSize = 550 // IG tier 2 margin limitProfitAccrued = 0 // when restarting strategy, enter profit or loss to dateEquity = Capital + ProfitAccrued + StrategyProfitPositionSize = Max(MinSize, Equity * (MinSize/Capital))if positionsize > MaxSize thenpositionsize = MaxSizeendifPositionSize = Round(PositionSize*100)PositionSize = PositionSize/100ENDIFThis is a nice piece of code and it works well, but as you can see, positionsize is based on the relation between capital and MinSize.
MinSize is a given, so what to enter as a value for Capital? It can’t just be the amount you happen to have in your account, as that could be any arbitrary amount – 5000, 10000, 50000 – and that would be the starting point for any positionsize increase: a 10% increment would occur only after a gain of 500, a gain of 1000 or a gain of 5000.
So how to calculate a useful figure for working capital, as it’s the only variable here with which to alter the speed of increase/decrease ?
My first thought was to use a factor of the margin cost, eg minsize margin * 15 ???
But it could also be related to the value of the stoploss ??
Or the max historical drawdown ??
Any thoughts on this greatly appreciated…
06/06/2021 at 11:02 AM #171243Well… I can post the one I am using (nLots is the Number of Lots to be traded):
123456789101112131415161718ONCE LotManagement = 1 //1=enable LOT size managementONCE DD = 825 //825 DrawDownONCE DDmultiplier = 3.0 //3.0 DD multiplierONCE MyMargin = 0.5 //0.5% margin required by the brokerONCE MyCapital = (DD * DDmultiplier) + (high * MyMargin / 100)IF IntraDayBarIndex = 0 THENMyCapital = (DD * DDmultiplier) + (high * MyMargin / 100)ENDIFONCE MinLots = 1ONCE InitialSize = 1ONCE nLots = InitialSizeIF LotManagement THENMyEquity = MyCapital + StrategyProfitMyInvestment = MyEquity / MyCapitalTempLot = InitialSize * MyInvestmentTempLot = round((TempLot * 10) - 0.5) / 10nLots = max(MinLots,TempLot)ENDIFIt is based on the DrawDown (write at line 2 the DrawDown showed with LotManagement=0, disabled) with a multipliying factor (DDmultiplier), sort of leverage or risk aversion factor (I always use 3) AND the margin % required by the broker.
06/06/2021 at 11:06 AM #171244At line 7, each new day, the required Capital is updated (considering the fluctuation of price) to affect calculations.
06/06/2021 at 11:20 AM #171245Thanks Roberto, that looks interesting – basing it on DD makes sense.
The only bit I don’t get is
1(high * MyMargin / 100)what does this achieve?
Also, could i add
1234ProfitAccrued = 0 // when restarting strategy, enter profit or loss to dateIF LotManagement THENMyEquity = MyCapital + ProfitAccrued + StrategyProfit06/06/2021 at 11:29 AM #171246Ok, so (high * MyMargin / 100) just removes the margin cost from your available funds – is that right?
06/06/2021 at 11:36 AM #171247another question: is StrategyProfit calculated in instrument currency or account currency?
The instruments I trade are either $ or € but my account is in GBP … so this would effect the margin calculation
06/06/2021 at 11:42 AM #171248Yes, it’s the margin calculation. I use HIGH, instead of CLOSE, to be more conservative.
Yes, you can either:
- use ProfitAccrued
- increase the InitialSize
1 user thanked author for this post.
06/06/2021 at 11:49 AM #171249STRATEGYPROFIT is in the currency of your account, while the margin is relative to the price of the instrument.
That’s a good point. We could change it to use an exchange rate. So if you trade Nikkei you could enter the GBP/JPY exchange rate. The drawback is that ProOrder won’t let you know what that rate is, so you will have to update it regularly (say each month).
06/06/2021 at 11:54 AM #171250Line 16 is to make sure there’s only 1 decimal. If you want to use 2, just replace 10 with 100, in both multiplication and division.
To use only integers replace 10 with 1 or simply remove both the multiplication and the division.
06/06/2021 at 12:02 PM #171253Out of interest, do you get 0.5% margin in Italy? I thought all of the EU was at 5% ?
My account is in Switzerland and the margin deposit is 0.5%
06/06/2021 at 12:03 PM #171254Sorry, STRATEGYPROFIT is returned according to the involved instrument.
This could be a solution (not tested):
1234567891011121314151617181920ONCE LotManagement = 1 //1=enable LOT size managementONCE DD = 825 //825 DrawDownONCE DDmultiplier = 3.0 //3.0 DD multiplierONCE MyMargin = 0.5 //0.5% margin required by the brokerONCE ExchangeRate = 1ONCE ProfitAccrued = 0 * ExchangeRateONCE MyCapital = ((DD * DDmultiplier) + (high * MyMargin / 100)) * ExchangeRateIF IntraDayBarIndex = 0 THENMyCapital = ((DD * DDmultiplier) + (high * MyMargin / 100)) * ExchangeRateENDIFONCE MinLots = 1ONCE InitialSize = 1ONCE nLots = InitialSizeIF LotManagement THENMyEquity = MyCapital + ProfitAccrued + StrategyProfitMyInvestment = MyEquity / MyCapitalTempLot = InitialSize * MyInvestmentTempLot = round((TempLot * 10) - 0.5) / 10nLots = max(MinLots,TempLot)ENDIF06/06/2021 at 12:06 PM #171255I’m not sure lines 7 and 9 require ExchangeRate.
I will test it asap.06/06/2021 at 12:53 PM #171256If you use the Max Drawdown from a backtest (without MM) then that figure will be in instrument currency, so yes I think lines 7 and 9 would need modification for ExchangeRate.
But ProfitAccrued would be in account currency, so I would leave it off there.
Also, if you were going to do a backtest with LotManagement = 1 (not for optimization but just out of curiosity) then you would need to add a limit for MaxSize, otherwise it can go up insanely high…
06/06/2021 at 1:18 PM #171257(high * Margin / 100) would be the margin value of 1 contract, no? … so wouldn’t a proper allowance for margin cost have to be
(nLots*(high * Margin / 100))
??
06/06/2021 at 2:16 PM #171260No, the calculation of lots is at lines 16-17.
-
AuthorPosts
Find exclusive trading pro-tools on