이하영

날짜별 데이터(daily_buy_list) 데이터베이스

...@@ -7,9 +7,6 @@ db_ip="localhost" ...@@ -7,9 +7,6 @@ db_ip="localhost"
7 # 모의투자 계좌번호 7 # 모의투자 계좌번호
8 test_account_no="8147766711" 8 test_account_no="8147766711"
9 9
10 -# 시뮬레이션을 돌리기 시작할 날짜
11 -start_buying='20190101'
12 -
13 test_num=1 10 test_num=1
14 test_bot_name="AutoBot"+str(test_num)+"_Test" 11 test_bot_name="AutoBot"+str(test_num)+"_Test"
15 12
...@@ -20,7 +17,17 @@ real_num=1 ...@@ -20,7 +17,17 @@ real_num=1
20 17
21 # 데이터베이스 이름 18 # 데이터베이스 이름
22 real_bot_name="AutoBot"+str(real_num) 19 real_bot_name="AutoBot"+str(real_num)
23 -real_stockInfo_name="stock_info" 20 +real_daily_craw_db_name = "daily_craw"
24 -real_dailyInfo_name="daily_info" 21 +real_daily_buy_list_db_name = "daily_buy_list"
22 +
23 +# 시뮬레이션을 돌리기 시작할 날짜
24 +start_daily_buy_list='20190102'
25 +
26 +# api를 최대 몇 번까지 호출 하고 봇을 끌지 설정 하는 옵션
27 +max_api_call=98
28 +
29 +# openapi 1회 조회 시 대기 시간(0.2 보다-> 0.3이 안정적)
30 +TR_REQ_TIME_INTERVAL = 0.3
25 31
26 -max_api_call=98
...\ No newline at end of file ...\ No newline at end of file
32 +# n회 조회를 1번 발생시킨 경우 대기 시간
33 +TR_REQ_TIME_INTERVAL_LONG = 1
...\ No newline at end of file ...\ No newline at end of file
......
1 +from sqlalchemy import *
2 +from pandas import DataFrame
3 +import datetime
4 +
5 +from daily_crawler import *
6 +import cf
7 +
8 +# -* daily_buy_list *-
9 +# 일자별로 주식종목에 대한 데이터를 저장하는 데이터베이스
10 +
11 +class daily_buy_list():
12 + def __init__(self):
13 + self.variable_setting()
14 +
15 + # 변수 설정
16 + def variable_setting(self):
17 + self.today = datetime.datetime.today().strftime("%Y%m%d")
18 + self.today_detail = datetime.datetime.today().strftime("%Y%m%d%H%M")
19 + self.start_date = cf.start_daily_buy_list
20 + self.engine_daily_craw = create_engine(
21 + "mysql+pymysql://" + cf.db_id + ":" + cf.db_pw + "@" + cf.db_ip + ":" + cf.db_port + "/daily_craw",
22 + encoding='utf-8')
23 + self.engine_daily_buy_list = create_engine(
24 + "mysql+pymysql://" + cf.db_id + ":" + cf.db_pw + "@" + cf.db_ip + ":" + cf.db_port + "/daily_buy_list",
25 + encoding='utf-8')
26 +
27 + # 설정한 날짜부터 현재까지 날짜 리스트를 저장하는 함수
28 + def date_rows_setting(self):
29 + query = "select date from `gs글로벌` where date >= '%s' group by date"
30 + self.date_rows = self.engine_daily_craw.execute(query % self.start_date).fetchall()
31 +
32 + # daily_buy_list 데이터베이스에 특정 이름(date)을 가진 테이블이 존재하는지 확인하는 함수
33 + def is_table_exist_daily_buy_list(self, date):
34 + query = "select 1 from information_schema.tables where table_schema ='daily_buy_list' and table_name = '%s'"
35 + rows = self.engine_daily_buy_list.execute(query % (date)).fetchall()
36 +
37 + if len(rows) == 1:
38 + return True
39 + elif len(rows) == 0:
40 + return False
41 +
42 + # daily_buy_list 데이터베이스 안에 테이블을 생성하는 함수
43 + def daily_buy_list(self):
44 + self.date_rows_setting()
45 + self.get_stock_item_all()
46 +
47 + for k in range(len(self.date_rows)):
48 + print(str(k) + " 번째 : " + datetime.datetime.today().strftime(" ******* %H : %M : %S *******"))
49 + # 특정 날짜의 이름을 가진 테이블 존재하는지 확인
50 + # 테이블이 존재하면 다음 날짜를 확인
51 + if self.is_table_exist_daily_buy_list(self.date_rows[k][0]):
52 + continue
53 + # 테이블이 존재하지 않으면 생성
54 + else:
55 + multi_list = list()
56 +
57 + for i in range(len(self.stock_item_all)):
58 + code = self.stock_item_all[i][1] # 종목코드
59 + code_name = self.stock_item_all[i][0] # 종목명
60 +
61 + # daily_craw 데이터베이스에 종목명을 가진 테이블이 존재하지 않는 경우 다음 데이터 처리
62 + # daily_craw에 저장한 종목에 대해서만 daily_buy_list에 날짜별 데이터를 저장한다
63 + if not self.is_table_exist_daily_craw(code, code_name):
64 + continue
65 +
66 + query = f"select * from '{self.stock_item_all[i][0]}' where date = '{self.date_rows[k][0]}' " \
67 + f"group by date"
68 + rows = self.engine_daily_craw.execute(query).fetchall()
69 + multi_list += rows
70 +
71 + if len(multi_list) != 0:
72 + df_temp = DataFrame(multi_list,
73 + columns=['index', 'date', 'check_item', 'code', 'code_name', 'd1_diff_rate',
74 + 'close', 'open', 'high', 'low','volume',
75 + 'clo5', 'clo10', 'clo20', 'clo60', 'clo120',
76 + "clo5_diff_rate", "clo10_diff_rate","clo20_diff_rate",
77 + "clo60_diff_rate", "clo120_diff_rate",
78 + 'yes_clo5', 'yes_clo10', 'yes_clo20', 'yes_clo60', 'yes_clo120',
79 + 'vol5', 'vol10', 'vol20', 'vol60', 'vol120'
80 + ])
81 +
82 + df_temp.to_sql(name=self.date_rows[k][0], con=self.engine_daily_buy_list, if_exists='replace')
83 +
84 + # stock_item_all(거래할 종목 리스트)에서 모든 항목을 가져오는 함수
85 + def get_stock_item_all(self):
86 + print("get_stock_item_all!!!!!!")
87 + sql = "select code_name,code from stock_item_all"
88 + self.stock_item_all = self.engine_daily_buy_list.execute(sql).fetchall()
89 +
90 + # daily_craw 데이터베이스에 특정 이름(code_name)을 가진 테이블이 존재하는지 확인하는 함수
91 + def is_table_exist_daily_craw(self, code, code_name):
92 + sql = "select 1 from information_schema.tables where table_schema ='daily_craw' and table_name = '%s'"
93 + rows = self.engine_daily_craw.execute(sql % (code_name)).fetchall()
94 +
95 + if len(rows) == 1:
96 + return True
97 + elif len(rows) == 0:
98 + return False
99 +
100 +if __name__ == "__main__":
101 + daily_buy_list = daily_buy_list()
102 + daily_buy_list.date_rows_setting()
...@@ -751,7 +751,7 @@ class Open_Api(QAxWidget): ...@@ -751,7 +751,7 @@ class Open_Api(QAxWidget):
751 def is_stock_table_exist(self,code_name): 751 def is_stock_table_exist(self,code_name):
752 query = "select 1 from information_schema.tables where table_schema ='stock_info' and table_name = '{}'" 752 query = "select 1 from information_schema.tables where table_schema ='stock_info' and table_name = '{}'"
753 result=self.engine_stock.execute(query.format(code_name)).fetchall() 753 result=self.engine_stock.execute(query.format(code_name)).fetchall()
754 - if result: 754 + if result:
755 return True 755 return True
756 else: 756 else:
757 return False 757 return False
......