-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
288 lines (233 loc) · 10.1 KB
/
main.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_ta as ta
from pandas_ta.volatility import atr
from python_bitvavo_api.bitvavo import Bitvavo
from datetime import datetime, timezone
"""
Kézzel írt backtesting fájl, borzasztó tökölős volt, de irónikus módon ez mégis jobban
működik mint bármelyik könyvtár amit eddig backtestingre használtam.
Faék egyszerű, sok helyet foglal, cserébe megbízható, nincs benne mágia.
Arra tökéletes, hogy elképzeléseket próbálgasson az ember, vagy lássa, hogy valami
alapból is hülyeség lenne. Csak kiszámolod az indikátort, átírod a
buy/sell_condition metódusokat, és már működik is.
Hátrány: Még mindig nem tudunk könyvtár nélkül optimalizálni
!!! FONTOS: Ez végső soron egy crypto trading bot lesz, és a Bitvavo nem enged
shortolni, szóval minden stratégiánál vegyük figyelembe, hogy csakis longgal
számolhatunk! Alapértelmezett tranzakciós díj 0.25%, de befektetett pénzzel csökken.
"""
def download_data():
bitvavo = Bitvavo()
start = datetime(year=2018, month=6, day=30, hour=0, minute=0, tzinfo=timezone.utc)
end = datetime(year=2023, month=6, day=30, hour=0, minute=0, tzinfo=timezone.utc)
candles = bitvavo.candles('PEPE-EUR', '30m')#, start=start, end=end)
data=pd.DataFrame(candles, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
data.set_index(data['timestamp'], inplace=True)
del data['timestamp']
data.to_csv("Bitcoin_prices.csv", index=True)
def load_df_from_csv(path:str):
data = pd.read_csv(path)
data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms')
data.set_index(data['timestamp'], inplace=True)
del data['timestamp']
data['open'] = pd.to_numeric(data['open'])
data['high'] = pd.to_numeric(data['high'])
data['low'] = pd.to_numeric(data['low'])
data['volume'] = pd.to_numeric(data['volume'])
data['close'] = pd.to_numeric(data['close'])
return data
def WMA(series, period):
weights = np.arange(1, period + 1)
return series.rolling(period).apply(lambda prices: np.dot(prices, weights) / weights.sum(), raw=True)
def HMA(series, period):
half_length = period // 2
sqrt_length = int(np.sqrt(period))
wma_half_length = WMA(series, half_length)
wma_full_length = WMA(series, period)
hull_ma = WMA(2 * wma_half_length - wma_full_length, sqrt_length)
return hull_ma
def MA_supertrend(high, low, close, length=10, multiplier=0.5, ma_length=150, offset=None, **kwargs):
"""Indicator: Supertrended moving average (rewritten from the supertrend function of pandas_ta"""
# Validate Arguments
length = int(length) if length and length > 0 else 7
multiplier = float(multiplier) if multiplier and multiplier > 0 else 3.0
high = ta.utils.verify_series(high, length)
low = ta.utils.verify_series(low, length)
close = ta.utils.verify_series(close, length)
offset = ta.utils.get_offset(offset)
if high is None or low is None or close is None: return
# Calculate Results
m = close.size
dir_, trend = [1] * m, [0] * m
long, short = [np.nan] * m, [np.nan] * m
hl2_ = HMA(close,ma_length)
matr = multiplier * atr(high, low, close, length)
upperband = hl2_ + matr
lowerband = hl2_ - matr
for i in range(1, m):
if close.iloc[i] > upperband.iloc[i - 1]:
dir_[i] = 1
elif close.iloc[i] < lowerband.iloc[i - 1]:
dir_[i] = -1
else:
dir_[i] = dir_[i - 1]
if dir_[i] > 0 and lowerband.iloc[i] < lowerband.iloc[i - 1]:
lowerband.iloc[i] = lowerband.iloc[i - 1]
if dir_[i] < 0 and upperband.iloc[i] > upperband.iloc[i - 1]:
upperband.iloc[i] = upperband.iloc[i - 1]
if dir_[i] > 0:
trend[i] = long[i] = lowerband.iloc[i]
else:
trend[i] = short[i] = upperband.iloc[i]
df = pd.DataFrame({f'MA_ST_{ma_length}': trend}, index=close.index)
return df
#%% Download data
download_data()
downloaded_data=load_df_from_csv("Bitcoin_prices.csv")
data=downloaded_data.copy().iloc[::-1] #Reverse rows because bitvavo gives data from newest to oldest by default, we need the opposite
#Iterate through data to see if everything is OK
#column_names = '\t\t'.join(data.columns)
#print("\tTimestamp\t\t\t\t" +column_names)
#for index, row in data.iterrows():
# Concatenate the index with the row values and convert to a string
# row_values = [str(index)] + [str(value) for value in row.values]
# Join the row values into a single string separated by a comma
# row_string = ',\t'.join(row_values)
# Print the row string
# print(row_string)
#%% Indicators
import yfinance as yf
currency="PEPE24478-USD"
data_copy = yf.download(currency, start="2024-05-22", end="2024-07-19", interval='30m')
#%%
#data=data_copy
data=data.drop(['volume'], axis=1)
data.columns = ['open', 'high','low','close']
heikin_ashi=ta.candles.ha(data.open,data.high,data.low,data.close)
heikin_ashi.columns=['Smooth_HA_Open', 'Smooth_HA_High', 'Smooth_HA_Low', 'Smooth_HA_Close']
def smooth_heikin_ashi(ha, window=10):
ha.columns = ['HA_open','HA_high','HA_low','HA_close']
smooth_ha = pd.DataFrame(index=ha.index, columns=['Smooth_HA_Open', 'Smooth_HA_High', 'Smooth_HA_Low', 'Smooth_HA_Close'])
smooth_ha['Smooth_HA_Open'] = ha['HA_open'].rolling(window=window).mean()
smooth_ha['Smooth_HA_High'] = ha['HA_high'].rolling(window=window).mean()
smooth_ha['Smooth_HA_Low'] = ha['HA_low'].rolling(window=window).mean()
smooth_ha['Smooth_HA_Close'] = ha['HA_close'].rolling(window=window).mean()
return smooth_ha
#smooth_ha=smooth_heikin_ashi(heikin_ashi)
#double_smoothed_ha=smooth_heikin_ashi(smooth_ha)
#data=pd.concat([data, double_smoothed_ha], axis=1)
rsi_length=5
data[f'rsi_{rsi_length}'] = ta.momentum.rsi(data.close, length=rsi_length)
#data.dropna(inplace=True)
length=20
multiplier=4.0
#ma_length=150
data=pd.concat([data, ta.overlap.supertrend(heikin_ashi['Smooth_HA_High'], heikin_ashi['Smooth_HA_Low'], heikin_ashi['Smooth_HA_Close'], length, multiplier)[f'SUPERT_{length}_{multiplier}']], axis=1)
#data=pd.concat([data,MA_supertrend(data['high'], data['low'], data['close'], length=length, multiplier=multiplier, ma_length=ma_length)], axis=1)
#data=data.drop(['open', 'high', 'low', 'Smooth_HA_High', 'Smooth_HA_Low' ], axis=1)
#data[f'ATR_{length}']=atr(data['high'], data['low'], data['close'], length)
data=data.drop(['open', 'high', 'low'], axis=1)
#data.columns = ['close', 'Smooth_HA_Open', 'Smooth_HA_Close',f'rsi_{rsi_length}','trend','direction','long','short']
data.columns = ['close', f'rsi_{rsi_length}', 'trend']
prev_row = data.iloc[length]
data=data.iloc[length+1:]
#%% Backtesting loop
################################ Backtest ################################
starting_eur = 1000
staring_coin = 0
trading_fees = 0.0025
eur = starting_eur
coin = staring_coin
trades = []
wallet = []
buyhold = [] #Our wallet if we just spent all our money on coins in the beginning and did no trades
is_first_trade = True
def buy_condition(row):
return row['trend'] < row['close'] and (prev_row['trend'] > prev_row['close'] or is_first_trade) #and row[f'rsi_{rsi_length}'] > 30 #Supertrend strategy
#return row['Smooth_HA_Close'] > row['Smooth_HA_Open'] #and row[f'rsi_{rsi_length}'] >=60 #Double-smoothed Heikin-Ashi
#return row[f'rsi_{rsi_length}'] <= 19
def sell_condition(row):
return row['trend'] > row['close'] and prev_row['trend'] < prev_row['close'] #Supertrend
#return row['Smooth_HA_Close'] < row['Smooth_HA_Open'] #Double-smoothed Heikin-Ashi
#return row[f'rsi_{rsi_length}'] >= 81
#Backtest loop
for index, row in data.iterrows():
value = row['close']
if buy_condition(row) and eur > 0:
coin = eur / value * (1-trading_fees)
eur = 0
is_first_trade = False
#take_profit = (row[f'ATR_{length}']/100*3+1)*value
#stop_loss = (1-row[f'ATR_{length}']/100*2)*value
trades.append({'Date': index, 'Action':'buy', 'Price':value, 'Coin':coin, 'Eur':eur, 'Wallet':coin*value, f'rsi_{rsi_length}': row[f'rsi_{rsi_length}']})
print(f"Bought BTC at {value} EUR on the {index}")
elif trades and trades[-1]['Action'] == 'buy' and sell_condition(row) and coin > 0:
eur = coin * value * (1-trading_fees)
coin = 0
trades.append({'Date': index, 'Action':'sell', 'Price':value, 'Coin':coin, 'Eur':eur, 'Wallet':coin*value, f'rsi_{rsi_length}': row[f'rsi_{rsi_length}']})
print(f"Sold BTC at {value} EUR on the {index}")
if eur == 0:
wallet.append(coin*value)
else:
wallet.append(eur)
prev_row = row
buyhold.append(starting_eur / data['close'].iloc[0] * value)
trades = pd.DataFrame(trades, columns=['Date', 'Action', 'Price', 'Coin', 'Eur', 'Wallet', f'rsi_{rsi_length}', 'take_profit', 'stop_loss']).round(2) #convert to df
#print(trades['Action'].value_counts())
#%% Results
print(f"\nStarting amount: {starting_eur} EUR")
print(f"Buy-hold: \t\t\t\t {buyhold[-1]} EUR\t ({round((buyhold[-1]/starting_eur -1)*100,2)})% profit)")
print(f"{length}-{multiplier} supertrend: \t\t {wallet[-1]} EUR\t ({round((wallet[-1]/starting_eur -1)*100,2)}% profit)")
plt.figure(figsize=(10,6))
plt.plot(
data.index,
wallet,
label=f"{length}-{multiplier} Supertrend",
color="gold"
)
plt.plot(
data.index,
buyhold,
label="Buy-hold",
color="purple"
)
for index, row in trades.iterrows():
if row['Action']=="buy":
plt.scatter(
row['Date'],
row['Wallet'] * 1.05,
color="green")
else:
plt.scatter(
row['Date'],
row['Eur'] * 0.95,
color="red")
plt.legend(fontsize=18, loc="upper left")
plt.ylabel("Value [EUR]", fontsize=20)
plt.yticks(fontsize=14)
plt.xlabel("Date", fontsize=20)
plt.xticks(fontsize=14)
plt.title(f"{currency}")
plt.tight_layout()
#%%
plt.figure(figsize=(10,6))
plt.plot(
data.index,
data['trend'],
label=f"{length}-{multiplier} Supertrend",
color="gold"
)
plt.plot(
data.index,
data['close'],
label="Price",
color="purple"
)
plt.legend(fontsize=18, loc="upper left")
plt.ylabel("Value [EUR]", fontsize=20)
plt.yticks(fontsize=14)
plt.xlabel("Date", fontsize=20)
plt.xticks(fontsize=14)
plt.title(f"{currency}")
plt.tight_layout()