feat: initialize project with Python version, main entry point, and Toss API integration
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
import os
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# .env 파일 로드
|
||||
load_dotenv()
|
||||
|
||||
class TossInvestAPI:
|
||||
def __init__(self):
|
||||
self.api_key = os.getenv("TOSS_API_KEY")
|
||||
self.secret_key = os.getenv("TOSS_SECRET_KEY")
|
||||
self.account_number = os.getenv("TOSS_ACCOUNT_NUMBER")
|
||||
self.base_url = os.getenv("TOSS_BASE_URL")
|
||||
|
||||
# 공통 헤더 설정 (실제 가이드에 맞게 수정 필요)
|
||||
self.headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"X-Secret-Key": self.secret_key,
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
def _request(self, method, endpoint, params=None, data=None):
|
||||
"""API 요청을 처리하는 내부 헬퍼 함수"""
|
||||
url = f"{self.base_url}{endpoint}"
|
||||
try:
|
||||
response = requests.request(method, url, headers=self.headers, params=params, json=data)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"API 요청 오류: {e}")
|
||||
return None
|
||||
|
||||
# ==========================================
|
||||
# 1. 조회 API
|
||||
# ==========================================
|
||||
def get_account_balance(self):
|
||||
"""계좌 잔고 및 보유 종목 조회"""
|
||||
endpoint = f"/v1/account/{self.account_number}/balance" # 예시 URL
|
||||
return self._request("GET", endpoint)
|
||||
|
||||
def get_current_price(self, ticker):
|
||||
"""특정 종목의 현재가 조회"""
|
||||
endpoint = f"/v1/market/price/{ticker}" # 예시 URL
|
||||
return self._request("GET", endpoint)
|
||||
|
||||
# ==========================================
|
||||
# 2. 주문 API (구매/판매/취소)
|
||||
# ==========================================
|
||||
def create_order(self, ticker, order_type, price, quantity):
|
||||
"""
|
||||
매수/매도 주문 실행
|
||||
- order_type: "BUY" (매수) 또는 "SELL" (매도)
|
||||
"""
|
||||
endpoint = "/v1/orders" # 예시 URL
|
||||
payload = {
|
||||
"account_number": self.account_number,
|
||||
"ticker": ticker,
|
||||
"order_type": order_type,
|
||||
"price": price,
|
||||
"quantity": quantity,
|
||||
"order_class": "LIMIT" # 지정가 주문 (단타 목표가 설정에 필수)
|
||||
}
|
||||
return self._request("POST", endpoint, data=payload)
|
||||
|
||||
def cancel_order(self, order_id):
|
||||
"""미체결 주문 취소 (지정가 매매 시 필수)"""
|
||||
endpoint = f"/v1/orders/{order_id}/cancel" # 예시 URL
|
||||
return self._request("POST", endpoint)
|
||||
Reference in New Issue
Block a user