Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef3b2b668c | |||
| 5cee3f06fd | |||
| f930e88592 | |||
| 908d0e9187 | |||
| 64dc36b1c3 | |||
| 8e6fe91b70 | |||
| 20315441a5 | |||
| 4a590b805a | |||
| b45039f91f | |||
| ae7551ef9d | |||
| 2c32e233e6 | |||
| 03e1b290b5 |
@@ -27,6 +27,9 @@ 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)
|
||||
{
|
||||
@@ -36,10 +39,14 @@ int TimeToBarsD1(string sym,int d)
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
double Median7(double &v[]) // mediana dei valori >0 in v[0..6]
|
||||
// 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(v[m]>0) { a[c]=v[m]; c++; }
|
||||
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];
|
||||
@@ -79,7 +86,7 @@ void OnStart()
|
||||
|
||||
//--- carico tutta la storia D1 (indice 0 = piu' vecchio)
|
||||
datetime tm[]; double cl[],hi[],lo[],atr[];
|
||||
double ma[7][];
|
||||
MABuf ma[7];
|
||||
ArraySetAsSeries(tm,false); ArraySetAsSeries(cl,false);
|
||||
ArraySetAsSeries(hi,false); ArraySetAsSeries(lo,false);
|
||||
ArraySetAsSeries(atr,false);
|
||||
@@ -90,8 +97,8 @@ void OnStart()
|
||||
if(CopyBuffer(hATR,0,0,total,atr)<=0) { Print("CopyATR KO"); return; }
|
||||
for(int m=0;m<7;m++)
|
||||
{
|
||||
ArraySetAsSeries(ma[m],false);
|
||||
if(CopyBuffer(hMA[m],0,0,total,ma[m])<=0) { Print("CopyBuffer MA KO m=",m); return; }
|
||||
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);
|
||||
|
||||
@@ -101,7 +108,7 @@ void OnStart()
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
double mv[7];
|
||||
for(int m=0;m<7;m++) mv[m] = ma[m][i];
|
||||
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
|
||||
@@ -127,6 +134,7 @@ void OnStart()
|
||||
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");
|
||||
|
||||
@@ -135,20 +143,26 @@ void OnStart()
|
||||
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];
|
||||
if(price<=0 || med<=0) continue;
|
||||
|
||||
//--- feature comuni a tutti gli incroci di questa barra
|
||||
double dist_med = (med>0)?(price-med)/med*100.0:0;
|
||||
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>0){ if(v>cmax)cmax=v; if(v<cmin)cmin=v; } }
|
||||
double cluster = (cmax>0 && price>0)?(cmax-cmin)/price*100.0:0;
|
||||
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>0){ if(v>cmaxP)cmaxP=v; if(v<cminP)cminP=v; } }
|
||||
double clusterP = (cmaxP>0)?(cmaxP-cminP)/S[i-KSLOPE][0]*100.0:0;
|
||||
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 && ma365>0)?1:-1;
|
||||
double atr_pct = (price>0)?atr[i]/price*100.0:0;
|
||||
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
|
||||
@@ -157,7 +171,7 @@ void OnStart()
|
||||
int jmax = MathMin(i+MAXH,n-1);
|
||||
for(int j=i+1;j<=jmax;j++)
|
||||
{
|
||||
if(S[j][0]<=0 || S[j][1]<=0) continue;
|
||||
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;
|
||||
@@ -168,15 +182,14 @@ void OnStart()
|
||||
for(int a=0;a<NSER;a++)
|
||||
for(int b=a+1;b<NSER;b++)
|
||||
{
|
||||
if(S[i][a]<=0||S[i][b]<=0||S[i-1][a]<=0||S[i-1][b]<=0) continue;
|
||||
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-KSLOPE][a]>0)?(S[i][a]-S[i-KSLOPE][a])/S[i-KSLOPE][a]*100.0:0;
|
||||
double slope_b = (S[i-KSLOPE][b]>0)?(S[i][b]-S[i-KSLOPE][b])/S[i-KSLOPE][b]*100.0:0;
|
||||
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,",
|
||||
@@ -204,6 +217,13 @@ void OnStart()
|
||||
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++;
|
||||
|
||||
+42
-5
@@ -15,6 +15,7 @@ input bool Smooth = true; // interpola tra i valori D1 -> linea morbida
|
||||
input bool ShowMA = true; // mostra anche le 7 MA (oltre alla mediana)
|
||||
|
||||
#define ANCHOR_TF PERIOD_D1 // calcolo ancorato a D1: linea identica su ogni TF
|
||||
#define KSLOPE 5 // barre D1 per pendenza/accelerazione
|
||||
|
||||
double Buff_Median[];
|
||||
double B0[],B1[],B2[],B3[],B4[],B5[],B6[];
|
||||
@@ -22,6 +23,7 @@ double B0[],B1[],B2[],B3[],B4[],B5[],B6[];
|
||||
int gDays[7] = {365,182,121,30,14,7,3};
|
||||
color gCol[7] = {clrDodgerBlue,clrDeepSkyBlue,clrTurquoise,clrLimeGreen,clrOrange,clrTomato,clrRed};
|
||||
int hMA[7];
|
||||
int hATR = INVALID_HANDLE;
|
||||
int bars[7];
|
||||
string _pfx = "PM_"; // oggetti pannello
|
||||
string _pfx2 = "PME_"; // etichette a fine linea
|
||||
@@ -80,6 +82,8 @@ int OnInit()
|
||||
hMA[i] = iMA(_Symbol,ANCHOR_TF,bars[i],0,MODE_SMA,PRICE_CLOSE);
|
||||
if(hMA[i]==INVALID_HANDLE) return INIT_FAILED;
|
||||
}
|
||||
hATR = iATR(_Symbol,ANCHOR_TF,14);
|
||||
if(hATR==INVALID_HANDLE) return INIT_FAILED;
|
||||
|
||||
SetIndexBuffer(0,Buff_Median,INDICATOR_DATA);
|
||||
SetIndexBuffer(1,B0,INDICATOR_DATA);
|
||||
@@ -120,6 +124,7 @@ int OnInit()
|
||||
void OnDeinit(const int reason)
|
||||
{
|
||||
for(int i=0;i<7;i++) if(hMA[i]!=INVALID_HANDLE) IndicatorRelease(hMA[i]);
|
||||
if(hATR!=INVALID_HANDLE) IndicatorRelease(hATR);
|
||||
ObjectsDeleteAll(0,_pfx);
|
||||
ObjectsDeleteAll(0,_pfx2);
|
||||
}
|
||||
@@ -134,6 +139,27 @@ double GetMA(int m,int d1shift)
|
||||
return 0;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// ATR D1 (volatilita') alla barra d1shift
|
||||
double GetATR(int d1shift)
|
||||
{
|
||||
if(d1shift<0) return 0;
|
||||
double b[1];
|
||||
if(CopyBuffer(hATR,0,d1shift,1,b)==1) return b[0];
|
||||
return 0;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
// dispersione media delle 7 MA dalla mediana (%) alla barra d1shift
|
||||
double ClusterAtD1(int d1shift)
|
||||
{
|
||||
double md = MedianAtD1(d1shift);
|
||||
if(md<=0) return 0;
|
||||
double ds=0; int dc=0;
|
||||
for(int m=0;m<7;m++) { double v=GetMA(m,d1shift); if(v>0) { ds+=MathAbs(v-md)/md*100; dc++; } }
|
||||
return (dc>0)?ds/dc:0;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
double Median(double &a[],int n)
|
||||
{
|
||||
@@ -298,11 +324,22 @@ void DrawInfo()
|
||||
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;
|
||||
|
||||
//--- 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;
|
||||
//--- Cluster (dispersione media delle MA dalla mediana)
|
||||
double vol = ClusterAtD1(1);
|
||||
Lbl("V","Cluster +/-"+DoubleToString(vol,3)+"%",x,y,FontSize,clrGray,false); y+=lh;
|
||||
|
||||
//--- Volatilita' (ATR D1 in % del prezzo)
|
||||
double atrv = GetATR(1);
|
||||
double atrPct = (atrv>0)?atrv/median*100:0;
|
||||
Lbl("VOL","Volatilita' ATR: "+DoubleToString(atrPct,3)+"%",x,y,FontSize,clrAqua,false); y+=lh;
|
||||
|
||||
//--- Accelerazione su KSLOPE barre D1: pendenza mediana + espansione cluster
|
||||
double medK = MedianAtD1(1+KSLOPE);
|
||||
double slope = (medK>0)?(median-medK)/medK*100:0;
|
||||
double clExp = vol - ClusterAtD1(1+KSLOPE);
|
||||
color accC = (slope>0)?clrLimeGreen:clrTomato;
|
||||
Lbl("ACC",StringFormat("Accel %dg: med %+.3f%% | cluster %+.3f%%",KSLOPE,slope,clExp),
|
||||
x,y,FontSize,accC,false); y+=lh+2;
|
||||
|
||||
//--- Valori delle 7 MA (giorni : valore) con colore = linea
|
||||
for(int m=0;m<7;m++)
|
||||
|
||||
Reference in New Issue
Block a user