AWS 서버에 올리기 위한 upbit auto trading algorithm
def post_message(token, channel, text): """슬랙 메시지 전송""" response = requests.post("https://slack.com/api/chat.postMessage", headers={"Authorization": "Bearer "+token}, data={"channel": channel,"text": text} )
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_ma15(ticker): """15일 이동 평균선 조회""" df = pyupbit.get_ohlcv(ticker, interval="day", count=15) ma15 = df['close'].rolling(15).mean().iloc[-1] return ma15
def get_balance(coin): """잔고 조회""" balances = upbit.get_balances() for b in balances: if b['currency'] == coin: if b['balance'] is not None: return float(b['balance']) else: return 0
def get_current_price(ticker): """현재가 조회""" return pyupbit.get_orderbook(tickers=ticker)[0]["orderbook_units"][0]["ask_price"]
upbit = pyupbit.Upbit(access, secret) print("autotrade start")
post_message(myToken,"#crypto", "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) # k값=0.5 설정 / 변동성*k에 따라 매수 시점 달라짐
ma15 = get_ma15("KRW-BTC")
current_price = get_current_price("KRW-BTC")
if target_price < current_price and ma15 < current_price:
krw = get_balance("KRW")
if krw > 5000:
buy_result = upbit.buy_market_order("KRW-BTC", krw*0.9995)
post_message(myToken,"#crypto", "BTC buy : " +str(buy_result))
else:
btc = get_balance("BTC")
if btc > 0.00008:
sell_result = upbit.sell_market_order("KRW-BTC", btc*0.9995)
post_message(myToken,"#crypto", "BTC buy : " +str(sell_result))
time.sleep(1)
except Exception as e:
print(e)
post_message(myToken,"#crypto", e)
time.sleep(1)
거래는 활발하게 진행되었으나, 하락장에서는 유의미한 양의 수익률을 보여주지는 못했다. 또한 업비트가 타 거래소에 비해 수수료 부담이 적은 거래소임에도 불구하고, 많은 거래로 수수료가 꽤 부담되었다. 전략을 수정해야 할 필요가 있다.