This ProBuilder code snippet demonstrates a trading strategy that dynamically adjusts the duration a trade remains open based on its performance, and optionally manages position sizing for risk control. The strategy increases the time in the market after a profitable trade and resets it after a loss. It also includes an option for averaging down on losing positions.
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
This code snippet includes several key components:
This strategy is designed for educational purposes to illustrate how trade management and risk exposure can be dynamically adjusted based on trading performance.
Check out this related content for more information:
https://www.prorealcode.com/topic/adjusted-time-in-market-based-on-performance/#post-103621
Visit Link