Skip to main content

A portfolio rebalancing task environment for reinforcement learning

Project description

pmenv

Introduction to the pmenv Package

The pmenv is a reinforcement learning environment for the Portfolio Rebalancing Task. It loads multiple stock market data using tickers and synchronizes them into a 3D tensor format suitable for learning. It is an efficient environment for implementing and testing various reinforcement learning algorithms.

Overview

The pmenv is composed of two modules, DataManager and Environment:

  • The Environment module plays a role in simulating the stock market environment and handling the transitions caused by price changes and transactions. The observation is a vector of Open, High, Low, Close price and Volume and the action space is $[-1,1]^N$ where $N$ is a number of risky assets in the portfolio. Portfolio is a vector of weights of all assets and the first entry always represents cash weight.

  • The DataManager module is responsible for loading multiple stock market data using tickers and synchronizing them into a unified format suitable for learning.

The mathematical details of the environment are implemented based on the following paper. Please refer to the paper for further information. An intelligent financial portfolio trading strategy using deep Q-learning, ESWA, 2020

Install

pip install pmenv

Basic Usage

import pmenv

datamanager = pmenv.DataManager()
environment = pmenv.Environment()

# Set transaction cost
config = {"CHARGE": 0.0025, "TEX": 0.001}
environment.config = config

# Tickers and Period
tickers = ["AAPL", "INCY", "REGN"]

train_start = "2019-01-02"
train_end = "2021-06-30"

# Load stock OHLCV dataframe with a ticker
data = datamanager.get_data(tickers[0], train_start, train_end)
# Load stock data tensor with ticker list
data_tensor = datamanager.get_data_tensor(tickers, train_start, train_end)

# Initialize the environment with balance
balance = 5000
observation = environment.reset(balance)

# Step
action = np.random.random(size=(len(tickers))
next_observation, reward, done = env.step(action)

Customizing

# Yon can define your MDP
import pmenv

# Example 
class CustomEnvironment(pmenv.Environment)
	def __init__(self):
		super().__init__()
	
	def get_state(self, observation):
		"""
		state is defined as [ohlcv, portfolio]
		"""
		portfolio = portfolio[:,np.newaxis]
		state = np.concatenate([obsevation, portfolio], axis=1)
		return state
	
	def get_reward(self):
		"""
		reward is defined as ~
		"""

Tutorial with RL

  1. Portfolio Rebalancing with Deep Q-Learning
  2. Portfolio Rebalancing with DDPG
  3. Portfolio Rebalancing with Dirichlet Distribution Trader

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pmenv-1.0.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

pmenv-1.0.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pmenv-1.0.0.tar.gz.

File metadata

  • Download URL: pmenv-1.0.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for pmenv-1.0.0.tar.gz
Algorithm Hash digest
SHA256 97da9cafd6b3e95da6e234a0147b6973c8737398676e84a29eda192d620759f8
MD5 bea83013b902612bd310cc8db8543c5c
BLAKE2b-256 9606e90467e32cd7a1d73eea156be5d6d3a2ffcaeb4834a053bd5d62236b317f

See more details on using hashes here.

File details

Details for the file pmenv-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pmenv-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for pmenv-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac98727b0a9da9f6cfb1d351ab638bc51937e41b6306bcc0d690a04f1a275a69
MD5 b8c9b76097794cda98b0fd6c707ed801
BLAKE2b-256 cf4611cb69326dfbc6867cca071d605868d84cc5c5fed7cf894ba3713454be50

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page