-
Notifications
You must be signed in to change notification settings - Fork 1
/
trading.py
442 lines (380 loc) · 12.9 KB
/
trading.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
from re import I
from ta.trend import SMAIndicator, WMAIndicator, EMAIndicator, MACD
from ta.momentum import RSIIndicator, StochRSIIndicator
from ta.volatility import BollingerBands
from ta.volume import VolumeWeightedAveragePrice
import pyupbit
import time
import datetime
import requests
import sys
import pandas as pd
import numpy as np
def get_sma(close, period):
df_sma = SMAIndicator(close, window=period).sma_indicator()
return df_sma
def add_sma(df, close, *sma):
for i in sma:
df[f"sma{i}"] = get_sma(close, i)
return df
def get_wma(close, period):
df_wma = WMAIndicator(close, window=period).wma()
return df_wma
def add_wma(df, close, *wma):
for i in wma:
df[f"wma{i}"] = get_wma(close, i)
return df
def get_ema(close, period):
df_ema = EMAIndicator(close, window=period).ema_indicator()
return df_ema
def add_ema(df, close, *ema):
for i in ema:
df[f"ema{i}"] = get_ema(close, i)
return df
def get_macd(close, period_slow, period_fast, period_sign):
macd = MACD(close, window_slow=period_slow, window_fast=period_fast, window_sign=period_sign)
df_macd = macd.macd()
df_macd_s = macd.macd_signal()
df_macd_d = macd.macd_diff()
return df_macd, df_macd_s, df_macd_d
def add_macd(df, close, period_slow, period_fast, period_sign):
macd = get_macd(close, period_slow, period_fast, period_sign)
df['macd'] = macd[0]
df['macd_s'] = macd[1]
df['macd_d'] = macd[2]
return df
def get_rsi(close, period):
df_rsi = RSIIndicator(close, window=period).rsi()
return df_rsi
def add_rsi(df, close, *rsi):
for i in rsi:
df[f"rsi{i}"] = get_rsi(close, i)
return df
def get_stochrsi(close, period, period_smooth1, period_smooth2):
stochrsi = StochRSIIndicator(close, window=period, smooth1=period_smooth1, smooth2=period_smooth2)
df_srsi = stochrsi.stochrsi()
df_srsik = stochrsi.stochrsi_k()
df_srsid = stochrsi.stochrsi_d()
return df_srsi, df_srsik, df_srsid
def add_stochrsi(df, close, period, period_smooth1, period_smooth2):
stochrsi = get_stochrsi(close, period, period_smooth1, period_smooth2)
df["srsi"] = stochrsi[0]
df["srsik"] = stochrsi[1]
df["srsid"] = stochrsi[2]
return df
def get_bb(close, period, period_dev):
bb = BollingerBands(close, window=period, window_dev=period_dev)
df_bh = bb.bollinger_hband()
df_bhi = bb.bollinger_hband_indicator()
df_bl = bb.bollinger_lband()
df_bli = bb.bollinger_lband_indicator()
df_bm = bb.bollinger_mavg()
df_bw = bb.bollinger_wband()
return df_bh, df_bhi, df_bl, df_bli, df_bm, df_bw
def add_bb(df, close, period, period_dev):
bb = get_bb(close, period, period_dev)
df["bh"] = bb[0]
df["bhi"] = bb[1]
df["bl"] = bb[2]
df["bli"] = bb[3]
df["bm"] = bb[4]
df["bw"] = bb[5]
return df
def get_vwap(high, low, close, volume, period):
vwap = VolumeWeightedAveragePrice(high=high, low=low, close=close, volume=volume, window=period)
df_vwap = vwap.volume_weighted_average_price()
return df_vwap
def add_vwap(df, high, low, close, volume, period):
df["vwap"] = get_vwap(high, low, close, volume, period)
return df
def get_buy_amount(buy_amt_unit, buy_cnt_limit, increase_rate):
buy_amt = 0
buy_amt_list = [0.0]
for i in range(buy_cnt_limit):
amt = buy_amt_unit + buy_amt * increase_rate
buy_amt = round(buy_amt + amt, 4)
buy_amt_list.append(buy_amt)
return buy_amt_list
def get_max_loss(close, buy_amt_unit, buy_cnt_limit, increase_rate, max_loss_rate):
buy_amt = 0
buy_price = 0
for i in range(buy_cnt_limit):
amt = buy_amt_unit + buy_amt * increase_rate
buy_price = round(buy_price + close * amt, 4)
buy_amt = round(buy_amt + amt, 4)
return round(buy_price * max_loss_rate, 4)
def adj_revenue(revenue, close, buy_amt_unit, buy_cnt_limit, increase_rate):
open_amt_list = get_buy_amount(buy_amt_unit, buy_cnt_limit, increase_rate)
max_amt = open_amt_list[len(open_amt_list)-1]
adj_revenue = (30 * revenue) / max_amt
return adj_revenue
def run_test(df, config):
revenue_rate = config["revenue_rate"]
max_loss_rate = config["max_loss_rate"]
increase_rate = config["increase_rate"]
buy_cnt_limit = int(config["buy_cnt_limit"])
buy_amt_unit = config["buy_amt_unit"]
trade_fee = 0.001
close = 880
buy_amt_list = get_buy_amount(buy_amt_unit, buy_cnt_limit, increase_rate)
max_loss = get_max_loss(close, buy_amt_unit, buy_cnt_limit, increase_rate, max_loss_rate)
buy_cnt = 0
buy_price = 0
buy_amt = 0
revenue = 0
revenue_t = 0
buy_cnt_tot = 0
df = df.iloc[df.shape[0]-144000:df.shape[0]-124000]
for i in range(df.shape[0]-1):
close1 = round(df.iloc[i:i+1]['c'].values[0], 4)
close2 = round(df.iloc[i+1:i+2]['c'].values[0], 4)
wma7 = round(df.iloc[i:i+1]["wma7"].values[0], 4)
wma99 = round(df.iloc[i:i+1]["wma99"].values[0], 4)
vwap = round(df.iloc[i:i+1]["vwap"].values[0], 4)
loss = buy_price - close2 * buy_amt
if loss > max_loss:
revenue_t = close2 * buy_amt - buy_price - buy_price * trade_fee
revenue = round(revenue + revenue_t, 4)
buy_cnt = 0
buy_amt = 0
buy_price = 0
continue
tp_revenue = close2 * buy_amt - (buy_price + buy_price * revenue_rate)
if buy_cnt > 0 and tp_revenue > 0:
revenue_t = close2 * buy_amt - buy_price - buy_price * trade_fee
revenue = round(revenue + revenue_t, 4)
buy_cnt = 0
buy_amt = 0
buy_price = 0
continue
if buy_cnt < buy_cnt_limit and close2 < vwap and close2 < wma7 and wma7 > wma99:
t_amt = buy_amt_unit + buy_amt * increase_rate
buy_price = round(buy_price + close2 * t_amt, 4)
buy_amt = round(buy_amt + t_amt, 4)
buy_cnt += 1
buy_cnt_tot += 1
return adj_revenue(revenue, close, buy_amt_unit, buy_cnt_limit, increase_rate)
def get_current_price(coin):
message = ""
result = "none"
try:
result = pyupbit.get_current_price(coin)
except:
message = f"{sys.exc_info()}"
try:
message = result["error"]["message"]
except:
if message == "":
message = "good"
return message, result
def get_balance(api, coin):
message = ""
result = "none"
trade_coin = "none"
buy_amt = 0
buy_price = 0
try:
trade_coin = coin.split('-')[1]
result = api.get_balances()
except:
message = f"{sys.exc_info()}"
try:
message = result[0]["error"]["message"]
except:
if message == "":
message = "good"
if message == "good":
for i in result:
if i["currency"] == trade_coin:
buy_amt = i["balance"]
buy_price = i["avg_buy_price"]
return message, buy_amt, buy_price
def get_order_status(api, uuid):
message = ""
state = "none"
price = "none"
amt = "none"
result = {"state":"none", "side":"none", "price":"0", "volume":"0"}
try:
result = api.get_order(uuid)
except:
message = f"{sys.exc_info()}"
try:
message = result["error"]["message"]
except:
if message == "":
message = "good"
state = result["state"]
price = result["price"]
amt = result["volume"]
return message, state, price, amt
def buy_limit_order(api, coin, price, amt):
message = ""
uuid = "none"
result = {"uuid":""}
try:
result = api.buy_limit_order(coin, price, amt)
except:
message = f"{sys.exc_info()}"
try:
message = result["error"]["message"]
except:
if message == "":
message = "good"
uuid = result["uuid"]
return message, uuid
def sell_limit_order(api, coin, price, amt):
message = ""
uuid = "none"
result = {"uuid":""}
try:
result = api.buy_limit_order(coin, price, amt)
except:
message = f"{sys.esc_info()}"
# try:
# message = result["error"]["message"]
# except:
if message == "":
message = "good"
uuid = result["uuid"]
return message, uuid
def buy_market_order(api, coin, price):
message = ""
uuid = "none"
result = {"uuid":""}
try:
result = api.buy_market_order(coin, price)
except:
message = f"{sys.exc_info()}"
try:
message = result["error"]["message"]
except:
if message == "":
message = "good"
uuid = result["uuid"]
return message, uuid
def sell_market_order(api, coin, amt):
message = ""
uuid = "none"
result = {"uuid": ""}
try:
result = api.sell_market_order(coin, amt)
except:
message = f"{sys.exc_info()}"
try:
message = result["error"]["message"]
except:
if message == "":
message = "good"
uuid = result["uuid"]
return message, uuid
def cancel_order(api, uuid):
message = ""
result = {"uuid":""}
try:
result = api.cancel_order(uuid)
except:
message = f"{sys.exc_info()}"
try:
message = result["error"]["message"]
except:
if message == "":
message = "good"
uuid = result["uuid"]
return message, uuid
def cancel_all_order(api, coin):
message = ""
result = {"uuid":""}
result_list = list()
try:
result_list = api.get_order(coin, state="wait")
except:
message = f"{sys.exc_info()}"
try:
message = result_list["error"]["message"]
except:
message = "good"
if message == "good":
for i in result_list:
try:
i = api.cancel_order(result["uuid"])
except:
message = f"{sys.exc_info()}"
break
return message
def take_profit(api, coin, buy_amt, buy_price, now_price, take_profit_rate):
buy_price_tot = float(buy_amt) * float(buy_price)
now_price_tot = float(buy_amt) * float(now_price)
revenue_price = buy_price_tot * float(take_profit_rate)
message = "not yet"
uuid = "none"
if now_price_tot - buy_price_tot > revenue_price:
try:
trade_price = "{:0.0{}f}".format(float(now_price), 0)
trade_amt = "{:0.0{}f}".format(float(buy_amt), 4)
message, uuid = api.sell_limit_order(coin, trade_price, trade_amt)
except:
message = f"{sys.exc_info()}"
return message, uuid
def stop_loss(api, coin, buy_amt, buy_price, now_price, stop_loss_rate):
buy_price_tot = float(buy_amt) * float(buy_price)
now_price_tot = float(buy_amt) * float(now_price)
stop_price = buy_price_tot * float(stop_loss_rate)
message = "not yet"
uuid = "none"
if buy_price_tot - now_price_tot > stop_price:
try:
trade_price = "{:0.0{}f}".format(float(now_price), 0)
trade_amt = "{:0.0{}f}".format(float(buy_amt), 4)
message, uuid = api.sell_limit_order(coin, trade_price, trade_amt)
except:
message= f"{sys.exc_info()}"
return message, uuid
def get_web_m_data(url):
df = pd.DataFrame()
cols = {"timestamp", "openingPrice", "highPrice", "lowPrice", "tradePrice", "candleAccTradeVolume"}
columns = {"timestamp":'t', "openingPrice":'o', "highPrice":'h', "lowPrice":'l', "tradePrice":'c', "candleAccTradeVolume":'v'}
for i in range(3):
try:
res = requests.get(url, timeout=3)
df_t = pd.read_json(res.content)
df_t = df_t[cols].sort_values(by="timestamp", ascending=True)
df = df_t.rename(columns=columns)
break
except:
print("error")
time.sleep(1)
return df
def log(message):
f = open("./logs/logs.txt", 'a')
f.write(f"{message}\n")
print(message)
f.close
def get_time(time, arg):
time = float(time)
KST = datetime.timezone(datetime.timedelta(hours=9))
dt = datetime.datetime.fromtimestamp(time, tz=KST)
return str(dt.strftime(f"%{arg}"))
def check_open_cnt(check_data, amt_list):
i = 1
for j in amt_list:
if round(float(check_data), 0) == round(float(j), 0):
return i
i += 1
return i
def get_buy_amt_list(buy_amt_unit, buy_cnt_limit, increase_rate):
buy_amt = 0
buy_amt_list = [0.0]
for i in range(buy_cnt_limit):
amt = buy_amt_unit + buy_amt * increase_rate
buy_amt = round(buy_amt + amt, 4)
buy_amt_list.append(buy_amt)
return buy_amt_list
def get_max_loss(close, buy_amt_unit, buy_cnt_limit, increase_rate, max_loss_rate):
buy_amt = 0
buy_price = 0
for i in range(buy_cnt_limit):
amt = buy_amt_unit + buy_amt * increase_rate
buy_price = round(buy_price + close * amt, 4)
buy_amt = round(buy_amt + amt, 4)
return round(buy_price * max_loss_rate, 4)