-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathta.pine
More file actions
367 lines (301 loc) · 12.2 KB
/
ta.pine
File metadata and controls
367 lines (301 loc) · 12.2 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//@version=5
// https://qpace.dev
library("ta")
// Total money flowing in and out (Accumulation/Distribution)
export accdist() =>
ta.accdist
// Adds up the values so far (running total)
export cum(series float src) =>
ta.cum(src)
// How much it moved from the previous bar
export change(series float src) =>
ta.change(src)
// Number of bars since something happened
export barssince(series bool condition) =>
ta.barssince(condition)
// Speed of change over given bars
export roc(series float src, int length = 14) =>
ta.roc(src, length)
// When line1 moves above line2
export crossover(series float source1, series float source2) =>
ta.crossover(source1, source2)
// When line1 drops below line2
export crossunder(series float source1, series float source2) =>
ta.crossunder(source1, source2)
// When two lines meet
export cross(series float source1, series float source2) =>
ta.cross(source1, source2)
// Bar index with highest value in period
export highestbars(series float src, int length = 14) =>
ta.highestbars(src, length)
// Bar index with lowest value in period
export lowestbars(series float src, int length = 14) =>
ta.lowestbars(src, length)
// Highest value in period
export highest(series float src, int length = 14) =>
ta.highest(src, length)
// Lowest value in period
export lowest(series float src, int length = 14) =>
ta.lowest(src, length)
// Smoothed weighted moving average line
export swma(series float src) =>
ta.swma(src)
// Simple moving average (plain average)
export sma(series float src, int length = 14) =>
ta.sma(src, length)
// Exponential moving average (reacts faster)
export ema(series float src, int length = 14) =>
ta.ema(src, length)
// RMA used inside RSI
export rma(series float src, int length = 14) =>
ta.rma(src, length)
// Weighted moving average (recent bars matter more)
export wma(series float src, int length = 14) =>
ta.wma(src, length)
// Linear weighted moving average
export lwma(series float src, int length = 14) =>
ta.lwma(src, length)
// Hull moving average (smooth and fast)
export hma(series float src, int length = 14) =>
ta.hma(src, length)
// Volume‑weighted moving average
export vwma(series float src, int length = 14) =>
ta.vwma(src, length)
// Standard deviation (how much it varies)
export dev(series float src, int length = 14) =>
ta.dev(src, length)
// True range (how much price moved in bar)
export tr(bool handle_na = true) =>
ta.tr(handle_na)
// Average true range (typical move size)
export atr(int length = 14) =>
ta.atr(length)
// Relative Strength Index (momentum strength)
export rsi(series float src, int length = 14) =>
ta.rsi(src, length)
// Commodity Channel Index (price vs average)
export cci(series float src, int length = 14) =>
ta.cci(src, length)
// Standard deviation over period
export stdev(series float src, int length, bool biased = true) =>
ta.stdev(src, length, biased)
// Aroon indicator (time since highs/lows)
export aroon(int length = 14) =>
ta.aroon(length)
// Supertrend line for direction
export supertrend(series float src, float factor, int atr_period) =>
ta.supertrend(src, factor, atr_period)
// Awesome Oscillator (momentum)
export awesome_oscillator(series float src, int slow_length = 5, int fast_length = 34) =>
ao = ta.sma(src, fast_length) - ta.sma(src, slow_length)
diff = ao - ao[1]
diff
// Balance of power between buyers and sellers
export balance_of_power() =>
(close - open) / (high - low)
// %B – where price is inside Bollinger Bands (0‑1)
export bollinger_bands_pct_b(series float src, int length = 20, float mult = 2.0) =>
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
bbr = (src - lower)/(upper - lower)
bbr
// Band width – how wide Bollinger Bands are
export bollinger_bands_width(series float src, int length = 20, float mult = 2.0) =>
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
bbw = ((upper - lower) / basis) * 100
bbw
// Gives upper and lower Bollinger Bands
export bollinger_bands(series float src, int length = 20, float mult = 2.0) =>
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
[upper, lower]
// Chaikin Money Flow – volume weighted flow
export chaikin_money_flow(int length = 20) =>
var cumVol = 0.
cumVol += nz(volume)
ad = close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume
mf = math.sum(ad, length) / math.sum(volume, length)
mf
// Chande‑Kroll stop lines
export chande_kroll_stop(int atr_length = 10, float atr_coeff = 1.0, int stop_length = 9) =>
p = atr_length
x = atr_coeff
q = stop_length
first_high_stop = ta.highest(high, p) - x * ta.atr(p)
first_low_stop = ta.lowest(low, p) + x * ta.atr(p)
stop_short = ta.highest(first_high_stop, q)
stop_long = ta.lowest(first_low_stop, q)
[stop_short, stop_long]
// Choppiness Index – tells if market is sideways or trending
export choppiness_index(int length = 14) =>
100 * math.log10(math.sum(ta.atr(1), length) / (ta.highest(length) - ta.lowest(length))) / math.log10(length)
connors_rsi_updown(series float s) =>
isEqual = s == s[1]
isGrowing = s > s[1]
ud = 0.0
ud := isEqual ? 0 : isGrowing ? (nz(ud[1]) <= 0 ? 1 : nz(ud[1])+1) : (nz(ud[1]) >= 0 ? -1 : nz(ud[1])-1)
ud
export connors_rsi(series float src, int rsi_length = 3, int up_down_length = 2, int roc_length = 100) =>
rsi = ta.rsi(src, rsi_length)
updownrsi = ta.rsi(connors_rsi_updown(src), up_down_length)
percentrank = ta.percentrank(ta.roc(src, 1), roc_length)
crsi = math.avg(rsi, updownrsi, up_down_length)
crsi
// Coppock Curve – long‑term momentum
export coppock_curve(series float src, int wma_length = 10, int long_roc_length = 14, int short_roc_length = 11) =>
ta.wma(ta.roc(src, long_roc_length) + ta.roc(src, short_roc_length), wma_length)
// @TODO: Directional Movement Index
// Donchian Channel highs/lows
export donchian_channel(series float src, int length = 20) =>
lower = ta.lowest(length)
upper = ta.highest(length)
basis = math.avg(upper, lower)
[upper, lower, basis]
// MACD line (fast trend vs slow)
export macd(series float src, int short_length = 12, int long_length = 26) =>
macd = ta.ema(src, short_length) - ta.ema(src, long_length)
macd
// Price oscillator in percent
export price_oscillator(series float src, int short_length = 12, int long_length = 26) =>
short = ta.ema(src, short_length)
long = ta.ema(src, long_length)
po = (short - long) / long * 100
po
// Relative Vigor Index – strength of close vs range
export relative_vigor_index(int length = 14) =>
math.sum(ta.swma(close-open), length)/math.sum(ta.swma(high-low),length)
// Relative Volatility Index – like RSI but for volatility
export relative_volatility_index(series float src, int length = 14) =>
stddev = ta.stdev(src, length)
upper = ta.ema(ta.change(src) <= 0 ? 0 : stddev, length)
lower = ta.ema(ta.change(src) > 0 ? 0 : stddev, length)
rvi = upper / (upper + lower) * 100
rvi
// Stochastic RSI
export stochastic_rsi(series float src, int stoch_length = 14, int rsi_length = 14, int k = 3, int d = 3) =>
rsi1 = ta.rsi(src, rsi_length)
res_k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, stoch_length), k)
res_d = ta.sma(k, d)
[res_k, res_d]
__ultimate_oscillator_average__(float bp, float tr_, int length) =>
math.sum(bp, length) / math.sum(tr_, length)
// Ultimate Oscillator – combines 3 speeds
export ultimate_oscillator(int fast_length = 7, int medium_length = 14, int slow_length = 28) =>
high_ = math.max(high, close[1])
low_ = math.min(low, close[1])
bp = close - low_
tr_ = high_ - low_
avg7 = __ultimate_oscillator_average__(bp, tr_, fast_length)
avg14 = __ultimate_oscillator_average__(bp, tr_, medium_length)
avg28 = __ultimate_oscillator_average__(bp, tr_, slow_length)
out = 100 * (4*avg7 + 2*avg14 + avg28)/7
out
// Volume Oscillator – volume momentum
export volume_oscillator(int short_length = 5, int long_length = 10) =>
short = ta.ema(volume, short_length)
long = ta.ema(volume, long_length)
vo = (short - long) / long * 100
vo
// Vortex Indicator – shows trend direction
export vortex_indicator(int length = 14) =>
VMP = math.sum(math.abs(high -low[1]), length )
VMM = math.sum(math.abs(low - high[1]), length )
STR = math.sum(ta.atr(1), length )
VIP = VMP / STR
VIM = VMM / STR
[VIP, VIM]
// Williams %R – overbought/oversold
export williams_pct_r(series float src, int length = 14) =>
max = ta.highest(length)
min = ta.lowest(length)
wpctr = 100 * (src - max) / (max - min)
wpctr
// Advance/Decline Ratio (Bars)
export advance_decline_ratio(int length = 9) =>
is_up = (close - open) >= 0.0
up_bars = math.sum(is_up ? 1 : 0, length)
down_bars = math.sum(not is_up ? 1 : 0, length)
ad = down_bars == 0 ? up_bars : up_bars / down_bars
ad
// Detrended Price Oscillator (DPO)
export detrended_price_oscillator(int length = 21, bool centered = false) =>
barsback = length / 2.0 + 1.0
ma = ta.sma(close, length)
dpo = centered ? close[barsback] - ma : close - ma[barsback]
dpo
// Bull Bear Power (BBP)
export bull_bear_power(int length = 13) =>
ma = ta.ema(close, length)
bull_power = high - ma
bear_power = low - ma
bbp = bull_power - bear_power
bbp
// Absolute Price Oscillator (APO)
export absolute_price_oscillator(series float src, int fast_length = 12, int slow_length = 26) =>
_fast = math.min(fast_length, slow_length)
_slow = math.max(fast_length, slow_length)
ta.ema(src, _fast) - ta.ema(src, _slow)
// Know Sure Thing (KST)
export know_sure_thing(series float src, int roc_length1 = 10, int roc_length2 = 15, int roc_length3 = 20, int roc_length4 = 30, int sma_length1 = 10, int sma_length2 = 10, int sma_length3 = 10, int sma_length4 = 15, int sig_length = 9) =>
smaroc1 = ta.sma(ta.roc(src, roc_length1), sma_length1)
smaroc2 = ta.sma(ta.roc(src, roc_length2), sma_length2)
smaroc3 = ta.sma(ta.roc(src, roc_length3), sma_length3)
smaroc4 = ta.sma(ta.roc(src, roc_length4), sma_length4)
kst = smaroc1 + smaroc2 * 2 + smaroc3 * 3 + smaroc4 * 4
sig = ta.sma(kst, sig_length)
[kst, sig]
// Momentum (MOM)
export momentum(series float src, int length = 10) =>
src - src[length]
// Trix
export trix(series float src, int length = 18) =>
10000 * ta.change(ta.ema(ta.ema(ta.ema(math.log(close), length), length), length))
// True Strength Index (TSI)
export true_strength_index(series float src, int long_length = 25, int short_length = 13) =>
pc = ta.change(src)
double_smoothed_pc = ta.ema(ta.ema(src, long_length), short_length)
double_smoothed_abs_pc = ta.ema(ta.ema(math.abs(pc), long_length), short_length)
tsi_value = 100.0 * (double_smoothed_pc / double_smoothed_abs_pc)
tsi_value
// Double Exponential Moving Average (DEMA)
export dema(series float src, int length = 9) =>
e1 = ta.ema(src, length)
e2 = ta.ema(e1, length)
dema = 2 * e1 - e2
dema
fwma_fiboWeight(int i) =>
phi = (1 + math.sqrt(5)) / 2
pow = math.pow(phi, i)
(pow - math.pow(-1, i) / pow) / math.sqrt(5.0)
// Fibonacci Weighted Moving Average (FWMA)
export fwma(series float src, int length = 14) =>
sum = 0.0
weightSum = 0.0
for i = 0 to length - 1
weight = fwma_fiboWeight(length - i)
sum := sum + nz(src[i]) * weight
weightSum := weightSum + weight
sum / weightSum
// Money Flow Index (MFI)
export money_flow_index(series float src, int length = 14) =>
ta.mfi(src, length)
// Ease of Movement (EOM)
export ease_of_movement(int length = 14, int divisor = 10000) =>
ta.sma(divisor * ta.change(hl2) * (high - low) / volume, length)
// Elder Force Index (EFI)
export elder_force_index(int length = 13) =>
ta.ema(ta.change(close) * volume, length)
// Tripple Exponential Moving Average (TEMA)
export tema(series float src, int length = 9) =>
e1 = ta.ema(src, length)
e2 = ta.ema(e1, length)
e3 = ta.ema(e2, length)
tema = 3 * e1 - 3 * e2 + e3
tema