Skip to content

Instantly share code, notes, and snippets.

@jensen
Created April 14, 2020 21:11
Show Gist options
  • Save jensen/edc3abec783b06facadd0d0ebfe06b48 to your computer and use it in GitHub Desktop.
Save jensen/edc3abec783b06facadd0d0ebfe06b48 to your computer and use it in GitHub Desktop.
Example of refactoring into functions
/* this function process and object x of the pair in parameters, and add this data to the existing data of the current 0 to 30sec interval.
after the 30 sec pass, a timeout will get the bmBuffer[pair].data and send it elsewhere, there as a constant. perhaps, but bmBuffer is a global let */
let bmBuffer = {};
function bmProcessPayload(x, pair) {
if (bmCfg.consoled.trade) {
consoled("trade", bmAutobotPayload(x, pair));
}
bmBuffer[pair].data.timestamp = new Date(x.timestamp);
bmBuffer[pair].data.date = new Date();
bmBuffer[pair].data.lag =
bmBuffer[pair].data.date - bmBuffer[pair].data.timestamp;
bmBuffer[pair].data.trade = !bmBuffer[pair].data.trade
? 1
: ++bmBuffer[pair].data.trade;
bmBuffer[pair].data.open =
bmBuffer[pair].data.open === null ? x.price : bmBuffer[pair].data.open;
bmBuffer[pair].data.high =
bmBuffer[pair].data.high === null
? x.price
: bmBuffer[pair].data.high < x.price
? x.price
: bmBuffer[pair].data.high;
bmBuffer[pair].data.low =
bmBuffer[pair].data.low === null
? x.price
: bmBuffer[pair].data.low > x.price
? x.price
: bmBuffer[pair].data.low;
bmBuffer[pair].data.close = x.price;
bmBuffer[pair].data.allVolume = !bmBuffer[pair].data.allVolume
? x.size
: bmBuffer[pair].data.allVolume + x.size;
bmBuffer[pair].data.allGrossValue = !bmBuffer[pair].data.allGrossValue
? x.grossValue
: bmBuffer[pair].data.allGrossValue + x.grossValue;
bmBuffer[pair].data.allHomeNotional = !bmBuffer[pair].data.allHomeNotional
? x.homeNotional
: bmBuffer[pair].data.allHomeNotional + x.homeNotional;
bmBuffer[pair].data.allForeignNotional = !bmBuffer[pair].data
.allForeignNotional
? x.foreignNotional
: bmBuffer[pair].data.allForeignNotional + x.foreignNotional;
switch (x.side) {
case "Sell":
bmBuffer[pair].data.sellsVolume = !bmBuffer[pair].data.sellsVolume
? x.size
: bmBuffer[pair].data.sellsVolume + x.size;
bmBuffer[pair].data.sellsGrossValue = !bmBuffer[pair].data.sellsGrossValue
? x.grossValue
: bmBuffer[pair].data.sellsGrossValue + x.grossValue;
bmBuffer[pair].data.sellsHomeNotional = !bmBuffer[pair].data
.sellsHomeNotional
? x.homeNotional
: bmBuffer[pair].data.sellsHomeNotional + x.homeNotional;
bmBuffer[pair].data.sellsForeignNotional = !bmBuffer[pair].data
.sellsForeignNotional
? x.foreignNotional
: bmBuffer[pair].data.sellsForeignNotional + x.foreignNotional;
break;
case "Buy":
bmBuffer[pair].data.buysVolume = !bmBuffer[pair].data.buysVolume
? x.size
: bmBuffer[pair].data.buysVolume + x.size;
bmBuffer[pair].data.buysGrossValue = !bmBuffer[pair].data.buysGrossValue
? x.grossValue
: bmBuffer[pair].data.buysGrossValue + x.grossValue;
bmBuffer[pair].data.buysHomeNotional = !bmBuffer[pair].data
.buysHomeNotional
? x.homeNotional
: bmBuffer[pair].data.buysHomeNotional + x.homeNotional;
bmBuffer[pair].data.buysForeignNotional = !bmBuffer[pair].data
.buysForeignNotional
? x.foreignNotional
: bmBuffer[pair].data.buysForeignNotional + x.foreignNotional;
break;
default:
}
bmBuffer[pair].data.sma100 = Number(smaX(pair, 100).toFixed(4));
bmBuffer[pair].data.sma50 = Number(smaX(pair, 50).toFixed(4));
bmBuffer[pair].data.sma30 = Number(smaX(pair, 30).toFixed(4));
bmBuffer[pair].data.sma21 = Number(smaX(pair, 21).toFixed(4));
bmBuffer[pair].data.sma20 = Number(smaX(pair, 20).toFixed(4));
bmBuffer[pair].data.sma10 = Number(smaX(pair, 10).toFixed(4));
bmBuffer[pair].data.ema21 = generateEma21(bmBuffer[pair].data);
bmBuffer[pair].data.ema10 = generateEma10(bmBuffer[pair].data);
bmBuffer[pair].data.macdLine = Number(
(bmBuffer[pair].data.ema10 - bmBuffer[pair].data.ema21).toFixed(4)
);
sum = 0;
i = bmHistory[pair].data.length - 8;
y = 8 + i;
while (i < y) {
sum += bmHistory[pair].data[i].macdLine;
++i;
}
sum = sum / 8;
bmBuffer[pair].data.signalLine = Number(
(bmBuffer[pair].data.macdLine * emaK(9) + sum * (1 - emaK(9))).toFixed(4)
);
bmBuffer[pair].data.macdHistogram = Number(
(bmBuffer[pair].data.macdLine - bmBuffer[pair].data.signalLine).toFixed(4)
);
bmBuffer[pair].status = true;
}
function generateEma21(data) {
let sum = 0;
let i = data.length - 20;
let y = 20 + i;
while (i < y) {
if (i === 0) {
sum += data[i].sma20;
} else {
sum += data[i].close;
}
++i;
}
return Number(
(
bmBuffer[pair].data.close * emaK(21) +
((sum + data.close) / 21) * (1 - emaK(21))
).toFixed(4)
);
}
function generateEma10(data) {
let sum = 0;
let i = data.length - 9;
let y = 9 + i;
while (i < y) {
if (i === 0) {
sum += data[i].sma10;
} else {
sum += data[i].close;
}
++i;
}
return Number((data.close * emaK(10) + ((sum + data.close) / 10) * (1 - emaK(10))).toFixed(4));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment