diff --git a/quants_lab/research_notebooks/mean_reversion_strat.ipynb b/quants_lab/research_notebooks/mean_reversion_strat.ipynb new file mode 100644 index 0000000..e96348f --- /dev/null +++ b/quants_lab/research_notebooks/mean_reversion_strat.ipynb @@ -0,0 +1,150 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true, + "ExecuteTime": { + "end_time": "2023-06-23T18:54:06.001204Z", + "start_time": "2023-06-23T18:54:05.418412Z" + } + }, + "outputs": [], + "source": [ + "import pandas_ta as ta\n", + "\n", + "from quants_lab.utils import data_management\n", + "from quants_lab.backtesting.backtesting import Backtesting\n", + "from quants_lab.backtesting.backtesting_analysis import BacktestingAnalysis\n", + "\n", + "df = data_management.get_dataframe(\n", + " exchange='binance_perpetual',\n", + " trading_pair='ETH-USDT',\n", + " interval='3m',\n", + ")\n", + "\n", + "def bbands_strategy(df):\n", + " df.ta.bbands(length=100, std=3, append=True)\n", + " df[\"side\"] = 0\n", + " long_condition = df[\"BBP_100_3.0\"] < 0.0\n", + " short_condition = df[\"BBP_100_3.0\"] > 1.0\n", + " df.loc[long_condition, \"side\"] = 1\n", + " df.loc[short_condition, \"side\"] = -1\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "outputs": [], + "source": [ + "backtesting = Backtesting(candles_df=df)\n", + "\n", + "positions = backtesting.run_backtesting(\n", + " strategy=bbands_strategy,\n", + " order_amount=50,\n", + " leverage=20,\n", + " initial_portfolio=100,\n", + " take_profit_multiplier=0.5,\n", + " stop_loss_multiplier=5.0,\n", + " time_limit=60 * 60 * 1,\n", + " std_span=None,\n", + ")\n", + "backtesting_report = BacktestingAnalysis(df, positions, extra_rows=1, show_volume=False)\n" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-06-23T18:54:08.381976Z", + "start_time": "2023-06-23T18:54:06.002029Z" + } + } + }, + { + "cell_type": "code", + "execution_count": 3, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Strategy Performance Report:\n", + " - Net Profit: -4.07 USD (-4.07%)\n", + " - Total Positions: 978\n", + " - Win Signals: 581\n", + " - Loss Signals: 397\n", + " - Accuracy: 0.59%\n", + " - Profit Factor: 0.97\n", + " - Max Drawdown: -13.85 USD | -13.85%\n", + " - Sharpe Ratio: -0.01\n", + " - Duration: 6,478.55 Hours\n", + " - Average Trade Duration: 48.62 minutes\n", + " \n" + ] + } + ], + "source": [ + "print(backtesting_report.text_report())" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2023-06-23T18:54:09.658773Z", + "start_time": "2023-06-23T18:54:09.628910Z" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "import pandas as pd\n", + "\n", + "# Set the backend to Plotly\n", + "pd.options.plotting.backend = \"plotly\"\n", + "\n", + "positions[\"ret_usd\"].cumsum().plot()" + ], + "metadata": { + "collapsed": false, + "is_executing": true, + "ExecuteTime": { + "start_time": "2023-06-23T18:54:11.277722Z" + } + } + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}