Guest User

Untitled

a guest
Oct 16th, 2018
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. let period = 20; // Number of days
  2. var averages = [];
  3. let current_average_sum = 0;
  4. let current_sum = 0;
  5. let current_time = Date.now();
  6. var closing_prices = [];
  7. let market = "ETHBTC"; // maybe an array of different markets?
  8. let candleSize = "1d";
  9. let multiplier = (2 / (period + 1));
  10. let Close = 0;
  11. let EMA = 0;
  12. let SMA = 0;
  13. let previous_ema = 0;
  14.  
  15. const binance = require('node-binance-api')().options({
  16. APIKEY: 'xxx', // CHANGE THIS AND THE API SECRET!
  17. APISECRET: 'xxx',
  18. useServerTime: true,
  19. test: true // True = SandboxMode
  20. });
  21.  
  22. function fetchPrice() {
  23. binance.candlesticks(market, candleSize, (error, ticks, symbol) => {
  24. for (i = 0; i < ticks.length; i++) {
  25. let last_tick = ticks[i];
  26. let [time, open, high, low, close, volume, closeTime, assetVolume, trades, buyBaseVolume, buyAssetVolume, ignored] = last_tick;
  27. closing_prices.push(close);
  28. current_close = closing_prices[i];
  29. Close = parseFloat(current_close);
  30. }
  31. calculateSMA();
  32. calculateEMA();
  33. }, {
  34. limit: period,
  35. endTime: current_time
  36. });
  37. }
  38.  
  39. function calculateSMA() {
  40. for (i = 0; i < period; i++) {
  41. selected_elements = closing_prices.slice(0, period);
  42. for (x = 0; x < (selected_elements.length); x++) {
  43. current_value = selected_elements[x];
  44. current_sum += parseFloat(current_value);
  45. }
  46. closing_prices.shift();
  47. averages.push(current_sum / period);
  48. current_sum = 0;
  49. }
  50. SMA = averages[0];
  51. console.log(SMA + " SMA ");
  52. return SMA;
  53. }
  54. fetchPrice();
  55.  
  56. function calculateEMA() {
  57. if (EMA == 0) {
  58. EMA = (Close - SMA) * multiplier + SMA;
  59. console.log(" EMA . " + EMA);
  60. previous_ema = EMA;
  61. //return EMA;
  62. calculateEMA();
  63. } else {
  64. for (a = 0; a < period; a++) {
  65. EMA = (Close - previous_ema) * multiplier + previous_ema;
  66. console.log(EMA + " ema");
  67. //console.log(previous_ema + " previous");
  68. previous_ema = EMA;
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment