Skip to content

FinMem: A Performance-Enhanced LLM Trading Agent with Layered Memory and Character Design

License

Notifications You must be signed in to change notification settings

edwarts/FinMem-LLM-StockTrading

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FINMEM: A Performance-Enhanced LLM Trading Agent with Layered Memory and Character Design

Python 3.10 License: MIT Black arXiv

"So we beat on, boats against the current, borne back ceaselessly into the past."
                                        -- F. Scott Fitzgerald: The Great Gatsby

This repo provides the Python source code for the paper: FINMEM: A Performance-Enhanced Large Language Model Trading Agent with Layered Memory and Character Design [PDF]

@misc{yu2023finmem,
      title={FinMem: A Performance-Enhanced LLM Trading Agent with Layered Memory and Character Design}, 
      author={Yangyang Yu and Haohang Li and Zhi Chen and Yuechen Jiang and Yang Li and Denghui Zhang and Rong Liu and Jordan W. Suchow and Khaldoun Khashanah},
      year={2023},
      eprint={2311.13743},
      archivePrefix={arXiv},
      primaryClass={q-fin.CP}
}

Abstract

Recent advancements in Large Language Models (LLMs) have exhibited notable efficacy in question-answering (QA) tasks across diverse domains. Their prowess in integrating extensive web knowledge has fueled interest in developing LLM-based autonomous agents. While LLMs are efficient in decoding human instructions and deriving solutions by holistically processing historical inputs, transitioning to purpose-driven agents requires a supplementary rational architecture to process multi-source information, establish reasoning chains, and prioritize critical tasks. Addressing this, we introduce FinMem, a novel LLM-based agent framework devised for financial decision-making, encompassing three core modules: Profiling, to outline the agent's characteristics; Memory, with layered processing, to aid the agent in assimilating realistic hierarchical financial data; and Decision-making, to convert insights gained from memories into investment decisions. Notably, FinMem's memory module aligns closely with the cognitive structure of human traders, offering robust interpretability and real-time tuning. Its adjustable cognitive span allows for the retention of critical information beyond human perceptual limits, thereby enhancing trading outcomes. This framework enables the agent to self-evolve its professional knowledge, react agilely to new investment cues, and continuously refine trading decisions in the volatile financial environment. We first compare FinMem with various algorithmic agents on a scalable real-world financial dataset, underscoring its leading trading performance in stocks and funds. We then fine-tuned the agent's perceptual spans to achieve a significant trading performance. Collectively, FinMem presents a cutting-edge LLM agent framework for automated trading, boosting cumulative investment returns.

1 2 3

Repository Structure

finmem-docker
├── LICENSE
├── README.md
├── config  -> Configurations for the program
├── data  -> Data
├── puppy  -> Source code
├── run.py  -> Entry point of the program, see below for details
├── run_examples.sh  -> Bash cmd for build the docker image and run the docker container

Usage

Setting Environment Variables & Configurations for Different Models

The model can be run with LLMs on HuggingFace that can be deployed via TGI and has sufficient instruction following ability. As we will always use the text-embedding-ada-002 as our embedding model, the OPENAI_API_KEY variable needs to be set in .env no matter what backbone LLM is used.

If the LLM is gated, the HF_TOKEN needs to be set in .env

OPENAI_API_KEY = "<Your OpenAI Key>"
HF_TOKEN = "<Your HF token>"

and set the config/config.toml

[chat]
model = "tgi"
end_point = "<set the your endpoint address>"
tokenization_model_name = "<model name>"
...

To run the OpenAI model, the configuration file should be set as

model = "gpt-4"
end_point = "https://api.openai.com/v1/chat/completions"
tokenization_model_name = "gpt-4"

and with comment out HF_TOKEN in .env

OPENAI_API_KEY = "<Your OpenAI Key>"
# HF_TOKEN = ""

Build Docker Image & Run the Container

The dockerfile is based on Python 3.10 at

.devcontainer/Dockerfile

To build the docker image, run

docker build -t test-finmem finmem/.devcontainer/. 

To start the container, run

docker run -it --rm -v $(pwd):/finmem test-finmem bash

This will enter the root folder of the project.

Program

The program has two main functionalities:

 Usage: run.py sim [OPTIONS]                                                                                                                
                                                                                                                                            
 Start Simulation                                                                                                                           
                                                                                                                                            
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --market-data-path    -mdp      TEXT  The environment data pickle path [default: data/06_input/subset_symbols.pkl]                       │
│ --start-time          -st       TEXT  The training or test start time [default: 2022-06-30 For Ticker 'TSLA']                                                               │
│ --end-time            -et       TEXT  The training or test end time [default: 2022-10-11]                                                                 │
│ --run-model           -rm       TEXT  Run mode: train or test [default: train]                                                           │
│ --config-path         -cp       TEXT  config file path [default: config/config.toml]                                                     │
│ --checkpoint-path     -ckp      TEXT  The checkpoint save path [default: data/10_checkpoint_test]                                             │
│ --result-path         -rp       TEXT  The result save path [default: data/11_train_result]                                               │
│ --trained-agent-path  -tap      TEXT  Only used in test mode, the path of trained agent [default: None. Can be changed to data/05_train_model_output OR data/06_train_checkpoint]                                  │
│ --help                                Show this message and exit.                                                                        │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
                              

Notice our model has two modes: train and test. In the train mode, the information populate the agent's memory. In the test mode, the agent will use the information in the memory and new information to make decisions. When test mode is selected, the trained agent must be provided.

When the program stopped due to exceptions(OpenAI API is not stable, etc.), the training/testing process can be resumed with

                                                                                                                                            
 Usage: run.py sim-checkpoint [OPTIONS]                                                                                                     
                                                                                                                                            
 Start Simulation from checkpoint                                                                                                           
                                                                                                                                            
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --checkpoint-path  -cp      TEXT  The checkpoint path [default: data/06_train_checkpoint]                                                │
│ --result-path      -rp      TEXT  The result save path [default: data/05_train_model_output]                                             │
│ --config-path      -ckp      TEXT  config file path [default: config/tsla_config.toml]                                                    │
│ --run-model        -rm      TEXT  Run mode: train or test [default: train]                                                               │
│ --help                            Show this message and exit.                                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

About

FinMem: A Performance-Enhanced LLM Trading Agent with Layered Memory and Character Design

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.8%
  • Dockerfile 0.2%