SWDesk
[Python] 주식 자동 주문 : 데이터 분석하기
inhae
2023. 6. 16. 08:42
<한국투자증권의 OpenAPI를 활용한 주식 자동 주문 프로그램 만들기>
* 특정 종목에 대해 수집된 과거 데이터를 분석하여 데이터 흐름을 파악하는 데에 활용
* 상승 일수, 하강 일수, 상승 폭, 하강 폭, 등록 횟수, 상승 비율, 하강 비율 등을 분석함.
[Source Code]
def TreatDailyData(self, dataIn):
# dataIn: output2
rstDF = DataFrame()
prices = []
highs = []
lows = []
Diffs = []
dateCount = 0
for data1 in dataIn:
if not data1: continue;
dateCount += 1
date1 = data1.get('stck_bsop_date')
price1 = int(data1.get('stck_clpr'))
prices.append(price1)
high1 = int(data1.get('stck_hgpr'))
highs.append(high1)
low1 = int(data1.get('stck_lwpr'))
lows.append(low1)
Diffs.append(high1-low1)
rst1 = {
'DateString': date1,
'FinalPrice': price1,
'HighPrice': high1,
'LowPrice': low1
}
rstDF = pd.concat([rstDF, DataFrame([rst1])], ignore_index=True, axis=0)
avg21 = np.mean(prices)
diffAvg21 = float(np.mean(Diffs))/float(avg21)*100.0 # [%]
diffMax21 = float(np.max(Diffs))/float(avg21)*100.0 # [%]
diffMin21 = float(np.min(Diffs))/float(avg21)*100.0 # [%]
max21 = 0
min21 = 0
refMax21 = avg21*1.05 # +5%
refMin21 = avg21*0.95 # -5%
slopeCount2 = 0
priceStatus2 = "0"
maxes = []
mins = []
riseDayCount = 0
fallDayCount = 0
dayNumber = len(prices)
priceYesterday = -1
for price2 in prices:
if priceYesterday>0:
if price2 > priceYesterday:
riseDayCount += 1
else:
fallDayCount += 1
priceYesterday = price2
if priceStatus2=="0":
if price2<refMin21:
priceStatus2=="-"
min21 = price2
elif price2>refMax21:
priceStatus2 = "+"
max21 = price2
elif priceStatus2=="+":
if price2>max21: max21 = price2
elif price2<refMin21:
maxes.append(max21)
max21 = 0
priceStatus2 = "-"
min21 = price2
slopeCount2 += 1
else:
if price2<min21: min21 = price2
elif price2>refMax21:
mins.append(min21)
min21 = 0
priceStatus2 = "+"
max21 = price2
slopeCount2 += 1
score21 = slopeCount2*10
directionDifferenceRatio = abs(fallDayCount - riseDayCount)/dayNumber
if directionDifferenceRatio<0.3:
score22 = 20*(1 - directionDifferenceRatio)
elif directionDifferenceRatio<0.5:
score22 = 10*(1 - directionDifferenceRatio)
elif directionDifferenceRatio>0.8:
score22 = -5*directionDifferenceRatio
else: score22 = 0
score2 = score21 + score22 + diffAvg21
rst2 = {
'SlopeCount': slopeCount2,
'Maxes': json.dumps({'Maxes': maxes}),
'MIns': json.dumps({'Mins': mins}),
'Average': avg21,
'DiffAvg': diffAvg21,
'DiffMax': diffMax21,
'DiffMin': diffMin21,
'Score': score2,
'DateCount': dateCount
}
return (rst2, rstDF)
반응형