|
| 1 | +async function indicator(tickerId, timeframe = '1w', periods = 500, stime, etime) { |
| 2 | + const pineTS = new PineTS(PineTS.Provider.Binance, tickerId, timeframe, periods, stime, etime); |
| 3 | + |
| 4 | + const { result, plots, marketData } = await pineTS.run((context) => { |
| 5 | + // This is a PineTS port of "Squeeze Momentum Indicator" indicator by LazyBear |
| 6 | + // List of all his indicators: https://www.tradingview.com/v/4IneGo8h/ |
| 7 | + const { close, high, low } = context.data; |
| 8 | + |
| 9 | + const ta = context.ta; |
| 10 | + const math = context.math; |
| 11 | + |
| 12 | + const input = context.input; |
| 13 | + const { plot, plotchar, nz, color } = context.core; |
| 14 | + |
| 15 | + const length = input.int(20, 'BB Length'); |
| 16 | + const mult = input.float(2.0, 'BB MultFactor'); |
| 17 | + const lengthKC = input.int(20, 'KC Length'); |
| 18 | + const multKC = input.float(1.5, 'KC MultFactor'); |
| 19 | + |
| 20 | + const useTrueRange = input.bool(true, 'Use TrueRange (KC)'); |
| 21 | + |
| 22 | + // Calculate BB |
| 23 | + let source = close; |
| 24 | + const basis = ta.sma(source, length); |
| 25 | + const dev = multKC * ta.stdev(source, length); |
| 26 | + const upperBB = basis + dev; |
| 27 | + const lowerBB = basis - dev; |
| 28 | + |
| 29 | + // Calculate KC |
| 30 | + const ma = ta.sma(source, lengthKC); |
| 31 | + const range_1 = useTrueRange ? ta.tr : high - low; |
| 32 | + const rangema = ta.sma(range_1, lengthKC); |
| 33 | + const upperKC = ma + rangema * multKC; |
| 34 | + const lowerKC = ma - rangema * multKC; |
| 35 | + |
| 36 | + const sqzOn = lowerBB > lowerKC && upperBB < upperKC; |
| 37 | + const sqzOff = lowerBB < lowerKC && upperBB > upperKC; |
| 38 | + const noSqz = sqzOn == false && sqzOff == false; |
| 39 | + |
| 40 | + const val = ta.linreg( |
| 41 | + source - math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)), ta.sma(close, lengthKC)), |
| 42 | + lengthKC, |
| 43 | + 0 |
| 44 | + ); |
| 45 | + |
| 46 | + const iff_1 = val > nz(val[1]) ? color.lime : color.green; |
| 47 | + const iff_2 = val < nz(val[1]) ? color.red : color.maroon; |
| 48 | + const bcolor = val > 0 ? iff_1 : iff_2; |
| 49 | + const scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray; |
| 50 | + plot(val, 'Momentum', { color: bcolor, style: 'histogram', linewidth: 4 }); |
| 51 | + plot(0, 'Cross', { color: scolor, style: 'cross', linewidth: 2 }); |
| 52 | + }); |
| 53 | + |
| 54 | + return { result, plots, marketData }; |
| 55 | +} |
0 commit comments