Freqtrade - An open source crypto bot
The opinions on trading with crypto are divided through the community. However, for financial flow, it is essential to provide liquidity for assets. Looking at the foreign exchange market, it's not much different. Freqtrade is an open source tool, everyone can use to take part in the crypto market and to automate their wealth generation.
Introduction
In the crypto world, there are basically two streams, one that prefers hodling, and the other one that is willing to accept risks and to lose or win some money while doing trading. Freqtrade is an open source tool, which allows you to run automated trading strategies against crypto markets and let's you sell gains or buy dips while you sleep.
The following will outline a setup on how to run freqtrade and what key components you can adjust to get it running.
But before we start, a short disclosure, that using such tools and trading crypto assets is very risky.
Don't do crypto trading, when you are not ready to lose the money you invested!
TOC
- Freqtrade prerequisites
- Running a bot
- Setting up your first configuration
- Setting up your first strategy
- Upcoming
- Further Links
Freqtrade the prerequisite
You can find a full documentation of Freqtrade on https://www.freqtrade.io/, apart from that the project is open source and lives on github https://github.com/freqtrade/freqtrade. Furthermore freqtrade is buildable as a docker container, and can run for example in a kubernetes cluster or with a simple docker-compose setup.
You will also find the docker container published on docker.io. To get started with automated crypto trading pretty much everything you need is on your hands.
If you wish to just checkout the project and to get familiar with how trading with crypto works, you are pretty much done here. If you would like to become the next crypto millionaire you would need to setup a market you wish to trade on.
Running a bot
While running your crypto bot you should consider at least the following:
-
Your bot needs to run constantly: It needs to evaluate your strategy and then react accordingly. Some strategies can take days or months to be executed and will not be able to run without being interrupted.
-
Your bot has your credentials: At least it needs to have an access key to the market you are trading on. Some markets allow to adjust keys for the trading purpose and you can avoid that someone can withdraw your funds, but in all cases executing an order is something an attacker could do without you knowing it.
Knowing this we can discuss further on how you can setup your bot.
You can find this example setup here: nerdjacuzzi freqtrade examples
Setting up your first configuration
For running freqtrade you need to configure your bot according to your environment. For a first docker-compose template you might want to have a look at freqtrade docker-compose starter
The most convenient way nowadays would be to use docker, as the docker image contains all packages you need and you can focus on adjusting your strategies.
Running docker-compose up will leave you with the next instruction you have to do:
freqtrade.exceptions.OperationalException:
Config file "/freqtrade/user_data/config.json" not found!
Please create a config file or check whether it exists.
Going through the docker-compose.yaml file will show you, that the bot tries to start trading, which is not possible without having a proper configuration.
# docker-compose.yaml
...
volumes:
- "./user_data:/freqtrade/user_data"
command: >
trade
--logfile /freqtrade/user_data/logs/freqtrade.log
--db-url sqlite:////freqtrade/user_data/tradesv3.sqlite
--config /freqtrade/user_data/config.json
--strategy SampleStrategy
...
Furthermore the bot mounts a local volume to be populated with runtime files, like logs and it's also as stated in the command the place where your config needs to go to.
Freqtrade exposes the following commands:
trade Trade module.
create-userdir Create user-data directory.
new-config Create new config
new-strategy Create new strategy
download-data Download backtesting data.
convert-data Convert candle (OHLCV) data from one format to
another.
convert-trade-data Convert trade data from one format to another.
trades-to-ohlcv Convert trade data to OHLCV data.
list-data List downloaded data.
backtesting Backtesting module.
backtesting-show Show past Backtest results
backtesting-analysis
Backtest Analysis module.
edge Edge module.
hyperopt Hyperopt module.
hyperopt-list List Hyperopt results
hyperopt-show Show details of Hyperopt results
list-exchanges Print available exchanges.
list-markets Print markets on exchange.
list-pairs Print pairs on exchange.
list-strategies Print available strategies.
list-freqaimodels Print available freqAI models.
list-timeframes Print available timeframes for the exchange.
show-trades Show trades.
test-pairlist Test your pairlist configuration.
convert-db Migrate database to different system
install-ui Install FreqUI
plot-dataframe Plot candles with indicators.
plot-profit Generate plot showing profits.
webserver Webserver module.
strategy-updater updates outdated strategy files to the current version
lookahead-analysis Check for potential look ahead bias.
recursive-analysis Check for potential recursive formula issue.
Let's use the new-config command to create a first config, you can use the docker-compose files living in the nerdjacuzzi freqtrade examples folder to get you started:
Running docker-compose -f docker-compose.create-config.yaml run freqtrade-new-config
will end up with freqtrade asking you some bootstrap questions:
? Do you want to enable Dry-run (simulated trades)? Yes
? Please insert your stake currency: USDT
? Please insert your stake amount (Number or 'unlimited'): unlimited
? Please insert max_open_trades (Integer or -1 for unlimited open trades): 3
? Time Have the strategy define timeframe.
? Please insert your display Currency (for reporting): USD
? Select exchange binance
? Do you want to trade Perpetual Swaps (perpetual futures)? No
? Do you want to enable Telegram? No
? Do you want to enable the Rest API (includes FreqUI)? No
For our first config, I decided to run the bot against binance in dry mode which will simulate the trades done by freqtrade. This gives you the oportunity to test your strategies, next to using e.g. backtesting, or to learn how freqtrade behaves while running.
The next step is that you have to decide which startegy you want to use for your bot.
Setting up your first strategy
The nerdjacuzzi freqtrade examples contains a full user_data example which will allow you to run the bot by simply using:
docker-compose up
The config and the strategy contained in the repo are not suitable for making money, and is only there to get you started running freqtrade.
Have a look at the SampleStrategy it does not much except of implementing the IStrategy class provided by freqtrade.
class SampleStrategy(IStrategy):
"""
SampleStrategy implements the strategy interface
This strategy is a dumy not doing something.
Please do not use this in production!
"""
minimal_roi = {
"0": 0.01,
"10": 0
}
stoploss = -0.05
timeframe = '5m'
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
return dataframe
We will discuss strategies at a later stage, but a good starting point to test out strategies would be freqtrade strategies.
Please always consider, that running these strategies against an exchange will activley do tradings for you. I higly suggest to start using the dry run option you can configure this in the config.json file of your bot.
# config.json
...
"dry_run": true,
"dry_run_wallet": 1000,
...
All stratgies can be configured to run adjusted to your needs, some common options are:
- max_open_trades: The bot will not open more trades than this, e.g. setting this to 3 will not open a fourth trade, even if a trade might have been lucrative.
- stake_currency: You can specify which currency should be trade with, you have to ensure, that you have this currency already staked on the exchange.
- exchange: All the exchange specific configuration go here. E.g. your access key pair, you really have to ensure that no one except of you can get access to this.
- pairlists: Is the place where you can define pairlist, which the bot should trade with your strategy. There are different methods, like VolumePairList which will select pairs by their trading volume.
- telegram: A place to put your telegram configuration inside. Freqtrade allows you to add a telegram bot connection to get realtime updates and to control your bot from your smartphone.
Upcoming
The next step is to get your bot to work, we will discuss different strategies and how to get your trading in production in later articles.