In this guide, we will construct the trading bot which will be transacting on the QUANTA blockchain. By the end of this guide, you will have the trading bot functioning. The language chosen is javascript, with es6, async.
The core control code will boot up, cancel all existing orders, and execute orders every 8 seconds. In the stock market, typical traders choose the frequency when they'd like to trade such as 1m, 5m, 30m and so on.
Control Logic
class TradeAgent {
constructor(key, spread, levels) {
}
async cancelAll() {
}
async runMarket(market) {
}
async runOnce() {
for (const m of this.quantaMarket) {
console.log("running for market ", m.ticker);
await this.runMarket(m)
}
}
async run() {
const self = this;
await sleep(1000);
await self.cancelAll();
setInterval(async () => {
await self.runOnce();
}, 8000)
}
}
new TradeAgent(process.env.KEY, 0.05, 5).run()
Creating the Client
The client will leverage two pre-built npm libraries which can be found here: quantajs , quantajs-ws
npm i --save @quantadex/bitsharesjs
npm i --save @quantadex/bitsharesjs-ws
We depend on the @quantadex/bitsharesjs-ws library for websocket connection. QUANTA uses bi-directional communication to send commands, retrieve information, as well as getting pushed messages to the user. In the connection below, we will retrieve all of the assets information so we know how to reference buy orders.
Often functions require user to reference their userId so here we look up the public key for the account information. In QUANTA blockchain, account system is independent from the key-signature. An account has userid, username, balances, orders, and assigns public key authorized to sign it. If you ever want to change the key, you can retain all the account information.
The send limit order takes in a user (from the retrieve account information section), base, counter asset, price, and amount. Orders are always specified from the seller. So we have a conversion function to make this easier for us.
If you were buying BTC with USD, then you are selling USD and receiving BTC.
If you were selling BTC and receive USD, then you are selling BTC, and receive USD.