Repository navigation
Expand file tree
/
Copy pathTrading_bot.py
More file actions
238 lines (197 loc) Β· 8.62 KB
/
Copy pathTrading_bot.py
File metadata and controls
238 lines (197 loc) Β· 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import logging
from binance.client import Client
from binance.exceptions import BinanceAPIException
import telegram
import pandas as pd
from datetime import datetime, timedelta
import time
import os
from dotenv import load_dotenv
import sys
import asyncio
from telegram import Bot
from telegram.error import TelegramError
import platform
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('crypto_bot.log'),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger(__name__)
class CryptoAnalysisBot:
def __init__(self):
# Initialize Binance client
self.binance_client = Client(
os.getenv('BINANCE_API_KEY'),
os.getenv('BINANCE_API_SECRET'),
)
# Initialize Telegram bot
self.telegram_bot = Bot(token=os.getenv('TELEGRAM_BOT_TOKEN'))
self.telegram_chat_id = os.getenv('TELEGRAM_CHAT_ID')
# Trading pairs to monitor
self.pairs = [
'BTCUSDT', 'ETHUSDT', 'BNBUSDT', 'ADAUSDT', 'DOGEUSDT',
'XRPUSDT', 'DOTUSDT', 'UNIUSDT', 'LINKUSDT', 'SOLUSDT',
'TRXUSDT', 'TONUSDT', 'MATICUSDT', 'LTCUSDT', 'AVAXUSDT',
'ATOMUSDT', 'APEUSDT', 'SHIBUSDT', 'APTUSDT', 'OPUSDT'
]
# Analysis parameters
self.timeframe = '4h'
self.lookback_days = 4
self.threshold_percentage = 3
# Message queue
self.message_queue = asyncio.Queue()
def get_historical_data(self, symbol, interval='1h', lookback_days=4):
"""Retrieve historical OHLCV data from Binance"""
try:
start_time = int((datetime.now() - timedelta(days=lookback_days)).timestamp() * 1000)
klines = self.binance_client.get_klines(
symbol=symbol,
interval=interval,
startTime=start_time,
)
df = pd.DataFrame(klines, 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['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
for col in ['open', 'high', 'low', 'close', 'volume']:
df[col] = df[col].astype(float)
return df
except BinanceAPIException as e:
logger.error(f"Error fetching data for {symbol}: {str(e)}")
return None
def find_local_extremes(self, df):
"""Find local maximum and minimum prices"""
if df is None or len(df) < 2:
return None, None
try:
local_max = df['high'].max()
local_min = df['low'].min()
return local_max, local_min
except Exception as e:
logger.error(f"Error calculating local extremes: {str(e)}")
return None, None
def is_price_near_extreme(self, current_price, extreme_price, threshold_percentage):
"""Check if current price is near local extreme"""
if current_price is None or extreme_price is None:
return False
difference_percentage = abs((current_price - extreme_price) / extreme_price * 100)
return difference_percentage <= threshold_percentage
async def send_telegram_alert(self, message: str) -> bool:
"""Send alert message via Telegram with retry logic"""
max_retries = 3
retry_delay = 5
for attempt in range(max_retries):
try:
await self.telegram_bot.send_message(
chat_id=self.telegram_chat_id,
text=message,
parse_mode='HTML'
)
logger.info("Telegram alert sent successfully")
return True
except Exception as e:
if attempt == max_retries - 1:
logger.error(f"Failed to send Telegram alert after {max_retries} attempts: {str(e)}")
return False
logger.warning(f"Telegram alert attempt {attempt + 1} failed, retrying in {retry_delay} seconds...")
await asyncio.sleep(retry_delay)
return False
async def analyze_pair(self, symbol):
"""Analyze a single trading pair"""
try:
df = self.get_historical_data(
symbol,
self.timeframe,
self.lookback_days
)
if df is None or df.empty:
logger.warning(f"No data available for {symbol}")
return
current_price = float(df['close'].iloc[-1])
local_max, local_min = self.find_local_extremes(df)
near_max = self.is_price_near_extreme(
current_price,
local_max,
self.threshold_percentage
)
near_min = self.is_price_near_extreme(
current_price,
local_min,
self.threshold_percentage
)
if near_max or near_min:
current_price_formatted = f"${current_price:,.2f}"
local_max_formatted = f"${local_max:,.2f}"
local_min_formatted = f"${local_min:,.2f}"
max_percentage = ((current_price - local_max) / local_max) * 100
min_percentage = ((current_price - local_min) / local_min) * 100
message = (
f"π¨ {symbol} Alert π¨\n\n"
f"π Current Price: {current_price_formatted}\n"
f"π Local High: {local_max_formatted}\n"
f"π Local Low: {local_min_formatted}\n\n"
)
if near_max:
message += (
f"β οΈ Price is approaching the local high(LONG!π’)\n"
f"π Distance to the high: {abs(max_percentage):.2f}%\n"
)
if near_min:
message += (
f"β οΈ Price is approaching the local low!(SHORT!π΄)\n"
f"π Distance to the low: {abs(min_percentage):.2f}%\n"
)
await self.send_telegram_alert(message)
except Exception as e:
logger.error(f"Error analyzing {symbol}: {str(e)}")
async def run(self):
"""Main bot loop"""
logger.info("Starting Crypto Analysis Bot...")
while True:
try:
for pair in self.pairs:
logger.info(f"Analyzing {pair}...")
await self.analyze_pair(pair)
await asyncio.sleep(1)
logger.info("Analysis round completed. Waiting for next round...")
await asyncio.sleep(300)
except Exception as e:
logger.error(f"Error in main loop: {str(e)}")
await asyncio.sleep(60)
async def main():
# Set up the event loop policy for Windows
if platform.system() == 'Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
required_env_vars = [
'BINANCE_API_KEY',
'BINANCE_API_SECRET',
'TELEGRAM_BOT_TOKEN',
'TELEGRAM_CHAT_ID'
]
missing_vars = [var for var in required_env_vars if not os.getenv(var)]
if missing_vars:
logger.error(f"Missing required environment variables: {', '.join(missing_vars)}")
if not os.path.exists('.env'):
with open('.env', 'w') as f:
f.write("\n".join(f"{var}=your_{var.lower()}" for var in required_env_vars))
logger.info("Created .env file template. Please fill in your API keys and tokens.")
sys.exit(1)
try:
bot = CryptoAnalysisBot()
await bot.run()
except KeyboardInterrupt:
logger.info("Bot stopped by user")
except Exception as e:
logger.error(f"Fatal error: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())