PaPP v2 indicatore: linea morbida (interpolazione D1, input Smooth) + pannello con i 7 valori MA a schermo
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
+48
-14
@@ -11,11 +11,13 @@
|
||||
#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_";
|
||||
@@ -35,10 +37,9 @@ int TimeToBars(int d)
|
||||
//+------------------------------------------------------------------+
|
||||
int OnInit()
|
||||
{
|
||||
int days[7] = {365,182,121,30,14,7,3};
|
||||
for(int i=0;i<7;i++)
|
||||
{
|
||||
bars[i] = TimeToBars(days[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;
|
||||
}
|
||||
@@ -81,6 +82,17 @@ double Median(double &a[],int n)
|
||||
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,
|
||||
@@ -102,19 +114,29 @@ int OnCalculate(const int rates_total,
|
||||
|
||||
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;
|
||||
//--- 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; }
|
||||
|
||||
double vals[]; int cnt=0; ArrayResize(vals,7);
|
||||
for(int m=0;m<7;m++)
|
||||
if(!Smooth)
|
||||
{
|
||||
double v = GetMA(m,d1);
|
||||
if(v>0) { vals[cnt]=v; cnt++; }
|
||||
//--- gradino: ultima barra D1 chiusa (non-repaint)
|
||||
Buff_Median[idx]=MedianAtD1(d1cur+1);
|
||||
continue;
|
||||
}
|
||||
ArrayResize(vals,cnt);
|
||||
Buff_Median[idx]=Median(vals,cnt);
|
||||
|
||||
//--- 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();
|
||||
@@ -125,7 +147,7 @@ int OnCalculate(const int rates_total,
|
||||
void DrawInfo()
|
||||
{
|
||||
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
|
||||
double median = Buff_Median[0];
|
||||
double median = MedianAtD1(1); // valore D1 chiuso (stabile, non-repaint)
|
||||
if(median<=0) return;
|
||||
|
||||
int x=10,y=30,lh=FontSize+4;
|
||||
@@ -144,7 +166,19 @@ void DrawInfo()
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user