Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
202 lines
6.8 KiB
Plaintext
202 lines
6.8 KiB
Plaintext
//+------------------------------------------------------------------+
|
|
//| PaPP_Median.mq5 |
|
|
//| PaPP v2 |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "PaPP v2"
|
|
#property version "2.00"
|
|
#property description "PaPP Median - Mediana 7 MA (3g-1y)"
|
|
#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;
|
|
input bool Smooth = true; // interpola tra i valori D1 -> linea morbida
|
|
|
|
#define ANCHOR_TF PERIOD_D1 // calcolo ancorato a D1: linea identica su ogni TF
|
|
|
|
double Buff_Median[];
|
|
|
|
int gDays[7] = {365,182,121,30,14,7,3};
|
|
int hMA[7];
|
|
int bars[7];
|
|
string _pfx = "PM_";
|
|
|
|
//+------------------------------------------------------------------+
|
|
int TimeToBars(int d)
|
|
{
|
|
datetime n = TimeCurrent();
|
|
if(n==0)
|
|
{
|
|
long s = (long)d*86400L, p = PeriodSeconds(ANCHOR_TF);
|
|
return (int)MathMax(1,s/p);
|
|
}
|
|
return MathMax(1,Bars(_Symbol,ANCHOR_TF,n-d*86400,n));
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
for(int i=0;i<7;i++)
|
|
{
|
|
bars[i] = TimeToBars(gDays[i]);
|
|
hMA[i] = iMA(_Symbol,ANCHOR_TF,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");
|
|
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
|
|
IndicatorSetString(INDICATOR_SHORTNAME,"PaPP Median");
|
|
return INIT_SUCCEEDED;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason)
|
|
{
|
|
for(int i=0;i<7;i++) if(hMA[i]!=INVALID_HANDLE) IndicatorRelease(hMA[i]);
|
|
ObjectsDeleteAll(0,_pfx);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
// 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,d1shift,1,buf)==1) return buf[0];
|
|
return 0;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
double Median(double &a[],int n)
|
|
{
|
|
if(n<=0) return 0;
|
|
ArraySort(a);
|
|
if((n&1)==1) return a[n/2];
|
|
return 0.5*(a[n/2-1]+a[n/2]);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
// Mediana delle 7 MA su una specifica barra D1
|
|
double MedianAtD1(int d1shift)
|
|
{
|
|
if(d1shift<0) return 0;
|
|
double vals[]; int cnt=0; ArrayResize(vals,7);
|
|
for(int m=0;m<7;m++) { double v=GetMA(m,d1shift); if(v>0) { vals[cnt]=v; cnt++; } }
|
|
ArrayResize(vals,cnt);
|
|
return Median(vals,cnt);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
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);
|
|
ArraySetAsSeries(time,true);
|
|
|
|
int limit = rates_total - prev_calculated;
|
|
if(limit>1) limit=rates_total-1;
|
|
|
|
for(int idx=limit;idx>=0;idx--)
|
|
{
|
|
//--- barra D1 che contiene il tempo di questa barra grafico
|
|
int d1cur = iBarShift(_Symbol,ANCHOR_TF,time[idx],false);
|
|
if(d1cur<0) { Buff_Median[idx]=0; continue; }
|
|
|
|
if(!Smooth)
|
|
{
|
|
//--- gradino: ultima barra D1 chiusa (non-repaint)
|
|
Buff_Median[idx]=MedianAtD1(d1cur+1);
|
|
continue;
|
|
}
|
|
|
|
//--- linea morbida: interpola tra mediana di inizio e fine giornata
|
|
double vEnd = MedianAtD1(d1cur); // giornata corrente
|
|
double vStart = MedianAtD1(d1cur+1); // giornata precedente
|
|
datetime t0 = iTime(_Symbol,ANCHOR_TF,d1cur);
|
|
datetime t1 = (d1cur>0) ? iTime(_Symbol,ANCHOR_TF,d1cur-1)
|
|
: t0 + PeriodSeconds(ANCHOR_TF);
|
|
double frac = (t1>t0) ? (double)(time[idx]-t0)/(double)(t1-t0) : 0.0;
|
|
frac = MathMax(0.0,MathMin(1.0,frac));
|
|
|
|
if(vStart>0 && vEnd>0) Buff_Median[idx]=vStart+frac*(vEnd-vStart);
|
|
else if(vEnd>0) Buff_Median[idx]=vEnd;
|
|
else Buff_Median[idx]=vStart;
|
|
}
|
|
|
|
DrawInfo();
|
|
return rates_total;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
void DrawInfo()
|
|
{
|
|
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
|
|
double median = MedianAtD1(1); // valore D1 chiuso (stabile, non-repaint)
|
|
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<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;
|
|
|
|
//--- Valori delle 7 MA (giorni : valore)
|
|
for(int m=0;m<7;m++)
|
|
{
|
|
double v=GetMA(m,1);
|
|
color cma = (v>median)?clrSalmon:clrLightGreen;
|
|
Lbl("a"+IntegerToString(m),
|
|
StringFormat("MA %3dg: %s",gDays[m],DoubleToString(v,_Digits)),
|
|
x,y,FontSize-1,(v>0)?cma:clrGray,false);
|
|
y+=lh-2;
|
|
}
|
|
y+=2;
|
|
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?"Consolas Bold":"Consolas");
|
|
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);
|
|
}
|
|
//+------------------------------------------------------------------+
|