Example #1
0
def backtest(agent, env):
    global PATH_prefix
    print("starting to backtest......")
    from agents.UCRP import UCRP
    from agents.Winner import WINNER
    from agents.Losser import LOSSER

    agents = []
    agents.extend(agent)
    agents.append(WINNER())
    #agents.append(UCRP())
    agents.append(LOSSER())
    labels = ['PG', 'Winner', 'Losser']

    wealths_result = []
    rs_result = []
    for i, agent in enumerate(agents):
        stocktrader = StockTrader()
        info = env.step(None, None, 'False')
        r, contin, s, w1, p, risk = parse_info(info)
        contin = 1
        wealth = 10000
        wealths = [wealth]
        rs = [1]
        while contin:
            w2 = agent.predict(s, w1)
            env_info = env.step(w1, w2, 'False')
            r, contin, s_next, w1, p, risk = parse_info(env_info)
            wealth = wealth * math.exp(r)
            rs.append(math.exp(r) - 1)
            wealths.append(wealth)
            s = s_next
            stocktrader.update_summary(0, r, 0, 0, w2, p)

        #stocktrader.write(map(lambda x: str(x), env.get_codes()),labels[i])
        print('finish one agent')
        wealths_result.append(wealths)
        rs_result.append(rs)

    print('agent', '   ', 'cumulative return', '  ', 'average daily return',
          '   ', 'sharpe ratio', '   ', 'maximum drawback')
    plt.figure(figsize=(8, 6), dpi=100)
    for i in range(len(agents)):
        plt.plot(wealths_result[i], label=labels[i])
        cumr = float((wealths_result[i][-1] - 10000) / 10000 * 100)
        mrr = float(np.mean(rs_result[i]) * 100)
        sharpe = float(
            np.mean(rs_result[i]) / np.std(rs_result[i]) * np.sqrt(252))
        maxdrawdown = float(
            max(1 - min(wealths_result[i]) /
                np.maximum.accumulate(wealths_result[i])))
        print(labels[i], '   ', round(cumr, 3), '%', ' ', round(mrr, 3), '%',
              '   ', round(sharpe, 3), '  ', round(maxdrawdown, 3), '%')
    plt.legend()
    plt.xlabel('time')
    plt.ylabel('wealth')
    plt.savefig(PATH_prefix + 'backtest.png')
    plt.show()
def backtest(agent, env, market):
    global PATH_prefix
    print("starting to backtest......")
    from agents.UCRP import UCRP
    from agents.Winner import WINNER
    from agents.Losser import LOSSER

    agents = []
    agents.extend(agent)
    agents.append(WINNER())
    agents.append(UCRP())
    agents.append(LOSSER())
    labels = ['PG', 'Winner', 'UCRP', 'Losser']

    wealths_result = []
    rs_result = []
    for i, agent in enumerate(agents):
        stocktrader = StockTrader()
        info = env.step(None, None, 'False')
        r, contin, s, w1, p, risk = parse_info(info)
        contin = 1
        wealth = 10000
        wealths = [wealth]
        rs = [1]
        while contin:
            w2 = agent.predict(s, w1)
            env_info = env.step(w1, w2, 'False')
            r, contin, s_next, w1, p, risk = parse_info(env_info)
            wealth = wealth * math.exp(r)
            rs.append(math.exp(r) - 1)
            wealths.append(wealth)
            s = s_next
            stocktrader.update_summary(0, r, 0, 0, w2, p)

        stocktrader.write(map(lambda x: str(x), env.get_codes()), labels[i])
        print('finish one agent')
        wealths_result.append(wealths)
        rs_result.append(rs)

#    print('资产名称','   ','平均日收益率','   ','夏普率','   ','最大回撤')
    print('Asset Name', '   ', 'Average daily rate of return', '   ',
          'Sharp rate', '   ', 'Maximum withdrawal')
    plt.figure(figsize=(8, 6), dpi=100)
    for i in range(len(agents)):
        plt.plot(wealths_result[i], label=labels[i])
        plt.title('RL - PM in the {} stock market'.format(market))
        mrr = float(np.mean(rs_result[i]) * 100)
        sharpe = float(
            np.mean(rs_result[i]) / np.std(rs_result[i]) * np.sqrt(252))
        maxdrawdown = float(
            max(1 - min(wealths_result[i]) /
                np.maximum.accumulate(wealths_result[i])))
        print(labels[i], '   ', round(mrr, 3), '%', '   ', round(sharpe, 3),
              '  ', round(maxdrawdown, 3))
    plt.legend()
    plt.savefig(PATH_prefix + 'backtest.png')
    plt.show()
def backtest(agent, env):
    global PATH_prefix
    print("starting to backtest......")
    from agents.Winner import WINNER
    from agents.Losser import LOSSER

    agents = []
    agents.extend(agent)
    agents.append(WINNER())
    # agents.append(UCRP())
    agents.append(LOSSER())
    labels = ['DDPG', 'Winner', 'Losser']

    wealths_result = []
    rs_result = []
    for i, agent in enumerate(agents):
        stocktrader = StockTrader()
        info = env.step(None, None, 'False')
        r, contin, s, w1, p, risk = parse_info(info)
        contin = 1
        wealth = 10000
        wealths = [wealth]
        rs = [1]
        while contin:
            w2 = agent.predict(s, w1)
            env_info = env.step(w1, w2, 'False')
            r, contin, s_next, w1, p, risk = parse_info(env_info)
            wealth = wealth * math.exp(r)
            rs.append(math.exp(r) - 1)
            wealths.append(wealth)
            s = s_next
            stocktrader.update_summary(0, r, 0, 0, w2, p)

        stocktrader.write(map(lambda x: str(x), env.get_codes()), labels[i])
        print('finish one agent')
        wealths_result.append(wealths)
        rs_result.append(rs)

    plt.figure(figsize=(8, 6), dpi=100)
    for i in range(len(agents)):
        plt.plot(wealths_result[i], label=labels[i])

    plt.legend()
    plt.savefig(PATH_prefix + 'backtest.png')
    plt.show()
    print(PATH_prefix)
def backtest(agent,env):
    print("starting to backtest......")
    from agents.UCRP import UCRP
    from agents.Winner import WINNER
    from agents.Losser import LOSSER

    agents=[]
    agents.append(agent)
    agents.append(WINNER())
    agents.append(UCRP())
    agents.append(LOSSER())
    labels=['PG','Winner','UCRP','Losser']

    wealths_result=[]
    rs_result=[]
    for i,agent in enumerate(agents):
        info = env.step(None, None)
        r, contin, s, w1, p, risk = parse_info(info)
        contin = 1
        wealth=10000
        wealths = [wealth]
        rs=[1]
        while contin:
            w2 = agent.predict(s, w1)
            if i==0:
                print(w2)
            env_info = env.step(w1, w2)
            r, contin, s_next, w1, p, risk = parse_info(env_info)
            wealth=wealth*math.exp(r)
            rs.append(math.exp(r)-1)
            wealths.append(wealth)
            s=s_next
        print('finish one agent')
        wealths_result.append(wealths)
        rs_result.append(rs)

    for i in range(len(agents)):
        plt.plot(wealths_result[i],label=labels[i])
        print(labels[i],'   ',np.mean(rs_result[i]),'   ',np.std(rs_result[i]))
    plt.legend()
    plt.show()
def backtest(agent, env):
    global PATH_prefix
    print("starting to backtest......")
    from agents.UCRP import UCRP
    from agents.Winner import WINNER
    from agents.Losser import LOSSER

    agents = []
    agents.extend(agent)
    agents.append(WINNER())
    agents.append(UCRP())
    agents.append(LOSSER())
    labels = ['PG', 'Winner', 'UCRP', 'Losser']
    wealths_result = []
    rs_result = []
    for i, agent in enumerate(agents):
        stocktrader = StockTrader()
        info = env.step(None, None, 'False')
        r, contin, s, w1, p, risk = parse_info(info)
        contin = 1
        wealth = 10000
        wealths = [wealth]
        rs = [1]
        while contin:
            w2 = agent.predict(s, w1)
            env_info = env.step(w1, w2, 'False')
            r, contin, s_next, w1, p, risk = parse_info(env_info)
            wealth = wealth * math.exp(r)
            rs.append(math.exp(r) - 1)
            wealths.append(wealth)
            s = s_next
            stocktrader.update_summary(0, r, 0, 0, w2, p)

        stocktrader.write(map(lambda x: str(x), env.get_codes()), labels[i])
        print('finish one agent')
        wealths_result.append(wealths)
        rs_result.append(rs)

    print('资产名称', '   ', '平均日收益率', '   ', '夏普率', '   ', '最大回撤')
    plt.figure(figsize=(8, 6), dpi=100)
    for i in range(len(agents)):
        # if labels[i] == 'UCRP' or labels[i] == 'Losser':
        #     continue
        plt.plot(wealths_result[i], label=labels[i])
        fileObject = open(labels[i] + '.txt', 'w')
        for ip in wealths_result[i]:
            fileObject.write(str(ip))
            fileObject.write(', ')
        fileObject.close()
        mrr = float(np.mean(rs_result[i]) * 100)
        sharpe = float(
            np.mean(rs_result[i]) / np.std(rs_result[i]) * np.sqrt(252))
        maxdrawdown = float(
            max(1 - min(wealths_result[i]) /
                np.maximum.accumulate(wealths_result[i])))
        print(labels[i], '   ', round(mrr, 3), '%', '   ', round(sharpe, 3),
              '  ', round(maxdrawdown, 3))
    # new_data = []
    # for i in LSTM:
    #     for j in range(8):
    #         new_data.append(i)
    # inter_op = interpolate.interp1d(range(len(hand_data)), hand_data, kind='linear')
    # hand_data = inter_op(7*range(len(hand_data)))

    # def longer(i):
    #     k = np.zeros(180)
    #     j = 0
    #     p = 0
    #     while j + 1 < len(i):
    #         print([p, j])
    #         k[p] = i[j]
    #         k[p + 1] = i[j]
    #         k[p + 2] = i[j + 1]
    #         p += 3
    #         j += 2
    #     return i
    # LSTM = longer(LSTM)
    # Random = longer(Random)
    # Uniform = longer(Uniform)
    # plt.plot(LSTM, label='LSTM')
    # plt.plot(Random, label='Random')
    # plt.plot(Uniform, label='Uniform')
    plt.legend()
    plt.savefig(PATH_prefix + 'backtest_with_hand.png')
    plt.show()