I have re-created a Binance API-key trading bot which uses NodeJS, CCXT & Axios (It pulls the API+secret key from a .env file). I am getting the error when trying to execute the trading-bot;;
PS C:\Users\mwalk\Documents\Crypto-Bot> node index.js C:\Users\mwalk\Documents\Crypto-Bot\node_modules\ccxt\js\base\Exchange.js:640 throw new exact[string] (message) ^
InvalidNonce: binance {"code":-1021,"msg":"Timestamp for this request is outside of the recvWindow."} at binance.throwExactlyMatchedException (C:\Users\mwalk\Documents\Crypto-Bot\node_modules\ccxt\js\base\Exchange.js:640:19)
require('dotenv').config();
const ccxt = require('ccxt');
const axios = require('axios');
const tick = async(config, binanceClient) => {
const { asset, base, spread, allocation } = config
const market = `${asset}/${base}`;
const orders = await binanceClient.fetchOpenOrders(market);
orders.forEach(async order => {
await binanceClient.cancelOrder(order.id);
});
const results = await Promise.all([
axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd'),
axios.get('https://api.coingecko.com/api/v3/simple/price?ids=tether&vs_currencies=usd')
]);
const marketPrice = results [0].data.bitcoin.usd / results[1].data.tether.usd;
const sellPrice = marketPrice * (1 + spread);
const buyPrice = marketPrice * (1 - spread);
const balances = await binanceClient.fetchBalance();
const assetBalance = balances.free[asset];
const baseBalance = balances.free[base];
const sellVolume = assetBalance * allocation;
const buyVolume = (baseBalance * allocation) / marketPrice;
await binanceClient.createLimitSellOrder(market, sellVolume, sellPrice);
await binanceClient.createLimitBuyOrder(market, buyVolume, buyPrice);
console.log(`
New tick for ${market}...
Created limit sell order for ${sellVolume}@${sellPrice}
Create limit buy order for ${buyVolume}@${buyPrice}
`);
}
const run = () => {
const config = {
asset: 'BTC',
base: 'USDT',
allocation: 0.1,
spread: 0.1,
tickInterval: 2000
};
const binanceClient = new ccxt.binance({
apiKey: process.env.API_ENV,
secret: process.env.API_SECRET
});
tick(config, binanceClient);
setInterval(tick, config.tickInterval, config, binanceClient);
};
run();
sync your pc time with nist option from your control panel