-
Notifications
You must be signed in to change notification settings - Fork 15
/
cryptoAutoTrade_v2.py
77 lines (63 loc) · 2.79 KB
/
cryptoAutoTrade_v2.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
import pyupbit
import datetime
import time, calendar
import auth # save 'access key' and 'secret key'
import numpy as np
def get_targetPrice(df, K) :
range = df['high'][-2] - df['low'][-2]
return df['open'][-1] + range * K
def buy_all(coin) :
balance = upbit.get_balance("KRW") * 0.9995
if balance >= 5000 :
print(upbit.buy_market_order(coin, balance))
def sell_all(coin) :
balance = upbit.get_balance(coin)
price = pyupbit.get_current_price(coin)
if price * balance >= 5000 :
print(upbit.sell_market_order(coin, balance))
def get_crr(df, fees, K) :
df['range'] = df['high'].shift(1) - df['low'].shift(1)
df['targetPrice'] = df['open'] + df['range'] * K
df['drr'] = np.where(df['high'] > df['targetPrice'], (df['close'] / (1 + fees)) / (df['targetPrice'] * (1 + fees)) , 1)
return df['drr'].cumprod()[-2]
def get_best_K(coin, fees) :
df = pyupbit.get_ohlcv(coin, interval = "day", count = 21)
max_crr = 0
best_K = 0.5
for k in np.arange(0.0, 1.0, 0.1) :
crr = get_crr(df, fees, k)
if crr > max_crr :
max_crr = crr
best_K = k
return best_K
if __name__ == '__main__':
try:
upbit = pyupbit.Upbit(auth.access, auth.secret)
# set variables
coin = "KRW-BTC"
fees = 0.0005
K = 0.5
start_balance = upbit.get_balance("KRW")
df = pyupbit.get_ohlcv(coin, count = 2, interval = "day")
targetPrice = get_targetPrice(df, get_best_K(coin, fees))
print(datetime.datetime.now().strftime('%y/%m/%d %H:%M:%S'), "\t\tBalance :", start_balance, "KRW \t\tYield :", ((start_balance / start_balance) - 1) * 100, "% \t\tNew targetPrice :", targetPrice, "KRW")
while True :
now = datetime.datetime.now()
if now.hour == 9 and now.minute == 0 and now.second == 0: # when am 09:02:00
sell_all(coin)
df = pyupbit.get_ohlcv(coin, count = 2, interval = "day")
targetPrice = get_targetPrice(df, get_best_K(coin, fees))
time.sleep(1)
cur_balance = upbit.get_balance("KRW")
print(now.strftime('%y/%m/%d %H:%M:%S'), "\t\tBalance :", cur_balance, "KRW \t\tYield :", ((cur_balance / start_balance) - 1) * 100, "% \t\tNew targetPrice :", targetPrice, "KRW")
elif targetPrice <= pyupbit.get_current_price(coin) :
buy_all(coin)
start_time = df.index[-1]
end_time = start_time + datetime.timedelta(days=1)
if end_time > now :
print((end_time - now).seconds)
time.sleep((end_time - now).seconds - 60)
time.sleep(1)
except Exception as e:
print(e)
time.sleep(1)