This code implements a Pump and Dump Bot that checks for pump and dump opportunities on various cryptocurrency exchanges. It uses the Binance API through trade_api.py to gather information about open orders, calculate minimum price and quantity for buying, place buy and sell orders, and check the status of existing orders.
Since python is shifting toward type safety, in this version we have leveraged the Pydantic library to handle our data integrity and type hinting.
In order to use this code, you must have a Binance account and API keys. You can create an account and generate API keys here: https://www.binance.com/en/register
The initial implementation of the bot system contained several issues related to code readability, maintainability, and best practices. The primary goal of our refactor was to:
- Improve code readability: We broke down complex code blocks into smaller, more manageable functions, making it easier to understand the system's logic and purpose.
- Reduce cognitive complexity: We simplified nested conditions and loops, which helped make the code more approachable and easier to reason about.
- Leverage modern Python features: We introduced the use of f-strings, type annotations, and namedtuples to make the code more expressive and easier to maintain.
- Improve error handling: We addressed potential issues related to list access, making the code more robust and less prone to errors.
if you have not already, install python 3.8 or higher and pip, then run the following commands in the terminal:
pip install -r requirements.txt
note: it may be pip3 if your on *nix
The .env file contains the following parameters:
api_key
your Binance API keyapi_secret
your Binance API secretsymbols
a list of symbols to check for pump and dump opportunitiesbuy_price_change
the expected price change in percentage for a buy order to be placedsell_price_change
the expected price change in percentage for a sell order to be placedbuy_price_change_threshold
the minimum price change in percentage for a buy order to be placedsell_price_change_threshold
the minimum price change in percentage for a sell order to be placed
The code is tested using pytest. You can run the tests by running the following command in the terminal:
pytest
The code is documented using docstrings. You can view the documentation by running the following command in the terminal:
python -m pydoc -b
then navigate to http://localhost:8000/ in your browser
To run the code, run the following command in the terminal:
python run.py
PumpDumpBot class is created with several key methods:
-
calculate_buy_price()
method takes a symbol as input and calculates the minimum price and quantity for buying, the final buy price, and the quantity to buy if the price and price change meet the specified criteria -
check_buy()
method checks for buy opportunities and buys a specified coin if the price is below the buy price -
check_sell()
method checks the current price against the price of a specified order and sells the order if the current price is greater than the order price by the expected change percentage -
check_order_status()
method checks the status of the specified order and deletes it from the database if it is filled -
run_loop()
method constantly checks for buy and sell opportunities and handling exceptions
Finally, a threaded execution is run on the run_loop() method for each specified symbol in config.symbols using a ThreadPoolExecutor object. The code relies on a module called trade_api which is a wrapper for the Binance API. It contains the following methods:
class TradeAPI
\
TradeAPI has several methods for retrieving data from Binance, such as:
-
get_symbol
()
Return the symbol data object that matches the symbol passed in. -
get_symbols_data
()
retrieves a list of SymbolData objects containing the symbol and its current price. -
get_ticker_info
()
returns a TickerData object containing information such as the last price, volume, high/low prices, etc. of the symbol. -
get_symbol_filters
()
returns TradeApiFilters objects that can be used to determine the minimum and maximum price. for a certain symbol. -
get_open_orders
()
will retrieve a list of open orders for a given symbol. -
get_order()
returns information about an order for a given symbol and order ID.
The TradeAPI class initializes with a default constructor that retrieves data from Binance and transforms the buy price change from 1% to 1.01.
We define several classes that extend Pedantic BaseModel and are used for parsing various Binance API responses, including models for:
- asset filters
- symbol data
- order lists
- ticker data
- asset information data