Hello,
I having trouble with my BOT, when I run node index.js , it executes only buy, but not HOLD, its stalls and returns to command, also the trades are not being recorded on website gemini sandbox, if anyone knows about this issue please advise, thanks.
Strategy does not loop HOLD, code stops abruptly ,
C:\Users\jflor\Documents\AlgorithmicTrading>node index.js
=======================
Executing Strategy
MA: 62135
Price: 60756.00
BUY!
C:\Users\jflor\Documents\AlgorithmicTrading>
index.js
global.fetch = require("node-fetch");
const indicators = require("./indicators.js");
const exchange = require("./exchange.js");
var hasPosition = false;
var strategy = function(){
// If BTC < MA ==> buy(if we have no position)
console.log(" ");
console.log("=======================");
console.log("Executing Strategy");
// If BTC > MA ==> sell (if we have a position)
indicators.hourlyMovingAverage("BTC","USD",100,function(ma){
exchange.bitcoinPrice()
.then(res => {
var price = res.last;
console.log("MA: ",ma);
console.log("Price: ", price);
if(price < ma && !hasPosition){
console.log("BUY!");
exchange.marketBuyBitcoin()
.then(res=>{
console.log("Buy successful");
hasPosition = true;
setTimeout(strategy,1000);
})
.catch(error => console.error)
}
else if(price > ma && hasPosition){
console.log("SELL!");
exchange.marketSellBitcoin()
.then(res=>{
console.log("Sell successful");
hasPosition = false;
setTimeout(strategy,1000);
})
.catch(error => console.error)
}
else{
console.log("HOLD!");
setTimeout(strategy,1000);
}
})
});
}
strategy();
exchange.js
const GeminiAPI = require("gemini-api").default;
const secret = "3iLGT133vQnkFePqaGoYHU7nGtcY";
const key = "account-XCa2X8uBDIBwfjWNDOBT";
const restClient = new GeminiAPI({key,secret, sandbox:true});
module.exports = {
marketBuyBitcoin:function(){
return restClient.newOrder({amount: 1,
price:60000,
side: "buy",
symbol: "btcusd",
options:["immediate-or-cancel"]})
},
marketSellBitcoin:function(){
return restClient.newOrder({amount: 1,
price:1,
side: "sell",
symbol: "btcusd",
options:["immediate-or-cancel"]})
},
bitcoinPrice:function(){
return restClient.getTicker("btcusd");
}
}
indicators.js
const CCAPIKey = "9372ee7b2e6bd4fa91f0822336bc293d5d580bffcf11c7c958568627c6ce8baf";
const CryptoCompareAPI = require("cryptocompare");
CryptoCompareAPI.setApiKey(CCAPIKey);
module.exports = {
hourlyMovingAverage:function(cryptoAsset, fiatCurrency, hours, callback){
if (hours>169) {
console.error("Only up to 169 hours allowed!");
return
}
CryptoCompareAPI.histoHour(cryptoAsset, fiatCurrency)
.then(data => {
data = data.reverse();
var sum = 0;
for (var i=0; i<hours; i++){
sum+=data[i].close;
}
var movingAverage = Math.floor(sum / hours);
callback(movingAverage);
})
.catch(console.error)
}
}
Now when I run on PowerShell I get this;
PS C:\Users\jflor\Documents\AlgorithmicTrading> node index.js
=======================
Executing Strategy
MA: 62026
Price: 62079.21
HOLD!
=======================
Executing Strategy
MA: 62026
Price: 62079.21
HOLD!
=======================
Executing Strategy
MA: 62026
Price: 62079.21
HOLD!
=======================
Executing Strategy
MA: 62026
Price: 62079.21
HOLD!
=======================
Executing Strategy
MA: 62026
Price: 62079.21
HOLD!
=======================
Executing Strategy
MA: 62026
Price: 62079.21
HOLD!
=======================
Executing Strategy
MA: 62026
Price: 62079.21
HOLD!
=======================
Executing Strategy
MA: 62025
Price: 62017.13
BUY!
PS C:\Users\jflor\Documents\AlgorithmicTrading>