Reinvesting full value of portfolio after each trade
Forums › ProRealTime English forum › ProOrder support › Reinvesting full value of portfolio after each trade
- This topic has 11 replies, 2 voices, and was last updated 3 years ago by
SkippyTrader.
-
-
03/15/2020 at 10:20 PM #122206
Hello everyone!
I have been playing around with the following code in regards to reinvesting for my tradingsystem (I found the code somewhere here on the forum):
1231 Capital = 100002 Equity = Capital + StrategyProfit3 Position = Max(1,Equity/Capital)And when for orders:
1211 buy Position contract at x stop12 sellshort Position contract at x stopHowever I have a problem, when I enter starting capital to lets say 10000 I get a return of 12000 for my system
And when I enter capital as 100000 I still get the return of 2000 (102000)
For the record I also change the sum “initial capital” to the right of the code itself
Maybe Im barking up the wrong tree here but Im looking for code that always reinvests 100% (or as close as possible to 100%) of my portfolio value at each trade..
Does anyone know where I have gone wrong and/or which adjustments I could try?
Thank you in advance!
03/15/2020 at 10:47 PM #12220703/16/2020 at 9:54 AM #12222003/16/2020 at 10:09 AM #122221You can’t invest 100% of your money because you need to keep some to cover margin otherwise you will get a margin call.
To calculate position size for 100% you would need to divide your equity by the price of what you are buying and buy that number of shares/contracts.
03/17/2020 at 9:47 AM #122308This is the Money Management i use. It’s based on the margin cost and is fully adjustable as to how much profit you want to reinvest. This is set up for the DOW and it works up to the IG 2nd margin tier, should you be so lucky as to ever get up there.
If you set your startpositionsize based on how much of your working capital you want to use, then this will handle the strategyprofit part. You could conceivably reinvest 100% of strategyprofit but if your margin is 100% of total equity, you’ll get a margin call as soon as it moves against you. ‘Factor’ is the variable that controls the percentage of strategyprofit. It actually works as a divisor, so it’s an inverse relation — higher the factor, lower percentage reinvested.
You will also need to check what leverage is offered for the instrument you are trading and the max position for each tier.
12345678910111213141516171819202122232425262728293031//Money Management DOWMM = 1 // = 0 for optimizationif MM = 0 thenpositionsize=1ENDIFif MM = 1 thenONCE startpositionsize = .4ONCE factor = f // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etcONCE factor2 = 20 // tier 2 factorONCE margin = (close*.005) // tier 1 margin value of 1 contract in instrument currency; change decimal according to available leverageONCE margin2 = (close*.01)// tier 2 margin value of 1 contract in instrument currency; change decimal according to available leverageONCE tier1 = 55 // DOW €1 IG first tier margin limitONCE maxpositionsize = 550 // DOW €1 IG tier 2 margin limitONCE minpositionsize = .2 // enter minimum position allowedIF Not OnMarket THENpositionsize = startpositionsize + Strategyprofit/(factor*margin)ENDIFIF Not OnMarket THENIF startpositionsize + Strategyprofit/(factor*margin) > tier1 thenpositionsize = (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor2*margin2)) + tier1 //incorporating tier 2 marginENDIFIF Not OnMarket THENif startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THENpositionsize = minpositionsize //keeps positionsize from going below allowed minimumENDIFIF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor2*margin2)) + tier1 > maxpositionsize thenpositionsize = maxpositionsize// keeps positionsize from going above IG tier 2 margin limitENDIFENDIFENDIFENDIF1 user thanked author for this post.
03/17/2020 at 11:07 AM #122311Whose code is above @nonetheless so I can credit them as being the Author in the Snippet Library … or are you now a Coding Wizard!? 🙂
I would add as Nonetheless (Author) or Author (N0netheless) in the Library.
Maybe it is from the Snippet Library anyway so I need not bother?
03/17/2020 at 11:21 AM #122315Whose code is above
It is – surprise surprise – of my very own device. Happy to take snippet credit, but even better if some True Coding Wiz could double check that it’s not doing anything grievously unintended.
03/17/2020 at 12:23 PM #122319Link added as Log 201 in here
Well done Nonetheless … the credit is all yours … please accept my apologies for doubting your ability! 🙂
03/17/2020 at 1:09 PM #122329I find that ‘doubting my ability’ is a very safe starting point. I’m so rarely disappointed!
1 user thanked author for this post.
04/06/2022 at 12:54 PM #191331Thanks, @nonetheless so helpful, as the other position managements are based on position size which would not necessarily work unless the underlying value of the contract remained the same. It would only work at that one time, eg. if index 20,000 vs 10,ooo, the value of that one contract is very different.
However i have one question, on line 16 why have you multiplied factor by margin? :
1positionsize = startpositionsize + Strategyprofit/(factor*margin)If factor is say 20, and leverage is 1/20, then would that not just make the denominator = the entire value of the underlying product then (without any leverage)? So if say the starting position of 1 contract that is a margin cost is $700 for 1 DAX contract currently, at a 20:1 leverage available, and a factor of 20 (as increases by 5%), and you have 100% profit = $700. Then your calculator is:
positionsize = 1 + 700/(20)x(14000*.05)
positionsize = 1.05
Instead of the positionsize = 2, which it should be.
Would this not work better?
12345678IF Not OnMarket THENpositionsize = startpositionsize + Strategyprofit/(margin)ENDIFroundedpositionsize = ROUND(positionsize, 1) // need to round to one decimal place if IG, or whole numbers only depending on contracts minimums.IF BUYCONDITIONS THENBUY roundedpositionsize CONTRACT AT MARKETAlso i added in ROUND(a, digits), as depending on the contract, IG would only accept position sizes to one decimal place, or a whole number; so need round it to the nearest. Otherwise might not be able place the trade.
04/06/2022 at 4:55 PM #191345why have you multiplied factor by margin?
because it wasn’t my intention to put all of the strategyprofit towards the new margin cost; factor of 20 means you are only reinvesting 1/20 of your gain. The factor value can be anything, depending on how aggressively you want to reinvest your profits. If you really want to use all of it then you could have a factor of 1 … but I think you’ll find that will blow up your account. Try optimising f with MM switched on, see what happens.
I am not using that MM any more, but when I was using it I found that anything less than 10 was trouble (in real world trading). A lot will depend on your particular strategy, the WIN%, Risk/Reward ratio and the general distribution of losses.
Rounding is definitely a good idea but I round to 2 decimal places and IG seem okay with it.
I think that IG Oz (and most other places) have leverage of 0.05 for both tier 1 and 2 so you could simplify it by removing margin2, factor2 etc…
1 user thanked author for this post.
04/07/2022 at 6:59 AM #191359Hi thanks, Makes sense. Yes i backtested and had something looked almost like a bell curve lol, rapidly up BUT then rapidly down. But yeah now i understand what you were doing i will try backtest with different values for the f and see what the equity curve looks like.
Yes your right, we only have the 20 leverage available in Australia for main indices or 10 for the other indices. So can simplify it.
-
AuthorPosts
Find exclusive trading pro-tools on