-
Notifications
You must be signed in to change notification settings - Fork 1
/
Combined Indicators.txt
201 lines (153 loc) · 8.73 KB
/
Combined Indicators.txt
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//@version=5
indicator("Combined Twin Range Filter + G Channel Trend Detection + Market Bias", shorttitle = "Combined Indicator", overlay = true)
// Inputs
source1 = input(defval=close, title='Source Twin Range', group="Twin Range Filter")
per1 = input.int(defval=25, minval=1, title='Fast period', group="Twin Range Filter")
mult1 = input.float(defval=1.5, minval=0.1, title='Fast range', group="Twin Range Filter")
per2 = input.int(defval=50, minval=1, title='Slow period', group="Twin Range Filter")
mult2 = input.float(defval=2, minval=0.1, title='Slow range', group="Twin Range Filter")
//////////
length = input.int(95,title = "Length G-Channel Trend", group="G-Channel Trend Detection")
src = input(close, title = "Source G-Channel Trend", group="G-Channel Trend Detection")
/////////
//@function This function forces the current timeframe on inputs that shouldn't take lower timeframes
FORCE_CTF(str_tf) =>
i_tf_sec = timeframe.in_seconds(str_tf)
ctf_sec = timeframe.in_seconds('')
// Returns the CTF if the passed timeframe is lower
i_tf_sec < ctf_sec ? '' : str_tf
//
// INPUTS
ha_htf = FORCE_CTF(input.timeframe('', 'Timeframe', tooltip="This timeframe must be equal to or greater than the chart's timeframe", group="HA Market Bias"))
ha_len = input(200, 'Period', group="HA Market Bias")
ha_len2 = input(100, 'Smoothing', group="HA Market Bias")
/////////////
// Script for Combined Indicators
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
smoothrng
smrng1 = smoothrng(source1, per1, mult1)
smrng2 = smoothrng(source1, per2, mult2)
smrng = (smrng1 + smrng2) / 2
// Range Filter
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
rngfilt
filt = rngfilt(source1, smrng)
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
hband = filt + smrng
lband = filt - smrng
longCond = bool(na)
shortCond = bool(na)
longCond := source1 > filt and source1 > source1[1] and upward > 0 or source1 > filt and source1 < source1[1] and upward > 0
shortCond := source1 < filt and source1 < source1[1] and downward > 0 or source1 < filt and source1 > source1[1] and downward > 0
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
long = longCond and CondIni[1] == -1
short = shortCond and CondIni[1] == 1
// Plots of Twin Range Filter
plotshape(long, title='Long', text='Long', style=shape.labelup, textcolor=color.new(color.black, 0), size=size.tiny, location=location.belowbar, color=color.new(color.lime, 0))
plotshape(short, title='Short', text='Short', style=shape.labeldown, textcolor=color.new(color.white, 0), size=size.tiny, location=location.abovebar, color=color.new(color.red, 0))
// Alerts of Twin Range Filter
alertcondition(long, title='Long', message='Long')
alertcondition(short, title='Short', message='Short')
/////////////////////////////////////////////////////////////////////////////////////////////////////////////\
// Calcuations for G-Channel Trend Detection
a = 0.
b = 0.
a := math.max(src, nz(a[1])) - nz(a[1] - b[1]) / length
b := math.min(src, nz(b[1])) + nz(a[1] - b[1]) / length
avg = math.avg(a, b)
//----
crossup = b[1] < close[1] and b > close
crossdn = a[1] < close[1] and a > close
bullish = ta.barssince(crossdn) <= ta.barssince(crossup)
c1 = bullish ? color.lime : color.red
//plot(a,"Upper",color=color.blue,linewidth=2,transp=100)
//plot(b,"Lower",color=color.blue,linewidth=2,transp=100)
plot(avg, 'Average', color=c1, linewidth=2)
// p2 = plot(close, 'Close price', color=c, linewidth=1, transp=100)
// fill(p1, p2, color=c, transp=90)
showcross = input(true)
// plotshape(showcross and not bullish and bullish[1] ? avg : na, location=location.absolute, style=shape.labeldown, color=color.new(color.red, 0), size=size.tiny, text='Sell', textcolor=color.new(#ffffff, 0), offset=-1)
// plotshape(showcross and bullish and not bullish[1] ? avg : na, location=location.absolute, style=shape.labelup, color=color.new(color.lime, 0), size=size.tiny, text='Buy', textcolor=color.new(#ffffff, 0), offset=-1)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calcuations for Market Bias Indicator
show_ha = input.bool(true, "Show HA Candles", inline='ha/mb display', group='Display Settings')
show_mb = input.bool(true, "Show Market Bias", inline='ha/mb display', group='Display Settings')
col_bull = input.color(color.lime, 'Color: Bullish', inline='bull/bear color', group='Display Settings', display=display.none)
col_bear = input.color(color.red, 'Bearish', inline='bull/bear color', group='Display Settings', display=display.none)
// These add an offset during data importation to avoid lookahead bias
indexHighTF = timeframe.in_seconds(ha_htf) == timeframe.in_seconds('') ? 0 : barstate.isrealtime ? 1 : 0
indexCurrTF = timeframe.in_seconds(ha_htf) == timeframe.in_seconds('') ? 0 : barstate.isrealtime ? 0 : 1
//@function Handles the import of data from other timeframes, while preventing repainting
//@param _resolution (str) : This is the timeframe to import data from.
//@param _expression (float | int) : This is the data to be imported
f_no_repaint_request(string _resolution, _expression) =>
request.security(syminfo.tickerid, _resolution, _expression[indexHighTF])[indexCurrTF]
//#endregion
//#region -----> CALCULATIONS
// Smoothen the OHLC values
o = ta.ema(open, ha_len)
c = ta.ema(close, ha_len)
h = ta.ema(high, ha_len)
l = ta.ema(low, ha_len)
// Calculate the Heikin Ashi OHLC values from it
haclose = f_no_repaint_request(ha_htf, (o + h + l + c) / 4)
xhaopen = f_no_repaint_request(ha_htf, (o + c) / 2)
haopen = na(xhaopen[1]) ? (o + c) / 2 : (xhaopen[1] + haclose[1]) / 2
hahigh = math.max(h, math.max(haopen, haclose))
halow = math.min(l, math.min(haopen, haclose))
// Smoothen the Heiken Ashi Candles
o2 = f_no_repaint_request(ha_htf, ta.ema(haopen, ha_len2))
c2 = f_no_repaint_request(ha_htf, ta.ema(haclose, ha_len2))
h2 = f_no_repaint_request(ha_htf, ta.ema(hahigh, ha_len2))
l2 = f_no_repaint_request(ha_htf, ta.ema(halow, ha_len2))
ha_avg = (h2 + l2) / 2
//TODO: Publish the Oscillator version of the indicator
// Oscillator {
osc_len = input.int(7, "Oscillator Period", group="HA Market Bias")
osc_bias = 100 * (c2 - o2)
osc_smooth = ta.ema(osc_bias, osc_len)
sigcolor = switch
(osc_bias > 0) and (osc_bias >= osc_smooth) => color.new(col_bull, 35)
(osc_bias > 0) and (osc_bias < osc_smooth) => color.new(col_bull, 75)
(osc_bias < 0) and (osc_bias <= osc_smooth) => color.new(col_bear, 35)
(osc_bias < 0) and (osc_bias > osc_smooth) => color.new(col_bear, 75)
=> color(na)
// }
//#endregion
//#region -----> PLOTS, ALERTS {
// Plots
p_h = plot(h2, "Bias High", color=color(na), display=display.data_window, editable=false)
p_l = plot(l2, "Bias Low", color=color(na), display=display.data_window, editable=false)
p_avg = plot(ha_avg, "Bias Avergae", color=color(na), display=display.data_window, editable=false)
fill(p_l, p_h, show_mb ? sigcolor : na)
col = o2 > c2 ? col_bear : col_bull
plotcandle(o2, h2, l2, c2, title='heikin smoothed', color=col, display=show_ha ? display.pane : display.data_window, editable=false)
// Alerts for Market Bias Indicator
// Bullish Trend Switch (Bearish -> Bullish)
alertcondition(ta.change(ta.change(math.sign(osc_bias)) > 0),
'Bullish Trend Switch (Bearish -> Bullish)', '{{exchange}}:{{ticker}}: Trend is now Bullish.')
// Bullish Trend Strengthens
alertcondition(osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0,
'Bullish Trend Strengthens', '{{exchange}}:{{ticker}}: Bullish Trend is now Stronger.')
// Bullish Trend Weakens
alertcondition(osc_bias > 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0,
'Bullish Trend Weakens', '{{exchange}}:{{ticker}}: Bullish Trend is now Weaker.')
// Bearish Trend Switch (Bullish -> Bearish)
alertcondition(ta.change(ta.change(math.sign(osc_bias)) < 0),
'Bearish Trend Switch (Bullish -> Bearish)', '{{exchange}}:{{ticker}}: Trend is now Bearish.')
// Bearish Trend Strengthens
alertcondition(osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) < 0,
'Bearish Trend Strengthens', '{{exchange}}:{{ticker}}: Bearish Trend is now Stronger.')
// Bearish Trend Weakens
alertcondition(osc_bias < 0 and ta.change(math.sign(osc_bias - osc_smooth)) > 0,
'Bearish Trend Weakens', '{{exchange}}:{{ticker}}: Bearish Trend is now Weaker.')
//#endregion