I’m sharing this small code part, in order to add it to any automated trading strategy in ProOrder.
Create the “position size” accordingly to risk and account equity
It will buy or sell the exact number of contracts each time. I.e. We want to risk maximum 1% of our equity, sometimes we will put stoploss short or longer, in any case it will know how many pips your are risking, and then calculate from there the number of contracts.
1 2 3 4 5 6 7 8 9 |
REM Money Management Capital = 10000 Risk = 0.01 StopLoss = 10 // Could be our variable X REM Calculate contracts equity = Capital + StrategyProfit maxrisk = round(equity*Risk) PositionSize = abs(round((maxrisk/StopLoss)/PointValue)*pipsize) |
Incorporate the money management code in a trading strategy
After that just need to change the number of contracts to “PositionSize“, like this:
1 2 3 4 5 6 7 8 9 |
// BUY order example IF conditionBuy then BUY PositionSize CONTRACTS AT MARKET ENDIF // SELL order example IF conditionSell then SELLSHORT PositionSize CONTRACTS AT MARKET ENDIF |
Hope it helps you,
Cheers
Thanks a lot for sharing your money management knowledge!
Thank’s for it…..
Thank for this, very awesome:-)
But what if you account holds other Valuta that the Instrument that you are trading? In my case I have Danish Kroner in my account, but trade contract in Euro. that about 8:1 factor. hwo would this affect the management?
Br. Kasper
Thanx for this share, could anyone possibly assist me. The minimum order I can input is 2 contracts. So I tried to code it as follows:
IF buycondition THEN
PositionSize = abs(round((maxrisk/StopLoss)/PointValue)*pipsize)
if positionsize<2 then BUY 2 contracts at market
else
BUY positionsize CONTRACT AT MARKET
endif
ENDIF
However, it gives me an error. On the command “BUY” :
Syntax error: line 34, character 24One of the following characters would be more suitable than “BUY”:– “n”
Could you possibly assist?
You can try with this instead:
IF buycondition THEN
PositionSize = MAX(2,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))
BUY PositionSize contracts at market
ENDIF
Nice fast way to define position size according to risk. However, may I point out we should assume you meant “We want to risk maximum 1% of our equity” in your description text and not just “0.01%” while using the risk=0.01 line in your code… For risking 0.01% of your equity, your risk value in your code would have been 0.0001
I am not trying to be picky just for the sake of it, it’s just that I saw thoughout the forum quite a few people coming here to find coding assistance and admitting they have very little knowledge in programming. We can easily assume some of them will use codes they find without checking all their lines match the description. So I’m just concerned by the possibility of someone thinking “Hey, I’m prepared to risk 1% instead of his 0.01%” who would replace the risk=0.01 line by risk=1. He would be indeed ending up with “maxrisk=round(equity*risk)”=(all of his equity) with risk=1, not just 1% of it…
So… you probably meant 1% in your description text and it’s a nicely efficient piece of code to define position size accordingly, and probably most people if not all saw it and just like just a typo didn’t feel this 0.01% business needed mentionning, but I’d rather take on the role of the annoying guy stating the obvious than having a silent reader/non-programmer risking 100 times more than he thought. Or am I making a factor 100 mistake myself that I can’t see here?
You are right Noobywan, I didn’t notice this mistake when I copy/paste the description made by Adolfo for this blog post. I’ll change the description accordingly to the code example. Thanks a lot for this warning to everyone.
Any member of the community can have its own code, tutorial or even trading tips posted into the blog section of the website. Like I said before, this site is yours 🙂
HI All ,
Could you confirm If , we start an automatik trading, the rule of the money manag. and some levrier consider the Strategy curve of the live trading first of all , and not the Strategy curve of the Back test ??
thanks
Regards .
Thanks! I incorporated it into an optimistic strategy that just loves to enter at any moment, 10000 to bust in half an hour…but it will feature in many more of my strategies.
I stuck on how to record highest value of account balance, please help!
You can record the highest value the “equity” variable of the article has met with this code:
recordhighest = MAX(recordhighest,equity)
Thanks for your reply!
Thanks Nicolas teach me how to record highest balance, I use this command and add below simple “Fuse” function to my system. If drawdown from 50% from highest balance then quit the system (cool down and review)
MaxD = 50 // Quit system if highest account balance drawdown exceed XX %
//Fuse systemif equity<QuitLevel thenquitendifrecordhighest = MAX(recordhighest,equity)QuitLevel = recordhighest*((100-MaxD)/100)
Hi, how do you define stop loss distance in a situation where the stop loss is defined by either a recent low (long) or high (short).
I was trying
Stoploss = abs(price-laststop)
Where my stoploss code is
//stop loss donchian
C1= Highest[DC](high)
C2=Lowest[DC](low)
if longonmarket and donchtime then
sell at c2[1] stop
laststop = c2[1]
endif
if shortonmarket and donchtime then
exitshort at c1[1] stop
laststop = c1[1]
endif
Did you try to GRAPH your ‘laststop’ or ‘stoploss’ variables and see if it were correctly calculated?
Hi Nicolas, I don’t know what that means I’m afraid.
I do know that the stop loss works as intended through live trading, but I’m not sure how I would use it as part of money management.
Does that help?
Hi I am new here esp to PRT language. After reading a few times, I still don’t understand. How can I get the value of my current equity?
The current equity is not calculated in this formula, if you are talking about the current orders profit on market, you can calculate it with this code snippet:
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
Thank you Nicolas..I mean if I go live trading forex with my code, how can I get my account equity information if I wish to enter the market 1% from my equity balance? If you familiar with mt4, it is “accountequity()” function that return the balance of my equity.
Hello can somebody help me how to run this script?
How to run the tool on forex ?
Just copy paste the code into your own program like the way it is described in the article and you are good.
Hello people can tell me how I can calculate the StrategyProfit?
Because in the backtest he always uses only the position size of the starting capital
StrategyProfit is in money and always calculated with closed positions only. It does take in consideration the lot size or contract quantity. So I don’t understand your issue..?
The system starts with 1000 capital = 2 positions, if the strategy now runs well and at 2000 is the system but 4 positions should use? It does not make it with me but it always buys with the 2 positions since it does not count with me the gains. What am I doing wrong?
The risk variable is in percentage, it doesn’t mean that if you double your account, so the lot size also., because 2 contracts could be the minimum lot size of the instrument you are trading with this code. Without your entire code, I’m afraid I can’t help much.
hello Nicolas
It would be possible to insert these codes for MoneyManagement on this code
So that less experienced people can understand where to insert it. ?
Should the MoneyManagement be added to the code or should it be changed? I enclose a code for S & P 500 with buy positions, without moneymanagement
Hi, I copied pasted the code to my strategy and when I back test it, it tells me there is an error. The PointValue and pipsize appear to be in blue but when I added copied pasted that, they turned pink.
I am using AUD/USD forex pair.
Can anyone help me in getting this right please?
Sorry but this code doesn’t work. Can you help me?
The position sizing works well on whatever instrument I am trading. Can we also have an overall position sizing e.g., EUR/USD starting capital$10000 after ,say, two months this increases to $12000 and PS increases from 8 to 9.6. At the same time USD/JPY trading so starting capital also $10000 ( 1000000 Yen)and after same two months increases to $12000 (1200000 Yen ) and PS increases from 10 to 12. However as overall equity has increased to +/- $14000. The EUR/USD should now be giving PS of 11.2 and USD/JPY 14. Can anyone help? This is important as my systems are in the market all the time, either long or short.