Trading bots are as they sound: automated asset trading programs. And you can learn how to code a trading bot for your investment needs. The main benefit of using an automated system is efficiency; bots can make much faster decisions using much more available data. They are also less costly than using human labor, which performs the same job less efficiently. Bots can work for you 24/7.
The way trading bots work is that they are programmed to your investment specifications and use all available data to analyze and predict market movement.
The Job of a Trading Robot
A trading bot uses simple code to perform several basic takes. If you want to customize your own bot you will need to become comfortable programming:
- Algorithmic trading strategy: this uses statistical models to predict market behavior
- Develop a personal risk profile: this will depend on your time and knowledge commitment, as well as your trading capital
- Backtesting: you will need to run your programs to test their success, these tests will be used to validate your bot’s programs
We will look at each of the above ideas in closer detail later on in the article.
If you are interested in automated cryptocurrency trading you will need to do is program your bot. In order to be effective, your bot must be able to use market analysis to make good decisions about when to buy and when to sell. It must also be programmed to suit your personal risk profile.
Before we go any further there is a caveat when it comes to coding a trading bot; to program a bot you need to understand basic programming, for Python or similar simple programming language. This may or may not be discouraging.
For those of you who are neither interested in learning or do not have the confidence to learn that is ok. Currently, there are many platforms that sell programs that have pre-programmed bots that you can customize easily.
If you are more interested in leaving it to the experts, then check out the article Best Crypto Trading Bots.
This article will give you a brief introduction on how to code a trading bot. It also offers several useful resources to help you get a start on your research. It is very important to feel confident and competent when you are designing your bot. So be sure to access as many resources as you need to get to that place.
Once you have reached a place of confidence, be sure to test your programs. Let’s have a look at how that all works.
Here are the things you need to get code your trading bot:
- A Windows or Mac operating system
- Python and PIP
- API Keys
- Private and Public keys
- Run MetaTrader 4 (MT4): an electronic trading platform that uses the MetaQuotes Language 4 (MQL4) for coding trading strategies. This is at Lucas Liam’s recommendation.
If you are still interested in programming your own bot, it might be helpful to start with Liam’s AngloTrading101 courses. This article is a really go place to start, but if you are serious, then arm yourself with knowledge by doing your homework.
Coding Your Bot
A trading bot is programmed to buy and sell assets for you. These functions are executed by aggregating market signals. The main components are for entry rules and exit rules. That means that your program must be designed to act on the limitations that you have set for it.
Limitations include entry and exit prices, the maximum you are willing to invest, and what assets you want to invest in. These details are very personal, and depend a lot on what your investment capital is and what your risk profile is.
Here’s What Your Program Looks Like
The following is a quick look at an example of a custom trading bot using Python and the Poloniex API.
If you are ready to get started programming, check out this YouTube channel. It is crucial to take away from the above demo that you will need to get comfortable with a programming language, such as Python.
Additionally, you also need to study and understand the markets you plan to invest in. While your bot is going to run the programs, you will have to set up strategies for the currencies and assets that you are interested in investing in.
What You Need To Do First
- Bot Code: a trading bot does basic things with more precision and efficiency than a human can.
- The simple code determines your entry rules. This is when to buy or to sell. And your exit rules. This determines when to close on your current position, and how many units want to sell or buy.
- Preliminary Research: Build a personal risk profile. This will depend on your time commitment and trading capital. Once you have these in place you can begin to build a bot that has suitable trading characteristics.
- Backtesting: Backtesting validates (or invalidates) your bot. This step is very important to learn if your bot is programmed to work as you designed it to. Be sure that your bot understands the following:
- Different time frames for entry and exit rulesThe value and movement of different asset classesDifferent market conditions. This is particularly important. Your bot needs to have multiple market scenarios. If your bot is only programmed to work with past data, there is too much artificial certainty built into the program. This is referred to as ‘overfitting bias.’ Basically, it means that the bot does not capture enough potential for risk or reward. This requires consistent data updates and your observation to ensure the program running is inclusive enough.
- Clean Data: This means sourcing and cleaning data to ensure that you have accurate data for backtesting. This data used must reflect the live trading environment as much as possible. Inaccurate data causes inaccurate test results. This is a trade-off between cost and accuracy. For accurate data, you need to spend more time and energy obtaining it.
- Select A Broker: You need to choose a broker that is suited to your needs: MM vs STP vs ECN. Forexpeacearmy.com is a handy resource to help you choose a retail broker reviews
- Design Optimization: ensure that you have enough in your program design to take into account market risks and operational risks.
- Operational risks include System crashes, loss of internet connection, poor execution algorithm, which leads to poorly executed prices or missed trades due to an inability to handle requotes. Also, counter-party risk, broker insolvency, and theft by hackers.
While your bot does the work, you need to ensure that it applies sound statistical models in order to build algorithmic trading strategies.
It is beneficial for your bot to take advantage of the following strategy type combinations:
- Macroeconomic news (ex. non-farm payroll or interest rate changes)
- Fundamental analysis (ex. using revenue data or earnings release notes)
- Statistical analysis (ex. correlation or co-integration)
- Technical analysis (ex. moving averages)
- The market microstructure (ex. arbitrage or trade infrastructure)
Are you with me so far? If you have not given up yet, great! But there is still more work to be done. This is just the beginning of your homework.
If you are keen to build your own bot. Here are a few of Lucas Liaw’s homework suggestions. The HedgTrade blog can answer many of your investment queries. So make sure you understand the following concepts and how they relate to your crypto investments:
- Market inefficiency
- price/asset/market relationships
- market participants
- market microstructure
- market fundamentals
- hard arbitrage
- soft arbitrage
- structured products
- order limit book
- depth of market
- retail traders vs hedge funds/institutional investors
- financial exchanges
- exchange-traded products
- price expectations vs reality
- price action and price behavior
Now that you understand a bit more of what is involved in coding your trading bot, let’s move on to the basic code that you can start with while you are backtesting.
Keep in mind this requires a serious amount of time as you get started. But once you are more comfortable with it will not feel so onerous. And, the better you get at it the more money you should make!
So, if your strategies are not working, you will need to revisit your trading strategies.
Basic Installation of Your Trading Bot
- Get Python and PIP ready on your computer: you will need to learn how to understand and work with basic Python language, as well as run a Python script.
- Install Python library.
- Get API keys: There is any number of accounts to get keys from. If you are starting out and do not know what to trade, Lucas Liew recommends Metatrader 4 (FX and CFDs on equity indices, stocks, commodities, and fixed income), Quantopian (stocks only) or Quantconnect (stocks and FX).
- The programming languages from the above platforms use MQL4, Python, and C#.
- Homework: Understand the following concepts
- Coding/programming trading strategiesMQL4, MQL5EasyLanguageAFLPython/C#/C++/R/MATLAB/VBA for financeTrading TechnologiesCQGMetaTrader 4, MetaTrader 5AmibrokerNinjaTraderMultiChartsTradeStation broker AP
- Name your key.
- DO NOT Allow Withdrawal. If you allow withdrawals then your bot can withdraw money from your accounts as needed. Doing so leaves your bot open to a greater risk of being attacked, or of spending money you did not intend to.
- You need to secure VPS and downtime handling, and evaluation procedures. It’s necessary to monitor your robots’ performance and analyze their strategies in relation to the real market. This includes: inefficiencies, backtests, and optimizations. This must be done throughout the lifespan of your bot.
- Keep a safe, written hard copy of your generated key pairs and never share your private keys.
Here is a basic coding example
First: Find the price on the orderbook: orderbook = api.get_order_book("BTCUSD") top_bid = orderbook.bids[ENTER PRICE].price top_ask = orderbook.asks[ENTER PRICE].price print("Top bid and ask:", top_bid,top_ask)
Second: Balance checking Try the following script with the below code and you should see your USD balance: balance_USD = api.get_balance('USD').balance.available_balance print("My USD balance:", balance_USD)
Third: Specify Trade Logics Ex. I want to buy BTC under these conditions only: I have more than 100 USD in my account The top ask price is less than 6000 This looks like: if (balance_USD > 100 and top_ask < 6000): print ("The time is now!!") api.create_order(currency_pair="BTCUSD", order_way="Bid", price=5900, amount=0.001) else: print("Patience...") Only if the conditions are met with the script place a buy order of 0.001 BTC at the price of 5900 with the above code.
Finally Code: Get your bot working for you This code needs to repeat until the conditions are met, and the order will execute. so, the above script needs to run in a loop. The following is a line of “code time.sleep(5)”, it will put the script to sleep for 5 seconds, after which it will run the script again.
While there are a lot of benefits to bot-traders, this is not a get rich quick scheme. You will need to: understand market strategies, learn basic code and maintain your trading bot with clean accurate data.
It is highly recommended that you familiarize yourself with some of the bots available currently. And if you do not think you are ready to code your own trading bot, then start with a bot from Cryptohopper or 3Commas.
Lucas Liew is a great resource if you are willing to get serious about coding a trading bot.
As the old adage goes; if it seems to good to be true, then it probably is. The real benefit to automated trading, once you have got it going, is a faster more accurate execution of entry and exit.
If your bot is working then you should be earning money. That will mean it is using as much information as possible to accurately read market behaviors and quickly and accurately executing sales.
Just because you are earning money does not mean that you do not need to maintain your bot or that you can stop backtesting. Stay on top of your portfolio and make sure your code is running the way you designed it to, and tweak it whenever necessary.
And remember, as always, DO YOUR HOMEWORK!