TLDR: Automated Futures bot made in 2021. Turned a profit until october 2022, ethereum changed from POW to POS and traded volume fell significantly. There was no major Generative AI easily avilable at the time of creation, This coded was conceived through a lot of StackOverflow and frustration.
Automated trading bot for executing futures orders on Binance, focused on the ETHBUSD pair.
The script operates based on technical analysis signals from TradingView and flow indicators (volume/rate) from the Binance API, executing market orders at short intervals.
The Binance API keys are hardcoded in the file.
Note: The keys present in the source code are expired/revoked and no longer represent a security risk. They are kept in the code for reference purposes only.
Before running, replace them with your own keys and, ideally, move them to environment variables or a .env file. Never share or version active API keys in public repositories.
- Automated Technical Analysis: consumes TradingView recommendations (BUY, SELL, NEUTRAL) for the ETHBUSD pair on the 15-minute timeframe.
- Order Execution: sends market orders (MARKET) on Binance Futures, with support for long and short positions.
- Position Management: checks current position, mark price, and dynamically calculates order size based on available balance and configured leverage.
- Flow Filter: uses volume/rate data from 1h Binance k-lines to determine if the market has enough flow to operate.
- Continuous Loop: runs indefinitely with automatic reconnection in case of errors.
- Logging: records all orders and errors via
logging.
- Python 3.8+
- Binance account with Futures enabled
- API keys with read and futures trading permissions
Install the required libraries via pip:
pip install pandas requests python-binance tradingview-taLibraries used:
python-binance– Binance API integration (spot and futures)pandas– price data manipulationrequests– HTTP requests to Binance endpointstradingview-ta– retrieval of TradingView technical signals
- Clone or copy the
futures.pyfile to your local environment. - Set up your API keys (recommended via environment variables):
export BINANCE_API_KEY="your_api_key"
export BINANCE_API_SECRET="your_api_secret"- Adjust the parameters in the script if necessary:
leverage = 5– leverage used (default: 5x)symbol = "ETHBUSD"– traded pair- Volume/rate thresholds for bot activation
- Sleep intervals and data refresh timing
Run the bot directly from the terminal:
python futures.pyThe bot will start by checking your current position, balance, and market price, entering a continuous loop of:
- Querying TradingView technical signals
- Checking flow/volume via the Binance API
- Executing or reversing positions according to the signal and current position
To stop, use Ctrl+C.
| Function | Description |
|---|---|
te() |
Tests connectivity with the Binance futures API |
search() |
Searches for balance of specific assets in the account |
simbolo() |
Gets quantity precision for the ETHUSD_PERP pair |
a() |
Returns available BUSD balance in the futures account |
compra() |
(Not used) Sends a sell (short) market order – legacy code |
liquida() |
(Not used) Sends a buy order to reduce a short position – legacy code |
b() |
Identifies whether the current position is long, short, or neutral |
preco() |
Returns the mark price of the ETHBUSD position |
quantidade() |
Calculates order size based on balance, leverage, and price |
pegaposicao() |
Updates the global variable for the current position |
mudanca() |
Executes orders according to the new signal (buy/sell/neutral) – main flow |
vendertudo() |
Intelligently closes open positions (reduceOnly) – main flow |
verifica() |
Compares current position with the latest signal and triggers reversal if needed – main flow |
pegafluxo() |
Fetches candles (klines) from 1h and 15m for volume analysis |
ping() |
Main loop: evaluates signals, flow, and executes orders – main flow |
attempts() |
Reconnection wrapper with exception handling – main flow |
Note on unused functions:
compra()andliquida()are defined in the code but are never called during execution. The active order execution logic is entirely contained withinmudanca()andvendertudo(), which are invoked by the main flow viaverifica()→ping()→attempts().
- Initialization: checks current position and calculates base quantity.
- Data Collection: fetches 1h and 15m candles from Binance, the data frame is valid for 2 minutes than its refreshed to save bandwidth.
- Flow Analysis: if volume/rate is above the threshold, proceeds.
- Technical Signal: queries TradingView recommendation.
- Execution:
- If the signal differs from the current position, closes the open position and opens the new one.
- If flow is insufficient or the signal is neutral, holds or closes positions.
- Waits 5 seconds and repeats the cycle.
- Binance exceptions are caught and logged via
logging.error. - Connectivity failures or timeouts trigger a retry after 5 seconds.
- The main loop is protected by
attempts()to prevent the bot from stopping due to temporary errors.
- The bot was designed to operate exclusively with ETHBUSD; adaptations for other pairs require adjustments to the
symbolparameter and quantity precision. - The liquidation quantity (
vendertudo()) is partially hardcoded in some flows (e.g.,quantity=10) – review according to your capital. - The code uses global variables for state control (
posicao,last_order,response, etc.). - Legacy functions:
compra()andliquida()are dead code (orphan functions) that are not part of the current execution flow. They may be removed or reactivated if you choose to refactor the order execution structure. - Extensive testing on testnet is recommended before operating with real capital.
This project is for personal and educational use. The author is not responsible for financial losses. Use at your own risk.