1 Preparation and Learning Basics
Required Knowledge:
- Python: Basic syntax, variables, functions, loops
- REST API and WebSockets: Basics of exchange communication
- Cryptocurrency Exchanges: Registration, order types, fees
Development Environment Setup:
- Install Python (latest stable version)
- Install a development environment (VS Code recommended)
- Create a project folder
Installing Required Libraries:
pip install python-binance
pip install pandas
pip install numpy
pip install ta
2 Getting API Keys from Exchange
Using Binance as an example:
- Log into your Binance account
- Navigate to API Management section
- Create a new API key
- IMPORTANT: Uncheck "Enable Withdrawals" permission
- Keep only "Enable Reading" and "Enable Spot & Margin Trading"
- Save API Key and Secret Key in a secure location
API_KEY = 'YOUR_API_KEY_HERE'
API_SECRET = 'YOUR_API_SECRET_HERE'
3 Writing Bot Code
Create crypto_bot.py file and add the following code:
import pandas as pd
from binance.client import Client
from binance.enums import *
import time
import config
client = Client(config.API_KEY, config.API_SECRET)
TRADE_SYMBOL = 'BTCUSDT'
TRADE_QUANTITY = 0.001
PROFIT_TARGET = 1.0
STOP_LOSS = 2.0
in_position = False
buy_price = 0.0
def get_price(symbol):
try:
ticker = client.get_symbol_ticker(symbol=symbol)
return float(ticker['price'])
except Exception as e:
print(f"Error getting price: {e}")
return None
def create_order(symbol, side, quantity):
try:
current_price = get_price(symbol)
if not current_price:
return None
if side == SIDE_BUY:
price = round(current_price * 0.995, 2)
elif side == SIDE_SELL:
price = round(current_price * 1.005, 2)
else:
return None
order = client.create_order(
symbol=symbol,
side=side,
type=ORDER_TYPE_LIMIT,
timeInForce=TIME_IN_FORCE_GTC,
quantity=quantity,
price=str(price)
)
print(f"Order placed: {side} {quantity} {symbol} at price {price}")
return order
except Exception as e:
print(f"Error placing order: {e}")
return None
This is the basic bot structure. The complete code with trading strategy and main loop is available in the full guide.