-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharima_test.py
318 lines (275 loc) · 9.66 KB
/
arima_test.py
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import pandas as pd
import numpy as np
import requests
from statsmodels.tsa.arima.model import ARIMA
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# ---------------------------
# Anpassbare Parameter
# ---------------------------
# Liste der gleitenden Durchschnitte und Farben (für stündliche Daten)
ma_windows = [9, 20, 50, 100, 200, 400]
ma_colors = ['yellow', 'red', 'orange', 'green', 'purple', 'turquoise']
ma_labels = [f'MA {window}' for window in ma_windows]
# Schwellenwert für minimale Abstände zwischen den MAs bei der Bodenerkennung
distance_threshold = 0.005 # Kann angepasst werden
# ---------------------------
# Daten einlesen und vorbereiten
# ---------------------------
def get_hourly_data(symbol, interval, limit):
base_url = 'https://api.binance.com'
endpoint = '/api/v3/klines'
params = {
'symbol': symbol,
'interval': interval,
'limit': limit
}
response = requests.get(base_url + endpoint, params=params)
data = response.json()
df = pd.DataFrame(data, columns=[
'Open 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'
])
df['Datum'] = pd.to_datetime(df['Close Time'], unit='ms')
df['Preis'] = df['Close'].astype(float)
df = df[['Datum', 'Preis']]
return df
# Beispielaufruf
symbol = 'BTCUSDT'
interval = '1h' # 1-Stunden-Intervall
limit = 1000 # Anzahl der Datenpunkte (kann angepasst werden)
df = get_hourly_data(symbol, interval, limit)
# ---------------------------
# Berechnung der gleitenden Durchschnitte
# ---------------------------
for window, label in zip(ma_windows, ma_labels):
df[label] = df['Preis'].rolling(window=window).mean()
# Abstände zwischen den MAs berechnen
df['Dist_MA9_MA20'] = df['MA 9'] - df['MA 20']
df['Dist_MA20_MA50'] = df['MA 20'] - df['MA 50']
df['Dist_MA50_MA100'] = df['MA 50'] - df['MA 100']
df['Dist_MA100_MA200'] = df['MA 100'] - df['MA 200']
df['Dist_MA200_MA400'] = df['MA 200'] - df['MA 400']
# ---------------------------
# Funktionen für ARIMA-Modell
# ---------------------------
from statsmodels.tsa.stattools import adfuller
def check_stationarity(timeseries):
result = adfuller(timeseries)
return result[1] <= 0.05 # p-Wert <= 0.05 deutet auf Stationarität hin
def arima_forecast(timeseries, steps=5):
# Überprüfen auf Stationarität
if not check_stationarity(timeseries):
d = 1 # Differenzierung
else:
d = 0
# ARIMA-Modell anpassen
try:
model = ARIMA(timeseries, order=(1, d, 1))
model_fit = model.fit()
forecast = model_fit.forecast(steps=steps)
return forecast
except:
# Falls ein Fehler auftritt, leere Prognose zurückgeben
return pd.Series()
# ---------------------------
# Funktionen zur Mustererkennung
# ---------------------------
# Funktion zur Erkennung von Kauf- und Verkaufsignalen mit ARIMA-Bestätigung
def detect_buy_sell_signals(df):
signals = []
for i in range(max(ma_windows), len(df)):
# Original-Signale basierend auf MAs
if df['MA 20'].iloc[i] > df['MA 50'].iloc[i] and df['MA 20'].iloc[i - 1] <= df['MA 50'].iloc[i - 1]:
signal = 'Kauf'
elif df['MA 20'].iloc[i] < df['MA 50'].iloc[i] and df['MA 20'].iloc[i - 1] >= df['MA 50'].iloc[i - 1]:
signal = 'Verkauf'
else:
continue
# ARIMA-Prognose abrufen
recent_data = df['Preis'].iloc[:i]
forecast = arima_forecast(recent_data)
if forecast.empty:
continue # Wenn keine Prognose verfügbar ist, überspringen
arima_trend = 'Aufwärts' if forecast.iloc[-1] > df['Preis'].iloc[i] else 'Abwärts'
# Signal bestätigen
if (signal == 'Kauf' and arima_trend == 'Aufwärts') or (signal == 'Verkauf' and arima_trend == 'Abwärts'):
signals.append((df['Datum'].iloc[i], signal))
else:
# Signal ignorieren oder als schwach markieren
pass
return signals
# ---------------------------
# Investitionsbetrag erfassen
# ---------------------------
# Eingabe des Investitionsbetrags
investitionsbetrag = float(input("Bitte geben Sie den Investitionsbetrag in USD ein: "))
# ---------------------------
# Mustererkennung durchführen
# ---------------------------
signals = detect_buy_sell_signals(df)
# ---------------------------
# Gewinnberechnung
# ---------------------------
def calculate_profit(df, signals, investitionsbetrag):
balance = investitionsbetrag
btc_holding = 0
last_action = None
trade_history = []
for date, signal in signals:
preis = df.loc[df['Datum'] == date, 'Preis'].values[0]
if signal == 'Kauf' and last_action != 'Kauf':
# Kaufen
btc_holding = balance / preis
balance = 0
last_action = 'Kauf'
trade_history.append({'Datum': date, 'Aktion': 'Kauf', 'Preis': preis, 'BTC': btc_holding, 'Balance': balance})
elif signal == 'Verkauf' and last_action == 'Kauf':
# Verkaufen
balance = btc_holding * preis
btc_holding = 0
last_action = 'Verkauf'
trade_history.append({'Datum': date, 'Aktion': 'Verkauf', 'Preis': preis, 'BTC': btc_holding, 'Balance': balance})
# Am Ende alles verkaufen, falls noch BTC gehalten werden
if btc_holding > 0:
preis = df['Preis'].iloc[-1]
balance = btc_holding * preis
trade_history.append({'Datum': df['Datum'].iloc[-1], 'Aktion': 'Verkauf (Ende)', 'Preis': preis, 'BTC': 0, 'Balance': balance})
btc_holding = 0
gesamtgewinn = balance - investitionsbetrag
return gesamtgewinn, trade_history
gesamtgewinn, trade_history = calculate_profit(df, signals, investitionsbetrag)
print(f"\nGesamtgewinn: {gesamtgewinn:.2f} USD")
# ---------------------------
# Visualisierung
# ---------------------------
# Subplots erstellen: 2 Reihen (Hauptdiagramm und Flächendiagramm)
fig = make_subplots(
rows=2, cols=1,
shared_xaxes=True,
vertical_spacing=0.05,
subplot_titles=(
'BTC Kurs mit gleitenden Durchschnitten und Signalen',
'Flächendiagramm der Abstände zwischen den MAs'
)
)
# Den tatsächlichen BTC-Kurs als weiße Linie hinzufügen (erstes Diagramm)
fig.add_trace(
go.Scatter(
x=df['Datum'],
y=df['Preis'],
mode='lines',
name='BTC Kurs',
line=dict(color='white', width=2),
hoverinfo='x+y',
),
row=1, col=1
)
# Gesamt-MAs hinzufügen (sichtbar)
for label, color in zip(ma_labels, ma_colors):
fig.add_trace(
go.Scatter(
x=df['Datum'],
y=df[label],
mode='lines',
name=label,
line=dict(color=color, width=2),
hoverinfo='x+y',
visible=True,
),
row=1, col=1
)
# Kauf- und Verkaufsignale markieren
for date, signal in signals:
preis = df.loc[df['Datum'] == date, 'Preis'].values[0]
color = 'green' if signal == 'Kauf' else 'red'
symbol = 'arrow-up' if signal == 'Kauf' else 'arrow-down'
fig.add_trace(
go.Scatter(
x=[date],
y=[preis],
mode='markers',
marker=dict(symbol=symbol, color=color, size=12),
name=signal,
showlegend=False,
hovertemplate=f'{signal}: {date.strftime("%d.%m.%Y %H:%M")}'
),
row=1, col=1
)
# ARIMA-Prognosen hinzufügen
forecast_steps = 5 # Anzahl der Stunden in die Zukunft
forecast_dates = pd.date_range(df['Datum'].iloc[-1], periods=forecast_steps+1, freq='H')[1:]
forecast_values = arima_forecast(df['Preis'], steps=forecast_steps)
if not forecast_values.empty:
fig.add_trace(
go.Scatter(
x=forecast_dates,
y=forecast_values,
mode='lines',
name='ARIMA Prognose',
line=dict(color='cyan', dash='dash'),
),
row=1, col=1
)
# Gestapeltes Flächendiagramm der Abstände zwischen den MAs hinzufügen (zweites Diagramm)
abstand_traces = [
('Dist_MA9_MA20', 'yellow', 'Abstand MA 9-20'),
('Dist_MA20_MA50', 'red', 'Abstand MA 20-50'),
('Dist_MA50_MA100', 'orange', 'Abstand MA 50-100'),
('Dist_MA100_MA200', 'green', 'Abstand MA 100-200'),
('Dist_MA200_MA400', 'purple', 'Abstand MA 200-400')
]
for dist_label, color, name in abstand_traces:
fig.add_trace(
go.Scatter(
x=df['Datum'],
y=df[dist_label],
mode='lines',
name=name,
line=dict(color=color, width=0),
fill='tozeroy',
stackgroup='one',
hoverinfo='x+y',
visible='legendonly', # Standardmäßig ausgeblendet
),
row=2, col=1
)
# Layout anpassen
fig.update_layout(
title='BTC Kurs und Abstände zwischen den gleitenden Durchschnitten (MAs)',
xaxis=dict(
rangeslider=dict(visible=True),
type='date'
),
yaxis=dict(
title='Preis in USD'
),
yaxis2=dict(
title='Abstand'
),
hovermode='x unified',
legend=dict(
title='Legende',
orientation='v',
x=1.02,
y=1,
bordercolor='white',
borderwidth=1,
),
template='plotly_dark',
autosize=True,
)
# Gewinnanzeige hinzufügen
fig.add_annotation(
xref='paper', yref='paper',
x=0.5, y=-0.2,
text=f"Investitionsbetrag: {investitionsbetrag:.2f} USD<br>Gesamtgewinn: {gesamtgewinn:.2f} USD",
showarrow=False,
font=dict(size=14, color='white'),
align='center'
)
# Responsives Design aktivieren
config = {'responsive': True}
# Diagramm anzeigen
fig.show(config=config)