오늘은 GPT를 활용하여 자동 매매 프로그램을 만들어 보려고합니다.
자동 매매 프로그램은 직접 코딩하려하면 상당한 시간과 노력이 필요한 작업인데 GPT를 통해서는
어떻게 만들 수 있는지 확인해보도록 하겠습니다.
자동 매매 프로그램은 직접 코딩하려하면 상당한 시간과 노력이 필요한 작업인데 GPT를 통해서는
어떻게 만들 수 있는지 확인해보도록 하겠습니다.
1. API키 발급
먼저 업비트 거래소에서 API키를 발급받아야 합니다.
페이지 로그인 후 MY로 들어가면 아래 그림과 같 'Open API관리'라는 항목을 클릭합니다.
그럼 아래와 같은 창으로 넘어가게 되는데 저 3가지만 check한 후에 'IP 주소'를 입력 후
API키를 발급받으면 됩니다.
(IP 주소 확인 방법은 네이버에 'ip 주소 확인'이라고 검색하면 됩니다)
그럼 'Access key'와 'Secret key'가 나오게 되는데 잘 기록해둡니다.
2. Chat GPT 에게 코드 요구(with visual studio)
1) 먼저 GPT에게 역할과 상황을 부여했습니다.
2) 다음으로 업비트에 접속하여 로그인 할 수 있는 코드를 작성하라고 합니다.
2) 다음으로 업비트에 접속하여 로그인 할 수 있는 코드를 작성하라고 합니다.
그 이유또한 합리적으로 말해줬는데 코딩부분에 있어 뛰어난 퍼포먼스를
보여주는 대목이라고 생각합니다.
GPT가 다음과 같은 코드를 작성해주었고
위 코드를 visual studio에서 실행한 결과입니다.

잔고가 성공적으로 조회되는걸 볼 수 있습니다.
이제 자동 트레이딩을 위한 코드를 요구해보겠습니다.
아래와 같은 프롬프트로 질문을 했습니다.
위 코드를 visual studio에서 실행한 결과입니다.
잔고가 성공적으로 조회되는걸 볼 수 있습니다.
이제 자동 트레이딩을 위한 코드를 요구해보겠습니다.
아래와 같은 프롬프트로 질문을 했습니다.
GPT가 다음과 같은 답을 줬습니다.import pyupbit
import time
import datetime
import numpy as np
access = "your-access-key"
secret = "your-secret-key"
upbit = pyupbit.Upbit(access, secret)
def get_ema(ticker, window):
df = pyupbit.get_ohlcv(ticker, interval="minute15")
close = df['close']
ema = close.ewm(span=window).mean()
return ema
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(tickers=ticker)[0]["orderbook_units"][0]["ask_price"]
ticker = "KRW-BTC"
buy_price = 0
while True:
try:
now = datetime.datetime.now()
start_time = datetime.time(9, 0, 0)
end_time = datetime.time(8, 59, 50)
current_time = now.time()
if start_time < current_time < end_time:
current_price = get_current_price(ticker)
ema = get_ema(ticker, 200).iloc[-1]
if current_price > ema:
krw = get_balance("KRW")
if krw > 5000:
buy_price = current_price
upbit.buy_market_order(ticker, krw*0.9995)
else:
btc = get_balance("BTC")
if btc > 0.00008:
if buy_price > 0:
if current_price >= buy_price * 1.03 or current_price <= buy_price * 0.985:
upbit.sell_market_order(ticker, btc*0.9995)
buy_price = 0
time.sleep(1)
except Exception as e:
print(e)
time.sleep(1)
요구한 그대로가 반영된 코드가 나오지는 않았습니다.
생각보다 변형이 많이 되어 나타나서 다시 간단한 수준부터 요구해보겠습니다.
2-1) retry - Chat GPT 에게 코드 요구
아래와 같은 프롬프트로 GPT에게 코드를 요구했습니다.
GPT의 답변 :
import pyupbit
import time
import datetime
access = "your-access-key"
secret = "your-secret-key"
upbit = pyupbit.Upbit(access, secret)
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(tickers=ticker)[0]["orderbook_units"][0]["ask_price"]
ticker = "KRW-BTC"
buy_price = 0
while True:
try:
now = datetime.datetime.now()
start_time = datetime.time(9, 0, 0)
end_time = datetime.time(8, 59, 50)
current_time = now.time()
if start_time < current_time < end_time:
krw = get_balance("KRW")
if krw > 10000:
buy_price = get_current_price(ticker)
upbit.buy_market_order(ticker, krw*0.5)
else:
btc = get_balance("BTC")
if btc > 0.00008:
current_price = get_current_price(ticker)
if buy_price > 0:
if current_price >= buy_price * 1.01:
upbit.sell_market_order(ticker, btc)
buy_price = 0
elif current_price <= buy_price * 0.99:
if krw > 10000:
buy_price = current_price
upbit.buy_market_order(ticker, krw*0.5)
time.sleep(1)
except Exception as e:
print(e)
time.sleep(1)
직접 수익을 낼 수 있는 매매 기법은 아니지만 간단한 수준의 요청에는 효과적으로 코드를 작성해주는 걸 확인했습니다. 다음 시간에는 직접 매매에 사용하는 기법을 사용할 수 있도록 프롬프트를 작성하고 실제 적용해보는 것까지 해보도록 하겠습니다.
결론 : 매매 자동화가 완벽한 수준까진 아니지만 조금 더 효과적인 프롬프트를 작성하고 작업을 한다면 충분히 좋은 수준으로 활용이 가능할 것 같습니다.