Skip to content

Instantly share code, notes, and snippets.

@temp3l
Last active February 19, 2022 22:48
Show Gist options
  • Save temp3l/49a220213c0576fd346fd954eead225b to your computer and use it in GitHub Desktop.
Save temp3l/49a220213c0576fd346fd954eead225b to your computer and use it in GitHub Desktop.
super trend ohlc technicalindicators
//fork of https://gist.github.com/sedhuait/985f84afba07e170357a8b8fa0129ebb
const ATR = require('technicalindicators').ATR;
const HA = require('technicalindicators').HeikinAshi;
const moment = require('moment');
const _ = require('lodash');
const Super = function(result, { multiplier = 3, period = 7, is_ha = true } = {}) {
let open = [], close = [], high = [], low = [], volume=[], timestamp=[];
_.each(result, function(item) {
open.push( Number( item.open ));
close.push( Number( item.close ));
high.push( Number( item.high ));
low.push( Number( item.low ));
volume.push( Number( item.volume ));
timestamp.push(item.date)
});
let atr_result=[];
if (is_ha) {
let ha_results_arr = HA.calculate({ high: high, low: low, close: close, open: open, volume: volume, timestamp: timestamp });
let ha_open = ha_results_arr.open;
let ha_high = ha_results_arr.high;
let ha_low = ha_results_arr.low;
let ha_close = ha_results_arr.close;
let ha_final_results = [];
atr_result = ATR.calculate({ high: ha_high, low: ha_low, close: ha_close, period: period });
_.each(result, function(item, i) {
ha_final_results.push({
open: Number(ha_open[i]),
high: Number(ha_high[i]),
low: Number(ha_low[i]),
close: Number(ha_close[i]),
date: new Date(item.date).getTime(),
timestamp: new Date(item.date).getTime()
});
});
result = ha_final_results;
} else {
atr_result = ATR.calculate({ high: high, low: low, close: close, period: period,volume:volume, timestamp: timestamp});
}
let previous_final_upper = 0, previous_final_low = 0, previous_st, finalres;
for (let i = 0; i < atr_result.length; i++) {
let result_counter = i + period,
tdata = result[result_counter],
atr = atr_result[i],
high = Number(tdata.high),
low = Number(tdata.low),
base_upper = (((high + low) / 2) + (multiplier * atr)),
base_lower = (((high + low) / 2) - (multiplier * atr)),
final_upper = 0,
final_lower = 0,
previous_close = 0,
previous_data = result[result_counter - 1];
previous_final_upper = 0;
previous_final_low = 0;
previous_final_upper = previous_data.fup || 0;
previous_final_low = previous_data.flow || 0;
previous_close = previous_data.close || 0;
previous_st = previous_data.st || 0;
let st,
result_action;
final_upper = (base_upper < previous_final_upper || previous_close > previous_final_upper) ? base_upper : previous_final_upper;
final_lower = (base_lower > previous_final_low || previous_close < previous_final_low) ? base_lower : previous_final_low;
if (previous_st == previous_final_upper && tdata.close <= final_upper) {
st = final_upper;
} else if (previous_st == previous_final_upper && tdata.close >= final_upper) {
st = final_lower;
} else if (previous_st == previous_final_low && tdata.close >= final_lower) {
st = final_lower;
} else if (previous_st == previous_final_low && tdata.close <= final_lower) {
st = final_upper;
} else {
st = 0;
}
result[result_counter].fup = final_upper;
result[result_counter].flow = final_lower;
result[result_counter].st = st;
if (tdata.close > st) {
result_action = "Buy";
} else if (tdata.close < st) {
result_action = "Sell";
}
finalres = {
timestamp: tdata.date,
open: Number(tdata.open).toFixed(2),
high: Number(tdata.high).toFixed(2),
low: Number(tdata.low).toFixed(2),
close: Number(tdata.close).toFixed(2),
pclose: Number(previous_close).toFixed(2),
atr: Number(atr).toFixed(2),
bu: Number(base_upper).toFixed(2),
bl: Number(base_lower).toFixed(2),
fu: Number(final_upper).toFixed(2),
fl: Number(final_lower).toFixed(2),
st: Number(st).toFixed(2),
action: result_action
};
}
console.log("HA ST Indicator", finalres);
return finalres;
};
export default Super
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment