def run_agent(agent): env = fingym.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']
def tsla_daily_random_walk_env(): return make('TSLA-Daily-Random-Walk')
def tsla_daily_v0_env(): return make('TSLA-Daily-v0')
def ba_daily_v0_env(): return make('BA-Daily-v0')
# See the License for the specific language governing permissions and # limitations under the License. import numpy as np from fingym import fingym from collections import deque import matplotlib.pyplot as plt import ray import os ray.init() env = fingym.make('SPY-Daily-v0') CONFIG = { 'env_name': 'SPY-Daily-v0', # removing time frame, stocks owned and cash in hand 'state_size': env.state_dim - 3, 'max_shares_to_trade_at_once': 100, 'time_frame': 30, 'sigma': 0.1, 'learning_rate': 0.03, 'population_size': 400, 'iterations': 50, 'train': False, 'eval': True, 'log_actions': True }
from fingym import fingym import numpy as np import matplotlib.pyplot as plt plt.figure(figsize=(20, 10)) if __name__ == '__main__': env = fingym.make('SPY-Daily-Random-Walk') random_walks = [] for _ in range(100): real_close = np.zeros(env.n_step) random_walk = np.zeros(env.n_step) obs = env.reset() real_close[0] = obs[3] random_walk[0] = obs[3] while True: obs, reward, done, info = env.step([0, 0]) real_close[env.cur_step] = info['original_close'] random_walk[env.cur_step] = obs[3] if done: break random_walks.append(random_walk) time = np.linspace(1, len(random_walk), len(random_walk))
def googl_daily_random_walk_env(): return make('GOOGL-Daily-Random-Walk')
def spy_daily_random_walk_env(): return make('SPY-Daily-Random-Walk')
def abbv_daily_random_walk_env(): return make('ABBV-Daily-Random-Walk')
def aapl_daily_random_walk_env(): return make('AAPL-Daily-Random-Walk')
def aapl_daily_v0_env(): return make('AAPL-Daily-v0')
def abbv_daily_v0_env(): return make('ABBV-Daily-v0')
def amd_daily_v0_env(): return make('AMD-Daily-v0')
def amzn_daily_v0_env(): return make('AMZN-Daily-v0')
def spy_daily_v0_env(): return make('SPY-Daily-v0')
def spy_intraday_v0_env(): return make('SPY-Minute-v0')
def amd_daily_random_walk_env(): return make('AMD-Daily-Random-Walk')
def googl_daily_v0_env(): return make('GOOGL-Daily-v0')
def amzn_daily_random_walk_env(): return make('AMZN-Daily-Random-Walk')
def cron_daily_v0_env(): return make('CRON-Daily-v0')
def ba_daily_random_walk_env(): return make('BA-Daily-Random-Walk')
self.bought_yet = True return [1, num_shares_to_buy] else: return [0, 0] 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 = fingym.make(args.env_id) agent = BuyAndHoldAgent(env.action_space) reward = 0 done = False cur_val = 0 ob = env.reset() initial_value = ob[1] while True: action = agent.act(ob, reward, done) ob, reward, done, info = env.step(action) if done: cur_val = info['cur_val']
def cgc_daily_random_walk_env(): return make('CGC-Daily-Random-Walk')
def cron_daily_random_walk_env(): return make('CRON-Daily-Random-Walk')
def cgc_daily_v0_env(): return make('CGC-Daily-v0')