Hopper1394
commited on
Commit
•
ba5c7ef
1
Parent(s):
4eee85d
Update README.md
Browse files
README.md
CHANGED
@@ -1,76 +1,150 @@
|
|
1 |
---
|
2 |
license: mit
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
+
language:
|
4 |
+
- en
|
5 |
+
library_name: transformers
|
6 |
+
pipeline_tag: depth-estimation
|
7 |
---
|
8 |
+
# config.py
|
9 |
+
BINANCE_API_KEY = 'your_binance_api_key'
|
10 |
+
ALPHA_VANTAGE_API_KEY = 'your_alpha_vantage_api_key'
|
11 |
+
YAHOO_FINANCE_API_KEY = 'your_yahoo_finance_api_key'
|
12 |
+
TRADING_VIEW_API_KEY = 'your_trading_view_api_key'
|
13 |
+
BINOMO_API_KEY = 'your_binomo_api_key'
|
14 |
+
TELEGRAM_BOT_API_KEY = 'your_telegram_bot_api_key'
|
15 |
+
|
16 |
+
# data_acquisition.py
|
17 |
+
import requests
|
18 |
+
import pandas as pd
|
19 |
+
import numpy as np
|
20 |
+
from sklearn.preprocessing import StandardScaler
|
21 |
+
from tensorflow.keras.models import Sequential
|
22 |
+
from tensorflow.keras.layers import LSTM, Dense, Dropout
|
23 |
+
from telegram.ext import Updater, CommandHandler
|
24 |
+
|
25 |
+
def fetch_binance_data(pair):
|
26 |
+
url = f"https://api.binance.com/api/v3/klines?symbol={pair}&interval=1h"
|
27 |
+
response = requests.get(url)
|
28 |
+
data = response.json()
|
29 |
+
df = pd.DataFrame(data, 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'])
|
30 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
|
31 |
+
return df[['timestamp', 'open', 'high', 'low', 'close', 'volume']]
|
32 |
+
|
33 |
+
def fetch_alpha_vantage_data(pair):
|
34 |
+
symbol = pair.split("USDT")[0] # Assuming pair like BTCUSDT
|
35 |
+
url = f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=60min&apikey={ALPHA_VANTAGE_API_KEY}"
|
36 |
+
response = requests.get(url)
|
37 |
+
data = response.json()
|
38 |
+
time_series_key = 'Time Series (60min)'
|
39 |
+
if time_series_key not in data:
|
40 |
+
raise ValueError(f"Error fetching data from Alpha Vantage: {data}")
|
41 |
+
df = pd.DataFrame(data[time_series_key]).T
|
42 |
+
df.columns = ['open', 'high', 'low', 'close', 'volume']
|
43 |
+
df.index = pd.to_datetime(df.index)
|
44 |
+
return df.reset_index().rename(columns={'index': 'timestamp'})
|
45 |
+
|
46 |
+
def fetch_yahoo_finance_data(pair):
|
47 |
+
url = f"https://yfapi.net/v8/finance/chart/{pair}?interval=60m"
|
48 |
+
headers = {'x-api-key': YAHOO_FINANCE_API_KEY}
|
49 |
+
response = requests.get(url, headers=headers)
|
50 |
+
data = response.json()
|
51 |
+
timestamps = data['chart']['result'][0]['timestamp']
|
52 |
+
ohlc = data['chart']['result'][0]['indicators']['quote'][0]
|
53 |
+
df = pd.DataFrame({
|
54 |
+
'timestamp': pd.to_datetime(timestamps, unit='s'),
|
55 |
+
'open': ohlc['open'],
|
56 |
+
'high': ohlc['high'],
|
57 |
+
'low': ohlc['low'],
|
58 |
+
'close': ohlc['close'],
|
59 |
+
'volume': ohlc['volume']
|
60 |
+
})
|
61 |
+
return df
|
62 |
+
|
63 |
+
def fetch_trading_view_data(pair):
|
64 |
+
# Placeholder for TradingView API data fetching
|
65 |
+
raise NotImplementedError("TradingView API integration not implemented.")
|
66 |
+
|
67 |
+
def fetch_binomo_data(pair):
|
68 |
+
# Placeholder for Binomo API data fetching
|
69 |
+
raise NotImplementedError("Binomo API integration not implemented.")
|
70 |
+
|
71 |
+
def get_combined_data(pair):
|
72 |
+
df_binance = fetch_binance_data(pair)
|
73 |
+
df_alpha = fetch_alpha_vantage_data(pair)
|
74 |
+
df_yahoo = fetch_yahoo_finance_data(pair)
|
75 |
+
# Merge dataframes on timestamp
|
76 |
+
df = pd.merge(df_binance, df_alpha, on='timestamp', suffixes=('_binance', '_alpha'))
|
77 |
+
df = pd.merge(df, df_yahoo, on='timestamp', suffixes=('', '_yahoo'))
|
78 |
+
# Drop any redundant columns or handle conflicts
|
79 |
+
return df
|
80 |
+
|
81 |
+
def preprocess_data(df):
|
82 |
+
df = df.dropna()
|
83 |
+
scaler = StandardScaler()
|
84 |
+
scaled_data = scaler.fit_transform(df[['open', 'high', 'low', 'close', 'volume']])
|
85 |
+
return scaled_data, scaler
|
86 |
+
|
87 |
+
def create_dataset(data, time_step=60):
|
88 |
+
X, Y = [], []
|
89 |
+
for i in range(len(data) - time_step - 1):
|
90 |
+
a = data[i:(i + time_step), :]
|
91 |
+
X.append(a)
|
92 |
+
Y.append(data[i + time_step, 3]) # Assuming 'close' price is the target
|
93 |
+
return np.array(X), np.array(Y)
|
94 |
+
|
95 |
+
def build_model(input_shape):
|
96 |
+
model = Sequential()
|
97 |
+
model.add(LSTM(50, return_sequences=True, input_shape=input_shape))
|
98 |
+
model.add(LSTM(50, return_sequences=False))
|
99 |
+
model.add(Dropout(0.2))
|
100 |
+
model.add(Dense(25))
|
101 |
+
model.add(Dense(1))
|
102 |
+
model.compile(optimizer='adam', loss='mean_squared_error')
|
103 |
+
return model
|
104 |
+
|
105 |
+
def train_model(df):
|
106 |
+
data, scaler = preprocess_data(df)
|
107 |
+
X, Y = create_dataset(data)
|
108 |
+
X_train, Y_train = X[:int(len(X) * 0.8)], Y[:int(len(Y) * 0.8)]
|
109 |
+
X_val, Y_val = X[int(len(X) * 0.8):], Y[int(len(Y) * 0.8):]
|
110 |
+
model = build_model((X_train.shape[1], X_train.shape[2]))
|
111 |
+
model.fit(X_train, Y_train, validation_data=(X_val, Y_val), epochs=20, batch_size=32)
|
112 |
+
return model, scaler
|
113 |
+
|
114 |
+
def generate_signal(pair):
|
115 |
+
df = get_combined_data(pair)
|
116 |
+
model, scaler = train_model(df)
|
117 |
+
recent_data = df.tail(60).drop(columns=['timestamp'])
|
118 |
+
scaled_recent_data = scaler.transform(recent_data)
|
119 |
+
prediction = model.predict(np.expand_dims(scaled_recent_data, axis=0))
|
120 |
+
last_close = df['close'].iloc[-1]
|
121 |
+
if prediction > last_close:
|
122 |
+
return "Buy"
|
123 |
+
else:
|
124 |
+
return "Sell"
|
125 |
+
|
126 |
+
def start(update, context):
|
127 |
+
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a trading bot, how can I help you today?")
|
128 |
+
|
129 |
+
def signal(update, context):
|
130 |
+
pair = context.args[0] if context.args else 'BTCUSDT'
|
131 |
+
try:
|
132 |
+
trade_signal = generate_signal(pair)
|
133 |
+
context.bot.send_message(chat_id=update.effective_chat.id, text=f"Trade Signal for {pair}: {trade_signal}")
|
134 |
+
except Exception as e:
|
135 |
+
context.bot.send_message(chat_id=update.effective_chat.id, text=f"Error: {e}")
|
136 |
+
|
137 |
+
def main():
|
138 |
+
updater = Updater(token=TELEGRAM_BOT_API_KEY, use_context=True)
|
139 |
+
dispatcher = updater.dispatcher
|
140 |
+
|
141 |
+
start_handler = CommandHandler('start', start)
|
142 |
+
signal_handler = CommandHandler('signal', signal)
|
143 |
+
|
144 |
+
dispatcher.add_handler(start_handler)
|
145 |
+
dispatcher.add_handler(signal_handler)
|
146 |
+
|
147 |
+
updater.start_polling()
|
148 |
+
|
149 |
+
if __name__ == '__main__':
|
150 |
+
main()
|