//+------------------------------------------------------------------+ //| EP_Demo.mq5 | //| MMQ — Muhammad Minhas Qamar | //| www.mql5.com/en/articles/24757 | //+------------------------------------------------------------------+ #property copyright "MMQ — Muhammad Minhas Qamar" #property link "https://www.mql5.com/en/articles/24757" #property version "1.00" #property strict #property script_show_inputs #property description "Builds one basket's joint history, compares the probability vectors" #property description "a risk number can stand on, conditions on today's volatility, adds" #property description "three views, and reports what each step does to the book's risk." #include #include #include #include #include input group "Data" input string InpSymbols = "EURUSD,GBPUSD,AUDUSD,USDJPY,USDCHF"; // basket input string InpWeights = "0.2,0.2,0.2,-0.2,-0.2"; // book notional per symbol, same order input ENUM_TIMEFRAMES InpTimeframe = PERIOD_D1; // bar size of one scenario input int InpBars = 5000; // bars requested per symbol input datetime InpAsOf = 0; // replay as of this date, 0 for today input group "Prior" input int InpWindow = 250; // rolling window it is compared with input double InpHalfLife = 250.0; // exponential decay half-life, bars input group "State" input int InpStateWindow = 20; // trailing volatility of the book, bars input double InpMinEns = 250; // fewest effective scenarios conditioning may leave input double InpKernelBand = 0.5; // kernel bandwidth, share of the state's std input group "Views" input string InpVolSymbol = "USDJPY"; // volatility view on input double InpVolScale = 1.5; // times its conditioned volatility input string InpTailSymbol = "USDCHF"; // tail view on input double InpTailMove = -0.02; // daily log return at or below input double InpTailProb = 0.01; // has at least this probability input string InpCorrA = "EURUSD"; // correlation view between input string InpCorrB = "GBPUSD"; // and input double InpCorr = 0.40; // at most this correlation input bool InpCorrPinVol = true; // hold both vols, so only the correlation moves input double InpConfidence = 1.0; // weight on the views, 0..1 input group "Risk" input double InpLevel = 0.025; // tail level for VaR and ES //--- one probability vector and its label struct SStage { string name; string code; // short label for wide tables vector p; }; SStage g_stage[]; //+------------------------------------------------------------------+ void AddStage(const string name,const string code,const vector &p) { const int n=ArraySize(g_stage); ArrayResize(g_stage,n+1); g_stage[n].name=name; g_stage[n].code=code; g_stage[n].p =p; } //+------------------------------------------------------------------+ //| Split a comma list, trimming spaces. | //+------------------------------------------------------------------+ int SplitList(const string text,string &out[]) { const int n=StringSplit(text,',',out); for(int i=0;i=1 && m.es<0.0 ? StringFormat("%.2fx",es_ref/m.es) : "")); } PrintFormat(" size: position multiple that keeps the %.1f%% ES of the rolling window", 100.0*InpLevel); } //+------------------------------------------------------------------+ void ReportSymbols(const CEpScenarios &scen,const int n) { const double ann=MathSqrt(252.0); Print("--- annualised volatility per symbol ----------------------------"); string head=StringFormat(" %-24s",""),line; for(int k=0;k0 ? ", as of "+TimeToString(InpAsOf,TIME_DATE) : "")); Print("================================================================"); CEpScenarios scen; if(!LoadScenarios(scen,symbols)) return; if(InpAsOf>0 && !scen.KeepUntil(InpAsOf)) { Print("EP_Demo: too little history before the replay date"); return; } //--- the book as a column, then its trailing volatility as the state vector book; EpPortfolio(scen,cols,weights,book); double values[]; ArrayResize(values,scen.Rows()); for(int t=0;t