이하영

simulator

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (py38_64)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
......
......@@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Python 3.8 (py38_64)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
......
File mode changed
......@@ -35,7 +35,7 @@ class simulator_api:
self.rotate_date()
else:
logger.error("Invalid option")
logger.error("Invalid simulator number/simulator option")
# 오늘 날짜를 설정하는 함수
def date_setting(self):
......@@ -54,6 +54,9 @@ class simulator_api:
self.buy_stop=False # 거래를 중지하는 변수
self.trade_check_num=False # 실시간 조건 매수 옵션. 분별 시뮬레이팅의 경우 True, 일별 시뮬레이팅의 경우 False
self.use_ai=False # ai(deep learning) 알고리즘 사용 여부
self.ai_num=1 # ai 알고리즘 번호
print("simul_num : ",self.simul_num)
self.volume_limit=1000
......@@ -266,7 +269,7 @@ class simulator_api:
# 시뮬레이팅 할 날짜 리스트를 가져오는 함수
# 장이 열렸던 날을 self.date_rows에 담기 위해 gs글로벌의 date값을 이용
def get_date_for_simul(self):
query = f"select date from `gs글로벌` " \
query = f"select date from `대한항공` " \
f"where date >= '{self.simul_start_date}' and date <= '{self.simul_end_date}' group by date"
self.date_rows = self.engine_daily_craw.execute(query).fetchall()
......@@ -277,12 +280,12 @@ class simulator_api:
return
query="select sum(valuation_profit) from all_stocks"
self.sum_valuation_profit=self.engine_simul.execute(query).fethcall()[0][0] # 총수익금 (종목별 평가 금액의 합)
self.sum_valuation_profit=self.engine_simul.execute(query).fetchall()[0][0] # 총수익금 (종목별 평가 금액의 합)
self.total_invest_price=self.start_invest_price+self.sum_valuation_profit # 총자산
query="select sum(item_total_purchase) from all_stocks where sell_date='%s'"
self.total_purchase_price=self.engine_simul.execute(query%(0)).fethcall()[0][0] # 총투자금액
self.total_purchase_price=self.engine_simul.execute(query%(0)).fetchall()[0][0] # 총투자금액
if self.total_purchase_price is None:
self.total_purchase_price=0
......@@ -436,12 +439,12 @@ class simulator_api:
if self.is_date_exist(date_rows_today) and self.is_date_exist(date_rows_yesterday):
self.db_to_realtime_daily_buy_list(date_rows_today, date_rows_yesterday, i) # 매수리스트 확인
self.trading_by_min(date_rows_today, date_rows_yesterday, i) # 시뮬레이팅 시작
self.db_to_jango(date_rows_today) # 잔고 테이블 업데이트
# all_stocks 테이블이 존재하고, 현재 보유 중인 종목이 있는 경우, 해당 종목에 대해 데이터베이스 업데이트
if self.is_simul_table_exist(self.db_name, "all_stocks") and len(self.get_data_from_possessed_item()) != 0:
self.update_all_db_by_date(date_rows_today, option='ALL')
self.db_to_jango(date_rows_today) # 정산
else:
print("테이블이 존재하지 않습니다")
......@@ -464,13 +467,13 @@ class simulator_api:
realtime_daily_buy_list=None # 다음날 매수할 종목 리스트를 저장하는 변수
# (5,20) 골든크로스
if self.db_to_realtime_daily_buy_list_num == 1:
query=f"select * from '{date_rows_yesterday}' " \
query=f"select * from `{date_rows_yesterday}` " \
f"where yes_clo20>yes_clo5 and clo5>clo20 and close<'{self.invest_unit}' group by code"
realtime_daily_buy_list=self.engine_daily_buy_list.execute(query).fetchall()
# (20,60) 골든크로스
elif self.db_to_realtime_daily_buy_list_num == 2:
query = f"select * from '{date_rows_yesterday}' " \
query = f"select * from `{date_rows_yesterday}` " \
f"where yes_clo40 > yes_clo5 and clo5 > clo40 and close < '{self.invest_unit}' group by code"
realtime_daily_buy_list = self.engine_daily_buy_list.execute(query).fetchall()
......@@ -526,7 +529,7 @@ class simulator_api:
realtime_daily_buy_list=self.engine_simul.execute(query).fetchall()
self.df_realtime_daily_buy_list=DataFrame(realtime_daily_buy_list,
columns=['index', 'index2', 'date', 'check_item', 'code',
columns=['index', 'index2', 'index3','date', 'check_item', 'code',
'code_name', 'd1_diff','d1_diff_rate',
'close', 'open', 'high','low', 'volume',
'clo5', 'clo10', 'clo20', 'clo60', 'clo120',
......@@ -547,11 +550,11 @@ class simulator_api:
self.update_all_db_by_date(date_rows_today,option='OPEN')
# 분별 시간 데이터를 가져오기
self.get_min_data_for_simul(date_rows_today)
if len(self.min_data_rows)!=0:
self.get_date_min_for_simul(date_rows_today)
if len(self.min_date_rows)!=0:
# 분 단위로 시뮬레이팅
for t in range(len(self.min_data_rows)):
min=self.min_data_rows[t][0]
for t in range(len(self.min_date_rows)):
min=self.min_date_rows[t][0]
# 현재 보유 종목이 있는 경우
if self.is_simul_table_exist(self.db_name,"all_stocks") and len(self.get_data_from_possessed_item())!=0:
self.show_info(min)
......@@ -627,13 +630,13 @@ class simulator_api:
# 보유한 종목의 이름을 반환하는 함수
def get_data_from_possessed_item(self):
query="select code_name from all_stocks where sell_date='0'"
result=self.engine_simul.execute(query).fetchall()
return result[0][0]
return self.engine_simul.execute(query).fetchall()
# daily_buy_list를 통해 특정 날짜에 해당하는 주가정보를 가져오는 함수
def get_now_price_by_date(self,code_name,date):
print(code_name)
query = f"select d1_diff,d1_diff_rate, close, open, high, low, volume, clo5, clo10, clo20, clo60, clo120 " \
f"from '{date}' " \
f"from `{date}` " \
f"where code_name = '{code_name}' group by code"
result=self.engine_daily_buy_list.execute(query).fetchall()
......@@ -675,7 +678,7 @@ class simulator_api:
simul_start_date_min=simul_start_date+self.start_min # 장 시작시간 = 9시
simul_end_date_min=simul_start_date+"1530" # 장 마감시간 = 3시 30분
query = f"select date from `gs글로벌` " \
query = f"select date from `대한항공` " \
f"where date >= '{simul_start_date_min}' and date <='{simul_end_date_min}' and open != 0 group by date"
self.min_date_rows = self.engine_craw.execute(query).fetchall()
......@@ -684,16 +687,16 @@ class simulator_api:
possessed_code_name=self.get_data_from_possessed_item()
for i in range(len(possessed_code_name)):
code_name=possessed_code_name[i][0]
current_close_price=self.get_current_close_price_by_min(code_name,min)
current_close_price=self.get_now_close_price_by_min(code_name,min)
if current_close_price:
self.db_to_transaction_update_present_price_by_min(code_name,current_close_price)
self.db_to_all_stocks_present_price_update_by_min(code_name,current_close_price)
else:
continue
# 분별 현재 종가(close)를 가져오는 함수
# 분별 데이터에서 종가는 현재가를 의미하므로 1분마다 시세를 가져오는 함수
def get_current_close_price_by_min(self,code_name,min):
query=f"select close from '{code_name}' " \
def get_now_close_price_by_min(self,code_name,min):
query=f"select close from `{code_name}` " \
f"where date='{min}' and open!=0 and volume!=0 order by sum_volume desc limit 1"
result=self.engine_craw.execute(query).fethcall()
if len(result)==1:
......@@ -703,7 +706,7 @@ class simulator_api:
# 보유한 종목에 현재가를 실시간으로 업데이트하는 함수
def db_to_all_stocks_present_price_update_by_min(self,code_name,current_close_price):
query=f"update all_stocks set present_price={current_close_price} " \
query=f"update all_stocks set present_price='{current_close_price}' " \
f"where code_name={code_name} and sell_date='{0}'"
self.engine_simul.execute(query)
......@@ -874,7 +877,7 @@ class simulator_api:
# 특정 날짜의 테이블에서 특정 종목의 시가를 가져오는 함수
def get_now_open_price_by_date(self,code,date):
query=f"select open from '{date}' where code = '{code}' group by code"
query=f"select open from `{date}` where code = '{code}' group by code"
open=self.engine_daily_buy_list.execute(query).fetchall()
if len(open)==1:
return open[0][0]
......@@ -883,7 +886,7 @@ class simulator_api:
# 전날 종가(close)를 가져오는 함수
def get_yes_close_price_by_date(self,code,date):
query = f"select close from '{date}' where code = '{code}' group by code"
query = f"select close from `{date}` where code = '{code}' group by code"
result = self.engine_daily_buy_list.execute(query).fetchall()
if len(result) == 1:
return result[0][0]
......@@ -892,7 +895,7 @@ class simulator_api:
# 분별 현재 누적 거래량을 가져오는 함수
def get_now_volume_by_min(self,code_name,min):
query= f"select sum_volume from '{code_name}' " \
query= f"select sum_volume from `{code_name}` " \
f"where date = '{min}' and open != 0 and volume !=0 order by sum_volume desc limit 1"
result = self.engine_craw.execute(query).fetchall()
if len(result) == 1:
......@@ -1000,8 +1003,8 @@ class simulator_api:
self.df_all_stocks.loc[0, 'clo5'] = df.loc[index, 'clo5']
self.df_all_stocks.loc[0, 'clo10'] = df.loc[index, 'clo10']
self.df_all_stocks.loc[0, 'clo20'] = df.loc[index, 'clo20']
self.df_all_stocks.loc[0, 'clo60'] = df.loc[index, 'clo40']
self.df_all_stocks.loc[0, 'clo120'] = df.loc[index, 'clo60']
self.df_all_stocks.loc[0, 'clo60'] = df.loc[index, 'clo60']
self.df_all_stocks.loc[0, 'clo120'] = df.loc[index, 'clo120']
if df.loc[index, 'clo5_diff_rate'] is not None:
self.df_all_stocks.loc[0, 'clo5_diff_rate'] = float(df.loc[index, 'clo5_diff_rate'])
......@@ -1212,7 +1215,7 @@ class simulator_api:
# 최근 daily_buy_list의 날짜 테이블에서 code에 해당하는 데이터만 가져오는 함수
def get_daily_buy_list_by_code(self,code,date):
query = f"select * from '{date}' where code = '{code}' group by code"
query = f"select * from `{date}` where code = '{code}' group by code"
daily_buy_list = self.engine_daily_buy_list.execute(query).fetchall()
......@@ -1229,7 +1232,7 @@ class simulator_api:
# 현재 특정 종목의 종가를 가져오는 함수
def get_now_close_price_by_date(self,code,date):
query=f"select close from '{date}' where code='{code}' group by code"
query=f"select close from `{date}` where code='{code}' group by code"
result=self.engine_daily_buy_list.execute(query).fetchall()
if len(result)==1:
......
No preview for this file type
No preview for this file type
......@@ -24,7 +24,7 @@ real_daily_buy_list_db_name = "daily_buy_list"
start_daily_buy_list='20190102'
# api를 최대 몇 번까지 호출 하고 봇을 끌지 설정 하는 옵션
max_api_call=98
max_api_call=998
# openapi 1회 조회 시 대기 시간(0.2 보다-> 0.3이 안정적)
TR_REQ_TIME_INTERVAL = 0.3
......
lxml
PyQt5
pymysql
selenium
numpy
dart_fss
sqlalchemy
pandas
cryptography
\ No newline at end of file
lxml
pymysql
selenium
numpy<1.19.0
sqlalchemy
pandas
dart_fss
PyQt5
cryptography
matplotlib
sklearn
tensorflow==2.3.0
keras==2.4.3
\ No newline at end of file
from library.simulator_api import *
class Simulator:
def __init__(self):
self.set_info()
self.input_value()
def set_info(self):
self.simul_num=int(input("시뮬레이팅 알고리즘 번호 선택 : "))
option=str(input("시뮬레이팅 데이터베이스 초기화 여부 (y/n) : "))
if option=="y":
self.simul_reset='reset'
elif option=="n":
self.simul_reset='continue'
else:
print("Invalid Input")
sys.exit()
def input_value(self):
simulator_api(self.simul_num,self.simul_reset,0)
if __name__=="__main__":
Simulator()
\ No newline at end of file
......@@ -100,6 +100,9 @@ class Trader(QMainWindow):
if self.open_api.jango_check() and self.buy_time_check() and self.open_api.buy_check():
self.auto_trade_stock()
else:
break
if __name__ == "__main__":
app = QApplication(sys.argv)
trader = Trader()
......