示例#1
0
def run_agent(agent):
    env = gym.make(CONFIG['env_name'])
    log_actions = CONFIG['log_actions']
    state = env.reset()
    # Removed time element from state
    state = np.delete(state, 2)

    next_state, reward, done, info = env.step([0, 0])
    if len(next_state) > agent.state_size:
        next_state = np.delete(next_state, 2)
    state_as_percentages = get_state_as_change_percentage(state, next_state)
    state = next_state

    done = False
    states_buy = []
    states_sell = []
    closes = []

    i = 0
    while not done:
        closes.append(state[5])
        action = agent.act(state_as_percentages)
        #if log_actions:
        #print('action: ',action)
        #print('state: ',state)
        next_state, reward, done, info = env.step(action)
        if len(next_state) > agent.state_size:
            next_state = np.delete(next_state, 2)
        if action[0] == 1 and action[1] > 0 and state[1] > state[2]:
            if log_actions:
                print('stocks owned: ', state[0])
                print('stocks to buy: ', action[1])
                print('stock price: ', state[2])
                print('cash in hand: ', state[1])
                print('total value: ', info['cur_val'])
            states_buy.append(i)
        if action[0] == 2 and action[1] > 0 and state[0] > 0:
            if log_actions:
                print('stocks owned: ', state[0])
                print('stocks to sell: ', action[1])
                print('stock price: ', state[2])
                print('cash in hand: ', state[1])
                print('total value: ', info['cur_val'])
            states_sell.append(i)
        state_as_percentages = get_state_as_change_percentage(
            state, next_state)
        state = next_state
        i += 1
    return closes, states_buy, states_sell, info['cur_val']
示例#2
0
    def __init__(self, action_space):
        self.action_space = action_space

    def act(self, observation, reward, done):
        return self.action_space.sample()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=None)
    parser.add_argument('env_id',
                        nargs='?',
                        default='SPY-Daily-v0',
                        help='Select the environment to run')
    args = parser.parse_args()

    env = gym.make(args.env_id)
    agent = RandomAgent(env.action_space)

    episode_count = 100
    reward = 0
    done = False

    final_vals = []

    initial_value = 0

    for i in range(episode_count):
        ob = env.reset()
        initial_value = ob[1]
        while True:
            action = agent.act(ob, reward, done)
示例#3
0
def aapl_daily_v0_env():
    return make('AAPL-Daily-v0')
示例#4
0
def abbv_daily_v0_env():
    return make('ABBV-Daily-v0')
示例#5
0
def amd_daily_v0_env():
    return make('AMD-Daily-v0')
示例#6
0
def amzn_daily_v0_env():
    return make('AMZN-Daily-v0')
示例#7
0
def ba_daily_v0_env():
    return make('BA-Daily-v0')
示例#8
0
def cgc_daily_v0_env():
    return make('CGC-Daily-v0')
示例#9
0
def cron_daily_v0_env():
    return make('CRON-Daily-v0')
示例#10
0
def googl_daily_v0_env():
    return make('GOOGL-Daily-v0')
示例#11
0
def spy_intraday_v0_env():
    return make('SPY-Minute-v0')
示例#12
0
def spy_daily_v0_env():
    return make('SPY-Daily-v0')
        if action[0] == 2 and state[0] > 0:
            states_sell.append(i)
        opn = (next_state[2] - state[2]) / next_state[2]
        high = (next_state[3] - state[3]) / next_state[3]
        low = (next_state[4] - state[4]) / next_state[4]
        close = (next_state[5] - state[5]) / next_state[5]
        volume = (next_state[6] - state[6]) / next_state[6]
        state_as_percentages = [
            next_state[0], next_state[1], opn, high, low, close, volume
        ]
        state = next_state
        i += 1
    return closes, states_buy, states_sell, info['cur_val']


env = gym.make('SPY-Daily-v0')
# removing time frame
state_size = env.state_dim - 1
time_frame = 30

dirname = os.path.dirname(__file__)
weights_file = os.path.join(dirname, 'evo_weights_5.h5')
model = build_compile_model(state_size * time_frame, 7)
model.load_weights(weights_file)

agent = EvoAgent(state_size, time_frame, model.get_weights())

closes, states_buy, states_sell, result = test_agent(env, agent, model)
print('result: {}'.format(str(result)))
plt.figure(figsize=(20, 10))
plt.plot(closes, label='true close', c='g')