← กลับหน้ารายการ

ADX Dynamic Zone Inside the SuperTrend with Support / Resistant

Strategy ผู้เขียน: fullmax Profit Factor: 1.416

ลิงก์ TradingView

เปิดใน TradingView

Equity Chart

Equity chart

เปิดรูปเต็มขนาด

คำอธิบาย

I like Simple Strategy and just code one using super trend and ADX
But like All super trend strategy buy and sell signal most of the time are to late
So I am using the super trend like indicator for trend direction and ADX
Dynamic buy or sell zone for pull back and if direction is ok we get in trade but only when we have support for long or resistant for short
Take profit is fix % and stop is Trailing stop
There is A choice if you want to use fixed sum for the trades or increase the size of the trade after # of losing trades
The increase of the size is working like D'Alembert System
Thanks to Trading view and pinecoders.com for the build in indicators and code
If any Questions Let Me Know
Happy Trading

รูป Preview

Preview

Pine Script Source

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fullmax


//@version=6
strategy(title = 'ADX-ZONE/ST/S-R', overlay = true, pyramiding = 0)
//
tradedir = input.string('Both', title = 'Trade Direction', options = ['Long', 'Short', 'Both'])
//  trail stop inputs
longTrailPerc = input.float(2.0, title = 'Trail Long Loss (%)', minval = 0.0, step = 0.1) * 0.01
shortTrailPerc = input.float(2.0, title = 'Trail Short Loss (%)', minval = 0.0, step = 0.1) * 0.01
///tps
tp_levelL = input.float(title="Take Profit long (%)", defval=1.5, step = 0.1)
tp_levelS = input.float(title="Take Profit short (%)", defval=1.5, step = 0.1)
//// super trend
atrPeriod = input.int(5, 'ATR Length', minval = 1)
factor = input.float(3.02, 'Factor', minval = 0.01, step = 0.01)
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
supertrend := barstate.isfirst ? na : supertrend
upd = direction < 0 ? supertrend : na
dnd = direction < 0 ? na : supertrend
supertrend := barstate.isfirst ? na : supertrend
upTrend =    plot(direction < 0 ? supertrend : na, "Up Trend",   color = color.green, style = plot.style_linebr)
downTrend =  plot(direction < 0 ? na : supertrend, "Down Trend", color = color.red,   style = plot.style_linebr)
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)
fill(bodyMiddle, upTrend,   title = "Uptrend background",   color = color.new(color.green, 90), fillgaps = false)
fill(bodyMiddle, downTrend, title = "Downtrend background", color = color.new(color.red,   90), fillgaps = false)
///// pivot S/R
S_R = input.bool(true, title="Support-Res")
sup = input.int(33, title="Lookback (Sup)", minval=1)
res = input.int(17, title="Lookback (Res)", minval=1)
support  =  ta.lowest(low[1], sup) 
resistan =  ta.highest(high[1], res) 
entrysup = low >= support
entryres = high <= resistan
///// ADX cal
adxlen = input(1, title = "ADX Smoothing")
dilen =  input(20, title = "DI Length")

dirmov(len) =>
	up = ta.change(high)
	down = -ta.change(low)
	plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
	minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
	truerange = ta.rma(ta.tr, len)
	plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
	minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
	[plus, minus]
adx(dilen, adxlen) =>
	[plus, minus] = dirmov(dilen)
	sum = plus + minus
	adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
////bay and sell zone from the trend
buyzone = upd * (1 + sig / 10000)
sellzone = dnd * (1 - sig / 10000)
plot(buyzone, "Buy zone",   color = color.blue, style = plot.style_linebr)
plot(sellzone, "Sell Zone",   color = color.orange, style = plot.style_linebr)
/// bay and sell signal
buy = low < buyzone and entrysup   
sell = high > sellzone and entryres

// Calculate the take-profit prices 
tp_price_long = strategy.position_avg_price * (1 + tp_levelL / 100)
tp_price_short = strategy.position_avg_price * (1 - tp_levelS / 100)
// Determine trail stop
longStopPrice = 0.0
shortStopPrice = 0.0

longStopPrice := if strategy.position_size > 0
    stopValue = close * (1 - longTrailPerc)
    math.max(stopValue, longStopPrice[1])
else
    0

shortStopPrice := if strategy.position_size < 0
    stopValue = close * (1 + shortTrailPerc)
    math.min(stopValue, shortStopPrice[1])
else
    999999

// Plot stop price and TPs
plot(strategy.position_size > 0 ? longStopPrice : na, color = color.blue, style = plot.style_circles, linewidth = 1, title = 'Long Trail Stop')
plot(strategy.position_size < 0 ? shortStopPrice : na, color = color.blue, style = plot.style_circles, linewidth = 1, title = 'Short Trail Stop')
plot(strategy.position_size > 0 ? tp_price_long : na, color = color.rgb(69, 54, 67), style = plot.style_circles, linewidth = 1, title = 'Long Trail Stop')
plot(strategy.position_size < 0 ? tp_price_short : na, color = color.rgb(69, 54, 67), style = plot.style_circles, linewidth = 1, title = 'Long Trail Stop')
//// size of the trade
SZZ = input(1000, title = 'Size of the trade in USD')
//increase trade cal
increaseT = input.bool(true,title = " Increase size of the trade after # of losing trades ")
nl = input.float(1,minval=1,title = "# of losing trades in the row")
osc = SZZ / close
// Check if there's a new losing trade that increased the streak
loss = (strategy.losstrades > strategy.losstrades[1]) and (strategy.wintrades == strategy.wintrades[1]) and (strategy.eventrades == strategy.eventrades[1])
win = strategy.wintrades > strategy.wintrades[1] and strategy.losstrades == strategy.losstrades[1] and strategy.eventrades == strategy.eventrades[1]

// Determine current losing streak length
streaklen = 0

streaklen := if loss
    nz(streaklen[1]) + 1
else
    if strategy.wintrades > strategy.wintrades[1] or
         strategy.eventrades > strategy.eventrades[1]
        0
    else
        nz(streaklen[1])

/// size increase  like D'Alembert 
var size_arr = array.from(osc/2, osc, osc, osc/2)
array.size(size_arr)
    
if win and array.size(size_arr) >= 4
    array.shift(size_arr)
    array.pop(size_arr)

else if win
    size_arr := array.from(osc/2, osc/2, osc/2,osc/2)

if streaklen > nl and loss
    array.push(size_arr, value= array.get(size_arr, 1) + array.last(size_arr))

first_elem = array.get(size_arr, 0)
last_elem  = array.last(size_arr)
fl = first_elem + last_elem

// strategy orders
if buy and (tradedir == "Long" or tradedir == "Both") and strategy.opentrades == 0
    strategy.entry('Long', strategy.long, qty = increaseT == true ?  fl : osc)

if sell and (tradedir == "Short" or tradedir == "Both") and strategy.opentrades == 0
    strategy.entry('Short', strategy.short,qty = increaseT == true ?  fl : osc )
    
// exit
if strategy.position_size > 0 
    strategy.exit('Long', stop  = longStopPrice, limit=tp_price_long)


if strategy.position_size < 0
    strategy.exit('Tp/stop', stop = shortStopPrice,limit=tp_price_short)

////