Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. require('./util');
  2.  
  3. const Candler = require('./candle-db');
  4.  
  5. const ATR_LENGTH = 24;
  6. const SMA_LENGTH = 24;
  7.  
  8. let trs = []; // series. needed by atr() function.
  9.  
  10. // Open the database, cut down version to save size, contains only 2019 1h candle data
  11. let candler = new Candler('./bitmex-xbt-1h-2019-only.db');
  12.  
  13. // Get H1 xbtusd candles from 2019 year open
  14. let candles = candler.get_range('bitmex', '1h', 'btcusd', date(2019,1,1), Date.now());
  15.  
  16.  
  17. // Go through each of the candles, starting from the first one at 00:00 on 1st January 2019
  18. for (let i=0; i < candles.length; i++)
  19. {
  20. // Current ATR for this candle
  21. let a = atr( candles, i, ATR_LENGTH );
  22.  
  23. // Let's also compute the SMA(24) of the close price:
  24. let s = sma ( (candles.slice(0, i + 1)).map ( e => e.close ), SMA_LENGTH );
  25.  
  26. // uncomment this line to see the atr and sma for this H1 candle:
  27. // console.log(`atr = ${a}, sma = ${s} timestamp = ${strdate(candles[i].timestamp)}`);
  28.  
  29. let dm = is_darth_maul( candles[i], a, s );
  30.  
  31. if (dm)
  32. {
  33. console.log(`\n🚨 The Sith will rule the galaxy: ${strdate(candles[i].timestamp)}`);
  34. console.log( candles[i] );
  35. }
  36.  
  37. }
  38.  
  39.  
  40. function is_darth_maul(candle, current_atr, current_sma)
  41. {
  42. const DARTH_MAUL_RANGE = 1.0; // wicks that are +100% outside the ATR band's range
  43.  
  44. // The way this works is we compute an ATR 'band' using the SMA of the close
  45. // price and the ATR of the current candle
  46.  
  47. let atr_upper_band = current_sma + current_atr;
  48. let atr_lower_band = current_sma - current_atr;
  49. let band_range = atr_upper_band - atr_lower_band;
  50.  
  51. // Now we see by how much (if at all) the wicks extended through the band's range
  52.  
  53. let upper_wick_dev = candle.high - atr_upper_band;
  54. let lower_wick_dev = atr_lower_band - candle.low;
  55.  
  56. // if either wick inside the band, it's definitely not a Darth Maul, return
  57.  
  58. if (upper_wick_dev < 0 || lower_wick_dev < 0)
  59. return false;
  60.  
  61. // Ok, they both exceeded the band, but by what factor?
  62.  
  63. let upper_factor = upper_wick_dev / band_range;
  64. let lower_factor = lower_wick_dev / band_range;
  65.  
  66. // Make sure they both did, upside or downside only doesn't count
  67. if (upper_factor >= DARTH_MAUL_RANGE && lower_factor >= DARTH_MAUL_RANGE)
  68. return true;
  69.  
  70.  
  71. return false;
  72. }
  73.  
  74.  
  75. // Function to compute ATR
  76. function atr(candles, index, length)
  77. {
  78. // Get the current candle
  79. let candle0 = candles[index];
  80.  
  81. // Get the previous candle
  82. let candle1 = (index >= 0) ? candles[index-1] : null;
  83.  
  84. // not sure if I need to do this or can use candle0 alone :thonking:
  85. if (!candle1) return;
  86.  
  87. // First, let's calculate the True Range, from the PineScript documentation:
  88. // max(high - low, abs(high - close[1]), abs(low - close[1]))
  89.  
  90. let tr = Math.max( candle0.high - candle0.low,
  91. Math.max(
  92. Math.abs(candle0.high - candle1.close),
  93. Math.abs(candle0.low - candle1.close)
  94. )
  95. );
  96.  
  97. // Save our ongoing series of True Ranges
  98. trs.push(tr)
  99.  
  100. // Now return the simple-moving-average of them (see util.js):
  101. return sma( trs, length );
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement