0

I have a function that reads in multiple JSON files in a loop. Problem is, they are being updated every fraction of a second via a websocket. so every so often this error (im guessing because its trying to read the file whilst its being updated) appears:

QSPBTC  rsi 48.09
BTSBTC  rsi 40.36
undefined:1

SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at checkPairs (C:\Users\user\Documents\bot\deletesoon.js:39:24)
at runCheckPairs (C:\Users\user\Documents\bot\deletesoon.js:81:5)
at Object.<anonymous> (C:\Users\user\Documents\bot\deletesoon.js:88:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3

So I trying To make a function that catches this error, and allows the function to start again (I also have the function being called every 5 minutes.)

function checkPairs(){

var RSI = require('technicalindicators').RSI;
const fs = require('fs');
var Twit = require('twit');
var config = require('./tconfig');
var T = new Twit(config);
var message = [];
var theLoop = ['ETHBTC','LTCBTC','BNBBTC','NEOBTC','BCCBTC','GASBTC'];




for(i = 0; i < theLoop.length; i++){

var symbol = theLoop[i];
var data = fs.readFileSync('charts/'+symbol+'.json');
var CurrentPair = JSON.parse(data);

var prices = [];
var period = 14;

var lowTotal = [];
var closeTotal = [];
var rsiArray = [];

for (var item in CurrentPair) {
prices.push(Number(CurrentPair[item].close));
} 


var newRSI = [];
newRSI = RSI.calculate({period : period, values : prices});
var rsiResult = newRSI[newRSI.length -1];
console.log(symbol + "  rsi " + rsiResult);

if(rsiResult <= 23 ){
var twitMessage = "RSI is LOW ("+rsiResult+") ";
var splitSymbol = "";

if(symbol.includes("BTC", 3) || symbol.includes("BTC", 4) || 
symbol.includes("BTC", 5) || symbol.includes("BTC", 6) ){
splitSymbol = symbol.replace("BTC", "");
};

T.post('statuses/update', { status: 'INDICATOR ALERT: '+ symbol + ' 
#'+splitSymbol +' : '+ twitMessage + ' \n \n #'+splitSymbol + ' #binance' + 
'   #dayTrading' + ' #TradingView'}, function(err, data, response) {
console.log('                              TWEETED  ' + symbol + "  RSI " + 
rsiResult);
})}; 
}; 
};


var runCheckPairs = function () {
setInterval(checkPairs, 300000);
checkPairs();
    if(err){
        console.log("error Handled")
        runCheckPairs();
    }
};

runCheckPairs();
console.log("\n\n");

Any help would be great thanks

tel90
  • 9
  • 7
  • maybe these links could help: https://stackoverflow.com/questions/9804777/how-to-test-if-a-string-is-json-or-not, https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string-in-javascript-without-using-try – Robbie Mar 28 '18 at 20:51
  • also, be careful calling `runCheckPairs` recursively like that... its going to end up creating multiple interval timers and execute far more frequently than you expect (if it even loops at all - where is `err` set?) – Robbie Mar 28 '18 at 20:56
  • Use a database instead so you don't have the problem to start with. For example `level` https://github.com/Level/level – ralphtheninja Mar 28 '18 at 21:14

0 Answers0