Skip to content

Sivchay/Mean-Reversion-Trading

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Mean-Reversion-Trading

This is a mean reversion strategy that uses an optimizer to find the best parameters. Works best with high volatility assets Run every cell before starting to make sure all variables are defined

DISCLAIMER

This code, along with anything else provided, is strictly meant for educational and informational purposes only. It is intended to be used for backtesting, research, and learning. This code is NOT designed or guarenteed to make you money.

IMPORTANT

  1. THE CURRENT VERSION HAS AN INVERSE MACD HISTOGRAM FORMULA. I'VE NOTICED SOME ASSETS DO BETTER WHEN THE FORMULA IS INVERSED

Semi important info

  1. I've been seeing an extra trade or so inside the training and test data, this shouldn't affect your trade much so ignore it.
  2. On TradingView, MACD Histogram is multiplied by 10 so that you can visualize it better. If you are manually checking values, multiply your values by 10 on the optimizer

TradingView

  1. Go to TradingView and press on "indicators" on the top left
  2. Add "Mean Reversion Trading" by Sivchay
  3. Press on the dropdown on the top left, press "MRTrading", and press on the cogwheel to input settings
  4. Input optimized parameters

Optimizer

  1. Download or copy "MRTradingOptimizer.ipynb" to Jupyter Notebook
  2. pip install pandas, optuna, plotly
  3. Change "NQ1_15M.csv" into your own csv. Download from TradingView in UNIX
  4. Adjust "tf" to your csv and goals. Check "Adjusting timeframe (tf)"
  5. Adjust "initial_increm" and "holdout"
  6. Run every cell
  7. Wait for parameters from Optuna

To restart trials, go to the final cell and find the storage filepath. Delete the storage file.

Adjusting timeframe (tf)

The "tf" variable determines the amount of rows used to make up one increment. An increment is a period of time the optimizer uses to train/test. For example, if my desired increment is 1 month for a 15M csv, I might convert amount of trading hours in a day into minutes, divide it by 15, multiply by amount of trading days in a week, then weeks in a month to get the amount of rows in one increment.

((8hr x 60M) / 15M) * 5D * 4W = 640

Initial_increm & holdout

The "initial_increm" variable is a safegaurd to make sure all calculations match to TradingView's calculations. The calculations for the optimizer are slightly off because it takes does calculations immediately, while TradingView waits until there are enough candles before apply calculations. To be safe, have at least 500 rows for the optimizer to catch up

The "holdout" variable is used as a test data, meaning a portion of the data will be withheld from the optimizer. This reduces overfitting in the model but doesn't eliminate it.

Manually checking

To check the trades taken by the algorithm, take the parameters outputted by Optuna and plug it into the respective variable in the first cell. To check on TradingView, select the cog wheel and input your parameters. This is still version one, expect bugs and glitches.

TODOs

  1. Better ways to prevent or reduce overfitting
  2. Pyramidding
  3. Calculations for futures/options

About

TradingView mean reversion strategy with an optimizer. This is version 1 so expect bugs and glitches

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published