feat: initialize project with Python version, main entry point, and Toss API integration
This commit is contained in:
@@ -0,0 +1 @@
|
||||
3.14
|
||||
@@ -0,0 +1,30 @@
|
||||
from toss_api import TossInvestAPI
|
||||
|
||||
def main():
|
||||
# 1. API 객체 초기화
|
||||
api = TossInvestAPI()
|
||||
|
||||
# 2. 내 계좌 잔고 확인
|
||||
print("계좌 잔고 조회 중...")
|
||||
balance = api.get_account_balance()
|
||||
print(balance)
|
||||
|
||||
# 3. 전략 예시: 전날 활발했던 종목(예: 삼성전자 '005930')의 중간 가격 계산 로직 (구현 필요)
|
||||
target_ticker = "005930"
|
||||
|
||||
print(f"{target_ticker} 현재가 조회 중...")
|
||||
current_price_info = api.get_current_price(target_ticker)
|
||||
print(current_price_info)
|
||||
|
||||
# 4. 하한 목표가 매수 주문 예시
|
||||
# buy_price = 계산된_하한가
|
||||
# buy_result = api.create_order(target_ticker, "BUY", price=buy_price, quantity=10)
|
||||
# print(buy_result)
|
||||
|
||||
# 5. 매수 완료 후 상한 목표가 매도 주문 예시
|
||||
# sell_price = 계산된_상한가
|
||||
# sell_result = api.create_order(target_ticker, "SELL", price=sell_price, quantity=10)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,7 @@
|
||||
[project]
|
||||
name = "toss-quant"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.14"
|
||||
dependencies = []
|
||||
+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