-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyTorch_Simple_Neural_Network.py
More file actions
190 lines (155 loc) · 6.77 KB
/
PyTorch_Simple_Neural_Network.py
File metadata and controls
190 lines (155 loc) · 6.77 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
import os
import pandas as pd
from datetime import datetime, timedelta
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from config import data_base_path
import random
import requests
import retrying
forecast_price = {}
binance_data_path = os.path.join(data_base_path, "binance/futures-klines")
MAX_DATA_SIZE = 100 # Максимальна кількість даних для збереження
INITIAL_FETCH_SIZE = 100 # Початковий розмір завантаження свічок
@retrying.retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=5)
def fetch_prices(symbol, interval="5m", limit=100, start_time=None, end_time=None):
try:
base_url = "https://fapi.binance.com"
endpoint = f"/fapi/v1/klines"
params = {
"symbol": symbol,
"interval": interval,
"limit": limit
}
if start_time:
params['startTime'] = start_time
if end_time:
params['endTime'] = end_time
url = base_url + endpoint
response = requests.get(url, params=params)
response.raise_for_status()
return response.json()
except Exception as e:
print(f'Failed to fetch prices for {symbol} from Binance API: {str(e)}')
raise e
def download_data(token):
symbols = f"{token.upper()}USDT"
interval = "5m"
current_datetime = datetime.now()
download_path = os.path.join(binance_data_path, token.lower())
file_path = os.path.join(download_path, f"{token.lower()}_5m_data.csv")
if os.path.exists(file_path):
start_time = int((current_datetime - timedelta(minutes=500)).timestamp() * 1000)
end_time = int(current_datetime.timestamp() * 1000)
new_data = fetch_prices(symbols, interval, 100, start_time, end_time)
else:
start_time = int((current_datetime - timedelta(minutes=INITIAL_FETCH_SIZE*5)).timestamp() * 1000)
end_time = int(current_datetime.timestamp() * 1000)
new_data = fetch_prices(symbols, interval, INITIAL_FETCH_SIZE, start_time, end_time)
new_df = pd.DataFrame(new_data, columns=[
"start_time", "open", "high", "low", "close", "volume", "close_time",
"quote_asset_volume", "number_of_trades", "taker_buy_base_asset_volume",
"taker_buy_quote_asset_volume", "ignore"
])
if os.path.exists(file_path):
old_df = pd.read_csv(file_path)
combined_df = pd.concat([old_df, new_df])
combined_df = combined_df.drop_duplicates(subset=['start_time'], keep='last')
else:
combined_df = new_df
if len(combined_df) > MAX_DATA_SIZE:
combined_df = combined_df.iloc[-MAX_DATA_SIZE:]
if not os.path.exists(download_path):
os.makedirs(download_path)
combined_df.to_csv(file_path, index=False)
print(f"Updated data for {token} saved to {file_path}. Total rows: {len(combined_df)}")
def format_data(token):
path = os.path.join(binance_data_path, token.lower())
file_path = os.path.join(path, f"{token.lower()}_5m_data.csv")
if not os.path.exists(file_path):
print(f"No data file found for {token}")
return
df = pd.read_csv(file_path)
columns_to_use = [
"start_time", "open", "high", "low", "close", "volume",
"close_time", "quote_asset_volume", "number_of_trades",
"taker_buy_base_asset_volume", "taker_buy_quote_asset_volume"
]
if set(columns_to_use).issubset(df.columns):
df = df[columns_to_use]
df.columns = [
"start_time", "open", "high", "low", "close", "volume",
"end_time", "quote_asset_volume", "n_trades",
"taker_volume", "taker_volume_usd"
]
df.index = pd.to_datetime(df["start_time"], unit='ms')
df.index.name = "date"
output_path = os.path.join(data_base_path, f"{token.lower()}_price_data.csv")
df.sort_index().to_csv(output_path)
print(f"Formatted data saved to {output_path}")
else:
print(f"Required columns are missing in {file_path}. Skipping this file.")
def train_model(token):
# Завантаження даних
price_data = pd.read_csv(os.path.join(data_base_path, f"{token.lower()}_price_data.csv"))
price_data["date"] = pd.to_datetime(price_data["date"])
price_data.set_index("date", inplace=True)
df = price_data.resample('10min').mean()
df = df.dropna() # Видалити NaN
X = np.array(range(len(df))).reshape(-1, 1)
# Створюємо другу ознаку, яка відрізняється від першої на 1-5%
X2 = X * (1 + np.random.uniform(0.01, 0.05, size=X.shape))
# Об'єднуємо обидві ознаки в одну матрицю
X_combined = np.hstack([X, X2])
y = df['close'].values
# Перетворюємо дані на тензори PyTorch
X_tensor = torch.tensor(X_combined, dtype=torch.float32)
y_tensor = torch.tensor(y, dtype=torch.float32).view(-1, 1)
# Визначення розмірності вхідних даних
input_dim = X_tensor.shape[1]
# Визначення моделі
model = nn.Sequential(
nn.Linear(input_dim, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 1)
)
# Визначення функції втрат і оптимізатора
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Навчання моделі
epochs = 100
for epoch in range(epochs):
model.train()
optimizer.zero_grad()
outputs = model(X_tensor)
loss = criterion(outputs, y_tensor)
loss.backward()
optimizer.step()
# Прогнозування
model.eval()
with torch.no_grad():
next_time_index = np.array([[len(df)]])
next_time_index2 = next_time_index * (1 + np.random.uniform(0.01, 0.05, size=next_time_index.shape))
next_time_combined = np.hstack([next_time_index, next_time_index2])
next_time_tensor = torch.tensor(next_time_combined, dtype=torch.float32)
predicted_price = model(next_time_tensor).item()
# Флуктуація ціни
fluctuation_range = 0.001 * predicted_price
min_price = predicted_price - fluctuation_range
max_price = predicted_price + fluctuation_range
price_predict = random.uniform(min_price, max_price)
forecast_price[token] = price_predict
print(f"Predicted_price: {predicted_price}, Min_price: {min_price}, Max_price: {max_price}")
print(f"Forecasted price for {token}: {forecast_price[token]}")
def update_data():
tokens = ["ETH", "BTC", "SOL"]
for token in tokens:
download_data(token)
format_data(token)
train_model(token)
if __name__ == "__main__":
update_data()