This script automatically buys bitcoin or a specific cryptocurrency based on user environment settings using the CCXT library.
Disclaimer: This script is for educational purposes only and is not intended to be used as financial advice. The author of this script is not responsible for any financial loss that may occur as a result of using this script. Use at your own risk.
The script utilizes the CCXT library to communicate with the exchange API and can be configured using environment variables. Once the necessary environment variables have been set up, the script will automatically place buy orders for the specified cryptocurrency at regular intervals using the dollar-cost averaging (DCA) strategy.
Dollar-cost averaging is a strategy that involves investing a fixed dollar amount in an asset at regular intervals, regardless of the asset's price. This approach helps investors to avoid trying to time the market and allows them to benefit from the natural fluctuations in the asset's price over time.
The script determines the optimal time to place the next order based on the user's environment settings and the current market price of the cryptocurrency. This calculation ensures that the script will spend the fixed budget for the asset for the specified duration without overspending or underspending.
If the exchange account runs out of funds (quote currency), the script logs the issue and can send a notification via Telegram.
The bot now includes state persistence to maintain the DCA schedule across restarts and rebuilds. The state is saved to a bot-state.json file that tracks:
- Last order timestamp
- Next scheduled order time
When the bot restarts, it checks this file to determine whether an order should be placed immediately or if it should wait until the next scheduled time. This prevents duplicate orders when the container is rebuilt or the service is restarted.
In Docker, the state file is stored in /app/data and mounted as a volume to persist across container updates.
Using a custom DCA script like this one provides flexibility in choosing your own DCA strategy and enables you to bypass exchange-specific DCA fees. While many exchanges offer their own DCA features, they are often limited to specific cryptocurrencies and can come with fees that reduce your profits over time.
To use this script, you will need:
- Node.js v16 or later
- An account on a supported exchange with API keys. Here is a list of supported exchanges.
The following environment variables are required:
-
SANDBOX: Set exchange sandbox mode if supported (true or false). -
EXCHANGE_ID: The ID of the exchange you are using (e.g.kraken). -
PUBLIC_KEY: Your exchange API public key. -
PRIVATE_KEY: Your exchange API private key. -
PAIR: The currency pair to trade (e.g.BTC/EUR). -
MIN_QUOTE_AMOUNT: (OPTIONAL) Minimum quote currency amount per order (e.g., 1 for 1 USDC). Use for exchanges with quote minimums. -
MIN_BASE_AMOUNT: (OPTIONAL) Minimum base currency amount per order (e.g., 0.00001 for 0.00001 BTC). Use for exchanges with base minimums. -
DCA_BUDGET: The total amount of quote currency (e.g. EUR) to use for DCA (e.g.1000). -
DCA_DURATION_IN_MS: The duration of the dollar cost averaging (DCA) period in milliseconds (e.g.2592000000for 30 days).Duration Value (ms) Day 86400000 Week 604800000 Month 2592000000 Year 31536000000
- Clone this repository.
- Install dependencies:
npm install - Create a
.envfile with your exchange API keys and other environment variables (see.env.examplefor an example). - Build the script:
npm run build - Start the script:
npm run start
- Clone this repository.
- Create a
.envfile with your exchange API keys and other environment variables (see.env.examplefor an example). - Build the Docker image:
docker build -t crypto-dca-bot .- Start the Docker container:
docker run -d --name crypto-dca-bot --env-file .env crypto-dca-botAlternatively, you can use Docker Compose to start the container:
- Clone this repository.
- Create a
.envfile with your exchange API keys and other environment variables (see.env.examplefor an example). - Run the following command to buld and start the container:
docker-compose up -dThis will start the container in detached mode. If you want to view the logs, you can use the following command:
docker-compose logs -fNote that the restart: unless-stopped option in the docker-compose.yml file will automatically restart the container if it stops or the system reboots.
This script is licensed under the MIT License.