Page 1 of 1

Donchian Channels with ADX conditions

Posted: Tue Sep 22, 2020 4:43 pm
by herac
Hi guys,

I am trying to code script for backtesting my setup and I am stuck at one point. For clarification, it is a Donchian Breakout strategy with conditions. So I found out the general strategy script (https://www.tradingview.com/script/8vuK ... -free-bot/) in a public library and what I want to do is to add to it some conditions. The first condition is that on the entry ADX must be higher than it was on a previous day plus it should be above 20 and the second one is for an exit - ADX is above 40 and it drops on the next day. Here is the code for ADX condition indicator. Whenever I add the variable - conditionADX to strategy.entry (when = .....) it simply ignores it. Could anyone help me?

Thanks,
Simon. :zen:

Code: Select all

//Inputs
len = input(title="ADX Length", type=input.integer, defval=14)
sT = input(title="ADX Threshold", type=input.integer, defval=20)
vT = input(title = "Bishop line", type = input.integer, defval = 40)

//Calculations
TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange) - (nz(SmoothedTrueRange)/len) + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, len)

conditionADX = ADX > ADX[1] and ADX > sT
exitBishop = ADX > vT and ADX < ADX[1]

//Plot
plotshape(conditionADX, color = color.green, style = shape.circle, show_last = 5, title = "ADX condition", location = location.bottom)
plotshape(exitBishop, color = color.red, style = shape.circle, show_last = 5, title = "Bishop", location = location.bottom)

Re: Donchian Channels with ADX conditions

Posted: Tue Oct 13, 2020 11:58 am
by herac
It is finished.

But I would like to ask you if there is a way to execute two orders on one candle. For example, If I close a trade on the open of a daily candle, can I open the next trade within that candle, if the breakout occurs?

Thanks.

Re: Donchian Channels with ADX conditions

Posted: Tue Oct 13, 2020 12:27 pm
by ocaptain
Shouldn't be a problem.... you are closing one order and opening another so there shouldn't be any conflict...