Configuration¶
The algorithm is designed to be highly configurable and can be adjusted to your needs. The following configuration variables can be either set as command-line arguments or via environment variables. They are mapped to the corresponding command-line arguments internally.
🛠️ Strategy configuration¶
In order to run the available strategies efficiently, one need to know the configuration of those. Even though these strategies follow different approaches, they share the same fundamental concept and configuration properties. In the following, the most important of them are explained in order to guide you through the configuration process.
🔭 Choosing the right strategy¶
Choosing the right strategy is crucial for success. The strategies differ in their approach and are designed for different market conditions (see Strategies). Make sure to understand all of them before choosing the right one for your needs.
💵 Choosing the right base and quote currency¶
The base and quote currency should be chosen wisely. The base currency is the currency you want to trade with, and the quote currency is the currency you want to trade against. The base and quote currencies should be chosen based on your trading preferences and the liquidity of the market. Currency pairs with high volatility can lead to higher profits but also to higher risks.
📈 Choosing the optimal interval¶
The interval defines the percentage difference between the buy and sell orders. The interval should be chosen based on the volatility of the market. A higher interval, e.g., 4 % leads to higher profits when selling an asset, and reduces the amount of orders in a moderate volatile market. A lower interval, e.g., 2 % leads to a higher amount of orders and a lower profit margin but might be more profitable in markets that oscillate in a smaller interval.
Consequently the interval should be chosen based on the volatility of the market and the risk you are willing to take. A higher interval is less risky but since prices not always reach the placed buy or sell orders (… since markets don’t go exactly e.g., 4 % up and down), it can lead to less executed orders resulting in lower profits. In contrast to that, a lower interval is more risky, as prices can rise and fall rapidly but can lead to a higher profits if the market’s volatility matches the grid interval.
Note
The value passed to the interval is a percentage value. For example,
an interval of 4 % means that the buy and sell orders are placed 4 %
apart from each other. In this case, you have to pass a value of
0.04 to --interval or INFINITY_GRID_RUN_INTERVAL.
⚖️ Choosing the optimal amount per grid¶
The amount per grid defines the amount of the quote currency to use per grid interval. It should be chosen based on the amount of the quote currency you want to invest in the market. For example if you want to invest 100 USD per grid interval (i.e. per trade), the amount per grid should be set to 100. Combining this amount with an interval of 4 % would result in a profit of about 4 USD per trade (excluding fees) depending on the strategy used.
When choosing the amount per grid, make sure to consider the amount of the quote currency in our account, since depending on the number of concurrent open buy orders, the amount per grid should be balanced. For example, if you have 5 open buy orders with an amount per grid of 100 USD, you should have at least 500 USD in your account to cover all open buy orders.
Another aspect to consider is the minimal order sizes of the quote currency that can be traded on the exchange. Make sure to set the amount per grid higher than the minimal amount to avoid errors. For further information, check the exchange’s minimum order size for trading documentation.
📊 Choosing the optimal amount of concurrent open buy orders¶
The number of concurrent open buy orders defines the number of buy orders that are open at the same time. This number should be chosen based on the volatility of the market and the risk you are willing to take. A higher number of open buy orders secures a rapid price drop of almost 10 % that can be caught immediately, in case the interval is set to 2 % and number of open buy orders is 5.
As mentioned before, the number of open buy orders should be balanced with the amount per grid. For example, if you have 5 open buy orders with an amount per grid of 100 USD, you should have at least 500 USD in your account to cover all open buy orders.
🏷️ Setting a custom fee¶
Lastly, exchanges charge fees. The fee is usually a percentage of the traded amount. If the fee is not set during program start, the highest taker fee for that currency pair is assumed which doesn’t mean that the highest fee is paid. It is used internally to calculate order sizes, prices, and ensures that profits are calculated correctly.
Setting a custom fee via the --fee or INFINITY_GRID_RUN_FEE option
enables, depending on the strategy, a more accurate profit calculation. For the
average user, this is not necessary. For more information about the fees, check
the fee schedule of the respective exchange.
Note
The fee is always paid in the quote currency.
🛡️ Risk management¶
Risk management is crucial for successful trading. The algorithm provides several options to manage the risk. The most important ones are the amount per grid, the interval, the number of concurrent open buy orders, and the maximum investment.
Especially the maximum investment is important to prevent the algorithm from
investing more than you are willing to lose. The maximum investment defines the
maximum amount of the quote currency that the algorithm will use for trading.
Setting the maximum investment is recommended and can be done via the
--max-investment or INFINITY_GRID_RUN_MAX_INVESTMENT option.
The amount per grid, the interval, and the number of concurrent open buy orders should be chosen based on the maximum investment. In order to demonstrate this, lets use and example:
Scenario: Trading BTC/USD
Starting price: 100,000.00 USD per BTC
Interval: 4.0% (i.e.
0.04)Amount per grid: 100.00
Maximum investment: 1,000.00
Current balance in the account: 5,000.00 USD
You will have the following grid levels for buy, based on the interval and the starting price, and ensuring to even catch a drawdown of about 34%:
Grid 1: 96,000.00 USD (-4.00%)
Grid 2: 92,160.00 USD (-7.84%)
Grid 3: 88,473.60 USD (-11.53%)
Grid 4: 84,934.66 USD (-15.07%)
Grid 5: 81,537.27 USD (-18.46%)
Grid 6: 78,275.78 USD (-21.72%)
Grid 7: 75,144.75 USD (-24.86%)
Grid 8: 72,138.96 USD (-27.86%)
Grid 9: 69,253.40 USD (-30.75%)
Grid 10: 66,483.26 USD (-33.52%)
Note that since the maximum investment is set, the algorithm will stop placing further buy orders if 1,000 USD are already invested with this instance. If a buy order was executed, depending on the strategy, a sell order will be placed at 4% higher than the buy order.
Note
The grid levels may also not be exactly the same as shown, they are just for demonstration purposes. In a real life scenario, the grid levels are calculated based on the interval and current market prices, can be shifted up or down based on the market’s volatility.
⌨️ Command-line Interface¶
infinity-grid provides a command-line interface (CLI) to configure and run the trading algorithm. The CLI is based on the Click library and provides a set of commands to interact with the algorithm.
infinity-grid¶
Command-line interface entry point
Usage
infinity-grid [OPTIONS] COMMAND [ARGS]...
Options
- --version¶
- --api-public-key <api_public_key>¶
Required The Spot API key
- --api-secret-key <api_secret_key>¶
Required The Spot API secret key
- -v, --verbose¶
Increase the verbosity of output. Use -vv for even more verbosity.
run¶
Run the trading algorithm using the specified options.
Usage
infinity-grid run [OPTIONS]
Options
- --strategy <strategy>¶
Required The strategy to run.
- Options:
cDCA | GridHODL | GridSell | SWING
- --name <name>¶
Required The name of the instance. Can be any name that is used to differentiate between instances of the infinity-grid.
- --exchange <exchange>¶
Required The exchange to trade on.
- Options:
Kraken
- --max-investment <max_investment>¶
The maximum investment, e.g. 1000 USD that the algorithm will manage.
- Default:
100000000000.0
- --userref <userref>¶
Required A reference number to identify the algorithm’s orders. This can be a timestamp or any positive integer number. Use different userref’s for different instances!
- --fee <fee>¶
The fee percentage to respect, e.g. ‘0.0026’ for 0.26 %. This value does not change the actual paid fee! It is used to estimate order sizes. If not passed, the highest maker fee for that asset pair will be assumed.
- --base-currency <base_currency>¶
Required The base currency. Can also be a tokenized asset like ‘AAPLx’ in case of xStocks on Kraken.
- --quote-currency <quote_currency>¶
Required The quote currency.
- --amount-per-grid <amount_per_grid>¶
The quote amount to use per interval.
- --interval <interval>¶
The interval between orders (e.g. 0.02 equals 2%).
- Default:
0.02
- --n-open-buy-orders <n_open_buy_orders>¶
The number of concurrent open buy orders e.g.,
5. The number of always open buy positions specifies how many buy positions should be open at the same time. If the interval is defined to 2%, a number of 5 open buy positions ensures that a rapid price drop of almost 10% that can be caught immediately.- Default:
3
- --dry-run¶
Enable dry-run mode which do not execute trades.
- --skip-permission-check¶
Disable the API key permission check. This should by only used together with the –dry-run flag in order to start and run the algorithm with API keys that do not have all the required permissions e.g., for testing purposes.
- --skip-price-timeout¶
Skip checking if there was a price update in the last 10 minutes. By default, the bot will exit if no recent price data is available. This might be useful for assets that aren’t traded that often.
- --db-name <db_name>¶
The database name.
- Default:
'infinity_grid'
- --sqlite-file <sqlite_file>¶
SQLite file to use as database.
- --in-memory¶
Use an in-memory database (similar to –sqlite-file=”:memory:”).
- Default:
False
- --db-user <db_user>¶
PostgreSQL DB user
- --db-password <db_password>¶
PostgreSQL DB password
- --db-host <db_host>¶
PostgreSQL DB host
- --db-port <db_port>¶
PostgreSQL DB port
- --telegram-token <telegram_token>¶
The Telegram token to use.
- --telegram-chat-id <telegram_chat_id>¶
The telegram chat ID to use.
- --telegram-thread-id <telegram_thread_id>¶
The telegram thread ID to use.
- --metrics-enabled, --no-metrics-enabled¶
Enable or disable the metrics HTTP server.
- Default:
True
- --metrics-host <metrics_host>¶
Host address for the metrics server.
- Default:
'127.0.0.1'
- --metrics-port <metrics_port>¶
Port for the metrics server.
- Default:
8080
🏕️ Environment Variables¶
Since infinity-grid is designed to be run in containerized
environments, the configuration can also be done via environment variables. The
naming pattern follows the convention of the command-line arguments respecting
the INFINITY_GRID_ prefix and Click’s
naming convention.
Variable |
Type |
Description |
|---|---|---|
|
|
The API public key provided by the exchange. |
|
|
The API secret key provided by the exchange. |
|
|
The exchange to trade on. |
|
|
The name of the instance. Can be any name that is used to differentiate between instances of the infinity-grid. |
|
|
A reference number to identify the algorithm’s orders. This can be a timestamp or any integer number. Use different userref’s for different instances! |
|
|
Enable verbose logging. |
|
|
The base currency e.g., |
|
|
The quote currency e.g., |
|
|
The quote amount to use per grid interval e.g., |
|
|
The interval between orders e.g., |
|
|
The number of concurrent open buy orders e.g., |
|
|
The maximum investment, e.g. |
|
|
A custom fee percentage, e.g. |
|
|
The trading strategy, e.g., |
|
|
Enable dry-run mode (no actual trades). |
|
|
Skip checking if there was a price update in the last 10 minutes. By default, the bot will exit if no recent price data is available. This might be useful for assets that aren’t traded that often. |
|
|
The Telegram bot token for notifications. |
|
|
The Telegram chat ID for notifications. |
|
|
The Telegram thread ID for notifications. |
|
|
The PostgreSQL database user. |
|
|
The PostgreSQL database name. |
|
|
The PostgreSQL database password. |
|
|
The PostgreSQL database host. |
|
|
The PostgreSQL database port. |
|
|
The path to a local SQLite database file, e.g., |
|
|
Use an in-memory database (similar to |
🖥 Database configuration¶
The algorithm requires a PostgreSQL or SQLite database to store the current orderbook, trades, and the algorithm’s state. The database configuration can be set via environment variables or command-line arguments.
PostgreSQL¶
When using the algorithm as proposed in Getting Started via the provided Docker Compose file, the PostgreSQL database is automatically configured.
The algorithm requires the following environment variables to be set, in order to connect to the PostgreSQL database:
INFINITY_GRID_RUN_DB_USERINFINITY_GRID_RUN_DB_NAMEINFINITY_GRID_RUN_DB_PASSWORDINFINITY_GRID_RUN_DB_HOSTINFINITY_GRID_RUN_DB_PORT
SQLite¶
When running the algorithm as a pure Python process or as a Docker container without further PostgreSQL deployment, the algorithm can use a SQLite database for local storage.
For this purpose, the option --sqlite-file can be used to specify the path
to the SQLite database file. The SQLite database is created automatically if it
does not exist.
Alternatively, the INFINITY_GRID_RUN_SQLITE_FILE environment variable can be used
to specify the path to the SQLite database file.
Note
Do not use :memory: for an in-memory database, as this will
result in data loss when the algorithm is restarted.