Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
/ melody-invest Public archive

Trading Strategy Backtesting and Evolution Platform

License

Notifications You must be signed in to change notification settings

ValueMelody/melody-invest

Repository files navigation

Concept

There’s no single “best” strategy, only strategies that adapt to changing market conditions. Through competition and evolution, better strategies emerge. Melody Invest enables you to create trading strategies by grouping together various trading behaviors. By backtesting these strategies with historical stock data, you can evaluate their performance across different environments. The platform then merges the most successful strategies to evolve and generate new ones, continuously repeating the cycle of competition and evolution.

Disclaimer

This platform is for experimental purposes only:

  • Backtesting results do not guarantee future success. Predefined conditions and rules can significantly impact backtesting outcomes, and relying solely on this platform’s results may be risky.
  • Investment, as well as providing investment advice, requires specific knowledge or certification. Your investment strategy should not be based solely on the results generated by this platform.
  • Stock API providers may have data-sharing policies. Be sure to review these policies before publicly hosting this platform.

Get Started

1. Install Node, Redis and Postgres

# Install redis
sudo apt install redis-server

# Install postgres
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt install postgresql-15

# Install node
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install -g pm2

2. Prerequisite

  • Create a PostgreSQL database named "melody-invest".
  • Obtain an API key from Tiingo

3. Prepare the codebase

git clone [email protected]:ValueMelody/melody-invest.git

# Replace frontend env vars if needed
cp ./client/.env.example ./client/.env

# Replace backend env vars, including redis, database, mailer configs
cp ./server/.env.example ./server/.env

npm install
npm run shared

cd server
npm run migrate

cd ../
npm run dev

4. Account setup

  1. Open http://127.0.0.1:3099 in your browser.
  2. Create a new account.
  3. Sign in and navigate to the “Profile” page.
  4. Save your Tiingo API key in the “Profile” page.
  5. Go to the “Manage Tickers” page
  6. Create several stock tickers that you want to use for backtesting your trading strategy.

5. Data syncing (Daily)

This process should be repeated daily after the initial setup:

cd server
npm run build

# Sync prices for your tickers
npm run syncTickerPrices yyyy-MM-DD

# Calculate price movements based on ticker prices
npm run calcPriceMovements

# Prepare daily ticker price data for final calculation
npm run calcDailyTickers

6. Data syncing (Weekly)

This process should be repeated weekly after the initial setup:

# Sync financial statements for your tickers
npm run syncTickerFinancials yyyy-MM

# Calculate financial movements based on financial data
npm run calcFinancialMovements

# Prepare daily ticker financial data for final calculation
npm run calcDailyTickers

7. Data sync (Monthly)

This process should be repeated monthly after the initial setup.

  1. Collect economic indicators from the following sources and store them in your indicator_monthly, indicator_quarterly, and indicator_yearly tables:

  2. Run the following commands to process the collected data:

# Calculate economic indicator movements based on collected data
npm run calcIndicatorMovements

# Prepare daily indicator data for final calculation
npm run calcDailyIndicators

8. Generate trader report and evolve

  1. Open http://127.0.0.1:3099 in your browser.
  2. Create an environment on the “Dashboard” page.
  3. Create at least two profiles on the “Dashboard” page.
  4. Run the following commands to generate reports and evolve new traders:
cd server

# Generate backtesting results for your traders
npm run calcTraderPerformances

# Evolve new traders based on performance
npm run calcTraderDescendants

You can keep triggering these commands over and over again to generate new traders and evaluate their performance

Screenshots

Dashboard

Dashboard

Trader Report

Trader Report

Env Report

Env Report

Combo Report

Combo Report

Manage Tickers

Manage Tickers

Create Trader

Create Trader

Create Env

Create Env

Create Combo

Create Combo

User Profile

User Profile