API 개요
타임퍼센트 Python SDK를 사용하여 전략을 개발하고 플랫폼과 상호작용할 수 있습니다.
설치
pip install timepercent-sdk
인증
API 키를 사용하여 인증합니다:
from timepercent import Client
client = Client(
api_key="your-api-key",
api_secret="your-api-secret"
)
또는 환경 변수 사용:
import os
from timepercent import Client
client = Client(
api_key=os.getenv("TIMEPERCENT_API_KEY"),
api_secret=os.getenv("TIMEPERCENT_API_SECRET")
)
주요 모듈
Strategy 모듈
전략 개발을 위한 핵심 클래스:
from timepercent import Strategy
class MyStrategy(Strategy):
def initialize(self):
pass
def on_data(self, data):
pass
Data 모듈
시장 데이터 접근:
from timepercent.data import get_historical_data, get_realtime_data
# 과거 데이터 조회
data = get_historical_data(
symbol="AAPL",
start_date="2023-01-01",
end_date="2023-12-31",
interval="1d"
)
# 실시간 데이터 스트리밍
for tick in get_realtime_data("AAPL"):
print(tick.price)
Indicators 모듈
기술적 지표:
from timepercent.indicators import SMA, RSI, MACD
sma = SMA(period=20)
rsi = RSI(period=14)
macd = MACD()
Orders 모듈
주문 관리:
from timepercent.orders import Order, OrderType
# 주문 생성
order = Order(
symbol="AAPL",
quantity=100,
order_type=OrderType.MARKET
)
# 주문 실행
order_id = client.submit_order(order)
# 주문 상태 확인
status = client.get_order_status(order_id)
REST API
HTTP API를 직접 호출할 수도 있습니다:
import requests
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# 계정 정보 조회
response = requests.get(
"https://api.timepercent.com/v1/account",
headers=headers
)
# 포지션 조회
response = requests.get(
"https://api.timepercent.com/v1/positions",
headers=headers
)
WebSocket API
실시간 데이터를 받기 위해 WebSocket을 사용할 수 있습니다:
from timepercent.websocket import WebSocketClient
def on_tick(tick):
print(f"{tick.symbol}: {tick.price}")
ws = WebSocketClient(
api_key=api_key,
api_secret=api_secret
)
ws.subscribe("AAPL", callback=on_tick)
ws.start()
API 제한
- 요청 제한: 초당 100회
- 주문 제한: 초당 10회
- 데이터 조회: 분당 1000회
에러 처리
from timepercent.exceptions import (
APIError,
AuthenticationError,
RateLimitError
)
try:
order = client.submit_order(order)
except AuthenticationError:
print("인증 실패: API 키를 확인하세요")
except RateLimitError:
print("요청 제한 초과: 잠시 후 다시 시도하세요")
except APIError as e:
print(f"API 오류: {e.message}")