I have a trading script that gets values from "coin" object, which is filled by an API call, then iterates inside async forEach
loop, then sends trade orders to the server.
The server requires at least 100 ms between each request. I used setTimeout
with promise, but I can see results coming to console all at once so server bans after a while.
How should I design delaying?
js
Object.keys(coin).forEach(async function(key) {
const coinz = coin[key];
let line1 = sellcoinCalc("sell", coinz.usdPair, coinz.usdOrder)
let line2 = buycoinCalc("buy", coinz.usdtPair, line1)
let result = line2-line1
if (result > 0){
console.log(result)
}
if (result >= profit){
await sellcoinTrade("sell", coinz.usdPair, coinz.usdOrder)
await buycoinTrade("buy", coinz.usdtPair, line1)
}
await new Promise(r => setTimeout(r, 200));
});