Guest User

Untitled

a guest
Jan 12th, 2019
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. const _ = require('lodash');
  2. const util = require('util');
  3. const log = require('../core/log.js');
  4. const config = require ('../core/util.js').getConfig();
  5. const candleBatcher = require('../core/candleBatcher');
  6. const TEMA = require('../strategies/indicators/TEMA.js');
  7. const TULIPASYNC = require('../strategies/indicators/TulipAsync.js');
  8. const TALIBASYNC = require('../strategies/indicators/TalibAsync.js');
  9. var obj;
  10.  
  11. var stratMain = {};
  12.  
  13.  
  14. stratMain.init = function (context) {
  15. if(context === undefined) {
  16. this.context = this;
  17. } else {
  18. this.context = context;
  19. }
  20.  
  21. obj = this;
  22. this.name = 'T5mainasync';
  23.  
  24. this.context.exposedMain = false;
  25. this.context.trendMain = {
  26. direction: 'none',
  27. duration: 0,
  28. persisted: false,
  29. adviced: false
  30. };
  31.  
  32. this.cb60 = new candleBatcher(60);
  33. this.cb60.on('candle', this.onCandle60M);
  34. this.requiredHistory = this.context.tradingAdvisor.historySize;
  35. this.intCandleSize = this.context.tradingAdvisor.candleSize;
  36.  
  37. this.RSIhigh = this.context.settings.thresholds.RSIhigh;
  38. this.RSIlow = this.context.settings.thresholds.RSIlow;
  39. this.MACDhigh = this.context.settings.thresholds.MACDhigh;
  40. this.MACDlow = this.context.settings.thresholds.MACDlow;
  41. this.persistance = this.context.settings.thresholds.persistance;
  42.  
  43. const MACDSettings = this.context.settings.MACD;
  44. const EMAshortSettings = this.context.settings.EMAshort;
  45. const EMAlongSettings = this.context.settings.EMAlong;
  46. const STOCHSettings = this.context.settings.STOCH;
  47. const RSISettings = this.context.settings.RSI;
  48.  
  49. this.macd60M = new TULIPASYNC({ indicator: 'macd', length: 500, candleinput: 'close', options:[ MACDSettings.optInFastPeriod, MACDSettings.optInSlowPeriod, MACDSettings.optInSignalPeriod ] });
  50. this.emashort60M = new TULIPASYNC({ indicator: 'ema', length: 500, candleinput: 'close', options:[ EMAshortSettings.optInTimePeriod ] });
  51. this.emalong60M = new TULIPASYNC({ indicator: 'ema', length: 500, candleinput: 'close', options:[ EMAlongSettings.optInTimePeriod ] });
  52. this.rsi60M = new TULIPASYNC({ indicator: 'rsi', length: 500, candleinput: 'close', options:[ RSISettings.optInTimePeriod ] });
  53. this.stoch60M = new TULIPASYNC({ indicator: 'stoch', length: 500, candleinput: 'high,low,close', options:[ STOCHSettings.optInFastKPeriod, STOCHSettings.optInSlowKPeriod, STOCHSettings.optInSlowDPeriod ] });
  54. this.mfi60M = new TALIBASYNC({ indicator: 'mfi', length: 5000, options: { optInTimePeriod: 14 } });
  55. this.tema60M = new TEMA({ weight: 440 });
  56. this.tema60M.count = 0;
  57. }
  58.  
  59.  
  60. // ***************************************************************************
  61. // * 1 Min candles - candle batching
  62. stratMain.onCandle = async function (candle) {
  63. this.cb60.write([candle]);
  64. this.cb60.flush();
  65. }
  66.  
  67.  
  68. // ***************************************************************************
  69. // * 60 Min candles - strong market strat
  70. stratMain.onCandle60M = async function (candle) {
  71. obj.tema60M.update(candle.close);
  72. obj.tema60M.count++;
  73. await obj.update60M(candle);
  74.  
  75. if (obj.tema60M.count < obj.requiredHistory) {
  76. if (obj.tema60M.count % 10 == 0) log.debug('Warmup indicators with history data:', obj.tema60M.count, '/', obj.requiredHistory, ' (', obj.intCandleSize*obj.requiredHistory/60/24, 'days )');
  77. return;
  78. }
  79.  
  80. //define necessary entry trend strength based on bollinger band width and ppo
  81. obj.tema60M.treshold = 100.065;
  82.  
  83. if ((obj.tema60M.trend > obj.tema60M.treshold) || obj.exposedMain) {
  84. obj.check60M(candle);
  85. }
  86. }
  87.  
  88.  
  89. // ***************************************************************************
  90. // * 60 Min candles - update
  91. stratMain.update60M = async function (candle) {
  92. this.macd60M.result = await this.macd60M.update(candle);
  93. this.macd60M.macd = this.macd60M.result[0];
  94. this.emashort60M.result = (await this.emashort60M.update(candle))[0];
  95. this.emalong60M.result = (await this.emalong60M.update(candle))[0];
  96. this.rsi60M.result = (await this.rsi60M.update(candle))[0];
  97. this.mfi60M.result = await this.mfi60M.update(candle);
  98. this.stoch60M.result = await this.stoch60M.update(candle);
  99. this.stoch60M.stochk = this.stoch60M.result[0];
  100. this.stoch60M.stochd = this.stoch60M.result[1];
  101. }
  102.  
  103.  
  104. // ***************************************************************************
  105. // * 60 Min candles - check
  106. stratMain.check60M = function(candle) {
  107. /*
  108. log.debug (
  109. '60M CANDLES:',
  110. candle.start.format(),
  111. candle.close,
  112. 'macd',
  113. this.macd60M.macd,
  114. 'emaShort',
  115. this.emashort60M.result,
  116. 'emaLong',
  117. this.emalong60M.result,
  118. 'rsi',
  119. this.rsi60M.result,
  120. 'mfi',
  121. this.mfi60M.result,
  122. 'stoch',
  123. this.stoch60M.stochk,
  124. this.stoch60M.stochd
  125. );
  126. */
  127.  
  128. if (this.emashort60M.result > this.emalong60M.result && this.stoch60M.stochk > this.stoch60M.stochd && this.macd60M.macd > this.MACDhigh && this.mfi60M.result > this.RSIhigh) {
  129. if (this.context.trendMain.direction !== 'up') {
  130. this.context.trendMain = {
  131. duration: 0,
  132. persisted: false,
  133. direction: 'up',
  134. adviced: false
  135. };
  136. }
  137.  
  138. this.context.trendMain.duration++;
  139.  
  140. if (this.context.trendMain.duration >= this.persistance) {
  141. this.context.trendMain.persisted = true;
  142. }
  143.  
  144. if (this.context.trendMain.persisted && !this.context.trendMain.adviced) {
  145. this.context.trendMain.adviced = true;
  146. this.context.exposedMain = true;
  147. log.debug('BUY with 60M main strategy...');
  148. this.context.advice('long');
  149. }
  150. } else if (this.emashort60M.result < this.emalong60M.result && this.stoch60M.stochk < this.stoch60M.stochd && this.macd60M.macd < this.MACDlow && this.mfi60M.result < this.RSIlow) {
  151. if (this.context.trendMain.direction !== 'down') {
  152. this.context.trendMain = {
  153. duration: 0,
  154. persisted: false,
  155. direction: 'down',
  156. adviced: false
  157. };
  158. }
  159.  
  160. this.context.trendMain.duration++;
  161.  
  162. if (this.context.trendMain.duration >= this.persistance) {
  163. this.context.trendMain.persisted = true;
  164. }
  165.  
  166. if (this.context.trendMain.persisted && !this.context.trendMain.adviced) {
  167. this.context.trendMain.adviced = true;
  168. this.context.exposedMain = false;
  169. log.debug('SELL with 60M main strategy...');
  170. this.context.advice('short');
  171. }
  172. }
  173. }
  174.  
  175.  
  176. stratMain.log = function () {
  177. }
  178.  
  179.  
  180. stratMain.update = function (candle) {
  181. //no need for strat update, we work with onCandle custom batching
  182. }
  183.  
  184.  
  185. stratMain.check = function (candle) {
  186. //no need for strat check, we work with onCandle custom batching and strat execution
  187. }
  188.  
  189.  
  190. module.exports = stratMain;
Advertisement
Add Comment
Please, Sign In to add comment