How to build stock portfolio using deep reinforcement learning in Python by taking care of indicators for each given stock?

How to build stock portfolio using deep reinforcement learning in Python by taking care of indicators for each given stock?

Manage alerts

Loading saved threads...

YoYoYo · External communityPost link
External question — Data Science Stack Exchange Author: YoYoYo Original post: https://datascience.stackexchange.com/questions/115167 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I have this Python code from this tutorial which is trading a stock (e.g. GME but it can be any) by taking care of its indicators ('SMA', 'RSI', 'OBV'). # Gym stuff import gym import gym_anytrading # Stable baselines - rl stuff from stable_baselines.common.vec_env import DummyVecEnv from stable_baselines import A2C # Processing libraries import numpy as np import pandas as pd from matplotlib import pyplot as plt # Custom indicators libraries: from gym_anytrading.envs import StocksEnv from finta import TA df = pd.read_csv('data/gmedata.csv') df['Date'] = pd.to_datetime(df['Date']) df.sort_values('Date', ascending=True, inplace=True) df.set_index('Date', inplace=True) env = gym.make('stocks-v0', df=df, frame_bound=(5,250), window_size=5) # Build Environment: state = env.reset() while True: action = env.action_space.sample() n_state, reward, done, info = env.step(action) if done: print("info", info) break #plt.figure(figsize=(15,6)) #plt.cla() #env.render_all() #plt.show() # Fix Volume Column: df['Volume'] = df['Volume'].apply(lambda x: float(x.replace(",", ""))) # Calculate SMA, RSI and OBV: df['SMA'] = TA.SMA(df, 12) df['RSI'] = TA.RSI(df) df['OBV'] = TA.OBV(df) df.fillna(0, inplace=True) # Create New Environments: def add_signals(env): start = env.frame_bound[0] - env.window_size end = env.frame_bound[1] prices = env.df.loc[:, 'Low'].to_numpy()[start:end] signal_features = env.df.loc[:, ['Low', 'Volume','SMA', 'RSI', 'OBV']].to_numpy()[start:end] return prices, signal_features class MyCustomEnv(StocksEnv): _process_data = add_signals env2 = MyCustomEnv(df=df, window_size=12, frame_bound=(12,50)) # Build Environment and Train: env_maker = lambda: env2 env = DummyVecEnv([env_maker]) model = A2C('MlpLstmPolicy', env, verbose=1) model.learn(total_timesteps=1000000) # Evaluation: env = MyCustomEnv(df=df, window_size=12, frame_bound=(80,250)) obs = env.reset() while True: obs = obs[np.newaxis, ...] action, _states = model.predict(obs) obs, rewards, done, info = env.step(action) if done: print("info", info) break plt.figure(figsize=(15,6)) plt.cla() env.render_all() plt.show() Here is the github repo for this code where you can find also the GME stock dataset too: https://github.com/nicknochnack/Reinforcement-Learning-for-Trading-Custom-Signals/blob/main/Custom%20Signals.ipynb Now, let's say instead of feeding this deep RL code with just one stock ticker (GME) with its indicators values ('SMA', 'RSI', 'OBV') you feed it with a bunch of tickers (e.g. 100 or 1000 or so) and each of them to have their own indicators values. How such a dataset should look like and how this code should be in order to accomplish such a thing because currently it is working for just one single ticker?
Quote
Report

Post Reply

Checking account access…