Skip to content

erv4gen/Monte-Carlo-Simulation-Engine

Repository files navigation

Monte Carlo Simulation Engine

First Instalation

  1. Install Python envirement Anaconda env manager is recommended. Can be downladed here
conda create --name mcs python=3.8
  1. Make sure the Python environment is activated:
conda activate mcs

and Python is installed:

python --version
  1. Install dependencies
pip install -r requirements.txt
  1. Create config file Make sure the config.json is available under the project root dir.

One can use the default settings by renaming default_config.json to config.json

mv default_config.json config.json

sample parametres for the backtest wih GHD distribution:

{
    "data_mode": "backtest",
    "return_function_params": {
        "mu": -0.0001,
        "alpha": 0.2444,
        "beta":0.053,
        "delta":0.0003,
        "lambda_":-0.52,

        "sigma": -99999.0,
        "current_price": 10000,
        "N": 100,
        "T": 1825
    },
    "return_function": "Generalized Hyperbolic",
    "save_logs": false,
    "strategy_function_params" : {
        "amount_multiple":   1.0,
        
        "percent_allocated": 0.5,
        
        "rebalance_threshold_down":  0.8,
        "rebalance_threshold_up":  1.2,

        "max_rebalances": 365,
        "rebalance_every": 365,

        "cash_interest": 0.01,
        "coin_interest": 0.01,

        
        "option_every_itervals":  365 ,
        "option_duration":  360,
        "option_amount_pct_of_notional":  0.3,
        "option_straddle_pct_from_strike":   0.07,
        
        "ticker_name" :  "ETH",
        "all_series_backtest" : false,
        "benchmark_strategy_name": "Buy and Hold"

        
        
                                }
    ,"plot_params": {"show_plot": false
                    ,"ci":0.975
                    }

}

Sample parametres for the log-normal random walk simulation:

{
    "data_mode": "simulation",
    "return_function_params": {
        "mu": 0,
        "sigma": 0.25,
        "current_price": 100,
        "N": 100,
        "T": 365
    },
    "return_function": "Lognormal Random Walk",
    "save_logs": false,
    "strategy_function_params" : {
        "amount_multiple":   1.0,
        
        "percent_allocated": 0.5,
        
        "rebalance_threshold_down":  0.8,
        "rebalance_threshold_up":  1.2,

        "max_rebalances": 365,
        "rebalance_every": 365,

        "cash_interest": 0.04,
        "coin_interest": 0.05,

        
        "option_every_itervals":  30 ,
        "option_duration":  22,
        "option_amount_pct_of_notional":  0.3,
        "option_straddle_pct_from_strike":   0.07,
        
        "ticker_name" :  "ETH",
        "all_series_backtest" : true,
        "benchmark_strategy_name": "Buy and Hold"

        
        
                                }
    ,"plot_params": {"show_plot": false
                    ,"ci":0.975
                    }

}

Distribution-specific params:

  • Generalized Hyperbolic Params:
{"return_function_params": {
    "mu": -0.0001,    # Smaller movements
    "alpha": 0.5,     # Lower variance
    "beta": 0.1,      # Less skewness
    "delta": 0.001,   # Less extreme events
    "lambda": -0.22   # Negative lambda for lighter tails
}}
  • Lognormal Random Walk:
{"return_function_params": {
        "mu": 0, #center
        "sigma": 0.25, #standard deviation
        "current_price": 100,
        "N": 100,
        "T": 365
    }
}

For your specific run adjust parameters if needed.

  1. Run the simulation To run the simulation by executing the Python scripy:
python run_simulation.py

Upon the simulation completion, results will be saved to the respective subfolder under the data/runs/<RUNID>, where RUNID is a unique id for the simulation with the format: YYYYMMDDHHMMSS

  1. Analyse Results Simulation results will be available in the result folder, which includes:
    1. Plots
    2. Run logs
    3. Summary statistics
    4. Pickled time series data

How to use

There are three ways you can use the MCS engine:

  1. CLI by calling run_simulation.py with parameters in the config.json file and output under data/runs/<run_ud>
  2. Web UI on the grading engine (see UI Frontend section)
  3. API endpoint on the flask engine (see API Backend section)

UI Frontend

There's a frontend available that is run on the gradio engine. To enable the frontend, run:

python run_gui.py

Then nagivate to the https://localhost:9085. If the envirement is properly set up, you shuld see the app:

You can run simulation or backtest by pressing the Run Simulation button. Below is an example of the simulation:

API Backeind

There's also an API backend avaialbe for the app that is run on the flask endinge. To enable the API endpoint, run:

python run_api.py 12345

Then you can access the API andpoint at:

curl -d {<params from the config.josn>} -X POST http://localhost:12345/simulation

Docker Container

There's a Dockerfile available, that runs both API and GUI at different ports. You can use it to serve the app as an standalone application or as a part of a bigger framework.

About

Monte Carlo Simulations for model portfolio with parameters

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published