import logging import os import settings import data_manager from policy_learner import PolicyLearner if __name__ == '__main__': stock_code = '005930' # 삼성전자 model_ver = '20180202000545' # 로그 기록 log_dir = os.path.join(settings.BASE_DIR, 'logs/%s' % stock_code) timestr = settings.get_time_str() file_handler = logging.FileHandler(filename=os.path.join( log_dir, "%s_%s.log" % (stock_code, timestr)), encoding='utf-8') stream_handler = logging.StreamHandler() file_handler.setLevel(logging.DEBUG) stream_handler.setLevel(logging.INFO) logging.basicConfig(format="%(message)s", handlers=[file_handler, stream_handler], level=logging.DEBUG) # 주식 데이터 준비 chart_data = data_manager.load_chart_data( os.path.join(settings.BASE_DIR, 'data/chart_data/{}.csv'.format(stock_code))) prep_data = data_manager.preprocess(chart_data) training_data = data_manager.build_training_data(prep_data) # 기간 필터링 training_data = training_data[(training_data['date'] >= '2018-01-01') &
def __init__(self): stock_code='BTCUSDT' global timestr #global 함수로 불러와 바로 사용 model_ver=timestr self.dataclose = self.datas[0].close log_dir = os.path.join(settings.BASE_DIR, 'logs/%s' % stock_code) timestr = settings.get_time_str() if not os.path.exists('logs/%s' % stock_code): os.makedirs('logs/%s' % stock_code) file_handler = logging.FileHandler(filename=os.path.join( log_dir, "%s_%s.log" % (stock_code, timestr)), encoding='utf-8') stream_handler = logging.StreamHandler() file_handler.setLevel(logging.DEBUG) stream_handler.setLevel(logging.INFO) logging.basicConfig(format="%(message)s", handlers=[file_handler, stream_handler], level=logging.DEBUG) chart_data = data_manager.load_chart_data( os.path.join(settings.BASE_DIR, '{}'.format(stock_code))) prep_data = data_manager.preprocess(chart_data) training_data = data_manager.build_training_data(prep_data) training_data = training_data.loc['2018-07-01 01:00:00':] features_chart_data = ['o_t', 'open', 'high', 'low', 'close', 'volume'] chart_data = training_data[features_chart_data] features_training_data = [ 'high_close_ratio', 'low_close_ratio', 'close_lastclose_ratio', 'volume_lastvolume_ratio', 'close_ma5_ratio', 'volume_ma5_ratio', 'close_ma10_ratio', 'volume_ma10_ratio', 'close_ma20_ratio', 'volume_ma20_ratio', 'close_ma60_ratio', 'volume_ma60_ratio', 'close_ma120_ratio', 'volume_ma120_ratio', 'ema12','ema26','dn','mavg','up','pctB','macd','signal','cci' ] training_data = training_data[features_training_data] #print (training_data[:3]) training_data = training_data.dropna(axis=1) chart_data=chart_data.dropna(axis=1) #chart_data = chart_data.loc[:1530352800000] #training_data = training_data.loc[:1530352800000] delayed_reward_threshold=.001 lr=0.1 self.TRADING_TAX =0 self.TRADING_CHARGE=0 self.stock_code = stock_code self.chart_data = chart_data self.environment = Environment(chart_data) self.agent = Agent(self.environment, delayed_reward_threshold=delayed_reward_threshold) self.training_data = training_data self.sample = None self.pvdata=[] self.training_data_idx = -1 self.num_features = self.training_data.shape[1] #+ self.agent.STATE_DIM self.policy_network = PolicyNetwork( input_dim=self.num_features, output_dim=self.agent.NUM_ACTIONS, lr=lr) model_path=os.path.join(settings.BASE_DIR, 'models/{}/model_{}.h5'.format(stock_code,model_ver)) self.policy_network.load_model(model_path=model_path) self.agent.set_balance(self.broker.getcash()) self.epsilon=0 self.num_stocks=0 df=pd.read_csv('out.csv') self.stopit=df.loc[7692]['c_p'] #2903 4135 7692
def main_before_run(before_start_date, before_end_date, before_min_unit, before_max_unit, before_delayed, before_learning, before_balance, before_epoch, before_epsilon): code_list = save_csv.load_skyrocket_list() for i in range(len(code_list)): stock_code = code_list[i] # 로그 기록 log_dir = os.path.join(settings.BASE_DIR, 'result/logs/%s' % stock_code) timestr = settings.get_time_str() if not os.path.isdir(log_dir): os.makedirs(log_dir) file_handler = logging.FileHandler(filename=os.path.join( log_dir, "%s_%s.log" % (stock_code, timestr)), encoding='utf-8') stream_handler = logging.StreamHandler() file_handler.setLevel(logging.DEBUG) stream_handler.setLevel(logging.INFO) logging.basicConfig(format="%(message)s", handlers=[file_handler, stream_handler], level=logging.DEBUG) # 데이터 준비 chart_data = data_management.load_chart_data( os.path.join(settings.BASE_DIR, 'data/csv_data/{}.csv'.format(stock_code))) prep_data = data_management.preprocess(chart_data) training_data = data_management.build_training_data(prep_data) # 기간 필터링 training_data = training_data[(training_data['date'] >= before_start_date) & (training_data['date'] <= before_end_date)] training_data = training_data.dropna() # 차트 데이터 분리 features_chart_data = ['date', 'open', 'high', 'low', 'close', 'volume'] chart_data = training_data[features_chart_data] # 학습 데이터 분리 features_training_data = [ 'open_lastclose_ratio', 'high_close_ratio', 'low_close_ratio', 'close_lastclose_ratio', 'volume_lastvolume_ratio', 'close_ma5_ratio', 'volume_ma5_ratio', 'close_ma10_ratio', 'volume_ma10_ratio', 'close_ma20_ratio', 'volume_ma20_ratio', 'close_ma60_ratio', 'volume_ma60_ratio', 'close_ma120_ratio', 'volume_ma120_ratio'] training_data = training_data[features_training_data] # 강화학습 시작 learner = Learner( stock_code=stock_code, # 종목 코드 chart_data=chart_data, # 차트 데이터 training_data=training_data, # 학습 데이터 min_trading_unit=before_min_unit, # 최소 투자 단위 max_trading_unit=before_max_unit, # 최대 투자 단위 delayed_reward_threshold=before_delayed, # 지연 보상 임계치 lr=before_learning) # 학습 속도 learner.fit(balance=before_balance, # 초기 자본금 num_epoches=before_epoch, # 수행할 Epoch 수 discount_factor=0, # 할인 요인 start_epsilon=before_epsilon) # 초기 탐험률 # 정책 신경망을 파일로 저장 date = datetime.datetime.strftime(datetime.datetime.today(), '%Y%m%d') model_dir = os.path.join(settings.BASE_DIR, 'result/models/%s' % stock_code) if not os.path.isdir(model_dir): os.makedirs(model_dir) model_path = os.path.join(model_dir, 'model_%s.h5' % date) learner.network.save_model(model_path)
def main_after_run(after_start_date, after_min_unit, after_max_unit): code_list = save_csv.load_skyrocket_list() min_trading_unit_buy_list = [] min_trading_unit_sell_list = [] code_list_buy = [] code_list_sell = [] for i in range(len(code_list)): stock_code = code_list[i] # 로그 기록 log_dir = os.path.join(settings.BASE_DIR, 'result/logs/%s' % stock_code) timestr = settings.get_time_str() file_handler = logging.FileHandler(filename=os.path.join( log_dir, "%s_%s.log" % (stock_code, timestr)), encoding='utf-8') stream_handler = logging.StreamHandler() file_handler.setLevel(logging.DEBUG) stream_handler.setLevel(logging.INFO) logging.basicConfig(format="%(message)s", handlers=[file_handler, stream_handler], level=logging.DEBUG) # 데이터 준비 chart_data = data_management.load_chart_data( os.path.join(settings.BASE_DIR, 'data/csv_data/{}.csv'.format(stock_code))) prep_data = data_management.preprocess(chart_data) training_data = data_management.build_training_data(prep_data) # 기간 필터링 today = datetime.datetime.strftime(datetime.datetime.today(), '%Y%m%d') training_data = training_data[ (training_data['date'] >= after_start_date) & (training_data['date'] <= today)] training_data = training_data.dropna() # 차트 데이터 분리 features_chart_data = [ 'date', 'open', 'high', 'low', 'close', 'volume' ] chart_data = training_data[features_chart_data] # 학습 데이터 분리 features_training_data = [ 'open_lastclose_ratio', 'high_close_ratio', 'low_close_ratio', 'close_lastclose_ratio', 'volume_lastvolume_ratio', 'close_ma5_ratio', 'volume_ma5_ratio', 'close_ma10_ratio', 'volume_ma10_ratio', 'close_ma20_ratio', 'volume_ma20_ratio', 'close_ma60_ratio', 'volume_ma60_ratio', 'close_ma120_ratio', 'volume_ma120_ratio' ] training_data = training_data[features_training_data] # 비 학습 투자 시뮬레이션 시작 learner = Learner( stock_code=stock_code, # 종목 코드 chart_data=chart_data, # 차트 데이터 training_data=training_data, # 학습 데이터 min_trading_unit=1, # 최소 투자 단위 max_trading_unit=3) # 최대 투자 단위 # trade() 함수 호출 validity, action, min_trading_unit = learner.trade( balance=10000000, model_path=os.path.join( settings.BASE_DIR, 'result/models/{}/model_{}.h5'.format(stock_code, today))) if validity == True: if action == 0: # 매수 min_trading_unit_buy_list.append(min_trading_unit) code_list_buy.append(stock_code) elif action == 1: # 매도 min_trading_unit_sell_list.append(min_trading_unit) code_list_sell.append(stock_code) # buy_list.txt와 sell_list.txt로 저장 b_list = open(os.path.join(settings.BASE_DIR, "data/list/buy_list.txt"), "wt") for i in range(len(code_list_buy)): b_list.writelines("buy;" + str(code_list_buy[i]) + ";market;" + str(min_trading_unit_buy_list[i]) + ";0;before\n") b_list.close() s_list = open(os.path.join(settings.BASE_DIR, "data/list/sell_list.txt"), "wt") for i in range(len(code_list_sell)): s_list.writelines("sell;" + str(code_list_sell[i]) + ";market;" + str(min_trading_unit_sell_list[i]) + ";0;before\n") s_list.close()