-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadx
More file actions
97 lines (82 loc) · 4.88 KB
/
Copy pathadx
File metadata and controls
97 lines (82 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//@version=5
strategy("ADX Di+ Di- Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, currency=currency.GBP)
// Input parameters
i_sigLen = input.int(14, title='ADX Smoothing')
i_diLen = input.int(14, title='DI Length')
i_hlRange = input.int(20, title='Level Range')
i_hlTrend = input.int(35, title='Level Trend')
i_alertOn = input.bool(true, title="■ Alert On/Off")
i_barColOn = input.bool(true, title="■ Bar Color On/Off")
// Function to calculate Directional Movement
f_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]
// Function to calculate ADX and DI
f_sig(_diLen, _sigLen) =>
[_plus, _minus] = f_dirMov(_diLen)
_sum = _plus + _minus
_sig = 100 * ta.rma(math.abs(_plus - _minus) / (_sum == 0 ? 1 : _sum), _sigLen)
[_sig]
// Calculations
condition = 0.0
[sig] = f_sig(i_diLen, i_sigLen)
[diPlus, _] = f_dirMov(i_diLen)
[_, diMinus] = f_dirMov(i_diLen)
hlRange = sig <= i_hlRange
diUp = diPlus >= diMinus
diUpUp = diPlus >= i_hlTrend
diDn = diMinus > diPlus
diDnDn = diMinus > i_hlTrend
crossDi = ta.cross(diPlus, diMinus)
sigUp = sig > sig[1]
sigDir = sig > sig[1] and diUp and not hlRange ? 1 : sig > sig[1] and diDn and not hlRange ? -1 : 0
// Entry and exit conditions
entryLong = not hlRange and diUp and sigUp and not diUp[1] or not hlRange and diUp and sigUp and sig > i_hlRange and hlRange[1]
entryLongStr = not hlRange and diUp and sigUp and diUpUp
exitLong = crossDi and diUp[1] or hlRange and not hlRange[1]
condition := condition[1] != 1 and entryLongStr ? 1 : condition[1] != 0.5 and entryLong ? 0.5 : condition[1] != 0 and exitLong ? 0 : nz(condition[1])
longE = barstate.isconfirmed and condition[1] != 0.5 and condition == 0.5
longEStr = barstate.isconfirmed and condition[1] != 1 and condition == 1
longX = barstate.isconfirmed and ((condition[1] == 0.5 and condition == 0) or (condition[1] == 1 and condition == 0))
// Colors for plots
C_GREEN = color.new(#006400, 0) //Green
C_RED = color.new(#8B0000, 0) //Red
c_sig = hlRange ? color.orange : sigUp and diUp ? C_GREEN : not sigUp and diUp ? color.new(C_GREEN, 50) : sigUp and diDn ? C_RED : not sigUp and diDn ? color.new(C_RED, 50) : na
c_fillAdx = hlRange ? color.new(color.orange, 90) : not hlRange and diUp and diUpUp ? color.new(C_GREEN, 90) : not hlRange and diUp and not diUpUp ? color.new(C_GREEN, 90) : not hlRange and diDn and diDnDn ? color.new(C_RED, 90) : not hlRange and diDn and not diDnDn ? color.new(C_RED, 90) : na
// Plots
plot(sig, title='ADX', color=c_sig, linewidth=3)
p_diPlus = plot(diPlus, title='+DI', color=C_GREEN)
p_diMinus = plot(diMinus, title='-DI', color=C_RED)
fill(p_diPlus, p_diMinus, title='Fill ADX', color=c_fillAdx)
plot(crossDi ? diPlus : na, title='Cross Di', color=#00000000, style=plot.style_circles, linewidth=2)
hline(i_hlRange, title='Level Range', color=color.gray, linestyle=hline.style_dotted, linewidth=1)
hline(i_hlTrend, title='Level Trend', color=color.gray, linestyle=hline.style_dotted, linewidth=1)
barcolor(i_barColOn ? c_sig : na)
// Alerts
plotshape(i_alertOn and longE ? i_hlTrend + 10 : na, title='Bullish\nTrend', color=C_GREEN, style=shape.triangleup, size=size.tiny, location=location.absolute)
plotshape(i_alertOn and longEStr ? i_hlTrend + 10 : na, title='Strong\nBullish\nTrend', color=C_GREEN, style=shape.triangleup, size=size.small, location=location.absolute)
plotshape(i_alertOn and longX ? i_hlTrend + 10 : na, title='End\nTrend', color=color.new(color.orange, 0), style=shape.xcross, size=size.small, location=location.absolute)
alertcondition(longE or longEStr or longX, title='Any Alert', message='Any Alert')
alertcondition(longE, title='Buy Weak Alert', message='Buy Weak Alert')
alertcondition(longEStr, title='Buy Strong Alert', message='Buy Strong Alert')
alertcondition(longX, title='Buy Close Alert', message='Buy Close Alert')
// Strategy logic
var float tpLevel = na
longCondition = longEStr
exitConditionLong = longX
if (longCondition)
strategy.entry("Long", strategy.long, stop=close * 0.9, limit=close * 1.1)
tpLevel := close * 1.05
line.new(x1=bar_index, y1=tpLevel, x2=bar_index + 1, y2=tpLevel, color=color.new(color.green, 0), style=line.style_dashed)
if (exitConditionLong)
strategy.close("Long", comment="Stop Loss")
//label.new(bar_index, high, "stop loss", color=color.red, textcolor=color.red, style=label.style_label_down, yloc=yloc.abovebar)
if (strategy.position_size > 0 and high >= tpLevel)
strategy.close("Long", comment="İşlem Tamam")
//label.new(bar_index, tpLevel, "işlem tamam", color=color.green, textcolor=color.green, style=label.style_label_up, yloc=yloc.belowbar)