diff --git a/PaPP v2/PaPP_Median.mq5 b/PaPP v2/PaPP_Median.mq5 index 981f552..0bd519f 100644 --- a/PaPP v2/PaPP_Median.mq5 +++ b/PaPP v2/PaPP_Median.mq5 @@ -10,9 +10,11 @@ #property indicator_buffers 8 #property indicator_plots 8 -input int FontSize = 9; -input bool Smooth = true; // interpola tra i valori D1 -> linea morbida -input bool ShowMA = true; // mostra anche le 7 MA (oltre alla mediana) +input int FontSize = 9; +input bool Smooth = true; // interpola tra i valori D1 -> linea morbida +input bool ShowMA = true; // mostra anche le 7 MA (oltre alla mediana) +input bool ShowPanel = true; // box con sfondo dietro al pannello info +input color PanelBg = C'20,20,25'; // colore sfondo del box #define ANCHOR_TF PERIOD_D1 // calcolo ancorato a D1: linea identica su ogni TF #define KSLOPE 5 // barre D1 per velocita'/accelerazione @@ -277,6 +279,40 @@ string DirWord(double val,double absPct,string up,string down,string flat) string forza = (absPct<0.40)?"lieve":((absPct<0.70)?"moderata":"forte"); return (val>0?up:down)+" "+forza; } +//+------------------------------------------------------------------+ +// descrizione a parole per la distanza prezzo-mediana (con segno) +string DistWord(double val,double absPct) + { + if(absPct<0.15) return "sulla mediana"; + string q = (absPct<0.40)?"poco":((absPct<0.70)?"moderatamente":"molto"); + return q+(val>0?" sopra":" sotto"); + } +//+------------------------------------------------------------------+ +// mediana delle 7 MA a barra j dai buffer mb +double MedMAatJ(PBuf &mb[],int j) + { + double v[7]; int c=0; + for(int m=0;m<7;m++){ double x=mb[m].v[j]; if(IsVal(x)) v[c++]=x; } + return MedArr(v,c); + } +//+------------------------------------------------------------------+ +// distanza prezzo-mediana: percentile storico (su |valore|) sugli ultimi 'win' giorni D1 +void DistStats(int win,double curDist,double &absPct) + { + absPct=0.5; + int need=win+2; + double cl[]; ArraySetAsSeries(cl,true); + if(CopyClose(_Symbol,ANCHOR_TF,0,need,cl)!=need) return; + PBuf mb[7]; + for(int m=0;m<7;m++){ ArraySetAsSeries(mb[m].v,true); if(CopyBuffer(hMA[m],0,0,need,mb[m].v)!=need) return; } + double dA[]; int dc=0; ArrayResize(dA,win); + for(int j=1;j<=win;j++) + { + double md=MedMAatJ(mb,j); if(md<=0) continue; + dA[dc++]=MathAbs((cl[j]-md)/md*100.0); + } + absPct=PctlOf(dA,dc,MathAbs(curDist)); + } //+------------------------------------------------------------------+ // interpolazione lineare con fallback se uno dei valori manca @@ -410,47 +446,55 @@ void DrawInfo() double median = MedianAtD1(1); // valore D1 chiuso (stabile, non-repaint) if(median<=0) return; - int x=10,y=30,lh=FontSize+4; - ObjectsDeleteAll(0,_pfx); - - Lbl("T","PaPP Median ["+EnumToString((ENUM_TIMEFRAMES)_Period)+"]",x,y,FontSize+2,clrGold,true); - y+=lh+4; + int x=10,y0=30,lh=FontSize+4; + //--- 1) calcolo metriche e preparo tutte le stringhe double distPct = (bid-median)/median*100; - string distS = StringFormat("%+.2f%%",distPct); - color distC = (distPct>0)?clrRed:clrLimeGreen; - Lbl("M","Median: "+DoubleToString(median,_Digits),x,y,FontSize,clrGold,false); y+=lh; - Lbl("D","Dist: "+distS,x,y,FontSize,distC,true); y+=lh+2; + color distC = (distPct>0)?clrRed:clrLimeGreen; + double distAbsPct; DistStats(CLWIN,distPct,distAbsPct); - //--- Cluster/Velocita'/Accelerazione/Volatilita' con percentile storico + descrizione a parole double cluCur,cluPct,velCur,velPct,accCur,accPct,volCur,volPct; AllMetrics(CLWIN,cluCur,cluPct,velCur,velPct,accCur,accPct,volCur,volPct); - string cluTxt = MagWord(cluPct,"molto stretto","stretto","normale","largo","molto largo"); - color cluC = (cluPct<0.40)?clrLimeGreen:((cluPct>0.60)?clrTomato:clrSilver); - Lbl("V",StringFormat("Cluster %.3f%% - %s (P%.0f)",cluCur,cluTxt,cluPct*100),x,y,FontSize,cluC,false); y+=lh; + string sT = "PaPP Median ["+EnumToString((ENUM_TIMEFRAMES)_Period)+"]"; + string sM = "Median: "+DoubleToString(median,_Digits); + string sD = StringFormat("Dist: %+.2f%% - %s (P%.0f)",distPct,DistWord(distPct,distAbsPct),distAbsPct*100); + string sV = StringFormat("Cluster %.3f%% - %s (P%.0f)",cluCur,MagWord(cluPct,"molto stretto","stretto","normale","largo","molto largo"),cluPct*100); + string sVE = StringFormat("Velocita' %+.3f%% - %s",velCur,DirWord(velCur,velPct,"in salita","in discesa","quasi piatta")); + string sAC = StringFormat("Accel %+.4f%% - %s",accCur,DirWord(accCur,accPct,"in accelerazione","in decelerazione","stabile")); + string sVO = StringFormat("Volatilita' %.4f%% - %s (P%.0f)",volCur,MagWord(volPct,"molto bassa","bassa","normale","alta","molto alta"),volPct*100); + string sMA[7]; + for(int m=0;m<7;m++) sMA[m]=StringFormat("MA %3dg: %s",gDays[m],DoubleToString(GetMA(m,1),_Digits)); + string sH = "Sopra=SELL | Sotto=BUY"; - string velTxt = DirWord(velCur,velPct,"in salita","in discesa","quasi piatta"); - Lbl("VEL",StringFormat("Velocita' %+.3f%% - %s",velCur,velTxt),x,y,FontSize,(velCur>0)?clrLimeGreen:clrTomato,false); y+=lh; + //--- 2) larghezza/altezza del box dal contenuto + int maxc=StringLen(sT); + int lens[7]; lens[0]=StringLen(sM); lens[1]=StringLen(sD); lens[2]=StringLen(sV); + lens[3]=StringLen(sVE); lens[4]=StringLen(sAC); lens[5]=StringLen(sVO); lens[6]=StringLen(sH); + for(int k=0;k<7;k++) if(lens[k]>maxc) maxc=lens[k]; + for(int m=0;m<7;m++) if(StringLen(sMA[m])>maxc) maxc=StringLen(sMA[m]); + int boxX=x-6, boxY=y0-6; + int w=(int)(maxc*FontSize*0.62)+18; + int bodyH=15*lh+6; // 7 righe in alto + 7 MA + riga finale + padding + PanelBox(boxX,boxY,w,bodyH); - string accTxt = DirWord(accCur,accPct,"in accelerazione","in decelerazione","stabile"); - Lbl("ACC",StringFormat("Accel %+.4f%% - %s",accCur,accTxt),x,y,FontSize,(accCur>0)?clrLimeGreen:clrTomato,false); y+=lh; - - string volTxt = MagWord(volPct,"molto bassa","bassa","normale","alta","molto alta"); - color volC = (volPct<0.40)?clrLimeGreen:((volPct>0.60)?clrTomato:clrAqua); - Lbl("VOL",StringFormat("Volatilita' %.4f%% - %s (P%.0f)",volCur,volTxt,volPct*100),x,y,FontSize,volC,false); y+=lh+2; - - //--- Valori delle 7 MA (giorni : valore) con colore = linea + //--- 3) scritte (aggiornate in-place: niente ObjectsDeleteAll -> niente lampeggio) + int y=y0; + Lbl("T",sT,x,y,FontSize+2,clrGold,true); y+=lh+4; + Lbl("M",sM,x,y,FontSize,clrGold,false); y+=lh; + Lbl("D",sD,x,y,FontSize,distC,true); y+=lh+2; + Lbl("V",sV,x,y,FontSize,(cluPct<0.40)?clrLimeGreen:((cluPct>0.60)?clrTomato:clrSilver),false); y+=lh; + Lbl("VEL",sVE,x,y,FontSize,(velCur>0)?clrLimeGreen:clrTomato,false); y+=lh; + Lbl("ACC",sAC,x,y,FontSize,(accCur>0)?clrLimeGreen:clrTomato,false); y+=lh; + Lbl("VOL",sVO,x,y,FontSize,(volPct<0.40)?clrLimeGreen:((volPct>0.60)?clrTomato:clrAqua),false); y+=lh+2; for(int m=0;m<7;m++) { double v=GetMA(m,1); - Lbl("a"+IntegerToString(m), - StringFormat("MA %3dg: %s",gDays[m],DoubleToString(v,_Digits)), - x,y,FontSize-1,(v>0)?gCol[m]:clrGray,false); + Lbl("a"+IntegerToString(m),sMA[m],x,y,FontSize-1,(v>0)?gCol[m]:clrGray,false); y+=lh-2; } y+=2; - Lbl("H","Sopra=SELL | Sotto=BUY",x,y,FontSize-1,clrGray,false); + Lbl("H",sH,x,y,FontSize-1,clrGray,false); } //+------------------------------------------------------------------+ @@ -498,6 +542,27 @@ void Lbl(string n,string t,int x,int y,int fs,color c,bool b) ObjectSetInteger(0,o,OBJPROP_FONTSIZE,fs); ObjectSetInteger(0,o,OBJPROP_COLOR,c); ObjectSetInteger(0,o,OBJPROP_BACK,false); + ObjectSetInteger(0,o,OBJPROP_ZORDER,1); // sopra il box di sfondo + ObjectSetInteger(0,o,OBJPROP_SELECTABLE,false); + ObjectSetInteger(0,o,OBJPROP_HIDDEN,true); + } +//+------------------------------------------------------------------+ +// riquadro di sfondo del pannello (dietro alle scritte) +void PanelBox(int px,int py,int w,int h) + { + string o=_pfx+"BOX"; + if(!ShowPanel) { ObjectDelete(0,o); return; } + if(ObjectFind(0,o)<0) ObjectCreate(0,o,OBJ_RECTANGLE_LABEL,0,0,0); + ObjectSetInteger(0,o,OBJPROP_CORNER,CORNER_LEFT_UPPER); + ObjectSetInteger(0,o,OBJPROP_XDISTANCE,px); + ObjectSetInteger(0,o,OBJPROP_YDISTANCE,py); + ObjectSetInteger(0,o,OBJPROP_XSIZE,w); + ObjectSetInteger(0,o,OBJPROP_YSIZE,h); + ObjectSetInteger(0,o,OBJPROP_BGCOLOR,PanelBg); + ObjectSetInteger(0,o,OBJPROP_BORDER_TYPE,BORDER_FLAT); + ObjectSetInteger(0,o,OBJPROP_COLOR,clrDimGray); // bordo + ObjectSetInteger(0,o,OBJPROP_BACK,false); + ObjectSetInteger(0,o,OBJPROP_ZORDER,0); // dietro alle scritte ObjectSetInteger(0,o,OBJPROP_SELECTABLE,false); ObjectSetInteger(0,o,OBJPROP_HIDDEN,true); }