Showing
13 changed files
with
187 additions
and
3 deletions
proj/library/Logger.py
0 → 100644
1 | +import logging.handlers | ||
2 | +import sys | ||
3 | + | ||
4 | +# formmater 생성 | ||
5 | +formatter = logging.Formatter('[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s > %(message)s') | ||
6 | + | ||
7 | +# logger 인스턴스를 생성 및 로그 레벨 설정 | ||
8 | +logger = logging.getLogger("crumbs") | ||
9 | +logger.setLevel(logging.DEBUG) | ||
10 | +streamHandler = logging.StreamHandler() | ||
11 | +streamHandler.setFormatter(formatter) | ||
12 | +logger.addHandler(streamHandler) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
proj/library/Simulator_Api.py
0 → 100644
1 | +import datetime | ||
2 | +from sqlalchemy import * | ||
3 | +import pymysql | ||
4 | +from pandas import DataFrame | ||
5 | + | ||
6 | +import config | ||
7 | + | ||
8 | +class Simulator_Api: | ||
9 | + def __init__(self,simul_num,op,db_name): | ||
10 | + self.simul_num=int(simul_num) | ||
11 | + | ||
12 | + if op=="real": | ||
13 | + self.op='real' | ||
14 | + self.simul_reset=False | ||
15 | + self.db_name=db_name | ||
16 | + self.set_variable() | ||
17 | + | ||
18 | + def set_date(self): | ||
19 | + self.today=datetime.datetime.today().strftime("%Y%m%d") | ||
20 | + | ||
21 | + def set_variable(self): | ||
22 | + self.set_date() | ||
23 | + self.set_database() | ||
24 | + | ||
25 | + # 매수/매도 알고리즘 선택 -> 차후 알고리즘 별로 구현 및 재설정 | ||
26 | + self.buy_algorithm=1 | ||
27 | + self.sell_algorithm=1 | ||
28 | + | ||
29 | + # 특정 데이터베이스 내에 특정 테이블이 존재하는지 확인하는 함수 | ||
30 | + 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'" | ||
32 | + result=self.engine_simul.execute(query%(db_name,table_name)).fetchall() | ||
33 | + if len(result)==1: | ||
34 | + return True | ||
35 | + else: | ||
36 | + return False | ||
37 | + | ||
38 | + # 데이터베이스 연결 설정 | ||
39 | + def set_database(self): | ||
40 | + if self.op=="real": | ||
41 | + self.engine_simul=create_engine("mysql+pymysql://"+config.db_id+":"+config.db_pw+"@"+config.db_ip+ | ||
42 | + ":"+config.db_port+"/"+str(self.db_name),encoding='utf-8') | ||
43 | + else: | ||
44 | + self.db_name='simulator'+str(self.simul_num) | ||
45 | + self.engine_simul=create_engine("mysql+pymysql://"+config.db_id+":"+config.db_pw+"@"+config.db_ip+ | ||
46 | + ":"+config.db_port+"/"+str(self.db_name),encoding='utf-8') | ||
47 | + | ||
48 | + self.engine_daily = create_engine("mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + | ||
49 | + ":" + config.db_port + "/daily_info" , encoding='utf-8') | ||
50 | + self.engine_stock = create_engine("mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + | ||
51 | + ":" + config.db_port + "/stock_info", encoding='utf-8') | ||
52 | + self.engine_minute = create_engine("mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + | ||
53 | + ":" + config.db_port + "/minute_info", encoding='utf-8') | ||
54 | + | ||
55 | + self.conn=pymysql.connect( | ||
56 | + host=config.db_ip, | ||
57 | + port=int(config.db_port), | ||
58 | + user=config.db_id, | ||
59 | + password=config.db_pw, | ||
60 | + charset='utf8' | ||
61 | + ) | ||
62 | + | ||
63 | + # 날짜별 주식 데이터를 저장해 놓은 데이터베이스에서 가장 최근 날짜를 가져오는 함수 | ||
64 | + def get_latest_date(self): | ||
65 | + query="select table_name from information_schema.tables where table_schema='daily_info' and " \ | ||
66 | + "table_name like '%s' order by table_name desc limit 1" | ||
67 | + result=self.engine_daily.execute(query%("20%%")).fetchall() | ||
68 | + if len(result)==0: | ||
69 | + return False | ||
70 | + else: | ||
71 | + return result[0][0] | ||
72 | + | ||
73 | + # transaction_history 테이블의 Dataframe 생성 | ||
74 | + def df_transaction_history(self): | ||
75 | + df_temp={'id':[]} | ||
76 | + self.df_th=DataFrame(df_temp, | ||
77 | + columns=['id','order_num','code','code_name','rate','purchase_rate', | ||
78 | + 'purchase_price','present_price','valuation_price','valuation_profit', | ||
79 | + 'holding_amount','buy_date','total_purchase_price','contract_check','per_invest', | ||
80 | + 'sell_date','sell_price','sell_rate' | ||
81 | + ]) | ||
82 | + | ||
83 | + # daily_info 데이터베이스에서 특정 날짜에서 특정 코드에 해당하는 정보만 가져오는 함수수 | ||
84 | + def get_daily_info_by_code(self,code,date): | ||
85 | + query="select * from {} where code='{}' group by code" | ||
86 | + daily_info=self.engine_daily.execute(query.format(date,code)).fetchall() | ||
87 | + df_daily_info=DataFrame(daily_info, | ||
88 | + columns=['index','index2','date','check_item','code', | ||
89 | + 'code_name','dff_rate', | ||
90 | + 'close','open','high','low','volume', | ||
91 | + 'avg5','avg10','avg20','avg60','avg120', | ||
92 | + 'prev_avg5','prev_avg10','prev_avg20','prev_avg60','prev_avg120', | ||
93 | + 'avg5_diff_rate','avg10_diff_rate','avg20_diff_rate','avg60_diff_rate', | ||
94 | + 'avg120_diff_rate', | ||
95 | + 'vol5','vol10','vol20','vol60','vol120']) | ||
96 | + return df_daily_info | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
1 | +from sqlalchemy import * | ||
2 | + | ||
1 | from open_api import * | 3 | from open_api import * |
4 | +from daily_info import * | ||
5 | +from stock_info import * | ||
6 | +import config | ||
2 | 7 | ||
3 | class CollectorApi(): | 8 | class CollectorApi(): |
4 | def __init__(self): | 9 | def __init__(self): |
5 | self.open_api=OpenApi() | 10 | self.open_api=OpenApi() |
6 | self.engine_bot=self.open_api.engine_bot | 11 | self.engine_bot=self.open_api.engine_bot |
7 | 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 | ... | ... |
... | @@ -9,5 +9,16 @@ test_account_no="8147766711" | ... | @@ -9,5 +9,16 @@ test_account_no="8147766711" |
9 | 9 | ||
10 | # 시뮬레이션을 돌리기 시작할 날짜 | 10 | # 시뮬레이션을 돌리기 시작할 날짜 |
11 | start_buying='20190101' | 11 | start_buying='20190101' |
12 | -simul_num1=1 | 12 | + |
13 | -simul_name1="AutoBot"+str(simul_num1)+"_Test" | 13 | +test_num=1 |
14 | +test_bot_name="AutoBot"+str(test_num)+"_Test" | ||
15 | + | ||
16 | +# 실전투자 정보 | ||
17 | +real_account_no="" | ||
18 | + | ||
19 | +real_num=1 | ||
20 | + | ||
21 | +# 데이터베이스 이름 | ||
22 | +real_bot_name="AutoBot"+str(real_num) | ||
23 | +real_stockInfo_name="stock_info" | ||
24 | +real_dailyInfo_name="daily_info" | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
This diff is collapsed. Click to expand it.
1 | +from sqlalchemy import * | ||
2 | +from PyQt5.QtCore import * | ||
3 | +import datetime | ||
4 | + | ||
5 | +import config | ||
6 | + | ||
1 | class StockInfo(): | 7 | class StockInfo(): |
2 | - def __init__(self): | 8 | + def __init__(self,db_name,stock_info_name,daily_info_name): |
9 | + if db_name!=0: | ||
10 | + self.db_name=db_name | ||
11 | + self.stockInfo_name=stock_info_name | ||
12 | + self.dailyInfo_name=daily_info_name | ||
13 | + | ||
14 | + self.engine=create_engine( | ||
15 | + "mysql+pymysql://" + config.db_id + ":" + config.db_pw + "@" + config.db_ip + ":" + config.db_port + | ||
16 | + "/stock_info", encoding='utf-8') | ||
17 | + self.conn=self.engine.connect() | ||
18 | + | ||
19 | + self.set_variable() | ||
20 | + | ||
21 | + # 변수 설정 | ||
22 | + def set_variable(self): | ||
23 | + self.mkt_start_time=QTime(9,0,0) | ||
24 | + self.mkt_end_time=QTime(15,31,0) | ||
25 | + | ||
26 | + self.today=datetime.datetime.today().strftime("%Y%m%d") | ||
27 | + | ||
28 | + # 현재 시간이 주식장이 열린 시간인지 확인하는 함수 | ||
29 | + def time_check(self): | ||
30 | + self.current_time=QTime.currentTime() | ||
31 | + if (self.current_time>self.mkt_start_time and self.current_time<self.mkt_end_time): | ||
32 | + return True | ||
33 | + else: | ||
34 | + return False | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment