-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoin.py
60 lines (52 loc) · 1.76 KB
/
bitcoin.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
import time
import pyupbit
import datetime
access = ""
secret = ""
def get_target_price(ticker, k):
"""변동성 돌파 전략으로 매수 목표가 조회"""
df = pyupbit.get_ohlcv(ticker, interval="day", count=2)
target_price = df.iloc[0]['close'] + (df.iloc[0]['high'] - df.iloc[0]['low']) * k
return target_price
def get_start_time(ticker):
"""시작 시간 조회"""
df = pyupbit.get_ohlcv(ticker, interval="day", count=1)
start_time = df.index[0]
return start_time
def get_balance(ticker):
"""잔고 조회"""
balances = upbit.get_balances()
for b in balances:
if b['currency'] == ticker:
if b['balance'] is not None:
return float(b['balance'])
else:
return 0
return 0
def get_current_price(ticker):
"""현재가 조회"""
return pyupbit.get_orderbook(ticker=ticker)["orderbook_units"][0]["ask_price"]
# 로그인
upbit = pyupbit.Upbit(access, secret)
print("autotrade start")
# 자동매매 시작
while True:
try:
now = datetime.datetime.now()
start_time = get_start_time("KRW-BTC")
end_time = start_time + datetime.timedelta(days=1)
if start_time < now < end_time - datetime.timedelta(seconds=10):
target_price = get_target_price("KRW-BTC", 0.5)
current_price = get_current_price("KRW-BTC")
if target_price < current_price:
krw = get_balance("KRW")
if krw > 5000:
upbit.buy_market_order("KRW-BTC", krw*0.9995)
else:
btc = get_balance("BTC")
if btc > 0.00008:
upbit.sell_market_order("KRW-BTC", btc*0.9995)
time.sleep(1)
except Exception as e:
print(e)
time.sleep(1)