diff --git a/PaPP v2/PaPP_Median.mq5 b/PaPP v2/PaPP_Median.mq5 index 50eb14f..bf3c1f0 100644 --- a/PaPP v2/PaPP_Median.mq5 +++ b/PaPP v2/PaPP_Median.mq5 @@ -5,13 +5,15 @@ #property copyright "PaPP v2" #property version "2.00" #property description "PaPP Median - Mediana 7 MA (3g-1y)" -#property description "Linea singola = fair value multi-TF" +#property description "Calcolo ancorato a D1 = linea uguale su ogni timeframe" #property indicator_chart_window #property indicator_buffers 1 #property indicator_plots 1 input int FontSize = 9; +#define ANCHOR_TF PERIOD_D1 // calcolo ancorato a D1: linea identica su ogni TF + double Buff_Median[]; int hMA[7]; @@ -24,10 +26,10 @@ int TimeToBars(int d) datetime n = TimeCurrent(); if(n==0) { - long s = (long)d*86400L, p = PeriodSeconds((ENUM_TIMEFRAMES)_Period); + long s = (long)d*86400L, p = PeriodSeconds(ANCHOR_TF); return (int)MathMax(1,s/p); } - return MathMax(1,Bars(_Symbol,_Period,n-d*86400,n)); + return MathMax(1,Bars(_Symbol,ANCHOR_TF,n-d*86400,n)); } //+------------------------------------------------------------------+ @@ -37,7 +39,7 @@ int OnInit() for(int i=0;i<7;i++) { bars[i] = TimeToBars(days[i]); - hMA[i] = iMA(_Symbol,_Period,bars[i],0,MODE_SMA,PRICE_CLOSE); + hMA[i] = iMA(_Symbol,ANCHOR_TF,bars[i],0,MODE_SMA,PRICE_CLOSE); if(hMA[i]==INVALID_HANDLE) return INIT_FAILED; } @@ -48,6 +50,7 @@ int OnInit() PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrGold); PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2); PlotIndexSetString(0,PLOT_LABEL,"PaPP Median"); + PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); IndicatorSetString(INDICATOR_SHORTNAME,"PaPP Median"); return INIT_SUCCEEDED; } @@ -60,10 +63,12 @@ void OnDeinit(const int reason) } //+------------------------------------------------------------------+ -double GetMA(int idx,int m) +// d1shift = indice (serie) della barra D1 da cui leggere la SMA +double GetMA(int m,int d1shift) { + if(d1shift<0) return 0; double buf[1]; - if(CopyBuffer(hMA[m],0,idx+1,1,buf)==1) return buf[0]; + if(CopyBuffer(hMA[m],0,d1shift,1,buf)==1) return buf[0]; return 0; } @@ -90,16 +95,22 @@ int OnCalculate(const int rates_total, { if(rates_total<2) return 0; ArraySetAsSeries(close,true); + ArraySetAsSeries(time,true); int limit = rates_total - prev_calculated; if(limit>1) limit=rates_total-1; for(int idx=limit;idx>=0;idx--) { + //--- ultima barra D1 chiusa rispetto al tempo di questa barra grafico + int d1 = iBarShift(_Symbol,ANCHOR_TF,time[idx],false); + if(d1<0) { Buff_Median[idx]=0; continue; } + d1 += 1; + double vals[]; int cnt=0; ArrayResize(vals,7); for(int m=0;m<7;m++) { - double v = GetMA(idx,m); + double v = GetMA(m,d1); if(v>0) { vals[cnt]=v; cnt++; } } ArrayResize(vals,cnt); @@ -131,7 +142,7 @@ void DrawInfo() //--- Dispersione MA e bande (info) double ds=0; int dc=0; - for(int m=0;m<7;m++) { double v=GetMA(0,m); if(v>0) { ds+=MathAbs(v-median)/median*100; dc++; } } + 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; Lbl("H","Sopra=SELL | Sotto=BUY",x,y,FontSize-1,clrGray,false); diff --git a/PaPP v2/PaPP_Median_EA.mq5 b/PaPP v2/PaPP_Median_EA.mq5 index 37ccd6e..f870726 100644 --- a/PaPP v2/PaPP_Median_EA.mq5 +++ b/PaPP v2/PaPP_Median_EA.mq5 @@ -18,6 +18,8 @@ input bool DebugPrint = true; input int Magic = 2024002; input int Slippage = 30; +#define ANCHOR_TF PERIOD_D1 // calcolo ancorato a D1: stessa fair value su ogni TF + int hMA[7]; int bars[7]; datetime lastBar = 0; @@ -31,10 +33,10 @@ int TimeToBars(int d) datetime n = TimeCurrent(); if(n==0) { - long s = (long)d*86400L, p = PeriodSeconds((ENUM_TIMEFRAMES)_Period); + long s = (long)d*86400L, p = PeriodSeconds(ANCHOR_TF); return (int)MathMax(1,s/p); } - return MathMax(1,Bars(_Symbol,_Period,n-d*86400,n)); + return MathMax(1,Bars(_Symbol,ANCHOR_TF,n-d*86400,n)); } //+------------------------------------------------------------------+ @@ -44,7 +46,7 @@ int OnInit() for(int i=0;i<7;i++) { bars[i] = TimeToBars(days[i]); - hMA[i] = iMA(_Symbol,_Period,bars[i],0,MODE_SMA,PRICE_CLOSE); + hMA[i] = iMA(_Symbol,ANCHOR_TF,bars[i],0,MODE_SMA,PRICE_CLOSE); if(hMA[i]==INVALID_HANDLE) return INIT_FAILED; } if(DebugPrint) Print("=== PaPP Median EA v2.00 INIT ==="); @@ -63,10 +65,11 @@ void OnDeinit(const int reason) } //+------------------------------------------------------------------+ -double GetMA(int idx,int m) +// legge la SMA dall'ultima barra D1 chiusa (non-repaint) +double GetMA(int m) { double buf[1]; - if(CopyBuffer(hMA[m],0,idx+1,1,buf)==1) return buf[0]; + if(CopyBuffer(hMA[m],0,1,1,buf)==1) return buf[0]; return 0; } @@ -174,7 +177,7 @@ void OnTick() //--- Leggi le 7 MA e calcola mediana double mv[7]; double vals[]; int valid=0; ArrayResize(vals,7); - for(int m=0;m<7;m++) { mv[m]=GetMA(0,m); if(mv[m]>0) { vals[valid]=mv[m]; valid++; } } + for(int m=0;m<7;m++) { mv[m]=GetMA(m); if(mv[m]>0) { vals[valid]=mv[m]; valid++; } } if(valid==0) { if(DebugPrint) Print("SKIP: valid=",valid); return; } ArrayResize(vals,valid);