This is the template for a trading bot for Binance Futures, have support of websockets.
/indicators/index.js
The function in that file should return a object that has signal inside it
return { content : 'BUY'}
or return { content : 'SELL'}
,
The bot would place the orders based on the signals returned from the indicators.
This is a cryptocurrency trading bot designed to interact with Binance Futures via the Binance API. It connects to Telegram using the Telegram Bot API and provides automated trading based on technical indicators. Users can issue commands through Telegram to manage trades for a single cryptocurrency.
-
Environment Configuration:
The bot usesdotenv
to load environment variables, including your Telegram Bot API key and other sensitive information. -
Key Libraries:
Telegraf
: To interact with Telegram.- Binance API client: For executing trades and retrieving market data.
- WebSocket: For streaming real-time market data.
- Custom Functions: Includes helper functions for balance retrieval (
balanceGiver
) and candlestick data analysis (klineDataGiver
).
- When a user starts the bot with
/start
, they receive a warm welcome message explaining the bot's functionality:"I can trade cryptos automatically using Super Trend. Ready to make stonks! 🤑"
- Command:
/balance
Retrieves and displays the user's USDT balance:- Available Balance
- Total Balance
-
Command:
/openposition <coin> <timeframe> <amount> <leverage>
Configures trade parameters:- Coin Name: e.g.,
BTC
- Timeframe: e.g.,
15m
for 15-minute candlesticks. - Amount: USDT amount to trade.
- Leverage: Leverage multiplier (e.g., 4x).
Once configured, the bot confirms the parameters and allows users to modify the coin or timeframe using:
/changetime <new_timeframe>
/changecoin <new_coin>
- Coin Name: e.g.,
- Command:
/confirm
After confirmation, the bot:- Retrieves candlestick data and calculates the quantity of the coin to trade.
- Opens a WebSocket connection for real-time market data updates.
- Uses the indicator function (
Indicator
) to generate BUY or SELL signals:- Opens a market position when a valid signal is received.
- Monitors open positions and checks for signal reversals:
- If the signal changes from BUY to SELL or vice versa, it closes the current position and opens a new one with doubled quantity.
- Closes the WebSocket if the position is completely exited.
- Command:
/help
Provides a list of all available commands and their descriptions.
- Catches errors gracefully and logs them for debugging purposes.
- User Interaction:
- Configure trades using
/openposition
. - Confirm trades with
/confirm
.
- Configure trades using
- Data Processing:
- Retrieves candlestick data via
klineDataGiver
. - Streams market data using Binance WebSocket.
- Evaluates trade signals with a custom Indicator function.
- Retrieves candlestick data via
- Trading Logic:
- Opens or reverses positions based on the indicator's signals.
- Calculates precise quantities using Binance’s asset precision.
- The bot currently supports only a single coin at a time.
- Requires accurate parameters (coin, timeframe, leverage) to function properly.
- Assumes prior setup of Binance API keys and access to futures trading.
This bot is a straightforward yet powerful tool for automated trading using Telegram, making it easy to configure and monitor trades without leaving the app.