Increase position based on equity drawdown
Forums › ProRealTime English forum › ProOrder support › Increase position based on equity drawdown
- This topic has 21 replies, 3 voices, and was last updated 5 years ago by
Vonasi.
-
-
03/06/2020 at 12:07 AM #121300
Hi,
This is probably an easy question for any one who nows coding in PRT. I would like some help to create a code that increases/cumulate a position if the equity drops x% below the strategy’s maxequity. I thought it would be something like the code below that I found but it clearly doesn’t work:
12345678910maxequity = max(equity,maxequity)Drawdown = 15 // % drawdown from max equity needed to increase positionif longonmarket and equity < maxequity * (1 - (Drawdown/100)) thenbuy 1 contract at marketendifif shortonmarket and equity < maxequity * (1 - (Drawdown/100)) thenbuy 1 contract at marketendifAny idea on how to fix it? Is it the definition of maxequity (line 1) that is wrong?
03/07/2020 at 12:15 PM #12143303/07/2020 at 12:38 PM #121434Perhaps something like this – not tested.
123456789101112131415161718192021capital = 10000 //starting capitalif longonmarket thenequity = capital + strategyprofit + ((close - positionprice) * countofposition)endifif shortonmarket thenequity = capital + strategyprofit + ((positionprice - close) * abs(countofposition))endifmaxequity = max(equity,maxequity)Drawdown = 15 // % drawdown from max equity needed to increase positionif longonmarket and equity < maxequity * (1 - (Drawdown/100)) thenbuy 1 contract at marketendifif shortonmarket and equity < maxequity * (1 - (Drawdown/100)) thenbuy 1 contract at marketendif03/07/2020 at 6:13 PM #121446Thank you @Vonasi , it worked! 🙂
It was also a minor disaster since it accumulated like crazy… do you know if there is a command to limit the increased order to only once per trade? Something like “max cumulateorders = 1”? I tried using the “countofposition” command but that only works if the strategy is static on the number of contracts to buy or sellshort and I prefer to use re-invest with increasing positions. I looked in the PRT manual but it didn’t have any example on limiting the number of cumulate orders.
03/07/2020 at 6:29 PM #12144712345678if longonmarket and equity < maxequity * (1 - (Drawdown/100)) and countofposition < 2 thenbuy 1 contract at marketendifif shortonmarket and equity < maxequity * (1 - (Drawdown/100)) and countofposition > -2 thenbuy 1 contract at marketendif1 user thanked author for this post.
04/18/2020 at 2:50 PM #12674104/18/2020 at 3:25 PM #126746Not sure that I fully understand what you are trying to do but this is what you have described. Not tested.
12345678910111213141516171819percentage = 5if not longonmarket and (your long entry conditions) thenbuy 1 contract at marketbuy percentage contracts at close * (1-(percentage/100)) limitendifif longonmarket thenbuy percentage contracts at positionprice * (1-(percentage/100)) limitendifif not shortonmarket and (your short entry conditions) thensellshort 1 contract at marketsellshort percentage contracts at close * (1+(percentage/100)) limitendifif shortonmarket thensellshort percentage contracts at positionprice * (1+(percentage/100)) limitendif04/18/2020 at 3:49 PM #126750We’re getting closer. We can focus on only longonmarket and ignore short. I would like the code to trigger a buy of x contracts at market if the current position has declined x % since buy.
Let’s say I have 0.2 contracts on DJI and if this position is down 0.5% in value I want to buy an additional 0.2 contracts. I would also like the maximum position to be 0.4 contracts.
04/18/2020 at 3:56 PM #12675104/18/2020 at 3:57 PM #126753So you don’t want to buy x contracts at a drop of x%. You want to buy y contracts at a drop of x%.
123456789101112percentage = 0.5positionsize = 0.2maxposition = 0.4if not onmarket and (your long entry conditions) thenbuy positionsize contracts at marketbuy positionsize contracts at close * (1-(percentage/100)) limitendifif onmarket and countofposition < maxposition thenbuy positionsize contracts at positionprice * (1-(percentage/100)) limitendif04/18/2020 at 4:02 PM #126754The above code places a pending order on the market at the same time that it opens the first trade so that if the market drops 0.5% in that first candle then a second position will open. This initial order however has to be calculated on the closing price of the previous candle and so if there is a gap down then it will not be 0.5% below your first buy price. After the first candle it is exactly 0.5% below because we now know what price we bought at.
04/18/2020 at 4:06 PM #126755You also have to consider that that code uses the POSITIONPRICE and not TRADEPRICE in the calculations so if you increase the maximum position size so that it opens more individual trades than two then the 0.5% is calculated off the average price you purchased at and not the last price you purchased at. Just change POSITIONPRICE to TRADEPRICE to match whichever you prefer.
04/18/2020 at 4:22 PM #126756It didn’t have any effect. No additional buy is being made. But it usually takes way more than one candle before the market has dropped to the trigger level. Does the limit order cancel itself after one candle?
123456789101112IF not longonmarket and c1def and c11m and c21m and c41m and c2def and c1def and c160m and not BadTime thenBuy positionsize contracts at marketbuy positionsize contracts at close * (1-(percentage/100)) limitendifif onmarket and countofposition < maxposition thenbuy positionsize contracts at tradeprice * (1-(percentage/100)) limitendifpercentage = 0.3positionsize = 0.2maxposition = 0.404/18/2020 at 5:59 PM #126770I just tested this version on DJI daily and it worked just fine.
1234567891011121314percentage = 0.5positionsize = 1maxposition = 2if not onmarket and close < open thenbuy positionsize contracts at marketbuy positionsize contracts at close * (1-(percentage/100)) limitendifif onmarket and countofposition < maxposition thenbuy positionsize contracts at positionprice * (1-(percentage/100)) limitendifset target %profit 1It did nothing though with a position size of 0.2.
Why have you moved the variable setting to the bottom of the code? They must be at the start of the code or they will not be known for the first bar on the chart.
04/18/2020 at 6:24 PM #126775 -
AuthorPosts
Find exclusive trading pro-tools on