-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_script.py
More file actions
92 lines (74 loc) · 2.74 KB
/
Copy pathmain_script.py
File metadata and controls
92 lines (74 loc) · 2.74 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
import requests
import time
import sys
import db_manager # This imports your database file
# Force UTF-8 encoding for Windows console
sys.stdout.reconfigure(encoding='utf-8')
# --- CONFIGURATION ---
TELEGRAM_BOT_TOKEN = "8257741647:AAEI-kwnFo4dINkZXisKBcHunvQ0Hfjf_OM"
TELEGRAM_CHAT_ID = "8518940489"
COINS = {
"BTC": "BTC",
"ETH": "ETH",
"SOL": "SOL",
"DOGE": "DOGE"
}
CHECK_INTERVAL = 60
# --- FUNCTIONS ---
def get_crypto_price(symbol):
# Using CryptoCompare
url = f"https://min-api.cryptocompare.com/data/price?fsym={symbol}&tsyms=USD"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
try:
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
data = response.json()
return data.get('USD')
else:
print(f"Error fetching price for {symbol}: Status {response.status_code}")
return None
except Exception as e:
print(f"Connection error: {e}")
return None
def send_telegram_alert(message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
payload = {
"chat_id": TELEGRAM_CHAT_ID,
"text": message
}
try:
requests.post(url, json=payload)
except Exception as e:
print(f"Failed to send alert: {e}")
# --- MAIN LOOP ---
def main():
print(f"Starting Crypto Tracker for {', '.join(COINS.keys())}...")
# STEP 1: Initialize the Database (Runs once at startup)
db_manager.init_db()
print("Database connected. Monitoring started.")
while True:
for symbol, mapped_id in COINS.items():
# STEP 2: Fetch current price
price = get_crypto_price(symbol)
if price:
print(f"[{time.strftime('%H:%M:%S')}] {symbol}: ${price}")
# STEP 3: Save to Database
db_manager.log_price(symbol, price)
# STEP 4: Check Logic (RSI)
should_alert, msg = db_manager.check_rsi_alert(symbol)
# If function returns None (collecting data), we just print the status
if should_alert is None:
print(f" {symbol} Status: {msg}")
# If we get a True/False signal
elif should_alert:
print(f"!!! {symbol} TRIGGER: {msg}")
send_telegram_alert(f"{symbol}: {msg}")
else:
print(f" {symbol} Status: {msg}")
# Sleep to respect API rate limits
print("-" * 30)
time.sleep(CHECK_INTERVAL)
if __name__ == "__main__":
main()