-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
180 lines (146 loc) · 6.59 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
import upbitopenapi.config as conf
from trading import *
coin = "KRW-XRP"
#df = pd.read_csv(f"./data/{coin}-index.csv")
access = conf.G_API_KEY
secret = conf.G_SECRET_KEY
api = pyupbit.Upbit(access, secret)
url = f"https://crix-api-cdn.upbit.com/v1/crix/candles/minutes/1?code=CRIX.UPBIT.{coin}&count=200"
# revenue_rate = 0.004795200288603781
# max_loss_rate = 0.2965237753456481
# increase_rate = 0.2000292756278028
# buy_cnt_limit = 1
# buy_amt_unit = 1
revenue_rate = 0.002
max_loss_rate = 0.003
increase_rate = 0.0
buy_cnt_limit = 2
buy_amt_unit = 7
buy_amt_list = []
buy_amt_list = get_buy_amount(buy_amt_unit, buy_cnt_limit, increase_rate)
max_loss = 0
trade_flag = 1
sleep_time = 0.2
order_uuid = ""
profit_uuid = ""
stop_uuid = ""
log("start")
message = cancel_all_order(api, coin)
if message != "good":
log(f"[{time.time()}]cancel_all_order error message: {message}")
while True:
check_sec = get_time(time.time(), 'S')
if check_sec in ("01", "02"):
df = get_web_m_data(url)
time.sleep(sleep_time)
df = add_wma(df, df['c'], 7, 99)
df = add_vwap(df, df['h'], df['l'], df['c'], df['v'], 14)
df_last = df.iloc[df.shape[0]-1:]
wma7 = df_last["wma7"].values[0]
wma99 = df_last["wma99"].values[0]
vwap = df_last["vwap"].values[0]
trade_flag = 0
if trade_flag == 0 and check_sec in ("59", "00"):
trade_flag = 1
if order_uuid != "":
message, status, price, amt = get_order_status(api, order_uuid)
if message != "good":
log(f"[{time.time()}] get_order_status error message: {message}, uuid: {order_uuid}")
continue
if status == "done":
order_uuid = ""
if status == "wait":
message, result = cancel_order(api, order_uuid)
if message != "good":
log(f"[{time.time()}] cancel_order error message: {message}, uuid: {order_uuid}")
continue
time.sleep(sleep_time)
message, buy_amt, buy_price = get_balance(api, coin)
if message != "good":
log(f"[{time.time()}] get_balance error message: {message}")
continue
time.sleep(sleep_time)
message, result = get_current_price(coin)
if message != "good":
log(f"[{time.time()}] get_current_price error message: {message}")
continue
close = float(result)
buy_cnt = check_open_cnt(buy_amt, buy_amt_list)
log(f"[{time.time()}] close: {round(close, 4)}, vwap: {round(vwap, 4)}, wma7: {round(wma7, 4)}, wma99: {round(wma99, 4)}, buy_cnt: {buy_cnt - 1}, buy_cnt_limit: {buy_cnt_limit}")
time.sleep(sleep_time)
if buy_cnt <= buy_cnt_limit and close < vwap and close < wma7 and wma7 > wma99:
order_buy_amt = buy_amt_unit + float(buy_amt) * increase_rate
message, result = get_current_price(coin)
if message != "good":
log(f"[{time.time()}] get_current_price error message: {message}")
continue
trade_price = "{:0.0{}f}".format(float(result), 0)
trade_amt = "{:0.0{}f}".format(order_buy_amt, 4)
print(trade_price)
print(trade_amt)
message, order_uuid = buy_limit_order(api, coin, trade_price, trade_amt)
if message != "good":
log(f"[{time.time()}] buy_limit_order error {message}")
order_uuid = ""
continue
trade_flag = 1
log(f"[{time.time()}] buy coin uuid: {order_uuid}, v: {round(vwap, 4)}, c: {close}, p: {trade_price}, a: {trade_amt}, m: {message}")
time.sleep(sleep_time)
if check_sec in ("10", "20", "30", "40", "50"):
if profit_uuid != "":
message, status, price, amt = get_order_status(api, coin, profit_uuid)
if message != "good":
log(f"[{time.time()}] get_order_status error message: {message} uuid: {profit_uuid}")
continue
if status == "done":
profit_uuid = ""
if status == "wait":
message, result = cancel_order(api, profit_uuid)
if message != "good":
log(f"[{time.time()}] cancel_order error message: {message}, uuid: {profit_uuid}")
continue
time.sleep(sleep_time)
message, buy_amt, buy_price = get_balance(api, coin)
if message != "good":
log(f"[{time.time()}] get_balance error message: {message}")
continue
time.sleep(sleep_time)
if float(buy_amt) > 0:
message, result = get_current_price(coin)
if message != "good":
log(f"[{time.time()}] get_current_price error message: {message}")
continue
message, uuid = take_profit(api, coin, buy_amt, buy_price, result, revenue_rate)
if message == "good":
profit_uuid = uuid
log(f"[{time.time()}] take profit uuid: {profit_uuid}, buy_amt: {buy_amt}, buy_price: {buy_price}, message: {message}")
time.sleep(sleep_time)
if stop_uuid != "":
message, stauts, price, amt = get_order_status(api, coin, stop_uuid)
if message != "good":
log(f"[{time.time()}] get_order_status error message: {message}, uuid: {stop_uuid}")
continue
if status == "done":
stop_uuid = ""
if status == "wait":
message, result = cancel_order(api, coin)
if message != "good":
log(f"[{time.time()}] cancel_order error message: {message}, uuid: {stop_uuid}")
continue
time.sleep(sleep_time)
message, buy_amt, buy_price = get_balance(api, coin)
if message != "good":
log(f"[{time.time()}] get_balance error message: {message}")
continue
time.sleep(sleep_time)
if float(buy_amt) > 0:
message, result = get_current_price(coin)
if message != "good":
log(f"[{time.time()}] get_current_price error message: {message}")
continue
message, uuid = stop_loss(api, coin, buy_amt, buy_price, float(result), max_loss_rate)
if message == "good":
stop_uuid = uuid
log(f"[{time.time()}] stop loss uuid: {stop_uuid}, buy_amt: {buy_amt}, buy_price: {buy_price}, now_price: {result}, message: {message}")
time.sleep(sleep_time)
time.sleep(1)