diff --git a/PaPP v2/PaPP_CrossExport.mq5 b/PaPP v2/PaPP_CrossExport.mq5 index fe95235..e95897c 100644 --- a/PaPP v2/PaPP_CrossExport.mq5 +++ b/PaPP v2/PaPP_CrossExport.mq5 @@ -16,12 +16,14 @@ 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 per pendenza / variazione cluster -#define MAXH 20 // orizzonte massimo per gli esiti +#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"}; @@ -53,6 +55,61 @@ double Median7(double &v[]) // mediana dei valori validi in v[0..6] 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=2) + { + double mean=0; for(int j=0;jMAXH+KSLOPE+50); + bool ok = (total>MAXH+LB+50); for(int m=0;m<7 && ok;m++) if(BarsCalculated(hMA[m])=total) break; + if(ok) break; Sleep(100); } total = Bars(sym,ANCHOR_TF); - if(total<=MAXH+KSLOPE+50) { Print("Storico D1 insufficiente: ",total," barre"); return; } + 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[],atr[]; + datetime tm[]; double cl[],hi[],lo[]; 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); @@ -117,8 +171,8 @@ void OnStart() //--- 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=from) { iStart=i; break; } + int iStart = LB+1, iEnd = n-1; + for(int i=LB+1;i=from) { iStart=i; break; } for(int i=n-1;i>=0;i--) if(tm[i]<=to) { iEnd=i; break; } //--- apro il file CSV @@ -128,7 +182,7 @@ void OnStart() //--- 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"; + "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]); @@ -138,16 +192,32 @@ void OnStart() 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; + long rows=0, brows=0; for(int i=iStart;i<=iEnd;i++) { - if(ima365)?1:-1; - double atr_pct = atr[i]/price*100.0; + //--- 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; @@ -178,6 +260,22 @@ void OnStart() 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;an-1) { row += ","; continue; } - row += ","+DoubleToString((cl[iB]-cl[i])/cl[i]*100.0,5); - } + 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++; @@ -232,10 +325,11 @@ void OnStart() } FileClose(fh); + if(fhB!=INVALID_HANDLE) FileClose(fhB); 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); + if(InpBaseline) PrintFormat("Baseline: %d barre -> PaPP_bars_%s_D1.csv",(int)brows,sym); Print("Periodo: ",TimeToString(tm[iStart])," -> ",TimeToString(tm[iEnd])); } //+------------------------------------------------------------------+