def get_stochastic(stock_code, k_period=5, d_period=3, slow_period=3):
cpIndex = win32com.client.Dispatch("CpSysDib.CpIndex")
cpIndex.SetInputValue(0, stock_code)
cpIndex.SetInputValue(1, 7) # 스토캐스틱
cpIndex.SetInputValue(2, k_period) # %K 기간
cpIndex.SetInputValue(3, d_period) # %D 기간
cpIndex.SetInputValue(4, slow_period) # Slow %K 기간
cpIndex.BlockRequest()
count = cpIndex.GetHeaderValue(3)
results = []
for i in range(min(10, count)):
fast_k = cpIndex.GetDataValue(0, i) # Fast %K
fast_d = cpIndex.GetDataValue(1, i) # Fast %D
slow_k = cpIndex.GetDataValue(2, i) # Slow %K
slow_d = cpIndex.GetDataValue(3, i) # Slow %D
results.append({
'date_index': i,
'fast_k': fast_k,
'fast_d': fast_d,
'slow_k': slow_k,
'slow_d': slow_d
})
return results
# 사용 예제
stoch_data = get_stochastic("A005930")
for data in stoch_data:
print(f"Fast K: {data['fast_k']:.2f}, Fast D: {data['fast_d']:.2f}")
3. 통합 지표 조회 함수
python
class CreonIndicator:
def __init__(self):
self.cpIndex = win32com.client.Dispatch("CpSysDib.CpIndex")
self.indicator_map = {
'macd': 0,
'rsi': 1,
'dmi': 2,
'cci': 3,
'adx': 4,
'adxr': 5,
'bollinger': 6,
'stochastic': 7,
'willr': 8,
'roc': 9,
'momentum': 10
}
def get_indicator(self, stock_code, indicator_name, **params):
indicator_code = self.indicator_map.get(indicator_name.lower())
if indicator_code is None:
raise ValueError(f"지원하지 않는 지표: {indicator_name}")
self.cpIndex.SetInputValue(0, stock_code)
self.cpIndex.SetInputValue(1, indicator_code)
# 매개변수 설정
for i, value in enumerate(params.values()):
self.cpIndex.SetInputValue(2 + i, value)
self.cpIndex.BlockRequest()
count = self.cpIndex.GetHeaderValue(3)
# 지표별 데이터 추출
results = []
for i in range(min(20, count)): # 최근 20개 데이터
data_point = {'date_index': i}
# 지표별 데이터 개수에 따라 동적으로 추출
data_count = self.cpIndex.GetHeaderValue(2) # 데이터 개수
for j in range(data_count):
value = self.cpIndex.GetDataValue(j, i)
data_point[f'value_{j}'] = value
results.append(data_point)
return results
# 사용 예제
indicator = CreonIndicator()
# DMI 조회
dmi_data = indicator.get_indicator("A005930", "dmi", period=14)
print("DMI 데이터:", dmi_data[0]) # 최근 데이터
# RSI 조회
rsi_data = indicator.get_indicator("A005930", "rsi", period=14)
print("RSI 데이터:", rsi_data[0])
# MACD 조회
macd_data = indicator.get_indicator("A005930", "macd", short=12, long=26, signal=9)
print("MACD 데이터:", macd_data[0])
4. 주의사항
4-1. 종목코드 형식
일반주식: "A" + 종목코드 (예: "A005930")
ETF: "Q" + 종목코드
선물: "1" + 종목코드
4-2. 데이터 인덱스
GetDataValue(데이터타입, 인덱스)에서 인덱스 0이 가장 최근 데이터입니다.
시간별 데이터를 원한다면 CpSysDib.StockChart를 사용해야 합니다.
4-3. 에러 처리
python
def safe_request(cpIndex):
try:
cpIndex.BlockRequest()
# 상태 코드 확인
status = cpIndex.GetDibStatus()
if status != 0:
print(f"에러 코드: {status}, 메시지: {cpIndex.GetDibMsg1()}")
return False
return True
except Exception as e:
print(f"요청 중 에러: {e}")
return False
이렇게 CpIndex 클래스를 활용하면 Creon 차트에 표시되는 다양한 보조지표 값을 직접 가져와 활용할 수 있습니다. 특정 지표에 대해 더 자세한 정보가 필요하시면 말씀해주세요!