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.
AverageDown = 1 //0=Off 1=On Turn on or off averaging down
PositionSizing = 0 //0=Off 1=On Turn on or off position sizing
StartPositionSize = 1 //Starting Position Size
mind = 2 //Minimum candles on market
maxd = 6 //Maximum candles on market
LongConditions = (Your Long Conditions)
ShortConditions = (Your Short Conditions)
once d = mind
//exit if in profit
if longonmarket and close > positionprice then
sell at market
d = min(d + 1,maxd)//extend time allowed in market after a win
endif
if shortonmarket and close < positionprice then
exitshort at market
d = min(d + 1,maxd)//extend time allowed in market after a win
endif
//exit if time is up
if longonmarket and barindex - startindex = d then
sell at market
endif
if shortonmarket and barindex - startindex = d then
exitshort at market
endif
//Reset time allowed on market to minimum after a loss
if strategyprofit < strategyprofit[1] then
d = mind
endif
//Buy first long position
if not longonmarket and LongConditions then
positionsize = StartPositionSize
buy positionsize contract at market
startindex = barindex
endif
//Buy extra long position if in a loss and conditions met again
if longonmarket and LongConditions and close < positionprice and averagedown then
if positionsizing then
positionsize = positionsize + (startpositionsize * d)
endif
buy positionsize contract at market
endif
//Buy first short position
if not shortonmarket and ShortConditions then
positionsize = StartPositionSize
sellshort positionsize contract at market
startindex = barindex
endif
//Buy extra short position if in a loss and conditions met again
if shortonmarket and ShortConditions and close > positionprice and averagedown then
if positionsizing then
positionsize = positionsize + (startpositionsize * d)
endif
sellshort positionsize contract at market
endif
graph positionsize
Link to above added to below with the Comment ,,,
Concept more than a working strategy
Snippet Link Library
PaulParticipant
Master
Hi 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
// display days in market
if displaydim then
if (not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket))) then
count=0
endif
if not ( dayofweek=1 and hour <= 1) then
if onmarket then
if openday <> openday[1] then
count = count + 1
endif
endif
endif
graph count
endif
once testV=1
if 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 market
once d = mind
//exit if in profit
if count>0 and pp>1 then
if longonmarket then
sell at market
d = min(count+1,maxd) //extend time allowed in market after a win
endif
if shortonmarket then
exitshort at market
d = min(count+1,maxd) //extend time allowed in market after a win
endif
endif
//exit if time is up
if count>0 then
if longonmarket and count - startday = d then
sell at market
endif
if shortonmarket and count - startday = d then
exitshort at market
endif
endif
//Reset time allowed on market to minimum after a loss
if strategyprofit < strategyprofit[1] then
d = mind
endif
endif
Also 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.
I’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:
d = min(d + 288,maxd)//extend time allowed in market after a win
Here 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.
minD = 576 //Minimum candles on market
maxD = 1728 //Maximum candles on market
Dstep = 288
LongConditions = (Your Long Conditions)
ShortConditions = (Your Short Conditions)
once d = mind
//exit if in profit
if longonmarket and close > positionprice then
sell at market
d = min(d + Dstep,maxd)//extend time allowed in market after a win
endif
if shortonmarket and close < positionprice then
exitshort at market
d = min(d + Dstep,maxd)//extend time allowed in market after a win
endif
//exit if time is up
if longonmarket and barindex - startindex = d then
sell at market
endif
if shortonmarket and barindex - startindex = d then
exitshort at market
endif
//Reset time allowed on market to minimum after a loss
if strategyprofit < strategyprofit[1] then
d = mind
endif
//Buy first long position
if not longonmarket and LongConditions then
buy positionsize contract at market
startindex = barindex
endif
//Buy first short position
if not shortonmarket and ShortConditions then
sellshort positionsize contract at market
startindex = barindex
endif