How to create artificial intelligence-enabled trading bot — part 1

Dewmal
5 min readDec 15, 2019

We are going to build a bot with artificial intelligence to create trading orders in the binary market. It's mean

Before go deep I need to share something with you. This is what I got after 48 hours of my bot.

within 2 days it can create a 6.05 % profit for you.

Ok now Let’s build your simple trading app within 99 lines of code.

First, you need to start from step 1

  1. Binary.com trading platform with API access
  2. Little bit knowledge about javascript/nodejs
  3. 10 minutes

There are a lot of binary trading platforms are available in the market but I found binary.com with good API access and so many options to create a trade than other platforms available in the market. also, they are providing a lot of resources to learn how trade using algorithms in here. If you don t have an account you can create it from here it will take less than 5 min

You can create your binary.com account in here

Ok, now you need to have 2 things from binary.com

If you don’t know how to create them I create a simple article for that too.

It is time to get dirty with 99 lines of code.

If you are new to nodejs development here is a simple guideline for that

First, we need to check everything is working in our previous steps

npm install --save ws

then we put this code in your index.js

It is simple but you need to replace your app id with their app id (1089)

There are a lot of API calls with a different parameter to build a bot with them, Here is the list

Can you read those things and create the bot in 10 minutes. it is a big NO. so then Lets,

We don`t want to worry about those WebSocket things anymore, binary.com also provides high-level API access to their APIs using this simple npm library, we also going to use that for reducing a lot of workloads.

Add binary-live-api to your project

npm install --save binary-live-api

And then we are going to replace our index.js with this.

This is not exactly in their guide to start with Live API, I make a little bit change for work it with the newest Javascript things. However, it is working. :D

Then Our boring steps are done, now we are going to build our trading bot using this simple footprint.

We are going to build it in 4 steps

  1. Connect our app to get asset details from market
  2. Add simple strategy to make decisions ( Simple Candlestick pattern )
  3. Create and place an order by API
  4. Add the withdrawal method for getting profits

We are going to use R_50 asset to build this trading app

Yeah that’s all

Let me explain important parts

api.getTickHistory("R_50", { end: 'latest', count: 10, "style": "candles", "subscribe": 1 });

using this part we subscribe to R_50 asset data in binary.com, we put some extra parameters to get candles with 10 history candles

api.events.on("candles", async (res) => {candles = res.candles;});

We can have those history candles from here
We used subscribe =1 in extra param that helps us to have ohlc data from every tick, but we don’t want them in every tick, we only need it per 1 minute then we add this to there

Now we need a way to store those candles for further analyzing things. so we use this

Store Candles

let candles = [];

Initializr candles variable with history recodes

api.events.on("candles", async (res) => { candles = res.candles; });

Push new data to candle

And also we use lodash to sort this candle to make sure it aligned

const _ = require("lodash"); _.sortBy(candles, (ca) => ca.epoch); // Sort Candle List using Lodash

OK now let's move to the next step

Add simple strategy to make decisions

We know there are 2 types of Candlesticks

And also there are a lot of available candlestick patterns. before we go further we need to have implement 2 functions.

  1. Identify trend
  2. Convert candles into the machine-readable way

If starting point candle close value is higher than endpoint candle then we can take there is a Down Trend, also we can take starting point candle close value lower than endpoint candle close value as an Up Trend

Convert human-readable Candles to Machine Readable

simple we convert all upper, lower and body sizes into percentage value using the above function it will help us to go further analyzing things easy.

Now we can move forward to identify the pattern.

We are going to use Bullish Engulfing

How to Identify the pattern

Here are the steps

  1. The trend should be a Down Trend
  2. Before the last candle should be Bearish Candle
  3. The last candle should be Bullish Candle
  4. candle’s body engulfs the prior (black) candle’s body

Please follow the steps in code

Let's place an order in binary.com

Now you need to withdraw your profit from binary.com

  1. and you can use this function to withdraw your profit from binary.com using this

Please refer this before use it https://developers.binary.com/api/#paymentagent_withdraw

Yeah, that’s all, now you can do anything with this code,

Sharpen your teethes I will comeback with artificial intelligence code parts.

- Ceylon App (@ceylonapps) December 24, 2019

Originally published at https://blogs.ceylon.app on December 15, 2019.

--

--