Esempio n. 1
0
def main(stocks, target_brand, layer1, layer2, load_sess, result_file=None):
    # 学習データのキャッシュ
    feed_cache = FeedCache(target_brand, str(REMOVE_NEWEST_DAYS))

    # 対象の銘柄名
    target_brand_name = brand_name(target_brand)

    if not feed_cache.is_exist():
        print('Make cache')
        # 株価指標データを読み込む
        all_data = load_exchange_dataframes(stocks, target_brand)

        # 終値を取得
        using_data = get_using_data(all_data, target_brand)

        # データを学習に使える形式に正規化
        log_return_data = get_log_return_data(stocks, using_data)

        # 答と学習データを作る
        training_test_data = build_training_data(stocks, log_return_data,
                                                 target_brand)
        feed_cache.save(training_test_data)
    else:
        print('Exist cache')
        training_test_data = feed_cache.load()

    # 学習データをトレーニング用とテスト用に分割する
    dataset = split_training_test_data(CLASS_COUNT, training_test_data)
    if len(dataset.training_predictors) < TRAIN_MIN:
        print('[{}]{}: 学習データが少なすぎるため計算を中止'.format(target_brand,
                                                  target_brand_name))
        with open('results.csv', 'a') as f:
            f.write('{},{},ERROR\n'.format(target_brand, target_brand_name))

        # レイヤー検証ログに保存
        brands = nikkei225_s
        codes = [code for (code, name, _) in brands]
        layerLog = LayerLog('layer_logs', '{}_{}.csv'.format(layer1, layer2),
                            codes)
        layerLog.add(target_brand, [-1, 0, 0, 0, 0, 0, 0, 0])
        return

    print('[{}]{}'.format(target_brand, target_brand_name))

    # 器械学習のネットワークを作成
    env = smarter_network(stocks, dataset, layer1, layer2)

    # 学習
    train_accuracy, money, trues, falses, actual_count, deal_logs, up_expectation_dates = train(
        load_sess, env, stocks[target_brand], target_brand)

    print('-- テスト --')
    # 各クラスの正解率
    rates = np.zeros(CLASS_COUNT, dtype=np.int64)
    # 各クラスの正解数
    counts = np.zeros(CLASS_COUNT, dtype=np.int64)
    for i in range(CLASS_COUNT):
        counts[i] = trues[i] + falses[i]
        if counts[i]:
            # 各クラスの正解率(予想数 / 正解数)
            rates[i] = int(float(trues[i]) / float(counts[i]) * 100)
    print('下げ正解率    : {}% 予想{}回'.format(rates[CLASS_DOWN], counts[CLASS_DOWN]))
    print('変化なし正解率: {}% 予想{}回'.format(rates[CLASS_NEUTRAL],
                                      counts[CLASS_NEUTRAL]))
    print('上げ正解率    : {}% 予想{}回'.format(rates[CLASS_UP], counts[CLASS_UP]))
    print('上げ予測日    : {}'.format(up_expectation_dates))

    print('-- 売買シミュレーション --')
    print('売買シミュレーション結果 {:,d}円'.format(money))

    # 結果をファイル保存
    if result_file:
        with open(result_file, 'a') as f:
            f.write('{},{},{},{},{},{},{},{},{}\n'.format(
                target_brand, target_brand_name, money, trues[CLASS_DOWN],
                falses[CLASS_DOWN], trues[CLASS_NEUTRAL],
                falses[CLASS_NEUTRAL], trues[CLASS_UP], falses[CLASS_UP]))
    else:
        print(target_brand, target_brand_name, money, trues[CLASS_DOWN],
              falses[CLASS_DOWN], trues[CLASS_NEUTRAL], falses[CLASS_NEUTRAL],
              trues[CLASS_UP], falses[CLASS_UP])

    # 売買履歴をファイルに保存
    save_deal_logs(target_brand, deal_logs)

    # レイヤー検証ログに保存
    brands = nikkei225_s
    codes = [code for (code, name, _) in brands]
    layerLog = LayerLog('layer_logs', '{}_{}.csv'.format(layer1, layer2),
                        codes)
    layerLog.add(target_brand, [
        train_accuracy, money, trues[CLASS_DOWN], falses[CLASS_DOWN],
        trues[CLASS_NEUTRAL], falses[CLASS_NEUTRAL], trues[CLASS_UP],
        falses[CLASS_UP]
    ])

    # 購入予想日を保存する
    save_up_expectation_dates(target_brand, up_expectation_dates)
    #print('器械学習のネットワークを作成')
    env = smarter_network(dataset)

    if args.inspect:
        import code
        print('Press Ctrl-d to proceed')
        code.interact(local=locals())

    #print('学習')
    return train(env, steps=args.steps, checkin_interval=args.checkin)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('target_brand')
    parser.add_argument('--steps', type=int, default=10000)
    parser.add_argument('--checkin', type=int, default=1000)
    parser.add_argument('--use-subset', type=float, default=None)
    parser.add_argument('--inspect', default=False, action='store_true')

    args = parser.parse_args()

    stocks[args.target_brand] = Stock(YahooJp, args.target_brand)
    result = main(args)
    target_brand_name = brand_name(args.target_brand)
    print('[{}]{}: {}'.format(args.target_brand, target_brand_name, result['Accuracy']))

    with open('results.csv', 'a') as f:
        f.write('{},{},{}\n'.format(args.target_brand, target_brand_name, str(result['Accuracy'])))
Esempio n. 3
0
    env = smarter_network(dataset)

    if args.inspect:
        import code
        print('Press Ctrl-d to proceed')
        code.interact(local=locals())

    #print('学習')
    return train(env, steps=args.steps, checkin_interval=args.checkin)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('target_brand')
    parser.add_argument('--steps', type=int, default=10000)
    parser.add_argument('--checkin', type=int, default=1000)
    parser.add_argument('--use-subset', type=float, default=None)
    parser.add_argument('--inspect', default=False, action='store_true')

    args = parser.parse_args()

    stocks[args.target_brand] = Stock(YahooJp, args.target_brand)
    result = main(args)
    target_brand_name = brand_name(args.target_brand)
    print('[{}]{}: {}'.format(args.target_brand, target_brand_name,
                              result['Accuracy']))

    with open('results.csv', 'a') as f:
        f.write('{},{},{}\n'.format(args.target_brand, target_brand_name,
                                    str(result['Accuracy'])))
Esempio n. 4
0
def main(stocks, target_brand, layer1, layer2, load_sess, result_file=None):
    # 学習データのキャッシュ
    feed_cache = FeedCache(target_brand, str(REMOVE_NEWEST_DAYS))

    # 対象の銘柄名
    target_brand_name = brand_name(target_brand)

    if not feed_cache.is_exist():
        print('Make cache')
        # 株価指標データを読み込む
        all_data  = load_exchange_dataframes(stocks, target_brand)

        # 終値を取得
        using_data = get_using_data(all_data, target_brand)

        # データを学習に使える形式に正規化
        log_return_data = get_log_return_data(stocks, using_data)

        # 答と学習データを作る
        training_test_data = build_training_data(
            stocks, log_return_data, target_brand)
        feed_cache.save(training_test_data)
    else:
        print('Exist cache')
        training_test_data = feed_cache.load()

    # 学習データをトレーニング用とテスト用に分割する
    dataset = split_training_test_data(CLASS_COUNT, training_test_data)
    if len(dataset.training_predictors) < TRAIN_MIN:
        print('[{}]{}: 学習データが少なすぎるため計算を中止'.format(target_brand, target_brand_name))
        with open('results.csv', 'a') as f:
            f.write('{},{},ERROR\n'.format(target_brand, target_brand_name))

        # レイヤー検証ログに保存
        brands = nikkei225_s
        codes = [code for (code, name, _) in brands]
        layerLog = LayerLog('layer_logs', '{}_{}.csv'.format(layer1, layer2), codes)
        layerLog.add(
            target_brand,
            [-1, 0, 0, 0, 0, 0, 0, 0]
        )
        return

    print('[{}]{}'.format(target_brand, target_brand_name))

    # 器械学習のネットワークを作成
    env = smarter_network(stocks, dataset, layer1, layer2)

    # 学習
    train_accuracy, money, trues, falses, actual_count, deal_logs, up_expectation_dates = train(load_sess, env, stocks[target_brand], target_brand)

    print('-- テスト --')
    # 各クラスの正解率
    rates = np.zeros(CLASS_COUNT, dtype=np.int64)
    # 各クラスの正解数
    counts = np.zeros(CLASS_COUNT, dtype=np.int64)
    for i in range(CLASS_COUNT):
        counts[i] = trues[i] + falses[i]
        if counts[i]:
            # 各クラスの正解率(予想数 / 正解数)
            rates[i] = int(float(trues[i]) / float(counts[i]) * 100)
    print('下げ正解率    : {}% 予想{}回'.format(rates[CLASS_DOWN], counts[CLASS_DOWN]))
    print('変化なし正解率: {}% 予想{}回'.format(rates[CLASS_NEUTRAL], counts[CLASS_NEUTRAL]))
    print('上げ正解率    : {}% 予想{}回'.format(rates[CLASS_UP], counts[CLASS_UP]))
    print('上げ予測日    : {}'.format(up_expectation_dates))

    print('-- 売買シミュレーション --')
    print('売買シミュレーション結果 {:,d}円'.format(money))

    # 結果をファイル保存
    if result_file:
        with open(result_file, 'a') as f:
            f.write('{},{},{},{},{},{},{},{},{}\n'.format(
                target_brand,
                target_brand_name,
                money,
                trues[CLASS_DOWN], falses[CLASS_DOWN],
                trues[CLASS_NEUTRAL], falses[CLASS_NEUTRAL],
                trues[CLASS_UP], falses[CLASS_UP]))
    else:
        print(
            target_brand,
            target_brand_name,
            money,
            trues[CLASS_DOWN], falses[CLASS_DOWN],
            trues[CLASS_NEUTRAL], falses[CLASS_NEUTRAL],
            trues[CLASS_UP], falses[CLASS_UP])

    # 売買履歴をファイルに保存
    save_deal_logs(target_brand, deal_logs)

    # レイヤー検証ログに保存
    brands = nikkei225_s
    codes = [code for (code, name, _) in brands]
    layerLog = LayerLog('layer_logs', '{}_{}.csv'.format(layer1, layer2), codes)
    layerLog.add(
        target_brand,
        [
            train_accuracy,
            money,
            trues[CLASS_DOWN], falses[CLASS_DOWN],
            trues[CLASS_NEUTRAL], falses[CLASS_NEUTRAL],
            trues[CLASS_UP], falses[CLASS_UP]
        ]
    )

    # 購入予想日を保存する
    save_up_expectation_dates(target_brand, up_expectation_dates)