PHP Code:// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AussieBogan
//@version=5
indicator('Dynamic Take Profit & Signal Provider', shorttitle='DTSP', overlay=true, max_bars_back=4000)
//**************************************************************************************//
//******************************Moving Average Strategy********************************//
timer = input(title='ATR Period', defval=14)
source = input(hl2, 'Source')
MTP = input.float(title='ATR Multiplier', step=0.1, defval=7.0)
sf = input.float(0.8, 'Sensitivity [0,1]', minval=0, maxval=1)
changeATR = input(title='Changing ATR method?', defval=false)
showsignals = input(title='Reveal Long/Short Signals ?', defval=true)
avg = 0.
srca = sf * source + (1 - sf) * nz(avg[1], source)
atrsecond = ta.sma(ta.tr, timer)
atr = changeATR ? ta.atr(timer) : atrsecond
upa = srca - MTP * atr
up1a = nz(upa[1], upa)
upa := close[1] > up1a ? math.max(upa, up1a) : upa
dna = srca + MTP * atr
dn1a = nz(dna[1], dna)
dna := close[1] < dn1a ? math.min(dna, dn1a) : dna
avg := math.avg(upa, dna)
trenda = 1
trenda := nz(trenda[1], trenda)
trenda := trenda == -1 and close > dn1a ? 1 : trenda == 1 and close < up1a ? -1 : trenda
LongSignal = trenda == 1 and trenda[1] == -1
ShortSignal = trenda == -1 and trenda[1] == 1
//***********************************************************************************************************************//
//*****************************************ATR STOP and Swing HIgh and Swing Low TP********************************************************************//
//Mode
src = input(title='Source', defval=close)
atrLen = input.int(title='ATR', defval=21, minval=1, maxval=100)
m1 = input.float(title='ATR Multiplier - ADX Rising', defval=3, minval=1, step=0.1, maxval=100)
m2 = input.float(title='ATR Multiplier - ADX Falling', defval=3, minval=1, step=0.1, maxval=100)
adxLen = input.int(title='ADX', defval=14, minval=1, maxval=100)
// DI-Pos, DI-Neg, ADX
hR = high - nz(high[1])
lR = nz(low[1]) - low
dmPos = hR > lR ? math.abs(hR) : 0
dmNeg = lR > hR ? math.abs(lR) : 0
var float sTR = na
sTR := nz(sTR[1]) - nz(sTR[1]) / adxLen + ta.tr
var float sDMPos = na
sDMPos := nz(sDMPos[1]) - nz(sDMPos[1]) / adxLen + dmPos
var float sDMNeg = na
sDMNeg := nz(sDMNeg[1]) - nz(sDMNeg[1]) / adxLen + dmNeg
DIP = sDMPos / sTR * 100
DIN = sDMNeg / sTR * 100
DX = math.abs(DIP - DIN) / (DIP + DIN) * 100
adx = ta.sma(DX, adxLen)
// Trailing ATR
var float m = na
m := ta.rising(adx, 1) ? m1 : ta.falling(adx, 1) ? m2 : nz(m[1])
up = hl2 - m * ta.atr(atrLen)
dn = hl2 + m * ta.atr(atrLen)
var float TUp = na
TUp := src[1] > TUp[1] ? math.max(up, TUp[1]) : up
var float TDown = na
TDown := src[1] < TDown[1] ? math.min(dn, TDown[1]) : dn
var float trend = na
trend := src > TDown[1] ? 1 : src < TUp[1] ? -1 : nz(trend[1], 1)
stop = trend == 1 ? TUp : TDown
trendChange = ta.change(trend)
// Plot
lineColor = not trendChange ? trend > 0 ? #00FF00DD : #FF0000DD : #00000000
shapeColor = trendChange ? trendChange > 0 ? #00FF00F8 : #FF0000F8 : #00000000
plot(stop, color=lineColor, style=plot.style_line, linewidth=1, title='ATR Trend')
barsback = input(90, title='Bars back to check for a swing')
swing_detection(index) =>
var float swing_high = na
var float swing_low = na
start = index * 2 - 1 // -1 so we have an even number of
swing_point_high = high[index]
swing_point_low = low[index]
//Swing Highs
for i = 0 to start by 1
// swing_high := true
if i < index
if high[i] > swing_point_high
swing_high := high[i]
break
// Have to do checks before pivot and after seperately because we can get
// two highs of the same value in a row. Notice the > and >= difference
if i > index
if high[i] >= swing_point_high
swing_high := high[i]
break
//Swing lows
for i = 0 to start by 1
// swing_low := true
if i < index
if low[i] < swing_point_low
swing_low := low[i]
break
// Have to do checks before pivot and after seperately because we can get
// two lows of the same value in a row. Notice the > and >= difference
if i > index
if low[i] <= swing_point_low
swing_low := low[i]
break
[swing_high, swing_low]
// Check for a swing
[sw_h, sw_l] = swing_detection(barsback)
new_bar(res) =>
ta.change(time(res)) != 0
adr(length) =>
range_1 = high - low
ta.sma(range_1[1], length)
adr_high(adr) =>
high - low < adr ? low + adr : close >= open ? low + adr : high
adr_low(adr) =>
high - low < adr ? high - adr : close >= open ? low : high - adr
plotshape(LongSignal and showsignals ? up : na, title='BUY', style=shape.arrowup, location=location.belowbar, size=size.auto, text='Buy', color=color.new(color.green, 0))
plotshape(ShortSignal and showsignals ? dn : na, title='SELL', style=shape.arrowdown, location=location.abovebar, size=size.auto, text='Sell', color=color.new(color.red, 0))
os = 0.
tp = 0.
os := LongSignal ? 1 : ShortSignal ? 0 : os[1]
tp := ta.change(os) == 1 ? sw_h : ta.change(os) == -1 ? sw_l : tp[1]
css = os == 1 ? #0cb51a : #2157f3
plot(tp, color=ta.change(os) ? na : fixnan(css))
tpl = input(50, 'Tp Length')
tpm = input(2., 'Tp Multiplicative Factor')
valong = ta.valuewhen(LongSignal, close, 0)
valshort = ta.valuewhen(ShortSignal, close, 0)
dev = ta.stdev(close, tpl) * tpm
up2 = valong + dev * 2
up1 = valong + dev
dn1 = valong - dev
dn2 = valong - dev * 2
plotshape(os == 1 ? up1 : dn1, title='TP 1', style=shape.circle, location=location.absolute, size=size.auto, text='TP 1', textcolor=css, color=fixnan(css), show_last=1, offset=14, transp=0)
plotshape(os == 1 ? up2 : dn2, title='TP 2', style=shape.circle, location=location.absolute, size=size.auto, text='TP 2', textcolor=css, color=fixnan(css), show_last=1, offset=14, transp=0)
plotshape(os == 0 ? na : valong, title='Long', style=shape.circle, location=location.absolute, size=size.auto, text='Long Position', textcolor=color.new(#0cb51a, 0), color=color.new(#0cb51a, 0), show_last=1, offset=14)
plotshape(os == 1 ? na : valshort, title='Short', style=shape.circle, location=location.absolute, size=size.auto, text='Short Position', textcolor=color.new(#ff1100, 0), color=color.new(#ff1100, 0), show_last=1, offset=14)



Alıntı yaparak yanıtla
Yer İmleri