Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var config = require ('../core/util.js').getConfig();
  2. var SMA = require('./indicators/SMA.js');
  3. var RSI = require('./indicators/RSI.js');
  4. var MACD = require('./indicators/MACD.js');
  5. var VWAP = require('./indicators/VWAP.js');
  6.  
  7. var strat = {};
  8.  
  9. // helpers
  10. var _ = require('lodash');
  11. var log = require('../core/log.js');
  12.  
  13. var timeToBuy       = false;
  14. var timeToSell      = false;
  15. var MacdReady       = false;
  16. var RSIReady        = false;
  17. var VWAPReady       = false;
  18.  
  19. var firstrun        = true;
  20. var timePeriods     = 10;
  21.  
  22. var buyPrice        = 0;
  23. var sellPrice       = 0;
  24. var cutLossesPrice  = 0;
  25.  
  26. var candlesHistory = [];
  27. var rsiHistory = [];
  28.  
  29. var buyPriceTimeStamp = "";
  30.  
  31.  
  32.  
  33. //MARK: - INIT()
  34. strat.init = function() {
  35.     console.log('init function');
  36.     this.name = 'Fckin asdf for new POLO';
  37.     this.requiredHistory = this.tradingAdvisor.historySize;
  38.    
  39.     this.trend = {
  40.         direction: 'none',
  41.         duration: 0,
  42.         persisted: false,
  43.         adviced: false
  44.       };
  45.    
  46.       // how many candles do we need as a base
  47.       // before we can start giving advice?
  48.       this.requiredHistory = this.tradingAdvisor.historySize;
  49.    
  50.       // define the indicators we need
  51.       this.addIndicator('macd', 'MACD', this.settings);
  52.       this.addIndicator('rsi', 'RSI', this.settings);
  53.  
  54.       this.addIndicator('vwap', 'VWAP', this.settings);
  55.  
  56.       console.log(this.name);
  57. }
  58.  
  59.  
  60.   // EMAs and diff.
  61. strat.log = function() {
  62.     var digits = 8;
  63.     var macd = this.indicators.macd;
  64.  
  65.     var diff = macd.diff;
  66.     var signal = macd.signal.result;
  67.  
  68.     log.debug('calculated MACD properties for candle:');
  69.     log.debug('\t', 'short:', macd.short.result.toFixed(digits));
  70.     log.debug('\t', 'long:', macd.long.result.toFixed(digits));
  71.     log.debug('\t', 'macd:', diff.toFixed(digits));
  72.     log.debug('\t', 'signal:', signal.toFixed(digits));
  73.     log.debug('\t', 'macdiff:', macd.result.toFixed(digits));
  74.   }
  75.  
  76.  
  77.  
  78. //MARK: - CHECK()
  79. strat.check = function(candle) {
  80.  
  81.     // if (firstrun == true) {
  82.     //     candlesHistory = [];
  83.     //     rsiHistory = [];
  84.        
  85.     //     firstrun = false;
  86.     // }
  87.  
  88.    candlesHistory.unshift(candle);
  89. //    console.log(candlesHistory);
  90.  
  91.  
  92.     if (candlesHistory.lenght > timePeriods) {
  93.        candlesHistory.pop();
  94.     }
  95.  
  96.     // this.indicators.vwap.input = this.candle;
  97.     var vwap = this.indicators.vwap;
  98.     console.log("--------");
  99.     console.log(vwap.result);
  100.     console.log("--------");
  101.  
  102.  
  103.     var rsi = this.indicators.rsi;
  104.     var rsiVal = rsi.result;
  105.  
  106.     rsiHistory.unshift(rsiVal);
  107.     // console.log(rsiHistory[0]);
  108.  
  109.    
  110.  
  111.     if (rsiHistory.lenght > timePeriods) {
  112.        rsiHistory.pop();
  113.     }
  114.  
  115.  
  116.  
  117.     var macddiff = this.indicators.macd.result;
  118.  
  119.     //MARK: - CONSTANT CHECK
  120.  
  121.     if(!timeToSell) {
  122.  
  123.         if(macddiff > this.settings.thresholds.up) {
  124.  
  125.             // new trend detected
  126.             if(this.trend.direction !== 'up') {
  127.             // reset the state for the new trend
  128.             this.trend = {
  129.                 duration: 1,
  130.                 persisted: false,
  131.                 direction: 'up',
  132.                 adviced: false
  133.             };
  134.  
  135.             this.trend.duration++;
  136.  
  137.             log.debug('In uptrend since', this.trend.duration, 'candle(s)');
  138.  
  139.             if(this.trend.duration >= this.settings.thresholds.persistence) {
  140.                 this.trend.persisted = true;
  141.  
  142.                 if(this.trend.persisted && !this.trend.adviced) {
  143.  
  144.                 this.trend.adviced = true;
  145.                 MacdReady = true
  146.                 if (rsiHistory.length > 1) {
  147.                     console.log("asdfasdf");
  148.                     if (rsiHistory[0] < 33  || rsiHistory[1] < 33) {
  149.                             console.log("rsi gogogo");
  150.                             RSIReady = true;
  151.                            
  152.                             // this.advice('long');
  153.                         } else {
  154.                             RSIReady = false;
  155.                         }
  156.                         }
  157.                     }
  158.                  }
  159.             }
  160.         }
  161.         //  else if(macddiff < this.settings.thresholds.down) {
  162.  
  163.             // // new trend detected
  164.             // if(this.trend.direction !== 'down')
  165.             // // reset the state for the new trend
  166.             // this.trend = {
  167.             //     duration: 2,
  168.             //     persisted: false,
  169.             //     direction: 'down',
  170.             //     adviced: false
  171.             // };
  172.        
  173.             // this.trend.duration++;
  174.        
  175.             // log.debug('In downtrend since', this.trend.duration, 'candle(s)');
  176.        
  177.             // if(this.trend.duration >= this.settings.thresholds.persistence)
  178.             // this.trend.persisted = true;
  179.            
  180.             // if(this.trend.persisted && !this.trend.adviced) {
  181.             // this.trend.adviced = true;
  182.             // MacdReady = true
  183.             // if (this.rsiHistory.lenght > 2) {
  184.             //     if (this.rsiHistory[this.rsiHistory.lenght] < 33
  185.             //         || this.rsiHistory[this.rsiHistory.lenght - 1] < 33
  186.             //         || this.rsiHistory[this.rsiHistory.lenght - 2] < 33) {
  187.             //             RSIReady = true;
  188.                        
  189.             //             // this.advice('long');
  190.             //         } else {
  191.             //             RSIReady = false;
  192.             //         }
  193.             //     }
  194.             //     // this.advice('short');
  195.             // } else {
  196.             //     MacdReady = false;
  197.             // }
  198.         // }
  199.  
  200.  
  201.  
  202.         if (MacdReady && RSIReady) {
  203.             timeToBuy = true;
  204.             timeToSell = false;
  205.         } else {
  206.             timeToBuy = false;
  207.             timeToSell = true;
  208.             MacdReady = false;
  209.             RSIReady = false;
  210.  
  211.         }
  212.     }
  213.  
  214.       if (timeToBuy) {
  215.         timeToBuy = false;
  216.         this.advice('long');
  217.         buyPrice = this.candle.close;
  218.         buyPriceTimeStamp = this.candle.start._d
  219.         sellPrice = this.candle.close * 1.005;
  220.         cutLossesPrice = this.candle.close * 0.98;
  221.  
  222.         timeToSell = true;
  223.       }
  224.  
  225.  
  226.       if (timeToSell) {
  227.           //MARK: - TAKE PROFITS
  228.         if (sellPrice <= this.candle.close) {
  229.             timeToSell = false;
  230.             this.advice('short');
  231.         }
  232.  
  233.         if (cutLossesPrice >= this.candle.close) {
  234.             timeToSell = false;
  235.             this.advice('short');
  236.         } else {
  237.             let buyinStamp = (new Date(buyPriceTimeStamp).getTime()/1000);
  238.             let shouldSelloutStamp = (new Date(this.candle.start._d).getTime()/1000);
  239.             let gap = shouldSelloutStamp - buyinStamp;
  240.  
  241.             //one hour 3600
  242.             //three hours 10800
  243.             //one day 86400
  244.  
  245.             // if (gap >= 10800) {
  246.             //     sellPrice = buyPrice * 1.003
  247.             //     if (sellPrice <= this.candle.close) {
  248.             //         timeToSell = false;
  249.             //         this.advice('short');
  250.             //     }
  251.             // }
  252.  
  253.             // if (gap >= 36000) {
  254.             //     this.advice('short');
  255.             // }
  256.             // console.log(buyinStamp);
  257.             // console.log(buyPriceTimeStamp);
  258.             // console.log(this.candle.start._d);
  259.  
  260.         }
  261.       }
  262.  
  263.    
  264.  
  265. }
  266.  
  267.  
  268. module.exports = strat;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement