Find more indicators by LazyBear here, if you find this indicator useful, please support the author.
PineTS makes it easy to port your Pine Script code to JavaScript.
See the example below, this is the "Squeeze Momentum Indicator" indicator converted from Pine Script to
PineTS.
you can barely tell the difference if you ignore JS variables declarations syntax (const/let/...etc)
Pine Script v5 code | PineTS Ported code | |
---|---|---|
|
|
Once you port the code, you can use PineTS to run it, plot directives returns objects with all required data to render the charts, This is how we rendered the indicator above.
const pineTS = new PineTS( PineTS.Provider.Binance,
'BTCUSDT', //the tickerId
'W', //the timeframe in Pine Script format
500, //the number of periods to query
);
const { plots } = await pineTS.run((context) => {
const { close, high, low } = context.data; //import OHLCV data
const { plot, plotchar, nz, color } = context.core; //import core functions
const ta = context.ta; //import technical analysis namespace
const math = context.math; //import math namespace
const input = context.input; //import input namespace
//below is the converted code from Pine Script to PineTS
const length = input.int(20, "BB Length");
const mult = input.float(2.0, "BB MultFactor");
const lengthKC = input.int(20, "KC Length");
const multKC = input.float(1.5, "KC MultFactor");
const useTrueRange = input.bool(true, "Use TrueRange (KC)");
// Calculate BB
let source = close;
const basis = ta.sma(source, length);
const dev = multKC * ta.stdev(source, length);
const upperBB = basis + dev;
const lowerBB = basis - dev;
// Calculate KC
const ma = ta.sma(source, lengthKC);
const range_1 = useTrueRange ? ta.tr : high - low;
const rangema = ta.sma(range_1, lengthKC);
const upperKC = ma + rangema * multKC;
const lowerKC = ma - rangema * multKC;
const sqzOn = lowerBB > lowerKC && upperBB < upperKC;
const sqzOff = lowerBB < lowerKC && upperBB > upperKC;
const noSqz = sqzOn == false && sqzOff == false;
const val = ta.linreg(source - math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)), ta.sma(close, lengthKC)), lengthKC, 0);
const iff_1 = val > nz(val[1]) ? color.lime : color.green;
const iff_2 = val < nz(val[1]) ? color.red : color.maroon;
const bcolor = val > 0 ? iff_1 : iff_2;
const scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray;
plot(val, "Momentum", { color: bcolor, style: "histogram", linewidth: 4 });
plot(0, "Cross", { color: scolor, style: "cross", linewidth: 2 });
});
console.log(plots);