Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a21fd7519e | |||
| 8e17edd4ae | |||
| 01907379e2 | |||
| 721c7965e8 | |||
| 3c271745e1 | |||
| b0012bf2fd | |||
| a1023b573e | |||
| cf3525f7af | |||
| 5cee3f06fd | |||
| f930e88592 | |||
| 908d0e9187 | |||
| 64dc36b1c3 | |||
| 8e6fe91b70 | |||
| a6f6c0510b | |||
| 20315441a5 | |||
| f1d1a2230f | |||
| 4a590b805a | |||
| b79cf56412 | |||
| b45039f91f | |||
| ae7551ef9d | |||
| 2c32e233e6 | |||
| 03e1b290b5 |
@@ -0,0 +1,241 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| PaPP_CrossExport.mq5 |
|
||||
//| PaPP v2 |
|
||||
//| Esporta in CSV TUTTI gli incroci tra le 9 linee (prezzo+Mediana |
|
||||
//| +7 MA), calcolati ANCORATI a D1 (come l'indicatore), con il |
|
||||
//| contesto di ogni incrocio e gli esiti futuri a 1/3/5/10/20 g. |
|
||||
//| Si puo' esportare un intervallo di date oppure tutto lo storico. |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "PaPP v2"
|
||||
#property version "1.00"
|
||||
#property script_show_inputs
|
||||
|
||||
//--- input
|
||||
input string InpSymbol = ""; // simbolo ("" = simbolo del grafico)
|
||||
input bool InpAllHistory = true; // true = tutto lo storico D1
|
||||
input datetime InpStart = D'2010.01.01'; // inizio intervallo (se AllHistory=false)
|
||||
input datetime InpEnd = D'2100.01.01'; // fine intervallo (se AllHistory=false)
|
||||
input string InpFileName = ""; // nome file ("" = automatico)
|
||||
|
||||
//--- costanti (stesse 7 MA dell'indicatore)
|
||||
#define ANCHOR_TF PERIOD_D1
|
||||
#define NSER 9 // PRICE, MED, MA365..MA3
|
||||
#define KSLOPE 5 // barre per pendenza / variazione cluster
|
||||
#define MAXH 20 // orizzonte massimo per gli esiti
|
||||
|
||||
int gDays[7] = {365,182,121,30,14,7,3};
|
||||
string SER[NSER]= {"PRICE","MED","MA365","MA182","MA121","MA30","MA14","MA7","MA3"};
|
||||
int gHor[5] = {1,3,5,10,20};
|
||||
|
||||
//--- contenitore per un buffer MA dinamico (MQL5 non permette double ma[7][])
|
||||
struct MABuf { double v[]; };
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
int TimeToBarsD1(string sym,int d)
|
||||
{
|
||||
datetime n = TimeCurrent();
|
||||
if(n==0) return MathMax(1,(int)((long)d*86400L/PeriodSeconds(ANCHOR_TF)));
|
||||
return MathMax(1,Bars(sym,ANCHOR_TF,n-(long)d*86400,n));
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// valore valido = positivo e finito (MT5 usa EMPTY_VALUE=DBL_MAX nei warmup)
|
||||
bool IsVal(double v) { return (v>0.0 && v<1.0e12); }
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
double Median7(double &v[]) // mediana dei valori validi in v[0..6]
|
||||
{
|
||||
double a[]; int c=0; ArrayResize(a,7);
|
||||
for(int m=0;m<7;m++) if(IsVal(v[m])) { a[c]=v[m]; c++; }
|
||||
if(c==0) return 0;
|
||||
ArrayResize(a,c); ArraySort(a);
|
||||
if((c&1)==1) return a[c/2];
|
||||
return 0.5*(a[c/2-1]+a[c/2]);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
string sym = (InpSymbol=="") ? _Symbol : InpSymbol;
|
||||
if(!SymbolSelect(sym,true)) { Print("Simbolo non valido: ",sym); return; }
|
||||
int digits = (int)SymbolInfoInteger(sym,SYMBOL_DIGITS);
|
||||
|
||||
//--- handle delle 7 MA su D1 + ATR
|
||||
int hMA[7];
|
||||
for(int m=0;m<7;m++)
|
||||
{
|
||||
int per = TimeToBarsD1(sym,gDays[m]);
|
||||
hMA[m] = iMA(sym,ANCHOR_TF,per,0,MODE_SMA,PRICE_CLOSE);
|
||||
if(hMA[m]==INVALID_HANDLE) { Print("iMA fallita m=",m); return; }
|
||||
}
|
||||
int hATR = iATR(sym,ANCHOR_TF,14);
|
||||
if(hATR==INVALID_HANDLE) { Print("iATR fallita"); return; }
|
||||
|
||||
//--- attendo che lo storico D1 sia pronto
|
||||
int total=0;
|
||||
for(int t=0; t<100; t++)
|
||||
{
|
||||
total = Bars(sym,ANCHOR_TF);
|
||||
bool ok = (total>MAXH+KSLOPE+50);
|
||||
for(int m=0;m<7 && ok;m++) if(BarsCalculated(hMA[m])<total) ok=false;
|
||||
if(ok && BarsCalculated(hATR)>=total) break;
|
||||
Sleep(100);
|
||||
}
|
||||
total = Bars(sym,ANCHOR_TF);
|
||||
if(total<=MAXH+KSLOPE+50) { Print("Storico D1 insufficiente: ",total," barre"); return; }
|
||||
|
||||
//--- carico tutta la storia D1 (indice 0 = piu' vecchio)
|
||||
datetime tm[]; double cl[],hi[],lo[],atr[];
|
||||
MABuf ma[7];
|
||||
ArraySetAsSeries(tm,false); ArraySetAsSeries(cl,false);
|
||||
ArraySetAsSeries(hi,false); ArraySetAsSeries(lo,false);
|
||||
ArraySetAsSeries(atr,false);
|
||||
if(CopyTime(sym,ANCHOR_TF,0,total,tm)<=0) { Print("CopyTime KO"); return; }
|
||||
if(CopyClose(sym,ANCHOR_TF,0,total,cl)<=0) { Print("CopyClose KO"); return; }
|
||||
if(CopyHigh(sym,ANCHOR_TF,0,total,hi)<=0) { Print("CopyHigh KO"); return; }
|
||||
if(CopyLow(sym,ANCHOR_TF,0,total,lo)<=0) { Print("CopyLow KO"); return; }
|
||||
if(CopyBuffer(hATR,0,0,total,atr)<=0) { Print("CopyATR KO"); return; }
|
||||
for(int m=0;m<7;m++)
|
||||
{
|
||||
ArraySetAsSeries(ma[m].v,false);
|
||||
if(CopyBuffer(hMA[m],0,0,total,ma[m].v)<=0) { Print("CopyBuffer MA KO m=",m); return; }
|
||||
}
|
||||
int n = ArraySize(cl);
|
||||
|
||||
//--- costruisco le 9 serie allineate per barra: S[i][k]
|
||||
double S[][NSER];
|
||||
ArrayResize(S,n);
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
double mv[7];
|
||||
for(int m=0;m<7;m++) mv[m] = ma[m].v[i];
|
||||
S[i][0] = cl[i]; // PRICE
|
||||
S[i][1] = Median7(mv); // MED
|
||||
for(int m=0;m<7;m++) S[i][2+m] = mv[m]; // MA365..MA3
|
||||
}
|
||||
|
||||
//--- intervallo di scansione (eventi)
|
||||
datetime from = InpAllHistory ? 0 : InpStart;
|
||||
datetime to = InpAllHistory ? TimeCurrent(): InpEnd;
|
||||
int iStart = KSLOPE+1, iEnd = n-1;
|
||||
for(int i=KSLOPE+1;i<n;i++) if(tm[i]>=from) { iStart=i; break; }
|
||||
for(int i=n-1;i>=0;i--) if(tm[i]<=to) { iEnd=i; break; }
|
||||
|
||||
//--- apro il file CSV
|
||||
string fname = (InpFileName=="") ? ("PaPP_crosses_"+sym+"_D1.csv") : InpFileName;
|
||||
int fh = FileOpen(fname,FILE_WRITE|FILE_TXT|FILE_ANSI);
|
||||
if(fh==INVALID_HANDLE) { Print("FileOpen KO: ",fname," err=",GetLastError()); return; }
|
||||
|
||||
//--- header
|
||||
string head = "time,symbol,pair,a,b,dir,price,med,ma365,ma182,ma121,ma30,ma14,ma7,ma3,"
|
||||
"dist_med_pct,cluster_pct,cluster_exp,slope_a,slope_b,trend,atr_pct,dow,month";
|
||||
for(int hh=0;hh<5;hh++)
|
||||
{
|
||||
string s=IntegerToString(gHor[hh]);
|
||||
head += ",ret_"+s+",mfe_"+s+",mae_"+s+",dir_"+s+",rev_"+s;
|
||||
}
|
||||
for(int b=1;b<=MAXH;b++) head += ",cret_"+IntegerToString(b); // traiettoria forward bar-by-bar
|
||||
head += ",bars_to_revert,disc_max_pct";
|
||||
FileWriteString(fh,head+"\r\n");
|
||||
|
||||
//--- scansione incroci
|
||||
long rows=0;
|
||||
for(int i=iStart;i<=iEnd;i++)
|
||||
{
|
||||
if(i<KSLOPE+1) continue;
|
||||
|
||||
//--- esporto solo barre con TUTTE le 9 serie valide (cluster completo)
|
||||
bool allok=true;
|
||||
for(int k=0;k<NSER && allok;k++)
|
||||
if(!IsVal(S[i][k]) || !IsVal(S[i-1][k]) || !IsVal(S[i-KSLOPE][k])) allok=false;
|
||||
if(!allok) continue;
|
||||
|
||||
double price=S[i][0], med=S[i][1], ma365=S[i][2];
|
||||
|
||||
//--- feature comuni a tutti gli incroci di questa barra
|
||||
double dist_med = (price-med)/med*100.0;
|
||||
double cmax=-1, cmin=1e18;
|
||||
for(int k=2;k<NSER;k++){ double v=S[i][k]; if(v>cmax)cmax=v; if(v<cmin)cmin=v; }
|
||||
double cluster = (cmax-cmin)/price*100.0;
|
||||
double cmaxP=-1,cminP=1e18;
|
||||
for(int k=2;k<NSER;k++){ double v=S[i-KSLOPE][k]; if(v>cmaxP)cmaxP=v; if(v<cminP)cminP=v; }
|
||||
double clusterP = (cmaxP-cminP)/S[i-KSLOPE][0]*100.0;
|
||||
double cluster_exp = cluster-clusterP;
|
||||
int trend = (price>ma365)?1:-1;
|
||||
double atr_pct = atr[i]/price*100.0;
|
||||
MqlDateTime dt; TimeToStruct(tm[i],dt);
|
||||
|
||||
//--- esito globale: ritorno alla Mediana entro MAXH
|
||||
int side = (price>med)?1:-1;
|
||||
int b2rev=-1; double discMax=0;
|
||||
int jmax = MathMin(i+MAXH,n-1);
|
||||
for(int j=i+1;j<=jmax;j++)
|
||||
{
|
||||
if(!IsVal(S[j][0]) || !IsVal(S[j][1])) continue;
|
||||
int sj=(S[j][0]>S[j][1])?1:-1;
|
||||
if(b2rev<0 && sj!=side) b2rev=j-i;
|
||||
double d=(S[j][0]-S[j][1])/S[j][1]*100.0;
|
||||
if(MathAbs(d)>MathAbs(discMax)) discMax=d;
|
||||
}
|
||||
|
||||
//--- controllo ogni coppia (a,b)
|
||||
for(int a=0;a<NSER;a++)
|
||||
for(int b=a+1;b<NSER;b++)
|
||||
{
|
||||
double d0=S[i-1][a]-S[i-1][b];
|
||||
double d1=S[i][a]-S[i][b];
|
||||
int dir=0;
|
||||
if(d0<=0 && d1>0) dir=1; else if(d0>=0 && d1<0) dir=-1;
|
||||
if(dir==0) continue;
|
||||
|
||||
double slope_a = (S[i][a]-S[i-KSLOPE][a])/S[i-KSLOPE][a]*100.0;
|
||||
double slope_b = (S[i][b]-S[i-KSLOPE][b])/S[i-KSLOPE][b]*100.0;
|
||||
double thr = 0.25*atr_pct;
|
||||
|
||||
string row = StringFormat("%s,%s,%sx%s,%s,%s,%d,",
|
||||
TimeToString(tm[i],TIME_DATE),sym,SER[a],SER[b],SER[a],SER[b],dir);
|
||||
row += DoubleToString(price,digits)+","+DoubleToString(med,digits)+",";
|
||||
for(int k=2;k<NSER;k++) row += DoubleToString(S[i][k],digits)+",";
|
||||
row += DoubleToString(dist_med,5)+","+DoubleToString(cluster,5)+","
|
||||
+DoubleToString(cluster_exp,5)+","+DoubleToString(slope_a,5)+","
|
||||
+DoubleToString(slope_b,5)+","+IntegerToString(trend)+","
|
||||
+DoubleToString(atr_pct,5)+","+IntegerToString(dt.day_of_week)+","
|
||||
+IntegerToString(dt.mon);
|
||||
|
||||
//--- esiti per orizzonte
|
||||
for(int hh=0;hh<5;hh++)
|
||||
{
|
||||
int h=gHor[hh], iH=i+h;
|
||||
if(iH>n-1 || S[i][0]<=0) { row += ",,,,,"; continue; }
|
||||
double ret=(cl[iH]-cl[i])/cl[i]*100.0;
|
||||
double mxh=-1e18,mnl=1e18;
|
||||
for(int j=i+1;j<=iH;j++){ if(hi[j]>mxh)mxh=hi[j]; if(lo[j]<mnl)mnl=lo[j]; }
|
||||
double mfe=(mxh-cl[i])/cl[i]*100.0;
|
||||
double mae=(mnl-cl[i])/cl[i]*100.0;
|
||||
int dlab=(ret>thr)?1:((ret<-thr)?-1:0);
|
||||
int rev=(b2rev>0 && b2rev<=h)?1:0;
|
||||
row += ","+DoubleToString(ret,5)+","+DoubleToString(mfe,5)+","
|
||||
+DoubleToString(mae,5)+","+IntegerToString(dlab)+","+IntegerToString(rev);
|
||||
}
|
||||
//--- traiettoria: rendimento cumulato a +1..+MAXH barre
|
||||
for(int b=1;b<=MAXH;b++)
|
||||
{
|
||||
int iB=i+b;
|
||||
if(iB>n-1) { row += ","; continue; }
|
||||
row += ","+DoubleToString((cl[iB]-cl[i])/cl[i]*100.0,5);
|
||||
}
|
||||
row += ","+(b2rev>0?IntegerToString(b2rev):"")+","+DoubleToString(discMax,5);
|
||||
FileWriteString(fh,row+"\r\n");
|
||||
rows++;
|
||||
}
|
||||
if((i%200)==0) Comment(StringFormat("PaPP export: %d/%d barre, %d eventi",i-iStart,iEnd-iStart,(int)rows));
|
||||
}
|
||||
|
||||
FileClose(fh);
|
||||
for(int m=0;m<7;m++) IndicatorRelease(hMA[m]);
|
||||
IndicatorRelease(hATR);
|
||||
Comment("");
|
||||
PrintFormat("PaPP export COMPLETATO: %d incroci -> %s (cartella MQL5\\Files)",(int)rows,fname);
|
||||
Print("Periodo: ",TimeToString(tm[iStart])," -> ",TimeToString(tm[iEnd]));
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
+289
-24
@@ -10,11 +10,16 @@
|
||||
#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
|
||||
#define NVOL 14 // barre D1 per la volatilita'
|
||||
#define CLWIN 252 // finestra D1 (~1 anno) per il percentile del cluster
|
||||
|
||||
double Buff_Median[];
|
||||
double B0[],B1[],B2[],B3[],B4[],B5[],B6[];
|
||||
@@ -23,7 +28,9 @@ int gDays[7] = {365,182,121,30,14,7,3};
|
||||
color gCol[7] = {clrDodgerBlue,clrDeepSkyBlue,clrTurquoise,clrLimeGreen,clrOrange,clrTomato,clrRed};
|
||||
int hMA[7];
|
||||
int bars[7];
|
||||
string _pfx = "PM_";
|
||||
struct PBuf { double v[]; }; // buffer dinamico per copiare una MA
|
||||
string _pfx = "PM_"; // oggetti pannello
|
||||
string _pfx2 = "PME_"; // etichette a fine linea
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// scrive il valore nel buffer della MA m
|
||||
@@ -41,6 +48,23 @@ void SetBuf(int m,int idx,double v)
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// legge il valore corrente (barra 0) del buffer della MA m
|
||||
double GetBuf(int m)
|
||||
{
|
||||
switch(m)
|
||||
{
|
||||
case 0: return B0[0];
|
||||
case 1: return B1[0];
|
||||
case 2: return B2[0];
|
||||
case 3: return B3[0];
|
||||
case 4: return B4[0];
|
||||
case 5: return B5[0];
|
||||
case 6: return B6[0];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
int TimeToBars(int d)
|
||||
{
|
||||
@@ -103,6 +127,7 @@ void OnDeinit(const int reason)
|
||||
{
|
||||
for(int i=0;i<7;i++) if(hMA[i]!=INVALID_HANDLE) IndicatorRelease(hMA[i]);
|
||||
ObjectsDeleteAll(0,_pfx);
|
||||
ObjectsDeleteAll(0,_pfx2);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -135,6 +160,160 @@ double MedianAtD1(int d1shift)
|
||||
return Median(vals,cnt);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// valore valido = positivo e finito (esclude EMPTY_VALUE di warmup)
|
||||
bool IsVal(double v) { return (v>0.0 && v<1.0e12); }
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// mediana dei primi c valori di src
|
||||
double MedArr(double &src[],int c)
|
||||
{
|
||||
if(c<=0) return 0;
|
||||
double a[]; ArrayResize(a,c);
|
||||
for(int j=0;j<c;j++) a[j]=src[j];
|
||||
ArraySort(a);
|
||||
if((c&1)==1) return a[c/2];
|
||||
return 0.5*(a[c/2-1]+a[c/2]);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// Metriche a una specifica barra D1 (j), calcolate sui buffer gia' copiati mb[7]
|
||||
double ClusterAtJ(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; }
|
||||
if(c<2) return 0;
|
||||
double md=MedArr(v,c); if(md<=0) return 0;
|
||||
double ds=0; for(int m=0;m<c;m++) ds+=MathAbs(v[m]-md)/md*100.0;
|
||||
return ds/c;
|
||||
}
|
||||
double VelAtJ(PBuf &mb[],int j,int K)
|
||||
{
|
||||
double v[7]; int c=0;
|
||||
for(int m=0;m<7;m++){ double a=mb[m].v[j], b=mb[m].v[j+K]; if(IsVal(a)&&IsVal(b)) v[c++]=(a-b)/b*100.0; }
|
||||
return MedArr(v,c);
|
||||
}
|
||||
double AccAtJ(PBuf &mb[],int j,int K)
|
||||
{
|
||||
double v[7]; int c=0;
|
||||
for(int m=0;m<7;m++){ double a=mb[m].v[j], b=mb[m].v[j+K], d=mb[m].v[j+2*K];
|
||||
if(IsVal(a)&&IsVal(b)&&IsVal(d)) v[c++]=(a-2.0*b+d)/d*100.0; }
|
||||
return MedArr(v,c);
|
||||
}
|
||||
double VolAtJ(PBuf &mb[],int j,int N)
|
||||
{
|
||||
double v[7]; int c=0;
|
||||
for(int m=0;m<7;m++)
|
||||
{
|
||||
double r[]; int rc=0; ArrayResize(r,N);
|
||||
for(int t=j;t<j+N;t++){ double a=mb[m].v[t], b=mb[m].v[t+1]; if(IsVal(a)&&IsVal(b)) r[rc++]=(a-b)/b*100.0; }
|
||||
if(rc>=2)
|
||||
{
|
||||
double mn=0; for(int q=0;q<rc;q++) mn+=r[q]; mn/=rc;
|
||||
double s=0; for(int q=0;q<rc;q++){ double dd=r[q]-mn; s+=dd*dd; }
|
||||
v[c++]=MathSqrt(s/(rc-1));
|
||||
}
|
||||
}
|
||||
return MedArr(v,c);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// percentile (0..1) di cur fra i primi cc valori di arr
|
||||
double PctlOf(double &arr[],int cc,double cur)
|
||||
{
|
||||
if(cc<=0) return 0.5;
|
||||
int b=0; for(int k=0;k<cc;k++) if(arr[k]<=cur) b++;
|
||||
return (double)b/cc;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// Valori correnti + percentile storico (win giorni D1) di cluster/velocita'/accel/volatilita'
|
||||
// per i segnali con segno (vel/acc) il percentile e' calcolato sul VALORE ASSOLUTO (forza)
|
||||
void AllMetrics(int win,
|
||||
double &cluCur,double &cluPct,
|
||||
double &velCur,double &velPct,
|
||||
double &accCur,double &accPct,
|
||||
double &volCur,double &volPct)
|
||||
{
|
||||
cluCur=velCur=accCur=volCur=0; cluPct=volPct=0.5; velPct=accPct=0;
|
||||
int ext = MathMax(2*KSLOPE,NVOL+1);
|
||||
int need = win+ext+2;
|
||||
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 cA[],vA[],aA[],oA[]; int cc=0,vc=0,ac=0,oc=0;
|
||||
ArrayResize(cA,win); ArrayResize(vA,win); ArrayResize(aA,win); ArrayResize(oA,win);
|
||||
for(int j=1;j<=win;j++)
|
||||
{
|
||||
double cv=ClusterAtJ(mb,j); if(cv>0) cA[cc++]=cv;
|
||||
vA[vc++]=MathAbs(VelAtJ(mb,j,KSLOPE));
|
||||
aA[ac++]=MathAbs(AccAtJ(mb,j,KSLOPE));
|
||||
double ov=VolAtJ(mb,j,NVOL); if(ov>0) oA[oc++]=ov;
|
||||
}
|
||||
cluCur=ClusterAtJ(mb,1); velCur=VelAtJ(mb,1,KSLOPE);
|
||||
accCur=AccAtJ(mb,1,KSLOPE); volCur=VolAtJ(mb,1,NVOL);
|
||||
cluPct=PctlOf(cA,cc,cluCur);
|
||||
volPct=PctlOf(oA,oc,volCur);
|
||||
velPct=PctlOf(vA,vc,MathAbs(velCur)); // forza (su |valore|)
|
||||
accPct=PctlOf(aA,ac,MathAbs(accCur));
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// descrizione a parole per una grandezza di sola ampiezza (cluster, volatilita')
|
||||
string MagWord(double pct,string w0,string w1,string w2,string w3,string w4)
|
||||
{
|
||||
if(pct<0.20) return w0;
|
||||
if(pct<0.40) return w1;
|
||||
if(pct<0.60) return w2;
|
||||
if(pct<0.80) return w3;
|
||||
return w4;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
// descrizione a parole per un segnale con segno (velocita', accelerazione)
|
||||
string DirWord(double val,double absPct,string up,string down,string flat)
|
||||
{
|
||||
if(absPct<0.15) return 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
|
||||
double Interp(double vStart,double vEnd,double frac)
|
||||
@@ -256,46 +435,111 @@ int OnCalculate(const int rates_total,
|
||||
}
|
||||
|
||||
DrawInfo();
|
||||
DrawTags(time[0]);
|
||||
return rates_total;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// larghezza in pixel di un testo nel font del pannello
|
||||
int TxtW(string s,int fs,bool bold)
|
||||
{
|
||||
TextSetFont(bold?"Consolas Bold":"Consolas",-fs*10);
|
||||
uint w=0,h=0; TextGetSize(s,w,h);
|
||||
return (int)w;
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
void DrawInfo()
|
||||
{
|
||||
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
|
||||
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,clrWhite,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);
|
||||
|
||||
//--- Dispersione MA e bande (info)
|
||||
double ds=0; int dc=0;
|
||||
for(int m=0;m<7;m++) { double v=GetMA(m,1); if(v>0) { ds+=MathAbs(v-median)/median*100; dc++; } }
|
||||
double vol = (dc>0)?ds/dc:0;
|
||||
Lbl("V","Cluster +/-"+DoubleToString(vol,3)+"%",x,y,FontSize,clrGray,false); y+=lh+2;
|
||||
double cluCur,cluPct,velCur,velPct,accCur,accPct,volCur,volPct;
|
||||
AllMetrics(CLWIN,cluCur,cluPct,velCur,velPct,accCur,accPct,volCur,volPct);
|
||||
|
||||
//--- Valori delle 7 MA (giorni : valore)
|
||||
string sT = "PaPP Median ["+StringSubstr(EnumToString((ENUM_TIMEFRAMES)_Period),7)+"]";
|
||||
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";
|
||||
|
||||
//--- 2) larghezza/altezza del box: misuro la larghezza REALE in pixel (TextGetSize)
|
||||
int maxw=0;
|
||||
maxw=MathMax(maxw,TxtW(sT,FontSize+2,true));
|
||||
maxw=MathMax(maxw,TxtW(sM,FontSize,false));
|
||||
maxw=MathMax(maxw,TxtW(sD,FontSize,true));
|
||||
maxw=MathMax(maxw,TxtW(sV,FontSize,false));
|
||||
maxw=MathMax(maxw,TxtW(sVE,FontSize,false));
|
||||
maxw=MathMax(maxw,TxtW(sAC,FontSize,false));
|
||||
maxw=MathMax(maxw,TxtW(sVO,FontSize,false));
|
||||
maxw=MathMax(maxw,TxtW(sH,FontSize-1,false));
|
||||
for(int m=0;m<7;m++) maxw=MathMax(maxw,TxtW(sMA[m],FontSize-1,false));
|
||||
int boxX=x-6, boxY=y0-6;
|
||||
int w=maxw+(x-boxX)+10; // padding sinistro (x-boxX) + destro 10
|
||||
int bodyH=15*lh+6; // 7 righe in alto + 7 MA + riga finale + padding
|
||||
PanelBox(boxX,boxY,w,bodyH);
|
||||
|
||||
//--- 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);
|
||||
color cma = (v>median)?clrSalmon:clrLightGreen;
|
||||
Lbl("a"+IntegerToString(m),
|
||||
StringFormat("MA %3dg: %s",gDays[m],DoubleToString(v,_Digits)),
|
||||
x,y,FontSize-1,(v>0)?cma: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);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// etichetta di testo ancorata a (t,price); se price<=0 la rimuove
|
||||
void Tag(string n,datetime t,double price,string txt,color c)
|
||||
{
|
||||
string o=_pfx2+n;
|
||||
if(price<=0) { ObjectDelete(0,o); return; }
|
||||
if(ObjectFind(0,o)<0) ObjectCreate(0,o,OBJ_TEXT,0,0,0);
|
||||
ObjectSetInteger(0,o,OBJPROP_TIME,t);
|
||||
ObjectSetDouble(0,o,OBJPROP_PRICE,price);
|
||||
ObjectSetString(0,o,OBJPROP_TEXT," "+txt);
|
||||
ObjectSetString(0,o,OBJPROP_FONT,"Consolas");
|
||||
ObjectSetInteger(0,o,OBJPROP_FONTSIZE,FontSize);
|
||||
ObjectSetInteger(0,o,OBJPROP_COLOR,c);
|
||||
ObjectSetInteger(0,o,OBJPROP_ANCHOR,ANCHOR_LEFT);
|
||||
ObjectSetInteger(0,o,OBJPROP_BACK,false);
|
||||
ObjectSetInteger(0,o,OBJPROP_SELECTABLE,false);
|
||||
ObjectSetInteger(0,o,OBJPROP_HIDDEN,true);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// etichette a fine linea, nel margine vuoto a destra
|
||||
void DrawTags(datetime tlast)
|
||||
{
|
||||
datetime tfut = tlast + 2*PeriodSeconds(_Period);
|
||||
Tag("med",tfut,Buff_Median[0],"Mediana",clrGold);
|
||||
for(int m=0;m<7;m++)
|
||||
{
|
||||
double p = ShowMA ? GetBuf(m) : 0.0;
|
||||
Tag(IntegerToString(m),tfut,p,IntegerToString(gDays[m])+"g",gCol[m]);
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -311,6 +555,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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user