-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptomainindbbrsifavs.py
More file actions
187 lines (149 loc) · 5.95 KB
/
cryptomainindbbrsifavs.py
File metadata and controls
187 lines (149 loc) · 5.95 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
import requests
import pandas as pd
import ta
import xml.etree.ElementTree as ET
import os
import tkinter as tk
from tkinter import ttk, messagebox, simpledialog
# ✅ File Paths
XML_FILE = "crypto_data.xml"
CONFIG_FILE = "crypto_config.txt"
# ✅ Binance API Endpoint
BINANCE_URL = "https://api.binance.com/api/v3/klines"
# ✅ Available Time Intervals
TIME_INTERVALS = {
"4h": "4h",
"8h": "8h",
"12h": "12h",
"1 Day": "1d",
"1 Week": "1w"
}
# ✅ Load Crypto Pairs
def load_crypto_pairs():
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r") as f:
pairs = [line.strip() for line in f.readlines()]
return {pair: pair.replace("USDT", "") for pair in pairs}
return {"BTCUSDT": "Bitcoin"} # Default Pair: BTC
# ✅ Save Crypto Pairs
def save_crypto_pairs(crypto_pairs):
with open(CONFIG_FILE, "w") as f:
for pair in crypto_pairs.keys():
f.write(pair + "\n")
# ✅ Fetch OHLCV Data
def get_historical_data(symbol, interval):
params = {"symbol": symbol, "interval": interval, "limit": 50}
response = requests.get(BINANCE_URL, params=params)
if response.status_code == 200:
data = response.json()
df = pd.DataFrame(data, columns=[
"timestamp", "open", "high", "low", "close", "volume",
"close_time", "quote_asset_volume", "trades",
"taker_buy_base", "taker_buy_quote", "ignore"
])
df["close"] = df["close"].astype(float)
return df
else:
messagebox.showerror("Error", f"Failed to fetch data for {symbol}")
return None
# ✅ Calculate RSI & Bollinger Bands
def calculate_indicators(df):
if df is None:
return None, None, None, None
df["RSI"] = ta.momentum.RSIIndicator(df["close"], window=14).rsi()
bb = ta.volatility.BollingerBands(df["close"], window=20, window_dev=2)
df["BB_upper"] = bb.bollinger_hband()
df["BB_lower"] = bb.bollinger_lband()
latest_price = df["close"].iloc[-1]
return latest_price, df["RSI"].iloc[-1], df["BB_upper"].iloc[-1], df["BB_lower"].iloc[-1]
# ✅ Save Data to XML
def save_to_xml(crypto_data):
root = ET.Element("CryptoData")
for symbol, data in crypto_data.items():
token = ET.SubElement(root, "Token", name=data["name"], symbol=symbol)
ET.SubElement(token, "Price").text = str(round(data["Price"], 2))
ET.SubElement(token, "RSI").text = str(round(data["RSI"], 2))
ET.SubElement(token, "BB_upper").text = str(round(data["BB_upper"], 2))
ET.SubElement(token, "BB_lower").text = str(round(data["BB_lower"], 2))
tree = ET.ElementTree(root)
tree.write(XML_FILE)
# ✅ Fetch Data and Display in Table
def fetch_data():
global crypto_pairs
crypto_data = {}
selected_interval = interval_var.get()
binance_interval = TIME_INTERVALS[selected_interval]
# Clear Table
for row in table.get_children():
table.delete(row)
for symbol, name in crypto_pairs.items():
df = get_historical_data(symbol, binance_interval)
price, rsi, bb_upper, bb_lower = calculate_indicators(df)
if price is not None:
crypto_data[symbol] = {
"name": name, "Price": price, "RSI": rsi, "BB_upper": bb_upper, "BB_lower": bb_lower
}
# Determine Color for Price & Indicators
price_color = "red" if price > bb_upper or price < bb_lower else "black"
rsi_color = "red" if rsi > 70 or rsi < 30 else "black"
table.insert("", "end", values=(
name, symbol, f"${price:.2f}", f"{rsi:.2f}", f"${bb_upper:.2f}", f"${bb_lower:.2f}"
), tags=(price_color, rsi_color))
save_to_xml(crypto_data)
# ✅ Add Token
def add_token():
global crypto_pairs
new_symbol = simpledialog.askstring("Add Token", "Enter token symbol (e.g., ETHUSDT):").upper()
new_name = simpledialog.askstring("Add Token", f"Enter the name for {new_symbol} (e.g., Ethereum):")
if new_symbol and new_name:
if new_symbol not in crypto_pairs:
crypto_pairs[new_symbol] = new_name
save_crypto_pairs(crypto_pairs)
token_listbox.insert(tk.END, f"{new_name} ({new_symbol})")
else:
messagebox.showwarning("Warning", "Token already exists!")
# ✅ Remove Token
def remove_token():
global crypto_pairs
selected = token_listbox.curselection()
if selected:
index = selected[0]
symbol = list(crypto_pairs.keys())[index]
del crypto_pairs[symbol]
save_crypto_pairs(crypto_pairs)
token_listbox.delete(index)
# ✅ Initialize GUI
root = tk.Tk()
root.title("Crypto RSI & Bollinger Bands Tracker")
root.geometry("700x600")
crypto_pairs = load_crypto_pairs()
# ✅ Interval Selection
interval_var = tk.StringVar(value="4h")
interval_menu = ttk.Combobox(root, textvariable=interval_var, values=list(TIME_INTERVALS.keys()))
interval_menu.pack(pady=5)
# ✅ Buttons
tk.Button(root, text="Fetch Data", command=fetch_data).pack(pady=5)
# ✅ Token Listbox
tk.Label(root, text="Tracked Tokens").pack()
token_listbox = tk.Listbox(root, height=5)
token_listbox.pack(fill=tk.BOTH, expand=True)
# ✅ Populate Listbox with existing tokens
for symbol, name in crypto_pairs.items():
token_listbox.insert(tk.END, f"{name} ({symbol})")
# ✅ Buttons for Add/Remove Tokens
tk.Button(root, text="Add Token", command=add_token).pack(pady=5)
tk.Button(root, text="Remove Token", command=remove_token).pack(pady=5)
# ✅ Table for Results
table_frame = ttk.Frame(root)
table_frame.pack(fill=tk.BOTH, expand=True)
columns = ("Name", "Symbol", "Price", "RSI", "BB Upper", "BB Lower")
table = ttk.Treeview(table_frame, columns=columns, show="headings")
for col in columns:
table.heading(col, text=col)
table.column(col, anchor="center")
table.pack(fill=tk.BOTH, expand=True)
# ✅ Color Tags for Warnings
table.tag_configure("red", foreground="red")
table.tag_configure("black", foreground="black")
# ✅ Run the App
root.mainloop()