//+------------------------------------------------------------------+ //| PaPP_Median.mq5 | //| PaPP v2 | //+------------------------------------------------------------------+ #property copyright "PaPP v2" #property version "2.00" #property description "PaPP Median - Media 8 MA (1g-1y)" #property description "Linea singola = fair value multi-TF" #property indicator_chart_window #property indicator_buffers 1 #property indicator_plots 1 input int FontSize = 9; double Buff_Median[]; int hMA[8]; int bars[8]; string _pfx = "PM_"; //+------------------------------------------------------------------+ int TimeToBars(int d) { datetime n = TimeCurrent(); if(n==0) { long s = (long)d*86400L, p = PeriodSeconds((ENUM_TIMEFRAMES)_Period); return (int)MathMax(1,s/p); } return MathMax(1,Bars(_Symbol,_Period,n-d*86400,n)); } //+------------------------------------------------------------------+ int OnInit() { int days[8] = {365,182,121,30,14,7,3,1}; for(int i=0;i<8;i++) { bars[i] = (i<7) ? TimeToBars(days[i]) : 1; hMA[i] = iMA(_Symbol,_Period,bars[i],0,MODE_SMA,PRICE_CLOSE); if(hMA[i]==INVALID_HANDLE) return INIT_FAILED; } SetIndexBuffer(0,Buff_Median,INDICATOR_DATA); ArraySetAsSeries(Buff_Median,true); PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE); PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrGold); PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2); PlotIndexSetString(0,PLOT_LABEL,"PaPP Median"); IndicatorSetString(INDICATOR_SHORTNAME,"PaPP Median"); return INIT_SUCCEEDED; } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { for(int i=0;i<8;i++) if(hMA[i]!=INVALID_HANDLE) IndicatorRelease(hMA[i]); ObjectsDeleteAll(0,_pfx); } //+------------------------------------------------------------------+ double GetMA(int idx,int m) { double buf[1]; if(CopyBuffer(hMA[m],0,idx+1,1,buf)==1) return buf[0]; return 0; } //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { if(rates_total<2) return 0; ArraySetAsSeries(close,true); int limit = rates_total - prev_calculated; if(limit>1) limit=rates_total-1; for(int idx=limit;idx>=0;idx--) { double sum=0; int cnt=0; for(int m=0;m<8;m++) { double v = GetMA(idx,m); if(v>0) { sum+=v; cnt++; } } Buff_Median[idx]=(cnt>0) ? sum/cnt : 0; } DrawInfo(); return rates_total; } //+------------------------------------------------------------------+ void DrawInfo() { double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); double median = Buff_Median[0]; 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; 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; //--- Dispersione MA e bande (info) double ds=0; int dc=0; for(int m=0;m<8;m++) { double v=GetMA(0,m); 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); } //+------------------------------------------------------------------+ void Lbl(string n,string t,int x,int y,int fs,color c,bool b) { string o=_pfx+n; if(ObjectFind(0,o)<0) ObjectCreate(0,o,OBJ_LABEL,0,0,0); ObjectSetInteger(0,o,OBJPROP_XDISTANCE,x); ObjectSetInteger(0,o,OBJPROP_YDISTANCE,y); ObjectSetInteger(0,o,OBJPROP_CORNER,CORNER_LEFT_UPPER); ObjectSetString(0,o,OBJPROP_TEXT,t); ObjectSetString(0,o,OBJPROP_FONT,b?"Lucida Console Bold":"Lucida Console"); ObjectSetInteger(0,o,OBJPROP_FONTSIZE,fs); ObjectSetInteger(0,o,OBJPROP_COLOR,c); ObjectSetInteger(0,o,OBJPROP_BACK,false); ObjectSetInteger(0,o,OBJPROP_SELECTABLE,false); ObjectSetInteger(0,o,OBJPROP_HIDDEN,true); } //+------------------------------------------------------------------+