Algorithm Trading
과거 10년간 n% 상승을 경험한 종목의 비중은 어떻게 될까?(KOSPI)
(이경수)
2017. 2. 21. 00:49
매해 4월 1일 종가를 기준으로 다음 해 3월 31일까지 n% 이상 상승을 경험한 종목의 비중을 분석해 봄.
- KOSPI
- 10%, 20%, 30%, 40%, 50%
- 수정종가 사용.
- 파생상품 제외함.
- 2017년 2월 상장된 회사의 종목코드들로 10년치 종가 data를 불러 왔기 때문에 과거로 갈수록 정확도가 떨어짐.
import sqlite3 from Kiwoom import * from PyQt5.QtWidgets import * import matplotlib.pyplot as plt MARKET_KOSPI = 0 class Up30: def run(self): #self.check_up30() self.draw_chart() def check_up30(self, success = 0, fail = 0, sf = 0): # Database connect con = sqlite3.connect("C:\\Users\\Kyoungsoo\\PycharmProjects\\Analysis\\stock_info.db") cursor = con.cursor() # cursor.execute(("DROP TABLE up30")) #cursor.execute("CREATE TABLE up30_3(Code text, StartDate text, EndDate text, SDPrice int, " # "MaxPrice int, SF50 int, SF40 int, SF30 int, SF20 int, SF10 int)") for year in range(2015, 2016): yr1 = str(year) yr2 = str(year + 1) start_day = yr1 +'-04-01' end_day = yr2 + '-03-31' cursor.execute("SELECT Code From Kospi_Codes1702") rows = cursor.fetchall() codes = [] for row in rows: codes.append(row[0]) for cd in codes: code = '"' + cd + '"' cursor.execute("SELECT Adj_Close FROM kospi " "WHERE kospi.Code = " + code + " and Date BETWEEN " + '"' + start_day + '"' + " AND " + "'" + end_day + " '" "ORDER BY kospi.Date asc") rows = cursor.fetchall() try: price = [] for row in rows: price.append(row[0]) start_day_price = price[0] max_price = max(price) if start_day_price * 1.5 <= max_price: sf50 = 1 sf40 = 1 sf30 = 1 sf20 = 1 sf10 = 1 elif start_day_price * 1.4 <= max_price: sf50 = 0 sf40 = 1 sf30 = 1 sf20 = 1 sf10 = 1 elif start_day_price * 1.3 <= max_price: sf50 = 0 sf40 = 0 sf30 = 1 sf20 = 1 sf10 = 1 elif start_day_price * 1.2 <= max_price: sf50 = 0 sf40 = 0 sf30 = 0 sf20 = 1 sf10 = 1 elif start_day_price * 1.1 <= max_price: sf50 = 0 sf40 = 0 sf30 = 0 sf20 = 0 sf10 = 1 else: sf50 = 0 sf40 = 0 sf30 = 0 sf20 = 0 sf10 = 0 except: start_day_price = 0 max_price = 0 sf50 = 2 sf40 = 2 sf30 = 2 sf20 = 2 sf10 = 2 # Insert data cursor.execute("INSERT INTO up30_3(Code, StartDate, EndDate, SDPrice, MaxPrice, SF50, SF40, SF30, SF20, SF10) " "VALUES(?,?,?,?,?,?,?,?,?,?)", (cd, start_day, end_day, start_day_price, max_price, sf50, sf40, sf30, sf20, sf10)) print(cd, start_day, end_day, start_day_price, max_price, sf50, sf40, sf30, sf20, sf10) con.commit() con.close() def draw_chart(self): suc_rates = {'10':[], '20':[], '30':[], '40':[], '50':[]} sfs = {'2006': [], '2007': [], '2008': [], '2009': [], '2010': [], '2011': [], '2012': [], '2013': [], '2014': [], '2015': []} # Database connect con = sqlite3.connect("C:\\Users\\Kyoungsoo\\PycharmProjects\\Analysis\\stock_info.db") cursor = con.cursor() for year in range(2006, 2016): yr = str(year) start_day = yr + '-04-01' for rate in range(10, 60, 10): rt = str(rate) cursor.execute("SELECT SF" + rt + " FROM up30_3 WHERE StartDate= " + '"' + start_day + '"') rows = cursor.fetchall() for row in rows: sfs[yr].append(row[0]) s_count = sfs[yr].count(1) f_count = sfs[yr].count(0) s_rate = s_count / (s_count + f_count) s_rate = round(s_rate * 100, 2) suc_rates[rt].append(s_rate) plt.style.use('ggplot') fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.set_xlim([2006, 2015]) ax.set_ylim([0, 100]) ax.set_xticks([2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015]) ax.set_xticklabels(['$06.4-07.3$','$07.4-08.3$','$08.4-09.3$','$09.4-10.3$','$10.4-11.3$','$11.4-12.3$','$12.4-13.3$','$13.4-14.3$','$14.4-15.3$','$15.4-16.3$'], rotation=45) term = range(2006, 2016) plt.plot(term, suc_rates['10'], 'ro-', label='10%') plt.plot(term, suc_rates['20'], 'co-', label='20%') plt.plot(term, suc_rates['30'], 'go-', label='30%') plt.plot(term, suc_rates['40'], 'mo-', label='40%') plt.plot(term, suc_rates['50'], 'bo-', label='50%') plt.xlabel('period') plt.ylabel('rate(%)') plt.legend(loc = 'best') plt.show() if __name__ == "__main__": app = QApplication(sys.argv) up30 = Up30() up30.run()