コード例 #1
0
    threads = []
    for worker in workers:  # worker threads
        t = threading.Thread(target=worker.work, args=())
        t.start()  # training
        threads.append(t)
    # add a PPO updating thread
    threads.append(threading.Thread(target=GLOBAL_PPO.update, ))
    threads[-1].start()
    COORD.join(threads)

    # plot reward change and test
    plt.plot(np.arange(len(GLOBAL_RUNNING_R)), GLOBAL_RUNNING_R)
    plt.xlabel('Episode')
    plt.ylabel('Moving reward')
    plt.ion()
    plt.show()
    #env = gym.make('Pendulum-v0')
    trading_fee = .007
    time_fee = .00724
    history_length = 1
    generator = get_CSV_data(filename="./test_6.csv")
    env = SpreadTrading(spread_coefficients=[1],
                        data_generator=generator,
                        trading_fee=trading_fee,
                        time_fee=time_fee,
                        history_length=history_length)
    while True:
        s = env.reset()
        for t in range(3455):
            env.render()
            s = env.step(GLOBAL_PPO.choose_action(s))[0]
コード例 #2
0
from tgym.core import DataGenerator
from tgym.envs import SpreadTrading
from tgym.gens.deterministic import WavySignal

generator = WavySignal(period_1=25, period_2=50, epsilon=-0.5)

game_length = 200
trading_fee = 0.2
time_fee = 0
# history_length number of historical states in the observation vector.
history_length = 2

environment = SpreadTrading(spread_coefficients=[1],
                            data_generator=generator,
                            trading_fee=trading_fee,
                            time_fee=time_fee,
                            history_length=history_length,
                            game_length=game_length)

environment.render()
while True:
    action = raw_input("Action: Buy (b) / Sell (s) / Hold (enter): ")
    if action == 'b':
        action = [0, 1, 0]
    elif action == 's':
        action = [0, 0, 1]
    else:
        action = [1, 0, 0]
    environment.step(action)
    environment.render()
コード例 #3
0
# In[104]:

# Running the agent
done = False
state = environment.reset()
while not done:
    action = agent.act(state)

    for position in environment._positions:
        if all(environment._position == environment._positions[position]):
            position_name = position

    for _action in environment._actions:
        if all(action == environment._actions[_action]):
            action_name = _action

    state, _, done, info = environment.step(action)

    for position in environment._positions:
        if all(environment._position == environment._positions[position]):
            next_position_name = position

    print position_name, action_name, next_position_name

    if 'status' in info and info['status'] == 'Closed plot':
        done = True
    else:
        environment.render(savefig=True)

# In[ ]: