Files
TR_Agent/PaPP v2/PaPP_CrossExport.mq5
T
Pietro GiacobazziandDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> 884354d67e PaPP v2 export: vel/acc/vol calcolate dalle 7 medie (mediana delle scale), rimosso ATR di MT5
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-06-17 06:50:24 +00:00

336 lines
14 KiB
Plaintext

//+------------------------------------------------------------------+
//| 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)
input bool InpBaseline = true; // crea anche il file baseline (tutte le barre D1)
//--- costanti (stesse 7 MA dell'indicatore)
#define ANCHOR_TF PERIOD_D1
#define NSER 9 // PRICE, MED, MA365..MA3
#define KSLOPE 5 // barre D1 per velocita'/accelerazione
#define NVOL 14 // barre D1 per la volatilita'
#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]);
}
//+------------------------------------------------------------------+
// 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]);
}
//+------------------------------------------------------------------+
// Velocita' = mediana delle 7 pendenze (variazione % di ogni MA su K barre D1)
double VelMed(const double &S[][NSER],int i,int K)
{
double v[7]; int c=0;
for(int m=0;m<7;m++)
{ double a=S[i][2+m], b=S[i-K][2+m]; if(IsVal(a)&&IsVal(b)) v[c++]=(a-b)/b*100.0; }
return MedArr(v,c);
}
//+------------------------------------------------------------------+
// Accelerazione = mediana delle 7 seconde differenze (cambio di pendenza)
double AccMed(const double &S[][NSER],int i,int K)
{
double v[7]; int c=0;
for(int m=0;m<7;m++)
{
double a=S[i][2+m], b=S[i-K][2+m], d=S[i-2*K][2+m];
if(IsVal(a)&&IsVal(b)&&IsVal(d)) v[c++]=(a-2.0*b+d)/d*100.0;
}
return MedArr(v,c);
}
//+------------------------------------------------------------------+
// Volatilita' = mediana delle 7 dev.std dei rendimenti giornalieri di ogni MA su N barre
double VolMed(const double &S[][NSER],int i,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=i-N+1;t<=i;t++)
{ double a=S[t][2+m], b=S[t-1][2+m]; if(IsVal(a)&&IsVal(b)) r[rc++]=(a-b)/b*100.0; }
if(rc>=2)
{
double mean=0; for(int j=0;j<rc;j++) mean+=r[j]; mean/=rc;
double s=0; for(int j=0;j<rc;j++){ double dd=r[j]-mean; s+=dd*dd; }
v[c++]=MathSqrt(s/(rc-1));
}
}
return MedArr(v,c);
}
//+------------------------------------------------------------------+
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 (volatilita'/accelerazione calcolate dalle MA, no ATR di MT5)
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 LB = MathMax(2*KSLOPE,NVOL); // lookback necessario per vel/acc/vol
//--- 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+LB+50);
for(int m=0;m<7 && ok;m++) if(BarsCalculated(hMA[m])<total) ok=false;
if(ok) break;
Sleep(100);
}
total = Bars(sym,ANCHOR_TF);
if(total<=MAXH+LB+50) { Print("Storico D1 insufficiente: ",total," barre"); return; }
//--- carico tutta la storia D1 (indice 0 = piu' vecchio)
datetime tm[]; double cl[],hi[],lo[];
MABuf ma[7];
ArraySetAsSeries(tm,false); ArraySetAsSeries(cl,false);
ArraySetAsSeries(hi,false); ArraySetAsSeries(lo,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; }
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 = LB+1, iEnd = n-1;
for(int i=LB+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,vel_med,acc_med,vol_med,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");
//--- file baseline (una riga per OGNI barra D1 valida): serve per il confronto
// "extra-rendimento" = incrocio vs giorno qualunque nello stesso regime
int fhB = INVALID_HANDLE;
if(InpBaseline)
{
string bname = "PaPP_bars_"+sym+"_D1.csv";
fhB = FileOpen(bname,FILE_WRITE|FILE_TXT|FILE_ANSI);
if(fhB==INVALID_HANDLE) Print("FileOpen baseline KO: ",bname," err=",GetLastError());
else
{
string bh = "time,symbol,price,med,dist_med_pct,cluster_pct,cluster_exp,trend,vel_med,acc_med,vol_med,dow,month,bars_to_revert,disc_max_pct";
for(int b=1;b<=MAXH;b++) bh += ",cret_"+IntegerToString(b);
FileWriteString(fhB,bh+"\r\n");
}
}
//--- scansione incroci
long rows=0, brows=0;
for(int i=iStart;i<=iEnd;i++)
{
if(i<LB+1) continue;
//--- esporto solo barre con TUTTE le 9 serie valide su tutto il lookback
bool allok=true;
for(int k=0;k<NSER && allok;k++)
if(!IsVal(S[i][k]) || !IsVal(S[i-1][k]) || !IsVal(S[i-LB][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;
//--- velocita'/accelerazione/volatilita': mediana delle 7 scale (no ATR di MT5)
double vel_med = VelMed(S,i,KSLOPE);
double acc_med = AccMed(S,i,KSLOPE);
double vol_med = VolMed(S,i,NVOL);
MqlDateTime dt; TimeToStruct(tm[i],dt);
//--- traiettoria forward (rendimento cumulato a +1..+MAXH), calcolata una volta per barra
string bpath="";
for(int b=1;b<=MAXH;b++)
{
int iB=i+b;
if(iB>n-1) bpath += ",";
else bpath += ","+DoubleToString((cl[iB]-cl[i])/cl[i]*100.0,5);
}
//--- 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;
}
//--- riga baseline (barra qualunque, indipendente dagli incroci)
if(fhB!=INVALID_HANDLE)
{
string br = TimeToString(tm[i],TIME_DATE)+","+sym+","
+DoubleToString(price,digits)+","+DoubleToString(med,digits)+","
+DoubleToString(dist_med,5)+","+DoubleToString(cluster,5)+","
+DoubleToString(cluster_exp,5)+","+IntegerToString(trend)+","
+DoubleToString(vel_med,5)+","+DoubleToString(acc_med,5)+","
+DoubleToString(vol_med,5)+","+IntegerToString(dt.day_of_week)+","
+IntegerToString(dt.mon)+","
+(b2rev>0?IntegerToString(b2rev):"")+","+DoubleToString(discMax,5)
+bpath;
FileWriteString(fhB,br+"\r\n");
brows++;
}
//--- 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*vol_med;
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(vel_med,5)+","+DoubleToString(acc_med,5)+","
+DoubleToString(vol_med,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);
}
row += bpath; // traiettoria forward (uguale per ogni coppia di questa barra)
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);
if(fhB!=INVALID_HANDLE) FileClose(fhB);
for(int m=0;m<7;m++) IndicatorRelease(hMA[m]);
Comment("");
PrintFormat("PaPP export COMPLETATO: %d incroci -> %s (cartella MQL5\\Files)",(int)rows,fname);
if(InpBaseline) PrintFormat("Baseline: %d barre -> PaPP_bars_%s_D1.csv",(int)brows,sym);
Print("Periodo: ",TimeToString(tm[iStart])," -> ",TimeToString(tm[iEnd]));
}
//+------------------------------------------------------------------+