PaPP v2: structural mean reversion with 8-MA cluster band, excursion flags, dynamic TP/SL
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| 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);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,234 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| PaPP_Median_EA.mq5 |
|
||||
//| PaPP v2 |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Trade/Trade.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "PaPP v2"
|
||||
#property version "2.00"
|
||||
#property description "PaPP Median EA - Mean Reversion puro"
|
||||
#property description "Linea mediana (media 8 MA 1g-1y) = unico segnale"
|
||||
#property description "Sopra = SELL | Sotto = BUY"
|
||||
|
||||
input double LotSize = 0.01;
|
||||
input int TrailStart = 0;
|
||||
input int TrailStep = 0;
|
||||
input int MaxPosPerSide = 3;
|
||||
input bool DebugPrint = true;
|
||||
input int Magic = 2024002;
|
||||
input int Slippage = 30;
|
||||
|
||||
int hMA[8];
|
||||
int bars[8];
|
||||
datetime lastBar = 0;
|
||||
bool buyFired = false;
|
||||
bool sellFired = false;
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
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;
|
||||
}
|
||||
if(DebugPrint) Print("=== PaPP Median EA v2.00 INIT ===");
|
||||
Print(" Lot=",LotSize," Trail=",TrailStart,"/",TrailStep," MaxPos=",MaxPosPerSide);
|
||||
for(int i=0;i<8;i++) Print(" MA[",i,"] bars=",bars[i]);
|
||||
return INIT_SUCCEEDED;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
void OnDeinit(const int reason)
|
||||
{
|
||||
for(int i=0;i<8;i++) if(hMA[i]!=INVALID_HANDLE) IndicatorRelease(hMA[i]);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
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 CountPos(int type)
|
||||
{
|
||||
int n=0;
|
||||
for(int i=PositionsTotal()-1; i>=0; i--)
|
||||
{
|
||||
ulong t=PositionGetTicket(i);
|
||||
if(t>0 && PositionSelectByTicket(t))
|
||||
if(PositionGetInteger(POSITION_MAGIC)==Magic && PositionGetString(POSITION_SYMBOL)==_Symbol)
|
||||
if(PositionGetInteger(POSITION_TYPE)==type) n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
void TrailAll()
|
||||
{
|
||||
if(TrailStart<=0 || TrailStep<=0) return;
|
||||
double point = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
|
||||
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
|
||||
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
|
||||
CTrade trade;
|
||||
for(int i=PositionsTotal()-1; i>=0; i--)
|
||||
{
|
||||
ulong t=PositionGetTicket(i);
|
||||
if(t<=0 || !PositionSelectByTicket(t)) continue;
|
||||
if(PositionGetInteger(POSITION_MAGIC)!=Magic || PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;
|
||||
|
||||
bool isBuy = (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY);
|
||||
double entry = PositionGetDouble(POSITION_PRICE_OPEN);
|
||||
double currSL = PositionGetDouble(POSITION_SL);
|
||||
double currTP = PositionGetDouble(POSITION_TP);
|
||||
double newSL=0;
|
||||
|
||||
if(isBuy && bid >= entry+TrailStart*point)
|
||||
{
|
||||
newSL = bid - TrailStep*point;
|
||||
if(newSL > currSL+point && trade.PositionModify(t,newSL,currTP))
|
||||
if(DebugPrint) Print(">>> TRAIL BUY t",t," SL->",DoubleToString(newSL,_Digits));
|
||||
}
|
||||
if(!isBuy && ask <= entry-TrailStart*point)
|
||||
{
|
||||
newSL = ask + TrailStep*point;
|
||||
if(currSL==0 || newSL < currSL-point)
|
||||
{
|
||||
if(trade.PositionModify(t,newSL,currTP))
|
||||
if(DebugPrint) Print(">>> TRAIL SELL t",t," SL->",DoubleToString(newSL,_Digits));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
void OnTick()
|
||||
{
|
||||
TrailAll();
|
||||
|
||||
datetime curBar = iTime(_Symbol,_Period,0);
|
||||
if(curBar==lastBar) return;
|
||||
lastBar = curBar;
|
||||
|
||||
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
|
||||
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
|
||||
double point = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
|
||||
|
||||
//--- Leggi le 8 MA e calcola mediana
|
||||
double mv[8]; int valid=0; double sum=0;
|
||||
for(int m=0;m<8;m++) { mv[m]=GetMA(0,m); if(mv[m]>0) { sum+=mv[m]; valid++; } }
|
||||
if(valid==0 || sum<=0) { if(DebugPrint) Print("SKIP: valid=",valid); return; }
|
||||
|
||||
double median = sum/valid;
|
||||
|
||||
//--- Dispersione MA = banda strutturale del cluster
|
||||
int dc=0; double ds=0;
|
||||
for(int m=0;m<8;m++)
|
||||
if(mv[m]>0) { ds+=MathAbs(mv[m]-median)/median*100; dc++; }
|
||||
double vol = (dc>0) ? ds/dc : 0;
|
||||
|
||||
double sellBand = median*(1+vol/100);
|
||||
double buyBand = median*(1-vol/100);
|
||||
double distPct = (bid-median)/median*100;
|
||||
|
||||
int nBuy = CountPos(POSITION_TYPE_BUY);
|
||||
int nSell = CountPos(POSITION_TYPE_SELL);
|
||||
|
||||
//--- Reset escursion flags quando price rientra nel cluster
|
||||
if(bid>=buyBand && bid<=sellBand) { buyFired=false; sellFired=false; }
|
||||
|
||||
//--- LOG
|
||||
if(DebugPrint)
|
||||
{
|
||||
Print("");
|
||||
Print("=== BAR: ",TimeToString(curBar)," ===");
|
||||
Print("Bid=",DoubleToString(bid,_Digits)," Median=",DoubleToString(median,_Digits));
|
||||
Print("Dist=",DoubleToString(distPct,3),"% DispMA=",DoubleToString(vol,4),"%");
|
||||
Print("Band: [",DoubleToString(buyBand,_Digits)," <- ",DoubleToString(sellBand,_Digits)," ] Pos: ",nBuy,"B ",nSell,"S");
|
||||
for(int m=0;m<8;m++)
|
||||
if(mv[m]>0)
|
||||
Print(" MA",m,"=",DoubleToString(mv[m],_Digits),
|
||||
" diff=",DoubleToString(bid-mv[m],_Digits));
|
||||
Print("---");
|
||||
}
|
||||
|
||||
//--- Entry: fuori dal cluster, TP=mediana, SL simmetrico
|
||||
if(bid < buyBand && !buyFired && nBuy < MaxPosPerSide)
|
||||
{
|
||||
double entry = ask;
|
||||
double tpDist = MathAbs(median-entry);
|
||||
double sl = NormalizeDouble(entry-tpDist,_Digits);
|
||||
double tp = NormalizeDouble(median,_Digits);
|
||||
if(sl>=point)
|
||||
{
|
||||
if(DebugPrint) Print(">>> BUY: bid=",DoubleToString(bid,_Digits),
|
||||
" < buyBand=",DoubleToString(buyBand,_Digits),
|
||||
" TP=",DoubleToString(tp,_Digits)," SL=",DoubleToString(sl,_Digits),
|
||||
" (",DoubleToString(tpDist/point,0),"pts)");
|
||||
MqlTradeRequest req={}; MqlTradeResult res={};
|
||||
req.action = TRADE_ACTION_DEAL; req.symbol = _Symbol;
|
||||
req.volume = LotSize; req.type = ORDER_TYPE_BUY; req.price = entry;
|
||||
req.sl = sl; req.tp = tp; req.deviation = Slippage; req.magic = Magic;
|
||||
req.comment = "Pv2B "+DoubleToString(distPct,1)+"%";
|
||||
if(OrderSend(req,res) && res.retcode==TRADE_RETCODE_DONE)
|
||||
{ if(DebugPrint) Print(">>> BUY OPENED t",res.order); buyFired=true; }
|
||||
else if(DebugPrint) Print("BUY fail: c",res.retcode);
|
||||
}
|
||||
else if(DebugPrint) Print("BUY SKIP: tpDist too small (",DoubleToString(tpDist/point,0),"pts)");
|
||||
}
|
||||
|
||||
if(bid > sellBand && !sellFired && nSell < MaxPosPerSide)
|
||||
{
|
||||
double entry = bid;
|
||||
double tpDist = MathAbs(entry-median);
|
||||
double sl = NormalizeDouble(entry+tpDist,_Digits);
|
||||
double tp = NormalizeDouble(median,_Digits);
|
||||
if(tpDist>=point)
|
||||
{
|
||||
if(DebugPrint) Print(">>> SELL: bid=",DoubleToString(bid,_Digits),
|
||||
" > sellBand=",DoubleToString(sellBand,_Digits),
|
||||
" TP=",DoubleToString(tp,_Digits)," SL=",DoubleToString(sl,_Digits),
|
||||
" (",DoubleToString(tpDist/point,0),"pts)");
|
||||
MqlTradeRequest req={}; MqlTradeResult res={};
|
||||
req.action = TRADE_ACTION_DEAL; req.symbol = _Symbol;
|
||||
req.volume = LotSize; req.type = ORDER_TYPE_SELL; req.price = entry;
|
||||
req.sl = sl; req.tp = tp; req.deviation = Slippage; req.magic = Magic;
|
||||
req.comment = "Pv2S "+DoubleToString(distPct,1)+"%";
|
||||
if(OrderSend(req,res) && res.retcode==TRADE_RETCODE_DONE)
|
||||
{ if(DebugPrint) Print(">>> SELL OPENED t",res.order); sellFired=true; }
|
||||
else if(DebugPrint) Print("SELL fail: c",res.retcode);
|
||||
}
|
||||
else if(DebugPrint) Print("SELL SKIP: tpDist too small (",DoubleToString(tpDist/point,0),"pts)");
|
||||
}
|
||||
|
||||
if(DebugPrint)
|
||||
{
|
||||
if(bid>=buyBand && bid<=sellBand)
|
||||
Print("NO ENTRY: inside cluster [",DoubleToString(buyBand,_Digits),
|
||||
" - ",DoubleToString(sellBand,_Digits),"]");
|
||||
else if(bid<buyBand && nBuy>=MaxPosPerSide)
|
||||
Print("BUY BLOCKED: max pos (",nBuy,"/",MaxPosPerSide,") bid=",DoubleToString(bid,_Digits),
|
||||
" < buyBand=",DoubleToString(buyBand,_Digits));
|
||||
else if(bid>sellBand && nSell>=MaxPosPerSide)
|
||||
Print("SELL BLOCKED: max pos (",nSell,"/",MaxPosPerSide,") bid=",DoubleToString(bid,_Digits),
|
||||
" > sellBand=",DoubleToString(sellBand,_Digits));
|
||||
}
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
Reference in New Issue
Block a user