-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Create TRADING BOT #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Create TRADING BOT #111
Conversation
An AI automated crypto trading bot for python-binance
|
@islambalikandakato-hash please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
|
`[10/31, 12:56 PM] ISLAM KATO: [10/30, 4:52 PM] ISLAM KATO: crypto_bot/ load_dotenv() API_KEY = os.getenv("BINANCE_API_KEY") Set up logginglogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def run_bot(symbol, timeframe, amount, testnet=True): if name == "main": [10/30, 4:55 PM] ISLAM KATO: import pandas as pd def get_historical_data(client, symbol, timeframe): def should_buy(df): def should_sell(df): [10/30, 4:55 PM] ISLAM KATO: import logging Define stop-loss and take-profit percentagesSTOP_LOSS_PCT = 0.02 # 2% def check_stop_loss(df, symbol): def check_take_profit(df, symbol): [10/30, 4:55 PM] ISLAM KATO: import pandas as pd Assuming get_historical_data() from strategy.py is availabledef run_backtest(): if name == 'main': def get_historical_data(client, symbol, timeframe, limit=200): def should_buy(df): def should_sell(df): [10/31, 12:44 PM] Aslam: https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage load_dotenv() TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN") def send_telegram_message(message): [10/31, 12:45 PM] Aslam: from notifier import send_telegram_message TRADE_LOG_FILE = "user_data/trade_history.csv" def log_trade(symbol, side, price, quantity): [10/31, 12:49 PM] Aslam: import pandas as pd def calculate_daily_profit(): [10/31, 12:49 PM] Aslam: from trade_logger import log_trade def schedule_daily_report(): if name == "main": |
An AI automated crypto trading bot for python-binance
[10/31, 12:56 PM] ISLAM KATO: [10/30, 4:52 PM] ISLAM KATO: crypto_bot/
├── config.py
├── main.py
├── requirements.txt
├── strategy.py
└── user_data/
└── api_keys.txt
[10/30, 4:52 PM] ISLAM KATO: python-binance
pandas
numpy
python-dotenv
backtesting
ta
[10/30, 4:53 PM] ISLAM KATO: pip install -r requirements.txt
[10/30, 4:53 PM] ISLAM KATO: BINANCE_API_KEY="u9281AAiMfFXviHjdCkOeqX8OZNblciGAHtFryj58Ehnq8OX9hGAJ9hCGLlLMzhU"
BINANCE_API_SECRET="LCCczPRxccBMgDajWikdZfTD4tZ7NmD1BwneSbZvCOWNROmZ4s4P9QNd9OacGh4z"
[10/30, 4:53 PM] ISLAM KATO: import os
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("BINANCE_API_KEY")
API_SECRET = os.getenv("BINANCE_API_SECRET")
[10/30, 4:54 PM] ISLAM KATO: import time
from binance.client import Client
from config import API_KEY, API_SECRET
from strategy import should_buy, should_sell, get_historical_data
from risk_management import check_stop_loss, check_take_profit
import logging
Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def run_bot(symbol, timeframe, amount, testnet=True):
"""
Main function to run the trading bot.
if name == "main":
# Parameters for the bot
TRADING_SYMBOL = 'BTCUSDT'
TIMEFRAME = '5m'
TRADE_AMOUNT = 0.001 # Quantity of the asset
USE_TESTNET = True # Set to False for live trading
[10/30, 4:55 PM] ISLAM KATO: import pandas as pd
import ta
def get_historical_data(client, symbol, timeframe):
"""
Fetches historical candlestick data from Binance.
"""
try:
candles = client.get_klines(symbol=symbol, interval=timeframe)
df = pd.DataFrame(candles, columns=[
'timestamp', 'open', 'high', 'low', 'close', 'volume',
'close_time', 'quote_asset_volume', 'number_of_trades',
'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'ignore'
])
df['close'] = pd.to_numeric(df['close'])
return df
except Exception as e:
logging.error(f"Failed to fetch historical data: {e}")
return None
def should_buy(df):
"""
Simple RSI-based buy strategy: buy when RSI crosses below 30.
"""
if df is None or df.empty:
return False
def should_sell(df):
"""
Simple RSI-based sell strategy: sell when RSI crosses above 70.
"""
if df is None or df.empty:
return False
[10/30, 4:55 PM] ISLAM KATO: import logging
Define stop-loss and take-profit percentages
STOP_LOSS_PCT = 0.02 # 2%
TAKE_PROFIT_PCT = 0.04 # 4%
def check_stop_loss(df, symbol):
"""
Checks for a stop-loss condition.
(This is a simplified example. A real bot needs to track entry price.)
"""
# For a live bot, you must track the entry price of your open position.
# For this example, we assume we just bought at the previous candle's close.
last_price = df['close'].iloc[-1]
entry_price = df['close'].iloc[-2]
def check_take_profit(df, symbol):
"""
Checks for a take-profit condition.
"""
last_price = df['close'].iloc[-1]
entry_price = df['close'].iloc[-2]
[10/30, 4:55 PM] ISLAM KATO: import pandas as pd
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
import ta
Assuming get_historical_data() from strategy.py is available
def run_backtest():
"""
Runs a backtest on the defined trading strategy.
"""
# You will need to download historical data for your backtest
# For example: df = get_historical_data(client, 'BTCUSDT', Client.KLINE_INTERVAL_5MINUTE)
# For this example, let's load a sample dataset
df = pd.read_csv("BTCUSDT_5m_sample.csv", index_col=0, parse_dates=True)
if name == 'main':
run_backtest()
[10/30, 4:56 PM] ISLAM KATO: git clone your_repo_url
[10/30, 4:56 PM] ISLAM KATO: pm2 start main.py --name crypto_bot
pm2 logs crypto_bot
[10/31, 12:56 PM] ISLAM KATO: [10/31, 12:42 PM] Aslam: import pandas as pd
import ta
import logging
def get_historical_data(client, symbol, timeframe, limit=200):
"""
Fetches historical candlestick data from Binance.
"""
try:
candles = client.get_klines(symbol=symbol, interval=timeframe, limit=limit)
df = pd.DataFrame(candles, columns=[
'timestamp', 'open', 'high', 'low', 'close', 'volume',
'close_time', 'quote_asset_volume', 'number_of_trades',
'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'ignore'
])
df['close'] = pd.to_numeric(df['close'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
def should_buy(df):
"""
RSI + EMA strategy:
- RSI crosses below 30 (oversold)
- Price is above the EMA (uptrend confirmation)
"""
if df is None or len(df) < 2:
return False
def should_sell(df):
"""
RSI + EMA strategy:
- RSI crosses above 70 (overbought)
- Price falls below the EMA (downtrend confirmation)
"""
if df is None or len(df) < 2:
return False
[10/31, 12:44 PM] Aslam: https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage
[10/31, 12:44 PM] Aslam: import os
import requests
from dotenv import load_dotenv
import logging
load_dotenv()
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
def send_telegram_message(message):
"""
Sends a Telegram message using your bot token and chat ID.
"""
if not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_ID:
logging.warning("Telegram credentials not found. Skipping notification.")
return
[10/31, 12:45 PM] Aslam: from notifier import send_telegram_message⚠️ STOP-LOSS triggered for {symbol}")
[10/31, 12:45 PM] Aslam: if should_buy(df):
logging.info(f"Buy signal detected for {symbol}. Placing buy order...")
order = client.create_market_buy_order(symbol, quantity=amount)
logging.info(f"Buy order placed: {order}")
send_telegram_message(f"✅ BUY ORDER placed for {symbol} — amount: {amount}")
open_position = True
[10/31, 12:46 PM] Aslam: elif check_take_profit(df, symbol):
logging.info(f"Take-profit triggered for {symbol}.")
order = client.create_market_sell_order(symbol, quantity=amount)
send_telegram_message(f"🎯 TAKE-PROFIT triggered for {symbol}")
open_position = False
elif check_stop_loss(df, symbol):
logging.warning(f"Stop-loss triggered for {symbol}.")
order = client.create_market_sell_order(symbol, quantity=amount)
send_telegram_message(f"
open_position = False
elif should_sell(df):
logging.info(f"Sell signal detected for {symbol}.")
order = client.create_market_sell_order(symbol, quantity=amount)
send_telegram_message(f"📉 SELL ORDER placed for {symbol}")
open_position = False
[10/31, 12:46 PM] Aslam: send_telegram_message(f"❌ ERROR in main loop: {e}")
[10/31, 12:48 PM] Aslam: crypto_bot/
├── trade_logger.py # Records all trades (buy/sell/time/price)
├── profit_reporter.py # Calculates daily P/L and sends a Telegram summary
[10/31, 12:48 PM] Aslam: import csv
import os
from datetime import datetime
TRADE_LOG_FILE = "user_data/trade_history.csv"
def log_trade(symbol, side, price, quantity):
"""
Logs each executed trade (buy/sell) to a CSV file.
"""
os.makedirs(os.path.dirname(TRADE_LOG_FILE), exist_ok=True)
file_exists = os.path.isfile(TRADE_LOG_FILE)
[10/31, 12:49 PM] Aslam: import pandas as pd
from datetime import datetime, timedelta
from notifier import send_telegram_message
from trade_logger import TRADE_LOG_FILE
def calculate_daily_profit():
"""
Calculates total profit/loss from trade history for the last 24 hours.
"""
try:
df = pd.read_csv(TRADE_LOG_FILE, parse_dates=['datetime'])
except FileNotFoundError:
send_telegram_message("📊 No trade history found yet.")
return
[10/31, 12:49 PM] Aslam: from trade_logger import log_trade
[10/31, 12:50 PM] Aslam: if should_buy(df):
logging.info(f"Buy signal detected for {symbol}. Placing buy order...")
order = client.create_market_buy_order(symbol, quantity=amount)
last_price = df['close'].iloc[-1]
log_trade(symbol, "BUY", last_price, amount)
send_telegram_message(f"✅ BUY {symbol} at {last_price} for {amount}")
open_position = True
[10/31, 12:51 PM] Aslam: elif should_sell(df):
logging.info(f"Sell signal detected for {symbol}. Placing sell order...")
order = client.create_market_sell_order(symbol, quantity=amount)
last_price = df['close'].iloc[-1]
log_trade(symbol, "SELL", last_price, amount)
send_telegram_message(f"📉 SELL {symbol} at {last_price} for {amount}")
open_position = False
[10/31, 12:52 PM] Aslam: pm2 start profit_reporter.py --name daily_report --cron "0 21 * * *"
[10/31, 12:52 PM] Aslam: import threading
from profit_reporter import calculate_daily_profit
def schedule_daily_report():
calculate_daily_profit()
threading.Timer(86400, schedule_daily_report).start() # 24h interval
if name == "main":
# existing bot setup...
schedule_daily_report()
run_bot(TRADING_SYMBOL, TIMEFRAME, TRADE_AMOUNT, USE_TESTNET)