Adjusted time in market based on performance
Forums › ProRealTime English forum › ProOrder support › Adjusted time in market based on performance
- This topic has 4 replies, 3 voices, and was last updated 5 years ago by Vonasi.
-
-
07/27/2019 at 4:23 PM #103621
Time in the market is an important factor – often it is good to give a trade time to achieve what you expect but increased time on the market can also be increased risk exposure. I got thinking about this and decided to write some code to vary the exposure to risk as a strategy trades and came up with the following that I post here just in case it is of interest to anyone. It is not a complete strategy – you have to enter your own long and short entry conditions to make it work! I created it around an end of day strategy idea but it should work on other time frames. In the following description I refer to ‘days’ but you might want to think in candles for other time frames.
The idea is that you can set a maximum number of days that you want a trade to stay open (minD) and then get out as soon as a candle closes in profit or time runs out as you reach the maximum number of days. If it is a profitable trade then the exposure time for the next trade is increased by one day. This continues up to a maximum number of days which is maxD. If a trade is a losing trade then the number of days is reset back to minD.
There is also an averaging down part of the code which can be turned on or off by setting ‘AverageDown’ to 1 or 0. With this turned on if we are in a trade already and it is losing and entry conditions are true again then an extra position is opened. Normally this would be a dangerous game but our maximum days allowed in the market allows us to reduce our potential risk as we can only lose as much as a market would normally move from our average price in our maximum allowed number of days (we hope!).
There is also ‘PositionSizing’ that can be turned on or off too. With it on the first position is opened at whatever we set ‘StartPositionSize’ at and then if we average down the size of the next trade is increased based on however many days is our current maximum allowed. Be warned that turning PositionSizing on can be very scary for your bank account!
With a very simple set of price action entry conditions and position sizing turned off I was able to get the attached equity curve in the first image on EURUSD Daily with minD of 2 and maxD of 6.
Please be aware that this was just an interesting experiment in the effects of adjusted market exposure based on trading performance and not a suggested way of going about trading – especially the averaging down with adjusted position sizing turned on – please see the very scary draw down in the second image as a warning!
Enter your own long and short conditions in lines 7 and 8.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667AverageDown = 1 //0=Off 1=On Turn on or off averaging downPositionSizing = 0 //0=Off 1=On Turn on or off position sizingStartPositionSize = 1 //Starting Position Sizemind = 2 //Minimum candles on marketmaxd = 6 //Maximum candles on marketLongConditions = (Your Long Conditions)ShortConditions = (Your Short Conditions)once d = mind//exit if in profitif longonmarket and close > positionprice thensell at marketd = min(d + 1,maxd)//extend time allowed in market after a winendifif shortonmarket and close < positionprice thenexitshort at marketd = min(d + 1,maxd)//extend time allowed in market after a winendif//exit if time is upif longonmarket and barindex - startindex = d thensell at marketendifif shortonmarket and barindex - startindex = d thenexitshort at marketendif//Reset time allowed on market to minimum after a lossif strategyprofit < strategyprofit[1] thend = mindendif//Buy first long positionif not longonmarket and LongConditions thenpositionsize = StartPositionSizebuy positionsize contract at marketstartindex = barindexendif//Buy extra long position if in a loss and conditions met againif longonmarket and LongConditions and close < positionprice and averagedown thenif positionsizing thenpositionsize = positionsize + (startpositionsize * d)endifbuy positionsize contract at marketendif//Buy first short positionif not shortonmarket and ShortConditions thenpositionsize = StartPositionSizesellshort positionsize contract at marketstartindex = barindexendif//Buy extra short position if in a loss and conditions met againif shortonmarket and ShortConditions and close > positionprice and averagedown thenif positionsizing thenpositionsize = positionsize + (startpositionsize * d)endifsellshort positionsize contract at marketendifgraph positionsize07/27/2019 at 7:07 PM #103632Link to above added to below with the Comment ,,,
Concept more than a working strategy
1 user thanked author for this post.
07/28/2019 at 6:56 PM #103668Hi Vonasi, interesting code and perhaps it could be used on the vectorial dax strategy.
That strategy has a few issues, like
- long costly exposure in the market
- signals in the same direction are are ignored as long there is no exit criteria matched
Trying to understand the code I’am focussing on the second paragraph.
It’s also modified because the days had to be counted and not the bars on a 5min TF.
To the entry conditions I ‘ve add startday=0
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354// display days in marketif displaydim thenif (not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket))) thencount=0endifif not ( dayofweek=1 and hour <= 1) thenif onmarket thenif openday <> openday[1] thencount = count + 1endifendifendifgraph countendifonce testV=1if testV then//pp=(positionperf*100) (already in code above)mind = 0 //Minimum days on market (counting days; first day is 0)maxd = 6 //Maximum days on marketonce d = mind//exit if in profitif count>0 and pp>1 thenif longonmarket thensell at marketd = min(count+1,maxd) //extend time allowed in market after a winendifif shortonmarket thenexitshort at marketd = min(count+1,maxd) //extend time allowed in market after a winendifendif//exit if time is upif count>0 thenif longonmarket and count - startday = d thensell at marketendifif shortonmarket and count - startday = d thenexitshort at marketendifendif//Reset time allowed on market to minimum after a lossif strategyprofit < strategyprofit[1] thend = mindendifendifAlso changed positionprice to a positionperformance, otherwise it would get out too often. Hope its correctly coded, got to check it some more and try to expand it.
07/28/2019 at 11:17 PM #103672I’ve not really been following the Vectorial DAX in close detail so it is difficult for me to comment on this addition to it without going back over an awfully large number of posts to understand it all!
My first thought was why over complicate things. 1 day is 288 * 5 minute candles so when a trade is opened and you want to give it a minimum number of days on the market then just set minD to a multiple of 288 and maxD to a multiple of 288.
You would also need to change the line that increases the time in the market to add 288 candles on instead of just 1 candle:
1d = min(d + 288,maxd)//extend time allowed in market after a win07/28/2019 at 11:27 PM #103673Here is a stripped down version with the averaging down and position sizing removed and a step variable added for increasing the time in the market. Settings are for a 5 minute chart with minD of 2 days and MaxD of 6 days.
Not tested.
123456789101112131415161718192021222324252627282930313233343536373839404142434445minD = 576 //Minimum candles on marketmaxD = 1728 //Maximum candles on marketDstep = 288LongConditions = (Your Long Conditions)ShortConditions = (Your Short Conditions)once d = mind//exit if in profitif longonmarket and close > positionprice thensell at marketd = min(d + Dstep,maxd)//extend time allowed in market after a winendifif shortonmarket and close < positionprice thenexitshort at marketd = min(d + Dstep,maxd)//extend time allowed in market after a winendif//exit if time is upif longonmarket and barindex - startindex = d thensell at marketendifif shortonmarket and barindex - startindex = d thenexitshort at marketendif//Reset time allowed on market to minimum after a lossif strategyprofit < strategyprofit[1] thend = mindendif//Buy first long positionif not longonmarket and LongConditions thenbuy positionsize contract at marketstartindex = barindexendif//Buy first short positionif not shortonmarket and ShortConditions thensellshort positionsize contract at marketstartindex = barindexendif -
AuthorPosts
Find exclusive trading pro-tools on