mirror of
https://github.com/aljazceru/hummingbot-dashboard.git
synced 2026-01-07 23:44:24 +01:00
(feat) move analize optimizations to root
This commit is contained in:
10911
quants_lab/research_notebooks/01_analyze_optimization_results.ipynb
Normal file
10911
quants_lab/research_notebooks/01_analyze_optimization_results.ipynb
Normal file
File diff suppressed because one or more lines are too long
@@ -1,444 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"root_path = os.path.abspath(os.path.join(os.getcwd(), '../../..'))\n",
|
||||
"sys.path.append(root_path)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"from utils.optuna_database_manager import OptunaDBManager\n",
|
||||
"\n",
|
||||
"db_root_path = \"../../../data/backtesting/\"\n",
|
||||
"db_name = \"backtesting_report.db\"\n",
|
||||
"\n",
|
||||
"optuna_db_manager = OptunaDBManager(db_name=db_name,\n",
|
||||
" db_root_path=db_root_path)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"optuna_db_manager.studies"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"study_name = \"super_trend_optimization_1\"\n",
|
||||
"df = optuna_db_manager.merged_df[optuna_db_manager.merged_df[\"study_name\"] == study_name]"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Bar Chart of Average Trading Time\n",
|
||||
"This bar chart compares the average trading time across trials. It helps to quickly identify trials with unusually long or short trading times.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import plotly.express as px\n",
|
||||
"\n",
|
||||
"fig = px.bar(df, x='trial_id', y='avg_trading_time_in_hours', title='Average Trading Time per Trial')\n",
|
||||
"fig.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Scatter Plot of Accuracy vs. Average Trading Time\n",
|
||||
"This scatter plot shows the relationship between the accuracy and the average trading time of each trial. It can help to identify if there is any correlation between these two metrics.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig = px.scatter(df, x='avg_trading_time_in_hours', y='accuracy', title='Accuracy vs. Average Trading Time')\n",
|
||||
"fig.show()\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Histogram of Total Positions\n",
|
||||
"The histogram represents the distribution of total positions across all trials. This visualization is useful for understanding the general spread and most common values of positions.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig = px.histogram(df, x='total_positions', title='Distribution of Total Positions')\n",
|
||||
"fig.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Pie Chart of Win Signals\n",
|
||||
"This pie chart shows the proportion of win signals in each trial, providing a visual representation of the success rate distribution across trials.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig = px.pie(df, names='trial_id', values='win_signals', title='Proportion of Win Signals per Trial')\n",
|
||||
"fig.show()\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Box Plot for Trial Value\n",
|
||||
"A box plot for trial values to identify the range, median, and any potential outliers in the trial values.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig = px.box(df, y='value', title='Box Plot of Trial PNL')\n",
|
||||
"fig.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Heatmap for Correlation Analysis\n",
|
||||
"This heatmap illustrates the correlation between various numerical variables such as accuracy, average trading time, total positions, win signals, and value.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import plotly.express as px\n",
|
||||
"\n",
|
||||
"# Calculate the correlation matrix\n",
|
||||
"correlation_matrix = df[['accuracy', 'avg_trading_time_in_hours', 'total_positions', 'win_signals', 'value']].corr()\n",
|
||||
"\n",
|
||||
"# Generate the heatmap\n",
|
||||
"fig = px.imshow(correlation_matrix,\n",
|
||||
" x=correlation_matrix.columns,\n",
|
||||
" y=correlation_matrix.columns,\n",
|
||||
" title='Correlation Heatmap',\n",
|
||||
" labels=dict(x=\"Variable\", y=\"Variable\", color=\"Correlation\"),\n",
|
||||
" color_continuous_scale='RdBu')\n",
|
||||
"\n",
|
||||
"fig.show()\n",
|
||||
"\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Stacked Bar Chart for Win Signals vs Total Positions\n",
|
||||
"A stacked bar chart displaying the ratio of win signals to total positions for each trial. This helps in visualizing the efficiency and effectiveness of each trial.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df['loss_signals'] = df['total_positions'] - df['win_signals']\n",
|
||||
"fig = px.bar(df, x='trial_id', y=['win_signals', 'loss_signals'], title='Win vs Loss Signals per Trial', labels={'value':'Number of Signals'}, hover_data=['total_positions'])\n",
|
||||
"fig.show()\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Scatter Plots Against PNL\n",
|
||||
"These scatter plots show how various metrics behave in relation to the PNL (Profit and Loss). This analysis can help in understanding which factors have a stronger relationship with financial outcomes.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Scatter Plot for Accuracy vs PNL\n",
|
||||
"fig_accuracy_pnl = px.scatter(df, x='accuracy', y='value', title='Accuracy vs PNL')\n",
|
||||
"fig_accuracy_pnl.show()\n",
|
||||
"\n",
|
||||
"# Scatter Plot for Average Trading Time vs PNL\n",
|
||||
"fig_tradingtime_pnl = px.scatter(df, x='avg_trading_time_in_hours', y='value', title='Average Trading Time vs PNL')\n",
|
||||
"fig_tradingtime_pnl.show()\n",
|
||||
"\n",
|
||||
"# Scatter Plot for Total Positions vs PNL\n",
|
||||
"fig_positions_pnl = px.scatter(df, x='total_positions', y='value', title='Total Positions vs PNL')\n",
|
||||
"fig_positions_pnl.show()\n",
|
||||
"\n",
|
||||
"# Scatter Plot for Win Signals vs PNL\n",
|
||||
"fig_winsignals_pnl = px.scatter(df, x='win_signals', y='value', title='Win Signals vs PNL')\n",
|
||||
"fig_winsignals_pnl.show()\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Scatter Matrix of All Variables\n",
|
||||
"A scatter matrix allows us to see both the distribution of single variables and the relationships between two variables. The diagonal shows histograms for each variable, and the scatter plots show correlations between them.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import plotly.express as px\n",
|
||||
"\n",
|
||||
"# Selecting columns for scatter matrix\n",
|
||||
"selected_columns = ['accuracy', 'avg_trading_time_in_hours', 'total_positions', 'win_signals', 'value']\n",
|
||||
"\n",
|
||||
"# Creating scatter matrix\n",
|
||||
"fig_matrix = px.scatter_matrix(df[selected_columns], dimensions=selected_columns, title='Scatter Matrix of Variables', height=800)\n",
|
||||
"fig_matrix.show()\n"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"from hummingbot.core.data_type.common import PositionMode\n",
|
||||
"from hummingbot.connector.connector_base import TradeType, OrderType\n",
|
||||
"from hummingbot.smart_components.utils.config_encoder_decoder import ConfigEncoderDecoder\n",
|
||||
"\n",
|
||||
"trial_to_analyze = 36\n",
|
||||
"\n",
|
||||
"trial = df[df[\"trial_id\"] == trial_to_analyze]"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"trial"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Transform trial config in a dictionary\n",
|
||||
"encoder_decoder = ConfigEncoderDecoder(TradeType, OrderType, PositionMode)\n",
|
||||
"trial_config = encoder_decoder.decode(json.loads(trial[\"config\"].item()))\n",
|
||||
"trial_config"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from quants_lab.controllers.supertrend import SuperTrend, SuperTrendConfig\n",
|
||||
"\n",
|
||||
"# In this case we are using the supertrend controller but we can also access to the controller by using the method load_controllers\n",
|
||||
"config = SuperTrendConfig(**trial_config)\n",
|
||||
"controller = SuperTrend(config)"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from quants_lab.strategy.strategy_analysis import StrategyAnalysis\n",
|
||||
"from hummingbot.smart_components.strategy_frameworks.directional_trading import DirectionalTradingBacktestingEngine\n",
|
||||
"\n",
|
||||
"# Backtest configuration\n",
|
||||
"trade_cost = 0.0006\n",
|
||||
"initial_portfolio_usd = 1000\n",
|
||||
"start = \"2023-01-01\"\n",
|
||||
"end = \"2024-01-02\"\n",
|
||||
"\n",
|
||||
"# Load the data\n",
|
||||
"engine = DirectionalTradingBacktestingEngine(controller=controller)\n",
|
||||
"engine.load_controller_data(\"../../data/candles\")\n",
|
||||
"backtesting_results = engine.run_backtesting(initial_portfolio_usd=initial_portfolio_usd,\n",
|
||||
" trade_cost=trade_cost,\n",
|
||||
" start=start, end=end)\n",
|
||||
"strategy_analysis = StrategyAnalysis(\n",
|
||||
" positions=backtesting_results[\"executors_df\"],\n",
|
||||
" candles_df=backtesting_results[\"processed_data\"],\n",
|
||||
")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"strategy_analysis.create_base_figure(volume=False, positions=False, trade_pnl=True)\n",
|
||||
"strategy_analysis.figure()"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"strategy_analysis.pnl_over_time()"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user