banner



How To Set Up Trading Bot

Photograph by Branko Stancevic on Unsplash

** Notation that the API used in this tutorial is no longer in service. This article should exist read for illustrative purposes with that in mind.

The trader'due south mind is the weak link in whatever trading strategy or program. Effective trading execution needs man inputs that run in the opposite direction to our instincts. We should purchase when our reptile brain wants to sell. We should sell when our guts desire us to buy more.

It is even more difficult to trade cryptocurrencies with a a disquisitional constitution. The young and emerging markets are flooded with "pump groups" that foster intense FOMO (fear of missing out) which drive prices sky-loftier before body-slamming them back downwards to earth. Many novice investors likewise trade on these markets, investors that perhaps never entered a trade on the NYSE. On every trade, at that place is a maker and a taker, and shrewd crypto investors find it easy to accept advantage of the novices flooding the space.

In order to detach my emotions from crypto trading and to accept reward of markets open 24/7, I decided to build a simple trading bot that would follow a simple strategy and execute trades as I slept.

Many "bot traders" every bit they are called, utilise the Python programming language to execute these trades. If you lot were to google, "crypto trading bot," you would find links to Python code in various Github repositories.

I'grand a data scientist, and R is my principal tool. I searched for a decent tutorial on using the R language to build a trading bot only found nothing. I was set on building my own package to interface with the GDAX API when I found the package rgdax, which is an R wrapper for the GDAX API. The post-obit is a guide to piecing together a trading bot that you tin can use to build your own strategies.

The Strategy

In a nutshell, we volition exist trading the Ethereum — USD pair on the GDAX exchange through their API via the rgdax wrapper. I like trading this pair because Ethereum (ETH) is typically in a bullish stance, which allows this strategy to polish.

Annotation: this is a super-simplistic strat that volition but make a few bucks in a bull market. For all intents and purposes, use this as a base for edifice your own strat.

We will exist buying when a combination of Relative Strength Alphabetize (RSI) indicators point to a temporarily oversold market, with the assumption that the bulls will one time again push the prices up and we can get together profits.

One time nosotros buy, the bot will enter 3 limit sell orders: one at 1% profit, another at iv% turn a profit and the last at 7% profit. This allows the states to chop-chop costless upwards funds to enter another merchandise with the 1st two orders, and the 7% order bolsters our overall profitability.

Software

We will exist using Rstudio and Windows job scheduler to execute our R code on a regular basis (every 10 minutes). Yous volition need a GDAX account to send orders to, and a Gmail account to receive trade notifications.

Our Process

Office 1: Phone call Libraries and Build Functions

We will begin by calling several libraries:

The bundle rgdax provides the interface to the GDAX api, mailR is used to send us e-mail updates with a Gmail account, stringi helps usa parse numbers from JSON and TTR allows us to perform technical indicator calculations.

Function: curr_bal_usd & curr_bal_eth

You will utilise your api primal, secret and passphrase that are generated from GDAX in the API section. These functions query your GDAX business relationship for the most recent balance which we volition employ repeatedly in our trading:

Office: RSI

We volition use the RSI or Relative Strength Index equally our principal indicators for this strategy. Curr_rsi14_api pulls in the value of the about recent xiv period RSI, using 15 infinitesimal candles. RSI14_api_less_one and so forth pull in the RSI for the periods prior:

Function: bid & ask

Adjacent, we volition need the current bid and inquire prices for our strategy:

Part: usd_hold, eth_hold and cancel_orders

In social club for us to place limit orders in an iterative manner we need to be able to pull in the current condition of our orders already placed, and be able to cancel orders that have moved too far down the order book to exist filled. We will employ the "holds" role of the rgdax package to do this for the former, and "cancel_order" for the latter:

Function: buy_exe

This is the big-daddy function that actual executes our limit orders. There are several steps that this function works through.

1. Order_size office calculates how much eth we tin can purchase, because we want to purchase equally much as possible each time, less 0.005 eth to account for rounding errors

2. Our WHILE function places limit orders while nosotros still have zero ETH.

three. An order is added at the bid() price, the organization sleeps 17 seconds to permit the order to be filled, and and so checks to see if the order was filled. If information technology wasn't and so the process repeats.

Part 2: Store Variables

Side by side, we need to store some our our RSI indicator variables every bit objects so the trading loop runs faster and and so that we don't exceed the rate limit of the API:

Part 3: Trading Loop Executes

Up until now, we have just been preparing our functions and variables in order to execute the trading loop. The post-obit is a verbal walk through of the actual trading loop:

If the electric current balance of our account in USD is greater than $twenty, we volition start the loop. Next, if the current RSI is greater than or equal to 30 AND the RSI in the previous menstruation was less than or equal to 30 AND the RSI in the previous 3 periods was less than 30 at least in one case, then we purchase equally much ETH as we can with the current USD balance.

Next, we relieve this buy price to a CSV file.

Then, nosotros send an electronic mail to ourselves to alert us of the purchase action.

The loop then prints "buy" so we can track that in our log file.

The system then sleeps for 3 seconds.

At present, we enter iii tiered limit sell orders to accept profits.

Our first limit sell order takes turn a profit at a 1% gain, the next takes profit at a four% proceeds, and the last takes profit at a vii% gain:

That'due south it, that'due south the unabridged script.

Part 4: Using Windows Task Scheduler to Automate the Script

The whole purpose of this bot is to have the human error out of the trade, and to permit us to enter trades without having to be present at a screen. We will apply Windows Task Scheduler to accomplish this.

Schedule script with Rstudio addin

Use the handy Rstudio add together in to easily schedule the script:

Alter the scheduled chore with Task Scheduler

Navigate to the job created by the Rstudio add in and adjust the trigger to fire at the interval you wish. In my case I choose every 10 minutes indefinitely.

Proceed an eye on your task with the log file

Every time your script runs it volition make an entry in a text log file, which allows you to troubleshoot errors in your script:

You can run across how the "Starting time LOG ENTRY" and "END LOG ENTRY" print function comes in handy to split up our entries.

Arrive Your Own

You can change this script to get in as simple or as complex as yous want. I'm working on improving this script with the addition of neural networks from the Keras module from Tensorflow for Rstudio. These neural networks add an exponentially more complex chemical element to the script, but are incredibly powerful for finding hidden patterns in the data.

In addition, the TTR parcel provides united states of america with a large number of fiscal functions and technical indicators that can be used to improve your model.

With all this being said, do non play with more money that you can afford to lose. The markets are not a game and you tin and will lose your shirt.

Link to Full Source Code on Github

Source: https://towardsdatascience.com/build-a-cryptocurrency-trading-bot-with-r-1445c429e1b1

0 Response to "How To Set Up Trading Bot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel