Ejemplo n.º 1
0
 def __init__(self,
              config,
              stockList,
              featureList,
              start_date,
              end_date,
              fake_data,
              restore_dir=None,
              save_path=None,
              agent=None,
              device="cpu"):
     config["training"]["buffer_biased"] = config["trading"][
         "buffer_biased"]
     config["training"]["learning_rate"] = config["trading"][
         "learning_rate"]
     TraderTrainer.__init__(self,
                            config,
                            stockList,
                            featureList,
                            start_date,
                            end_date,
                            fake_data,
                            restore_dir=restore_dir,
                            save_path=save_path,
                            agent=agent,
                            device=device)
Ejemplo n.º 2
0
def train_main(device='cpu'):

    # logging level 설정
    console_level = logging.INFO
    logfile_level = logging.DEBUG

    # net_config.json 파일이 위치한 디렉토리
    config_dir = "train_package"

    # 디렉토리명을 현재 시간으로 설정 및 생성
    now_dt = datetime.datetime.today()
    now_str = now_dt.strftime('%y%m%d_%H%M')
    now_str = 'run' + now_str
    base_log_dir = config_dir + '/' + now_str
    os.makedirs(base_log_dir)

    # 프로그램 로그파일(programlog)와 콘솔 로그 레벨 설정
    # 파일에 출력할 로그와 콘솔에 출력할 로그를 구분한다는 의미임
    logging.basicConfig(filename=base_log_dir + '/programlog', level=logfile_level)
    console = logging.StreamHandler()
    console.setLevel(console_level)
    logging.getLogger().addHandler(console)

    print("training at {} started".format(now_str))
    tt =  TraderTrainer(load_config(), save_path=base_log_dir + '/netfile', device=device)      # load_config() : load json file depending on Python version.
    train_retval = tt.train_net(log_file_dir=base_log_dir + '/tensorboard', index=now_str)
    print("training at {} finished".format(now_str))

    return train_retval
Ejemplo n.º 3
0
def train_one(save_path, config, log_file_dir, index, logfile_level,
              console_level, device):
    """
    train an agent
    :param save_path: the path to save the tensorflow model (.ckpt), could be None
    :param config: the json configuration file
    :param log_file_dir: the directory to save the tensorboard logging file, could be None
    :param index: identifier of this train, which is also the sub directory in the train_package,
    if it is 0. nothing would be saved into the summary file.
    :param logfile_level: logging level of the file
    :param console_level: logging level of the console
    :param device: 0 or 1 to show which gpu to use, if 0, means use cpu instead of gpu
    :return : the Result namedtuple
    """
    if log_file_dir:
        logging.basicConfig(filename=log_file_dir.replace(
            "tensorboard", "programlog"),
                            level=logfile_level)
        console = logging.StreamHandler()
        console.setLevel(console_level)
        logging.getLogger().addHandler(console)
    else:
        print('log_file_dir is not existent')
    print("training at %s started" % index)
    pdb.set_trace()
    trainer = TraderTrainer(config, save_path=save_path,
                            device=device)  #初始化训练器
    return trainer.train_net(log_file_dir=log_file_dir, index=index)  #训练网络
 def __init__(self,
              config,
              restore_dir=None,
              save_path=None,
              agent=None,
              device="cpu"):
     config["training"]["buffer_biased"] = config["trading"][
         "buffer_biased"]
     config["training"]["learning_rate"] = config["trading"][
         "learning_rate"]
     TraderTrainer.__init__(self,
                            config,
                            restore_dir=restore_dir,
                            save_path=save_path,
                            agent=agent,
                            device=device)
Ejemplo n.º 5
0
def train_one(save_path, config, log_file_dir, index, logfile_level,
              console_level, device):
    """
    aim: train an agent
    :param save_path: the path to save the tensorflow model (.ckpt), could be None  存储模型的checkpoints文件
    :param config: the json configuration file  存储config的json文件
    :param log_file_dir: the directory to save the tensorboard logging file, could be None  存储tensorboard的文件夹
    :param index: identifier of this train, which is also the sub directory in the train_package,    训练的子文件夹是第几个?
    if it is 0. nothing would be saved into the summary file.
    :param logfile_level: logging level of the file 
    :param console_level: logging level of the console
    :param device: 0 or 1 to show which gpu to use, if 0, means use cpu instead of gpu
    :return : the Result namedtuple 
    """
    if log_file_dir:
        logging.basicConfig(
            filename=log_file_dir.replace("tensorboard", "programlog"),
            level=logfile_level)  #initialize the logging console
        console = logging.StreamHandler(
        )  # output the logging information to sys.stdout, sys.stderr
        console.setLevel(console_level)
        logging.getLogger().addHandler(console)
    print("training at %s started" % index)
    # train the network and execute backtest
    return TraderTrainer(config, save_path=save_path,
                         device=device).train_net(log_file_dir=log_file_dir,
                                                  index=index)
Ejemplo n.º 6
0
def train_one(save_path, config, log_file_dir, index, logfile_level,
              console_level, device):
    """
    エージェントをトレーニングします。
    :param save_path: TensorFlowのモデル(.ckpt)を保存するパス。None可。
    :param config: jsonのコンフィグファイル
    :param log_file_dir: TensorBoardのログファイルを保存するディレクトリ。None可。
    :param index: トレーニングを一意に定めるインデックス。train_packageのサブディレクトリ名。
    :param logfile_level: ファイルのログレベル
    :param console_level: コンソールのログレベル
    :param device: 0ならCPU、1ならGPU。
    :return: 結果のnamedtuple
    """
    if log_file_dir:
        logging.basicConfig(filename=log_file_dir.replace(
            'tensorboard', 'programlog'),
                            lovel=logfile_level)
        console = logging.StreamHandler()
        console.setLevel(console_level)
        logging.getLogger().addHandler(console)
    print('training at %s started' % index)
    trainer = TraderTrainer(config, save_path=save_path, device=device)
    return trainer.train_net(log_file_dir=log_file_dir, index=index)
@author: Joukey
"""

import os
os.chdir('D:/EIIE_keras/')

from pgportfolio.learn.tradertrainer import TraderTrainer
from pgportfolio.tools.configprocess import load_config

config_2 = load_config(2)
train_dir = "train_package"

s_path = "./" + train_dir + "/" + '2' + "/netfile"
l_path = "./" + train_dir + "/" + '2' + "/tensorboard"

t_trainer = TraderTrainer(config_2, save_path=s_path, device='gpu')

#%%

import logging

console_level = logging.INFO
logfile_level = logging.DEBUG

logging.basicConfig(filename=l_path.replace("tensorboard", "programlog"),
                    level=console_level)
console = logging.StreamHandler()
console.setLevel(console_level)
logging.getLogger().addHandler(console)

t_trainer.train_net(log_file_dir=l_path, index='2')
Ejemplo n.º 8
0
 def __init__(self, config, restore_dir=None, save_path=None, agent=None, device="cpu"):
     config["training"]["buffer_biased"] = config["trading"]["buffer_biased"]
     config["training"]["learning_rate"] = config["trading"]["learning_rate"]
     TraderTrainer.__init__(self, config, restore_dir=restore_dir, save_path=save_path,
                            agent=agent, device=device)
Ejemplo n.º 9
0
def main(logPath, device):
    parser = build_parser()
    options = parser.parse_args()
    '''
    options.folder = oneKey
    options.stockList = stockList
    options.featureList = featureList
    options.start_date = startDate
    options.end_date = endDate
    '''
    if not os.path.exists("./" + "database"):
        os.makedirs("./" + "database")
    #options.repeat = 1
    if options.mode == "train":  #训练数据
        if not options.algo:
            save_path = logPath + str(options.folder) + "/netfile"
            # 读取配置文件
            with open(logPath + str(options.folder) +
                      "\\net_config.json") as file:
                config_json = None
                config_json = json.load(file)
            config = preprocess_config(config_json)
            log_file_dir = logPath + str(options.folder) + "/tensorboard"
            # 定义错误等级
            logfile_level = logging.DEBUG
            console_level = logging.INFO
            logging.basicConfig(filename=log_file_dir.replace(
                "tensorboard", "programlog"),
                                level=logfile_level)
            console = logging.StreamHandler()
            console.setLevel(console_level)
            logging.getLogger().addHandler(console)
            trainer = TraderTrainer(config,
                                    options.stockList,
                                    options.featureList,
                                    options.start_date,
                                    options.end_date,
                                    save_path=save_path,
                                    device=device)  #初始化训练器
            trainer.train_net(log_file_dir=log_file_dir,
                              index=str(options.folder))  #训练网络
        else:
            for folder in options.folder:
                raise NotImplementedError()

    # 生成配置文件到路径中,要想修改配置,直接修改PGPortfolio\pgportfolio\net_config.json
    elif options.mode == "generate":
        import pgportfolio.autotrain.generate as generate
        logging.basicConfig(level=logging.INFO)
        config_ = load_config()
        train_dir = logPath
        generate.add_packages(train_dir, load_config(), int(options.repeat))
    elif options.mode == "download_data":
        from pgportfolio.marketdata.datamatrices import DataMatrices
        with open("./pgportfolio/net_config.json") as file:
            config = json.load(file)
        config = preprocess_config(config)
        start = time.mktime(
            datetime.strptime(options.start_date, "%Y/%m/%d").timetuple())
        end = time.mktime(
            datetime.strptime(options.end_date, "%Y/%m/%d").timetuple())
        DataMatrices(
            start=start,
            end=end,
            feature_number=len(options.featureList),
            window_size=config["input"]["window_size"],
            online=True,
            period=config["input"]["global_period"],
            volume_average_days=config["input"]["volume_average_days"],
            coin_filter=len(options["stockList"]),
            is_permed=config["input"]["is_permed"],
            test_portion=config["input"]["test_portion"],
            portion_reversed=config["input"]["portion_reversed"])
    elif options.mode == "backtest":
        config = _config_by_algo(options.algo)  #读取配置文件
        _set_logging_by_algo(logging.DEBUG, logging.DEBUG, options.algo,
                             "backtestlog")  #设置log的路径
        values = execute_backtest(options.algo, config)  #执行回测的步数为训练集的长度
    elif options.mode == "save_test_data":
        # This is used to export the test data
        save_test_data(load_config(options.folder))  #保存测试集数据
    elif options.mode == "plot":
        logging.basicConfig(level=logging.INFO)
        algos = options.algos.split(",")
        if options.labels:
            labels = options.labels.replace("_", " ")
            labels = labels.split(",")
        else:
            labels = algos
        plot.plot_backtest(load_config(), algos, labels)
    elif options.mode == "table":
        algos = options.algos.split(",")
        if options.labels:
            labels = options.labels.replace("_", " ")
            labels = labels.split(",")
        else:
            labels = algos
        plot.table_backtest(load_config(),
                            algos,
                            labels,
                            format=options.format)