PEF Portfolio Efficiency Front
- Utilità
- Better Trader Every Day
- Versione: 2.0
- Aggiornato: 7 settembre 2025
- Attivazioni: 5
A more detailed explanation with examples of PEF input/output is found in this blog page.
1. Overview
The PEF script is a tool for portfolio optimization. It computes the Pareto Efficient Frontier between portfolio return and risk (either CVaR or Standard Deviation) using Monte Carlo simulations and bootstrapping methods.
The script enables traders and quantitative analysts to:
-
Load historical price data for multiple instruments.
-
Simulate portfolio outcomes with random or custom weight allocations.
-
Estimate performance metrics such as mean return, CVaR, standard deviation, Sharpe ratio.
-
Identify and export the Pareto frontier portfolios to a CSV file for further analysis.
2. Input Parameters
Price Data
-
InstrumentsList : Comma-separated list of symbols (e.g., "XAUUSD, USDJPY, EURUSD" ).
-
historyBars : Number of historical bars to load for each symbol.
-
DataTimeFrame : Timeframe of data (e.g., PERIOD_D1 ).
Simulation Parameters
-
ParetoAxesSelection : Selects which risk metric is used on the horizontal axis:
-
RetVsCVaR : Return vs Conditional Value-at-Risk.
-
RetVsStd : Return vs Standard Deviation.
-
-
SimuDays : Number of trading days to simulate (e.g., 21.6 ≈ 1 month).
-
SimMethodSelection : Method for simulations:
-
MCBootstraping : Monte Carlo with bootstrapping (resampled returns).
-
MC : Monte Carlo only (using historical return distribution).
-
-
NumberMCsimulations : Number of portfolio simulations.
-
NumberBootstrapTrials : Number of bootstrap resamples (only for bootstrapping).
-
randMethod : Random number generation method:
-
Parallel : Faster, less random (pre-generated random numbers).
-
Sequential : Slower, more random (generated sequentially).
-
-
AnnualRiskFreeReturnPct : Annualized risk-free return (%) used in Sharpe ratio.
-
BetaConfidence : Confidence level for CVaR (default 0.95).
-
customWeights : Comma-separated list of custom portfolio weights (optional).
Output Parameters
-
printEqualWeightSet : Show results for equal-weight portfolio.
-
printSepCovCorr : Print covariance and correlation matrices separately.
-
printCovCorr : Print combined covariance-correlation matrix.
-
saveCSVfile : Save Pareto front to a CSV file.
-
AddNonPareto2CSVfile : Include non-Pareto portfolios in CSV.
-
ParetoCSVfilename : Name of the output CSV file.
3. Methodology
-
Data Collection
-
Historical closing prices are fetched for the selected instruments.
-
Returns are calculated as percentage price changes.
-
-
Portfolio Simulations
-
A large number of random weight vectors are generated.
-
Two possible simulation methods:
-
MC (Monte Carlo): Direct simulation from historical return distribution.
-
MCBootstraping: Resampling returns with replacement to account for variability.
-
-
-
Risk & Return Calculation
-
For each weight set, the script computes:
-
Mean Return (%).
-
CVaR (%): Average of the worst (1–β)% outcomes.
-
Standard Deviation (%).
-
-
-
Pareto Frontier Extraction
-
A portfolio is Pareto-optimal if no other portfolio achieves higher return with equal or lower risk.
-
Non-dominated portfolios are selected as the Pareto front.
-
-
Sharpe Ratio Optimization
-
The script finds the portfolio with the maximum Sharpe ratio relative to the chosen risk-free rate.
-
4. Outputs
Expert Tab in MetaTrader5
-
Input parameter summary.
-
Covariance/correlation matrices.
-
Pareto frontier table (risk, return, CVaR/std, weights).
-
Equal-weight portfolio performance.
-
Custom-weight portfolio performance (if provided).
-
Maximum Sharpe ratio portfolio.
CSV Output (if enabled)
-
File saved under MQL5/Files/ with name specified in ParetoCSVfilename .
-
Columns include:
-
Risk (CVaR% or Std%),
-
Mean Return%,
-
Secondary risk metric,
-
Asset weights.
-
All other output can be seen in the Expert tab of the MetaTrader5 terminal.
