Create Crypto Trading Bot – Javascript Trading Bot

A JavaScript trading bot can really help you to eliminate the weak link in any trading strategy—human error. See how to base your trading bot on JavaScript.

  1. Build Functions and Call Libraries
  2. Store Variables
  3. Trading Loop Executes
  4. Automating the Script Using Windows Task Scheduler

While human traders can have good judgement, they also bring plenty of shortcomings to the table, which JavaScript trading bot will not share. One of the best judgements that fallible humans can make is to build a JavaScript trading bot of their own, to minimize the likelihood of stakes, so here is how to do it.

We’ll be using Windows task scheduler for code execution and the Coinbase exchange, so Bisley you’re going to need a CoinbasePro account. This shouldn’t be any kind of hardship though because it’s an excellent platform with an excellent interface. You will also use a Gmail account to send orders and pickup notifications of trades.

Build Functions and Call Libraries

Call libraries are the first thing you going to need, and here are some of the main ones we will use:

  • mailR
  • stringi
  • Rgdax
  • curL
  • xts
  • TTR

The GDAX interface comes from the rgdax package and mailR handles the email updates. Stringi assists with passing numbers from JSON, and TTR helps with calculating technical indicators. Here are a few of the functions that you will need:

  • Curr_bal_usd & curr_bal_eth - These two will return the latest GDAX account balance numbers and they will be called for a lot by your JavaScript trading bot.

  • RSI - RSI stands for Relative Strength Index and it’s a prime indicator with this strategy.

  • Bid & ask - This one tells you the present bid and ask prices.

  • Usd_hold, eth_hold & cancel_orders - Both the usd_hold and eth_hold functions are there to assist with obtaining the present status of placed orders, and they’ll be doing this a lot. The cancel_order function has the job of scratching orders that have shifted too low in the order book.

  • Buy_exe - This one has a number of steps. The order_size function works out how much crypto you can purchase. The WHILE function sets limit orders while you still have no crypto. When an order has been placed at a particular bid price, the system waits until the order has been filled. If this doesn’t happen then the process simply runs through again.

Store Variables

The next step for building the JavaScript trading bot is to save some of the RSI indicator variables as objects. This helps to speed up the trading loop so that it doesn’t go above the rate limit of the API.

Trading Loop Executes

Basing our trading bot on JavaScript is going well but these steps have only shown us how we can put together functions and variables that will help to run the trading loop. Now, let’s go through an actual example of one in action using GBP and ETH.

We’ll get started with the loop when the balance of the account is above 25 GBP. If the present RSI is greater than or equal to 35 and the previous RSI was lower than 35 on at least one occasion, then we buy as much ETH as we can using the present GBP balance, then we save the price in a spreadsheet file.

Once this is done, we receive an email from ourselves as a reminder of the buy action. The loop then prints “buy” for tracking in the log file. The system will then sleep for approximately three seconds, then we enter a total of three sell orders with different limits to realize the profits. The first one takes profit at 1% gain, the second at 4% and the third at 7%. So that’s what the script does.

Automating the Script Using Windows Task Scheduler

As we’ve mentioned, the benefit of a JavaScript trading bot is to free our trading from the kinds of mistakes that people are prone to, and a trading bot on JavaScript can get on with its job without human intervention, so it’s a solution where you can ‘set it and forget it’. It’s for the automation element that we turn to Windows task scheduler. Here are the steps:

  • schedule script with Rstudio add-in
  • alter scheduled task with task scheduler
  • use log file so you know what’s going on