이하영

면담확인서

...@@ -26,6 +26,12 @@ class Simulator_Api: ...@@ -26,6 +26,12 @@ class Simulator_Api:
26 self.buy_algorithm=1 26 self.buy_algorithm=1
27 self.sell_algorithm=1 27 self.sell_algorithm=1
28 28
29 + # 투자에서 사용할 변수 설정
30 + self.per_invest=1000000 # 한 항목 당 구매할 금액
31 +
32 + self.sell_point=10 # 매도 수익률
33 + self.limit_money=1000000 # 최소로 남겨놓을 금액
34 +
29 # 특정 데이터베이스 내에 특정 테이블이 존재하는지 확인하는 함수 35 # 특정 데이터베이스 내에 특정 테이블이 존재하는지 확인하는 함수
30 def is_table_exist(self,db_name,table_name): 36 def is_table_exist(self,db_name,table_name):
31 query="select 1 from information_schema.tables where table_schema='%s' and table_name='%s'" 37 query="select 1 from information_schema.tables where table_schema='%s' and table_name='%s'"
......
1 -from sqlalchemy import * 1 +from Open_Api import *
2 +from collections import OrderedDict
3 +from pandas import DataFrame
2 4
3 -from open_api import * 5 +from Daily_Info import *
4 -from daily_info import * 6 +from Stock_Info import *
5 -from stock_info import *
6 import config 7 import config
7 8
8 -class CollectorApi(): 9 +class Collector_Api():
9 def __init__(self): 10 def __init__(self):
10 - self.open_api=OpenApi() 11 + self.open_api=Open_Api()
11 self.engine_bot=self.open_api.engine_bot 12 self.engine_bot=self.open_api.engine_bot
13 + self.set_variable()
12 14
15 + # 변수 설정
13 def set_variable(self): 16 def set_variable(self):
14 - self.open_api.sort="collector" 17 + self.open_api.use="collector"
15 - self.stock_info=StockInfo(config.real_bot,config.real_stockInfo,config.real_dailyInofo) 18 + self.stock_info=Stock_Info(config.real_bot_name,config.real_dailyInfo_name,config.real_stockInfo_name)
16 - self.daily_info=DailyInfo() 19 + self.daily_info=Daily_Info()
17 -
18 - def update_code(self):
19 - print("update code")
20 - query = "select code_update,jango_data_db_check, possessed_item, today_profit, final_chegyul_check, " \
21 - "db_to_buy_list,today_buy_list, daily_crawler , min_crawler, daily_buy_list " \
22 - "from setting_data limit 1"
23 - result=self.engine_bot.execute(query).fetchall()
24 20
25 - print(result) 21 + def code_update_check(self):
22 + query="select code_update,balance_to_db,posses_stocks,today_profit,contract_check,db_to_daily_info," \
23 + "today_buy_list,stock_info,min_info,daily_info from setting_data"
24 + result=self.engine_bot.execute(query).fetchall()
26 25
27 - if result[0][0]!=self.open_api.today(): 26 + # 오늘 날짜에 종목리스트가 업데이트 되지 않았다면 업데이트를 실행
28 - self.open_api.check_balance() 27 + if result[0][0]!=self.open_api.today:
28 + self.open_api.get_balance()
29 self.get_code_list() 29 self.get_code_list()
30 30
31 + # 계좌정보 업데이트
32 + if result[0][1]!=self.open_api.today or result[0][2]!=self.open_api.today:
33 + self.check_balance()
34 + self.open_api.set_per_invest()
35 +
36 + # 현재 보유종목 테이블 업데이트
37 + if result[0][2]!=self.open_api.today:
38 + self.open_api.get_posses_item()
39 + self.open_api.setting_data_posses_stock()
40 +
41 + if result[0][3]!=self.open_api.today:
42 + self.update_today_profit_list()
43 +
44 + if result[0][7]!=self.open_api.today:
45 + self.check_daily_info()
46 +
47 +
48 +
49 + # 코스피, 코스닥 리스트 확인 및 데이터베이스 업데이트
50 + def get_code_list(self):
51 + self.stock_info.get_item_kospi()
52 + self.stock_info.get_item_kosdaq()
53 +
54 + stock_data=OrderedDict(
55 + kospi=self.stock_info.kospi_list,
56 + kosdaq=self.stock_info.kosdaq_list
57 + )
58 +
59 + for s_type,data in stock_data.items():
60 + stock_data[s_type]=self.create_stock_table(data,s_type)
61 +
62 + stock_all=stock_data['kospi'].append(stock_data['kosdaq'],ignore_index=True)
63 + self.create_stock_table(stock_all,"all")
64 + query="update setting_data set code_update='%s'"
65 + self.engine_bot.execute(query%(self.open_api.today))
66 +
67 + # kospi, kosdaq 주식 종목을 저장하는 테이블 생성
68 + def create_stock_table(self,data,type):
69 + checking=['kospi','kosdaq']
70 + stock_df=DataFrame()
71 + stock_df['code']=data['code']
72 + stock_list=list()
73 + # 주식 code를 이용하여 실제 kiwoom 증권에서 사용하는 주식이름으로 변경
74 + for kind in data.itertuples():
75 + kiwoom_name=self.open_api.dynamicCall("GetMasterCodeName(QString)",kind.code).strip()
76 + stock_list.append(kiwoom_name)
77 + stock_df['code_name']=stock_list
78 + stock_df['check_item']=0
79 +
80 + if type in checking:
81 + stock_df=stock_df[stock_df['code_name'].map(len)>0]
82 +
83 + if type=="all":
84 + stock_df['check_stock']="0"
85 + stock_df['check_min']="0"
86 +
87 + # 데이터 타입을 설정
88 + # code, code_name 칼럼은 Text. check_item 칼럼은 Integer
89 + dtypes=dict(zip(list(stock_df.columns),[Text]*len(stock_df.columns)))
90 + dtypes['check_item']=Integer
91 +
92 + table_name='stock_'+str(type)
93 + stock_df.to_sql(table_name,self.open_api.engine_daily,if_exists='replace',dtype=dtypes)
94 + return stock_df
95 +
96 + def check_balance(self):
97 + self.open_api.reset_opw00018_output()
98 + # 예수금 상세현황 요청
99 + self.open_api.set_input_value("계좌번호",self.open_api.account_no)
100 + self.open_api.set_input_value("비밀번호입력매체구분",00)
101 + self.open_api.set_input_value("조회구분",1)
102 + self.open_api.comm_rq_data("opw00001_req","opw00001",0,"2000")
103 + # 계좌평가 잔고내역 요청
104 + self.open_api.set_input_value("계좌번호",self.open_api.account_no)
105 + self.open_api.comm_rq_data("opw00018_req","opw00018",0,"2000")
106 + while self.open_api.remained_data:
107 + self.open_api.set_input_value("계좌번호",self.open_api.account_no)
108 + self.open_api.comm_rq_data("opw00018_req","opw00018",2,"2000")
109 + # 일자별 실현손익 요청
110 + self.open_api.set_input_value("계좌번호",self.open_api.account_no)
111 + self.open_api.set_input_value("시작일자","20170101")
112 + self.open_api.set_input_value("종료일자",self.open_api.today)
113 + self.open_api.comm_rq_data("opt10074_req","opt10074",0,"0329")
114 + while self.open_api.remained_data:
115 + self.open_api.set_input_value("계좌번호", self.open_api.account_no)
116 + self.open_api.set_input_value("시작일자", "20170101")
117 + self.open_api.set_input_value("종료일자", self.open_api.today)
118 + self.open_api.set_input_value("구분","0")
119 + self.open_api.comm_rq_data("opt10074_req", "opt10074", 2, "0329")
120 +
121 + self.create_balance_table()
122 +
123 + # balance_data 테이블 생성
124 + def create_balance_table(self):
125 + self.total_invest=int(self.open_api.deposit)+int(self.open_api.total_purchase)
126 + balance_data={'id':[],'date':[],'total_asset':[],'today_profit':[],"total_profit":[],
127 + 'total_invest':[],'deposit':[],'total_purchase':[],'total_evaluation':[],'today_invest':[],
128 + 'today_rate':[],'estimated_asset':[]}
129 +
130 + balance_cols=['date','today_earning_rate','total_asset','deposit',
131 + 'today_profit','total_profit','today_invest','total_invest',
132 + 'total_purchase','today_evaluation','today_sell_count',
133 + 'today_rate','estimated_asset','sell_point','per_invest','limit_money',
134 + 'today_buy_count','today_sell_count','total_posses_count','today_buy_price','today_sell_price'
135 + ]
136 +
137 + balance=DataFrame(balance_data,columns=balance_cols,index=balance_data['id'])
138 +
139 + balance.loc[0,'date']=self.open_api.today
140 + balance.loc[0,'today_profit']=self.open_api.today_profit
141 + balance.loc[0,'total_profit']=self.open_api.total_profit
142 + balance.loc[0,'total_invest']=self.open_api.total_invest
143 + balance.loc[0,'deposit']=self.open_api.deposit
144 + balance.loc[0,'total_purchase']=self.open_api.total_purchase
145 + balance.loc[0,'total_evaluation']=self.open_api.total_evaluated_price
146 + balance.loc[0,'today_invest']=self.open_api.total_valuation
147 + balance.loc[0,'today_rate']=float(self.open_api.earning_rate/self.open_api.mod_classify)
148 + balance.loc[0,'estimate_asset']=self.open_api.estimated_deposit
149 + balance.loc[0,'sell_point']=self.open_api.simul_api.sell_point
150 + balance.loc[0,'per_invest']=self.open_api.per_invest
151 + balance.loc[0,'limit_money']=self.open_api.simul_api.limit_money
152 +
153 + # balance_data 테이블 생성
154 + balance.to_sql('balance_data', self.engine_bot, if_exists='append')
155 +
156 + query = "select date from balance_data"
157 + rows = self.engine_bot.execute(query).fetchall()
158 +
159 + for i in range(len(rows)):
160 + # today_earning_rate
161 + query = "update balance_data " \
162 + "set " \
163 + "today_earning_rate =round(today_profit / total_invest * '%s',2) WHERE date='%s'"
164 + self.engine_bot.execute(query % (100, rows[i][0]))
165 + # today_buy_count
166 + query = "UPDATE balance_data " \
167 + "SET " \
168 + "today_buy_count=" \
169 + "(select count(*) " \
170 + "from " \
171 + "(select code from transaction_history where buy_date like '%s' group by code )) " \
172 + "WHERE date='%s'"
173 + self.engine_bot.execute(query % (rows[i][0] + "%%", rows[i][0]))
174 + # today_sell_count
175 + query="UPDATE balance_data " \
176 + "SET " \
177 + "today_sell_count=" \
178 + "(select count(*) " \
179 + "from " \
180 + "(select code from transaction_history" \
181 + "where buy_date like '%s' and sell_date is not null group by code) temp) " \
182 + "WHERE date='%s'"
183 + self.engine_bot.execute(query % (rows[i][0] + "%%", rows[i][0]))
184 + # today_sell_price
185 + query="UPDATE balance_data " \
186 + "SET" \
187 + "today_sell_price=" \
188 + "(select sum(*) " \
189 + "from " \
190 + "(select sell_price from transaction_history " \
191 + "where sell_date like '%s')" \
192 + "where date='%s'"
193 + self.engine_bot.execute(query%(rows[i][0])+"%%",rows[i][0])
194 + # today_buy_price
195 + query="UPDATE balance_data " \
196 + "SET" \
197 + "today_sell_price=" \
198 + "(select sum(*) " \
199 + "from " \
200 + "(select purchase_price from transaction_history " \
201 + "where sell_date like '%s')" \
202 + "where date='%s'"
203 + self.engine_bot.execute(query%(rows[i][0])+"%%",rows[i][0])
204 + # total_posses_count
205 + query="UPDATE balance_data " \
206 + "SET" \
207 + "total_posses_count=" \
208 + "(select count(*) " \
209 + "from" \
210 + "transaction_history where sell_date is Null)" \
211 + "where date='%s'"
212 + self.engine_bot.execute(query%(rows[i][0]))
213 +
214 + sql = "UPDATE setting_data SET balance_to_db='%s' limit 1"
215 + self.engine_bot.execute(sql % (self.open_api.today))
216 +
217 + def is_table_exist(self,db_name,table_name):
218 + query="select 1 from information_schema.tables where table_schema='{}' and table_name='{}'"
219 + result=self.open_api.engine_minute.execute(query.format(db_name,table_name)).fetchall()
220 + if len(result)==1:
221 + return True
222 + else:
223 + return False
224 +
225 + def update_today_profit_list(self):
226 + self.open_api.reset_opt10073_output()
227 +
228 + self.open_api.set_input_value("계좌번호",self.open_api.account_no)
229 + self.open_api.set_input_value("시작일자",self.open_api.today)
230 + self.open_api.set_input_value("종료일자",self.open_api.today)
231 + self.open_api.comm_rq_data("opt10073_req","opt10073",0,"0328")
232 +
233 + while self.open_api.remained_data:
234 + self.open_api.set_input_value("계좌번호",self.open_api.account_no)
235 + self.open_api.comm_rq_data("opt10073_req","opt10073",2,"0328")
236 +
237 + today_profit_item_temp = {'date': [], 'code': [], 'code_name': [], 'amount': [], 'today_profit': [],
238 + 'earning_rate': []}
239 +
240 + today_profit_item = DataFrame(today_profit_item_temp,
241 + columns=['date', 'code', 'code_name', 'amount', 'today_profit',
242 + 'earning_rate'])
243 +
244 + item_count = len(self.open_api.opt10073_output['multi'])
245 + for i in range(item_count):
246 + row = self.open_api.opt10073_output['multi'][i]
247 + today_profit_item.loc[i, 'date'] = row[0]
248 + today_profit_item.loc[i, 'code'] = row[1]
249 + today_profit_item.loc[i, 'code_name'] = row[2]
250 + today_profit_item.loc[i, 'amount'] = int(row[3])
251 + today_profit_item.loc[i, 'today_profit'] = float(row[4])
252 + today_profit_item.loc[i, 'earning_rate'] = float(row[5])
253 +
254 + if len(today_profit_item) > 0:
255 + today_profit_item.to_sql('today_profit_list', self.engine_bot, if_exists='append')
256 + query = "UPDATE setting_data SET today_profit='%s' limit 1"
257 + self.engine_bot.execute(query % (self.open_api.today))
258 +
259 + def check_daily_info(self):
260 + self.update_daily_info()
261 + query="update setting_data set daily_info='%s'"
262 + self.engine_bot.execute(query%(self.open_api.today))
263 +
264 + def update_daily_info(self):
265 + query="select code,code_name,check_stock from stock_all"
266 + code_list=self.open_api.engine_daily.execute(query).fetchall()
267 + num=len(code_list)
268 + query="update stock_all set check_stock='%s' where code='%s'"
269 + for i in range(num):
270 + if int(code_list[i][2] in (1,3)):
271 + continue
272 +
273 + code=code_list[i][0]
274 + code_name=code_list[i][1]
275 +
276 + check_item_sort=self.set_minute_info_table(code,code_name)
277 +
278 + self.open_api.engine_daily.execute(query%(check_item_sort,code))
279 +
280 + def set_minute_info_table(self,code,code_name):
281 + df=self.open_api.
31 282
32 - def set_db_minute_info(self):
33 - print("Make Minute Info Database")
34 - query="select code,code_name from stock_all"
35 - target=self.open_api.engine_dInfo.execute(query).fetchall()
36 - print(target)
37 283
38 -app = QApplication(sys.argv)
39 -c=CollectorApi()
40 -c.update_code()
...\ No newline at end of file ...\ No newline at end of file
284 +app=QApplication(sys.argv)
285 +c=Collector_Api()
286 +c.get_code_list()
...\ No newline at end of file ...\ No newline at end of file
......
1 +from sqlalchemy import *
2 +
3 +from open_api import *
4 +from daily_info import *
5 +from stock_info import *
6 +import config
7 +
8 +class CollectorApi():
9 + def __init__(self):
10 + self.open_api=OpenApi()
11 + self.engine_bot=self.open_api.engine_bot
12 +
13 + def set_variable(self):
14 + self.open_api.sort="collector"
15 + self.stock_info=StockInfo(config.real_bot,config.real_stockInfo,config.real_dailyInofo)
16 + self.daily_info=DailyInfo()
17 +
18 + def update_code(self):
19 + print("update code")
20 + query = "select code_update,jango_data_db_check, possessed_item, today_profit, final_chegyul_check, " \
21 + "db_to_buy_list,today_buy_list, daily_crawler , min_crawler, daily_buy_list " \
22 + "from setting_data limit 1"
23 + result=self.engine_bot.execute(query).fetchall()
24 +
25 + print(result)
26 +
27 + if result[0][0]!=self.open_api.today():
28 + self.open_api.check_balance()
29 + self.get_code_list()
30 +
31 +
32 + def set_db_minute_info(self):
33 + print("Make Minute Info Database")
34 + query="select code,code_name from stock_all"
35 + target=self.open_api.engine_dInfo.execute(query).fetchall()
36 + print(target)
37 +
38 +app = QApplication(sys.argv)
39 +c=CollectorApi()
40 +c.update_code()
...\ No newline at end of file ...\ No newline at end of file
1 import datetime 1 import datetime
2 from sqlalchemy import * 2 from sqlalchemy import *
3 -import pandas as pd
4 from pandas import DataFrame 3 from pandas import DataFrame
5 4
6 import config 5 import config
7 6
8 -class DailyInfo(): 7 +class Daily_Info():
9 def __init__(self): 8 def __init__(self):
10 - self.date_setting() 9 + self.set_variable()
11 - self.engine_setting()
12 - self.create_stocks()
13 10
14 - # 날짜 정보 설정 11 + def set_variable(self):
15 - def date_setting(self):
16 self.today=datetime.datetime.today().strftime("%Y%m%d") 12 self.today=datetime.datetime.today().strftime("%Y%m%d")
13 + self.today_time=datetime.datetime.today().strftime("%Y%m%d%H%M")
17 self.start_date=config.start_buying 14 self.start_date=config.start_buying
18 -
19 - # 데이터베이스 엔진 설정
20 - def engine_setting(self):
21 self.engine_daily=create_engine( 15 self.engine_daily=create_engine(
22 "mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + ":" + config.db_port + 16 "mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + ":" + config.db_port +
23 "/daily_info", encoding='utf-8') 17 "/daily_info", encoding='utf-8')
...@@ -25,6 +19,13 @@ class DailyInfo(): ...@@ -25,6 +19,13 @@ class DailyInfo():
25 "mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + ":" + config.db_port + 19 "mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + ":" + config.db_port +
26 "/stock_info", encoding='utf-8') 20 "/stock_info", encoding='utf-8')
27 21
22 + # 지정한 날짜부터 현재까지의 날짜 리스트를 반환
23 + # 모든 지정한 날짜에 대해 정확한 값을 반환하기 위해
24 + # 한국에서 가장 오래된 상장기업 중 하나인 'CJ대한통운' 테이블을 활용한다
25 + def set_date_rows(self):
26 + query="select date from 'CJ대한통운' where date>=%s group by date"
27 + self.date_rows=self.engine_stock.execute(query%self.start_date)
28 +
28 # date에 해당하는 이름을 가진 테이블이 daily_info 데이터베이스 안에 존재하는지 확인하는 함수 29 # date에 해당하는 이름을 가진 테이블이 daily_info 데이터베이스 안에 존재하는지 확인하는 함수
29 def is_date_table_exist(self,date): 30 def is_date_table_exist(self,date):
30 query = "select 1 from information_schema.tables where table_schema ='daily_info' and table_name = '%s'" 31 query = "select 1 from information_schema.tables where table_schema ='daily_info' and table_name = '%s'"
...@@ -34,19 +35,21 @@ class DailyInfo(): ...@@ -34,19 +35,21 @@ class DailyInfo():
34 else: 35 else:
35 return True 36 return True
36 37
38 + # 날짜에 해당하는 테이블 생성
37 def create_daily_table(self): 39 def create_daily_table(self):
38 print("setting daily_info database!!") 40 print("setting daily_info database!!")
39 - self.get_date_list() 41 + self.set_date_rows()
42 + self.get_all_stock_list()
40 43
41 - for date in self.date_list: 44 + for date in self.date_rows:
42 if not self.is_date_table_exist(date): 45 if not self.is_date_table_exist(date):
43 print(date,"테이블이 존재하지 않습니다. 테이블을 생성합니다") 46 print(date,"테이블이 존재하지 않습니다. 테이블을 생성합니다")
44 47
45 daily_list=list() 48 daily_list=list()
46 49
47 - for i in range(len(self.all_list)): 50 + for i in range(len(self.stock_all)):
48 - code_name=self.all_list.loc[i][0] 51 + code_name=self.stock_all.loc[i][0]
49 - code=self.all_list.loc[i][1] 52 + code=self.stock_all.loc[i][1]
50 53
51 if self.is_stock_table_exist(code,code_name): 54 if self.is_stock_table_exist(code,code_name):
52 query="select * from {} where date='{}' group by date" 55 query="select * from {} where date='{}' group by date"
...@@ -75,70 +78,10 @@ class DailyInfo(): ...@@ -75,70 +78,10 @@ class DailyInfo():
75 ]) 78 ])
76 df.to_sql(name=date,con=self.engine_daily,if_exists='replace') 79 df.to_sql(name=date,con=self.engine_daily,if_exists='replace')
77 80
78 - # 지정한 날짜부터 현재까지의 날짜 리스트를 반환 81 + # 데이터베이스에 저장된 모든 주식 리스트를 가져오는 함수
79 - # 모든 지정한 날짜에 대해 정확한 값을 반환하기 위해 82 + def get_all_stock_list(self):
80 - # 한국에서 가장 오래된 상장기업 중 하나인 'CJ대한통운' 테이블을 활용한다 83 + query="select code_name,code from stock_all"
81 - def get_date_list(self): 84 + self.stock_all=self.engine_daily.execute(query).fetchall()
82 - query="select date from 'CJ대한통운' where date>=%s group by date"
83 - self.date_list=self.engine_stock.execute(query%self.start_date)
84 - print(self.date_list)
85 -
86 - # 코스피 주식 리스트 저장
87 - def get_item_kospi(self):
88 - print("get_item_kospi!!")
89 - self.kospi_list = pd.read_html(
90 - 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13&marketType=stockMkt',
91 - header=0)[0]
92 -
93 - self.kospi_list.종목코드 = self.kospi_list.종목코드.map('{:06d}'.format)
94 - self.kospi_list = self.kospi_list[['회사명', '종목코드']]
95 - self.kospi_list = self.kospi_list.rename(columns={'회사명': 'code_name', '종목코드': 'code'})
96 -
97 - # 코스닥 주식 리스트 저장
98 - def get_item_kosdaq(self):
99 - print("get_item_kosdaq!!")
100 - self.kosdaq_list = pd.read_html(
101 - 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13&marketType=kosdaqMkt',
102 - header=0)[0]
103 -
104 - self.kosdaq_list.종목코드 = self.kosdaq_list.종목코드.map('{:06d}'.format)
105 - self.kosdaq_list = self.kosdaq_list[['회사명', '종목코드']]
106 - self.kosdaq_list = self.kosdaq_list.rename(columns={'회사명': 'code_name', '종목코드': 'code'})
107 -
108 - # 코스피 테이블 생성
109 - def create_kospi_table(self):
110 - self.get_item_kospi()
111 - table_kospi='stock_kospi'
112 - query="select 1 from information_schema.tables where table_schema ='daily_info' and table_name = '%s'"
113 - result=self.engine_daily.execute(query%table_kospi).fetchall()
114 - if len(result)==0:
115 - df=DataFrame(self.kospi_list)
116 - df.to_sql(name=table_kospi,con=self.engine_daily,if_exists='replace')
117 -
118 - # 코스닥 테이블 생성
119 - def create_kosdaq_table(self):
120 - self.get_item_kosdaq()
121 - table_kosdaq="stock_kosdaq"
122 - query = "select 1 from information_schema.tables where table_schema ='daily_info' and table_name = '%s'"
123 - result=self.engine_daily.execute(query%table_kosdaq).fetchall()
124 - if len(result)==0:
125 - df=DataFrame(self.kosdaq_list)
126 - df.to_sql(name=table_kosdaq,con=self.engine_daily,if_exists='replace')
127 -
128 - # 코스피 + 코스닥 테이블 생성
129 - def create_stock_all_table(self):
130 - self.all_list=pd.concat([self.kospi_list,self.kosdaq_list],ignore_index=True)
131 - table_all="stock_all"
132 - query = "select 1 from information_schema.tables where table_schema ='daily_info' and table_name = '%s'"
133 - result=self.engine_daily.execute(query%table_all).fetchall()
134 - if len(result)==0:
135 - self.all_list.to_sql(name=table_all,con=self.engine_daily,if_exists='replace')
136 -
137 - # 주식 리스트 생성
138 - def create_stocks(self):
139 - self.create_kospi_table()
140 - self.create_kosdaq_table()
141 - self.create_stock_all_table()
142 85
143 # stock info 데이터베이스에 code_name 테이블이 존재하는지 확인하는 함수 86 # stock info 데이터베이스에 code_name 테이블이 존재하는지 확인하는 함수
144 def is_stock_table_exist(self,code,code_name): 87 def is_stock_table_exist(self,code,code_name):
......
...@@ -124,7 +124,7 @@ class Open_Api(QAxWidget): ...@@ -124,7 +124,7 @@ class Open_Api(QAxWidget):
124 self._opw00018(sRQName,sTrCode) 124 self._opw00018(sRQName,sTrCode)
125 elif sRQName == "opt10074_req": 125 elif sRQName == "opt10074_req":
126 logger.debug("일자별실현손익요청") 126 logger.debug("일자별실현손익요청")
127 - self._opt10084(sRQName,sTrCode) 127 + self._opt10074(sRQName,sTrCode)
128 elif sRQName == "opw00015_req": 128 elif sRQName == "opw00015_req":
129 logger.debug("위탁종합거래내역요청") 129 logger.debug("위탁종합거래내역요청")
130 self._opw00015(sRQName,sTrCode) 130 self._opw00015(sRQName,sTrCode)
...@@ -201,7 +201,7 @@ class Open_Api(QAxWidget): ...@@ -201,7 +201,7 @@ class Open_Api(QAxWidget):
201 # 조회요청시 TR의 Input값을 지정하는 함수 201 # 조회요청시 TR의 Input값을 지정하는 함수
202 # param : sId - TR에 명시된 Input이름 202 # param : sId - TR에 명시된 Input이름
203 # svalue - Input이름으로 지정한 값 203 # svalue - Input이름으로 지정한 값
204 - def _set_input_value(self,sId,sValue): 204 + def set_input_value(self,sId,sValue):
205 try: 205 try:
206 self.dynamicCall("SetInputValue(QString, QString)", sId, sValue) 206 self.dynamicCall("SetInputValue(QString, QString)", sId, sValue)
207 except Exception as e: 207 except Exception as e:
...@@ -213,7 +213,7 @@ class Open_Api(QAxWidget): ...@@ -213,7 +213,7 @@ class Open_Api(QAxWidget):
213 # sTrCode - 조회하려는 TR이름 213 # sTrCode - 조회하려는 TR이름
214 # nPrevNext - 연속조회여부 214 # nPrevNext - 연속조회여부
215 # sScreenNo - 화면번호 215 # sScreenNo - 화면번호
216 - def _comm_rq_data(self,sRQName,sTrData,nPrevNext,sScrNo): 216 + def comm_rq_data(self,sRQName,sTrData,nPrevNext,sScrNo):
217 self.dynamicCall("CommRqData(QString, QString, int, QString", sRQName, sTrData, nPrevNext, sScrNo) 217 self.dynamicCall("CommRqData(QString, QString, int, QString", sRQName, sTrData, nPrevNext, sScrNo)
218 self.tr_event_loop.exec_() 218 self.tr_event_loop.exec_()
219 219
...@@ -241,18 +241,27 @@ class Open_Api(QAxWidget): ...@@ -241,18 +241,27 @@ class Open_Api(QAxWidget):
241 # 실전투자인지 모의투자인지 여부를 확인하고 그에 해당하는 데이터베이스를 생성하는 함수 241 # 실전투자인지 모의투자인지 여부를 확인하고 그에 해당하는 데이터베이스를 생성하는 함수
242 def set_variable(self): 242 def set_variable(self):
243 self.config=config 243 self.config=config
244 + self.reset_opw00018_output()
245 +
244 if self.account_no==self.config.real_account_no: 246 if self.account_no==self.config.real_account_no:
245 logger.debug("실전투자") 247 logger.debug("실전투자")
246 self.simul_num=self.config.real_num 248 self.simul_num=self.config.real_num
247 self.set_database(self.config.real_bot_name) 249 self.set_database(self.config.real_bot_name)
250 + self.mod_classify=100
251 +
248 elif self.account_no==self.config.test_account_no: 252 elif self.account_no==self.config.test_account_no:
249 logger.debug("모의투자") 253 logger.debug("모의투자")
250 self.simul_num=self.config.test_num 254 self.simul_num=self.config.test_num
251 self.set_database(self.config.test_bot_name) 255 self.set_database(self.config.test_bot_name)
256 + self.mod_classify=1
257 +
252 else: 258 else:
253 logger.debug("Invalid Account Number. Check the Config.py") 259 logger.debug("Invalid Account Number. Check the Config.py")
254 exit(1) 260 exit(1)
255 261
262 + self.is_balance_null=True
263 + self.use=False
264 +
256 # 데이터베이스 생성 및 엔진 설정 265 # 데이터베이스 생성 및 엔진 설정
257 def set_database(self,db_name): 266 def set_database(self,db_name):
258 self.db_name=db_name 267 self.db_name=db_name
...@@ -319,12 +328,14 @@ class Open_Api(QAxWidget): ...@@ -319,12 +328,14 @@ class Open_Api(QAxWidget):
319 328
320 def create_setting_data(self): 329 def create_setting_data(self):
321 df_data={"limit_money":[],"per_invest":[],"max_per_invest":[],"min_per_invest":[],"set_per_invest":[], 330 df_data={"limit_money":[],"per_invest":[],"max_per_invest":[],"min_per_invest":[],"set_per_invest":[],
322 - "code_update":[],"today_finish":[],"balance_to_db":[],"posses_stocks":[],"today_profit":[], 331 + "code_update":[],"today_finish":[],"balance_to_db":[],"posses_stocks":[],"today_profit":[],
323 - "contract_check":[],"db_to_daily_info":[],"today_buy_list":[],"stock_info":[],"daily_info":[]} 332 + "contract_check":[],"db_to_daily_info":[],"today_buy_list":[],"stock_info":[],"min_info":[],
333 + "daily_info":[]}
324 df_setting_data=DataFrame(df_data, 334 df_setting_data=DataFrame(df_data,
325 columns=['limit_money','per_invest','max_per_invest','min_per_invest','set_per_invest', 335 columns=['limit_money','per_invest','max_per_invest','min_per_invest','set_per_invest',
326 'code_update','today_finish','balance_to_db','posses_stocks','today_profit', 336 'code_update','today_finish','balance_to_db','posses_stocks','today_profit',
327 - 'contract_check','db_to_daily_info','today_buy_list','stock_info','daily_info']) 337 + 'contract_check','db_to_daily_info','today_buy_list','stock_info','min_info',
338 + 'daily_info'])
328 339
329 df_setting_data.loc[0,'limit_money']=int(0) 340 df_setting_data.loc[0,'limit_money']=int(0)
330 df_setting_data.loc[0,'per_invest']=int(0) 341 df_setting_data.loc[0,'per_invest']=int(0)
...@@ -342,10 +353,13 @@ class Open_Api(QAxWidget): ...@@ -342,10 +353,13 @@ class Open_Api(QAxWidget):
342 df_setting_data.loc[0,'db_to_daily_info']=str(0) 353 df_setting_data.loc[0,'db_to_daily_info']=str(0)
343 df_setting_data.loc[0,'today_buy_list']=str(0) 354 df_setting_data.loc[0,'today_buy_list']=str(0)
344 df_setting_data.loc[0,'stock_info']=str(0) 355 df_setting_data.loc[0,'stock_info']=str(0)
356 + df_setting_data.loc[0,'min_info']=str(0)
357 +
345 df_setting_data.loc[0,'daily_info']=str(0) 358 df_setting_data.loc[0,'daily_info']=str(0)
346 359
347 df_setting_data.to_sql("setting_data",self.engine_bot,if_exists="replace") 360 df_setting_data.to_sql("setting_data",self.engine_bot,if_exists="replace")
348 361
362 + # 시뮬레이터에서 사용할 변수들을 설정
349 def set_simul_variable(self): 363 def set_simul_variable(self):
350 self.latest_date=self.simul_api.get_latest_date() 364 self.latest_date=self.simul_api.get_latest_date()
351 if not self.simul_api.is_table_exist(self.db_name,"transaction_history"): 365 if not self.simul_api.is_table_exist(self.db_name,"transaction_history"):
...@@ -354,6 +368,12 @@ class Open_Api(QAxWidget): ...@@ -354,6 +368,12 @@ class Open_Api(QAxWidget):
354 self.create_transaction_history(0,0,0,0,0) 368 self.create_transaction_history(0,0,0,0,0)
355 self.delete_item_by_code(0) 369 self.delete_item_by_code(0)
356 370
371 + if not self.check_per_invest():
372 + self.set_per_invest()
373 + else:
374 + self.per_invest=self.get_per_invest()
375 + self.simul_api.per_invest=self.per_invest
376 +
357 # transaction_history 테이블 생성 및 column 설정 377 # transaction_history 테이블 생성 및 column 설정
358 def create_transaction_history(self,order_num,code,contract_check,purchase_price,rate): 378 def create_transaction_history(self,order_num,code,contract_check,purchase_price,rate):
359 logger.debug("creating transaction_history table") 379 logger.debug("creating transaction_history table")
...@@ -384,30 +404,55 @@ class Open_Api(QAxWidget): ...@@ -384,30 +404,55 @@ class Open_Api(QAxWidget):
384 else: 404 else:
385 return False 405 return False
386 406
407 + # 데이터베이스에서 per_invest항목을 반환
408 + def get_per_invest(self):
409 + query="select per_invest from setting_data"
410 + result=self.engine_bot.execute(query).fetchall()
411 + return result[0][0]
412 +
413 + # per_invest 항목 설정
414 + # per_ invest : 한 종목 당 투자할 금액
387 def set_per_invest(self): 415 def set_per_invest(self):
388 self.get_deposit() 416 self.get_deposit()
417 + self.get_balance()
418 + self.total_invest=self.deposit+self.total_purchase
419 +
420 + self.per_invest=self.simul_api.per_invest
421 + query="update setting_data set per_invest='%s', set_per_invest='%s'"
422 + self.engine_bot.execute(query%(self.per_invest,self.today))
389 423
390 # 예수금 조회 및 저장 424 # 예수금 조회 및 저장
391 def get_deposit(self): 425 def get_deposit(self):
392 - self._set_input_value("계좌번호",self.account_no) 426 + self.set_input_value("계좌번호",self.account_no)
393 - self._set_input_value("비밀번호입력매체구분",00) 427 + self.set_input_value("비밀번호입력매체구분",00)
394 - self._set_input_value("조회구분",1) 428 + self.set_input_value("조회구분",1)
395 - self._comm_rq_data("opw00001_req","opw00001",0,"2000") 429 + self.comm_rq_data("opw00001_req","opw00001",0,"2000")
396 430
397 # 예수금 상세현황요청 함수 431 # 예수금 상세현황요청 함수
398 def _opw00001(self,sRQName,sTrCode): 432 def _opw00001(self,sRQName,sTrCode):
399 self.deposit=self._get_comm_data(sTrCode,sRQName,0,"d+2출금가능금액") 433 self.deposit=self._get_comm_data(sTrCode,sRQName,0,"d+2출금가능금액")
400 self.deposit=int(self.deposit) 434 self.deposit=int(self.deposit)
401 435
436 + # 잔고 조회 및 저장
402 def get_balance(self): 437 def get_balance(self):
403 - self._set_input_value("계좌번호",self.account_no) 438 + self.reset_opw00018_output()
404 - self._comm_rq_data("opw00018_req","opw00018",0,"2000") 439 + self.set_input_value("계좌번호",self.account_no)
440 + self.comm_rq_data("opw00018_req","opw00018",0,"2000")
405 # 다음페이지가 존재할 경우 계속해서 조회 441 # 다음페이지가 존재할 경우 계속해서 조회
406 while self.remained_data: 442 while self.remained_data:
407 - self._set_input_value("계좌번호",self.account_no) 443 + self.set_input_value("계좌번호",self.account_no)
408 - self._comm_rq_data("opw00018_req","opw00018",2,"2000") 444 + self.comm_rq_data("opw00018_req","opw00018",2,"2000")
445 +
446 + # 계좌평가 잔고내역을 저장하는 변수 초기화
447 + def reset_opw00018_output(self):
448 + self.opw00018_output={'single':[],'multi':[]}
449 +
409 450
451 + # 계좌평가 잔고내역 요청
452 + # 싱글데이터를 통해 계좌에 대한 평가 잔고 데이터를 제공
453 + # 멀티데이터를 통해 보유 종목별 평가 잔고 데이터를 제공
410 def _opw00018(self,sRQName,sTrCode): 454 def _opw00018(self,sRQName,sTrCode):
455 + # 계좌평가 잔고 데이터 - 싱글데이터 저장
411 self.total_purchase=self._get_comm_data(sTrCode,sRQName,0,"총매입금액") 456 self.total_purchase=self._get_comm_data(sTrCode,sRQName,0,"총매입금액")
412 self.total_evaluated_price=self._get_comm_data(sTrCode,sRQName,0,"총평가금액") 457 self.total_evaluated_price=self._get_comm_data(sTrCode,sRQName,0,"총평가금액")
413 self.total_valuation=self._get_comm_data(sTrCode,sRQName,0,"총평가손익금액") 458 self.total_valuation=self._get_comm_data(sTrCode,sRQName,0,"총평가손익금액")
...@@ -420,7 +465,134 @@ class Open_Api(QAxWidget): ...@@ -420,7 +465,134 @@ class Open_Api(QAxWidget):
420 self.earning_rate=float(self.earning_rate) 465 self.earning_rate=float(self.earning_rate)
421 self.estimated_deposit=int(self.estimated_deposit) 466 self.estimated_deposit=int(self.estimated_deposit)
422 467
468 + self.opw00018_output['single'].append(self.total_purchase)
469 + self.opw00018_output['single'].append(self.total_evaluated_price)
470 + self.opw00018_output['single'].append(self.total_valuation)
471 + self.opw00018_output['single'].append(self.earning_rate)
472 + self.opw00018_output['single'].append(self.estimated_deposit)
473 +
474 + # 종목별 평가 잔고 데이터 - 멀티데이터 저장
475 + rows=self._get_repeat_cnt(sTrCode,sRQName)
476 +
477 + for i in range(rows):
478 + code=self._get_comm_data(sTrCode,sRQName,i,"종목번호")
479 + name=self._get_comm_data(sTrCode,sRQName,i,"종목명")
480 + quantity=self._get_comm_data(sTrCode,sRQName,i,"보유수량")
481 + purchase_price=self._get_comm_data(sTrCode,sRQName,i,"매입가")
482 + current_price=self._get_comm_data(sTrCode,sRQName,i,"현재가")
483 + total_valuation=self._get_comm_data(sTrCode,sRQName,i,"평가손익")
484 + earning_rate=self._get_comm_data(sTrCode,sRQName,i,"수익률(%)")
485 + total_purchase=self._get_comm_data(sTrCode,sRQName,i,"매입금액")
486 +
487 + code=code[1:]
488 + quantity=int(quantity)
489 + purchase_price=int(purchase_price)
490 + current_price=int(current_price)
491 + total_valuation=int(total_valuation)
492 + earning_rate=float(earning_rate)
493 + total_purchase=int(total_purchase)
494 +
495 + self.opw00018_output['multi'].append(
496 + [name,quantity,purchase_price,current_price,total_valuation,earning_rate,total_purchase,code]
497 + )
498 +
499 + # 일자별 실현 손익 요청
500 + def _opt10074(self,sRQName,sTrCode):
501 + self.total_profit=self._get_comm_data(sTrCode,sRQName,0,"실현손익")
502 + self.today_profit=self._get_comm_data(sTrCode,sRQName,0,"당일매도손익")
503 +
504 + # open_api를 통해 보유한 종목을 가져오는 함수
505 + # 가져온 정보를 posses_item이라는 테이블에 저장
506 + def get_posses_item(self):
507 + item_count=len(self.opw00018_output['multi'])
508 + posses_item_data={'date':[],'code':[],'code_name':[],'holding_amount':[],'purchase_price':[],
509 + 'present_price':[],'valuation_profit':[],'rate':[],'item_total_purchase':[]}
510 +
511 + posses_item=DataFrame(posses_item_data,
512 + columns=['date','code','code_name','holding_amount','purchase_price',
513 + 'present_price','valuation_profit','rate','item_total_purchase'])
514 +
515 + for i in range(item_count):
516 + item=self.opw00018_output['multi'][i]
517 +
518 + posses_item.loc[i,'date']=self.today
519 + posses_item.loc[i,'code']=item[7]
520 + posses_item.loc[i,'code_name']=item[0]
521 + posses_item.loc[i,'holding_amount']=int(item[1])
522 + posses_item.loc[i,'purchase_price']=int(item[2])
523 + posses_item.loc[i,'present_price']=int(item[3])
524 + posses_item.loc[i,'valuation_profit']=int(item[4])
525 + posses_item.loc[i,'rate']=float(item[5])
526 + posses_item.loc[i,'item_total_purchase']=int(item[6])
527 +
528 + posses_item.to_sql("posses_item",self.engine_bot,if_exists='replace')
529 + self.contract_sync()
530 +
531 + # 현재 소유하고 있는 종목에 대해 transaction_history 테이블을 업데이트
532 + def contract_sync(self):
533 + query="select code,code_name,rate from posses_item p" \
534 + "where p.code not in (select a.code from transaction_history a" \
535 + "where a.sell_date='0' group by a.code)" \
536 + "group by p.code"
537 + result=self.engine_bot.execute(query).fetchall()
538 +
539 + for item in result:
540 + self.set_input_value("종목코드",item.code)
541 + self.set_input_value("조회구분",1)
542 + self.set_input_value("계좌번호",self.account_no)
543 + self.comm_rq_data("opt10076_req","opt10076",0,"0350")
544 +
545 + if self.not_contract['주문구분']=="+매수":
546 + if self.not_contract['미체결수량']==0:
547 + contract_check=0
548 + else:
549 + contract_check=1
550 + elif self.not_contract['주문구분']=='':
551 + self.create_transaction_history(self.today,item.code,0,0,item.rate)
552 + continue
553 + else:
554 + continue
555 +
556 + self.create_transaction_history(self.not_contract['주문번호'],item.code,contract_check,self.not_contract['체결가'],item.rate)
557 +
558 + def _opt10076(self,sRQName,sTrCode):
559 + outputs=['주문번호','종목명','주문구분','주문가격','주문수량','체결가','체결량','미체결수량',
560 + '당일매매수수료','당일매매세금','주문상태','매매구분','원주문번호','주문시간','종목코드']
561 + self.not_contract={}
562 +
563 + for key in outputs:
564 + if key not in ['주문번호','원주문번호','주문시간','종목코드']:
565 + self.not_contract[key]=int(self._get_comm_data(sTrCode,sRQName,0,key))
566 + self.not_contract[key]=self._get_comm_data(sTrCode,sRQName,0,key)
567 +
568 + # posses_item 테이블을 업데이트 했을 경우 setting data 테이블에 업데이트 한 날짜를 표시
569 + def setting_data_posses_stock(self):
570 + query="update setting_data set posses_stocks='%s'"
571 + self.engine_bot.execute(query%self.today)
572 +
573 + def reset_opt10073_output(self):
574 + self.opt10073_output={'single':[],'multi':[]}
575 +
576 + def _opt10073(self,sRQName,sTrCode):
577 + rows=self._get_repeat_cnt(sTrCode,sRQName)
578 +
579 + for i in range(rows):
580 + date=self._get_comm_data(sTrCode,sRQName,i,"일자")
581 + code=self._get_comm_data(sTrCode,sRQName,i,"종목코드")
582 + code_name=self._get_comm_data(sTrCode,sRQName,i,"종목명")
583 + amount=self._get_comm_data(sTrCode,sRQName,i,"체결량")
584 + today_profit=self._get_comm_data(sTrCode,sRQName,i,"당일매도손익")
585 + earning_rate=self._get_comm_data(sTrCode,sRQName,i,"손익율")
586 +
587 + code=code.lstrip('A')
588 +
589 + self.opt10073_output['multi'].append([date,code,code_name,amount,today_profit,earning_rate])
590 +
591 +
592 +
593 +
594 +
595 +
423 if __name__=="__main__": 596 if __name__=="__main__":
424 app=QApplication(sys.argv) 597 app=QApplication(sys.argv)
425 - a=Open_Api()
426 - a.get_balance()
...\ No newline at end of file ...\ No newline at end of file
598 + a=Open_Api()
...\ No newline at end of file ...\ No newline at end of file
......
1 from sqlalchemy import * 1 from sqlalchemy import *
2 from PyQt5.QtCore import * 2 from PyQt5.QtCore import *
3 import datetime 3 import datetime
4 +import pandas as pd
4 5
5 import config 6 import config
6 7
7 -class StockInfo(): 8 +class Stock_Info():
8 - def __init__(self,db_name,stock_info_name,daily_info_name): 9 + def __init__(self,db_name,daily_db_name,stock_db_name):
9 if db_name!=0: 10 if db_name!=0:
10 self.db_name=db_name 11 self.db_name=db_name
11 - self.stockInfo_name=stock_info_name 12 + self.daily_db_name=daily_db_name
12 - self.dailyInfo_name=daily_info_name 13 + self.stock_db_name=stock_db_name
13 14
14 self.engine=create_engine( 15 self.engine=create_engine(
15 "mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + ":" + config.db_port + 16 "mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + ":" + config.db_port +
...@@ -24,11 +25,32 @@ class StockInfo(): ...@@ -24,11 +25,32 @@ class StockInfo():
24 self.mkt_end_time=QTime(15,31,0) 25 self.mkt_end_time=QTime(15,31,0)
25 26
26 self.today=datetime.datetime.today().strftime("%Y%m%d") 27 self.today=datetime.datetime.today().strftime("%Y%m%d")
28 + self.today_time=datetime.datetime.today().strftime("%Y%m%d%H%M")
27 29
28 - # 현재 시간이 주식장이 열린 시간인지 확인하는 함수 30 + # 현재 시간이 주식 장이 열린 시간인지 확인하는 함수
29 - def time_check(self): 31 + def check_time(self):
30 self.current_time=QTime.currentTime() 32 self.current_time=QTime.currentTime()
31 - if (self.current_time>self.mkt_start_time and self.current_time<self.mkt_end_time): 33 + if self.current_time>self.mkt_start_time and self.current_time<self.mkt_end_time:
32 return True 34 return True
33 else: 35 else:
34 - return False
...\ No newline at end of file ...\ No newline at end of file
36 + return False
37 +
38 + # 코스피 주식 리스트 저장
39 + def get_item_kospi(self):
40 + self.kospi_list = pd.read_html(
41 + 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13&marketType=stockMkt',
42 + header=0)[0]
43 +
44 + self.kospi_list.종목코드 = self.kospi_list.종목코드.map('{:06d}'.format)
45 + self.kospi_list = self.kospi_list[['회사명', '종목코드']]
46 + self.kospi_list = self.kospi_list.rename(columns={'회사명': 'code_name', '종목코드': 'code'})
47 +
48 + # 코스닥 주식 리스트 저장
49 + def get_item_kosdaq(self):
50 + self.kosdaq_list = pd.read_html(
51 + 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13&marketType=kosdaqMkt',
52 + header=0)[0]
53 +
54 + self.kosdaq_list.종목코드 = self.kosdaq_list.종목코드.map('{:06d}'.format)
55 + self.kosdaq_list = self.kosdaq_list[['회사명', '종목코드']]
56 + self.kosdaq_list = self.kosdaq_list.rename(columns={'회사명': 'code_name', '종목코드': 'code'})
...\ No newline at end of file ...\ No newline at end of file
......
No preview for this file type