//+——————————————————————+
//| MorningFlat.mq4 |
//+——————————————————————+
#property copyright “”
#property link “”
#property version “1.0”
#property strict
#property indicator_chart_window
#property indicator_buffers 6
input int StartHour=0;
input int EndHour=8;
input double TargetLevel1 = 261.8;// Target level 1 by Fibo
input double TargetLevel2 = 423.6;// Target level 2 by Fibo
input bool AlertTL=true;//Alert crossing Target Level
input bool AlertFL=false;//Alert crossing Flat Level
input color UpColor = clrAqua;
input color DnColor = clrDeepPink;
input color TargetUpColor = clrDarkGreen;
input color TargetDnColor = clrRed;
//—- buffers
double Up[],Down[],TargetUp[],TargetDn[],TargetUp2[],TargetDn2[];
datetime LastDay;
static datetime lastAlertTarget=0,lastAlertFlat=0,lastAlertTarget2=0;
static double lastFlatMin=0,lastFlatMax=0;
static bool RealAlertFL=AlertFL;
//+——————————————————————+
//| Custom indicator initialization function |
//+——————————————————————+
int init()
{
//—- indicators
if(Period()>PERIOD_H1)
{
Alert(“The indicator works on time frames less than H4!”);
return(INIT_PARAMETERS_INCORRECT);
}
if(StartHour<0 || EndHour<0 || StartHour>23 || EndHour>23 || StartHour>=EndHour)
{
Alert(“The StartHour and EndHour values must be in the range 0 to 24 and StartHour < EndHour.”);
return(INIT_PARAMETERS_INCORRECT);
}
SetIndexBuffer(0,Up);
SetIndexBuffer(1,Down);
SetIndexBuffer(2,TargetUp);
SetIndexBuffer(3,TargetDn);
SetIndexBuffer(4,TargetUp2);
SetIndexBuffer(5,TargetDn2);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,UpColor);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,DnColor);
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1,TargetUpColor);
SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1,TargetDnColor);
SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,1,TargetUpColor);
SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,1,TargetDnColor);
SetIndexEmptyValue(0, 0.0);
SetIndexEmptyValue(1, 0.0);
SetIndexEmptyValue(2, 0.0);
SetIndexEmptyValue(3, 0.0);
SetIndexEmptyValue(4, 0.0);
SetIndexEmptyValue(5, 0.0);
//—-
return(INIT_SUCCEEDED);
}
//+——————————————————————+
//| Custom indicator deinitialization function |
//+——————————————————————+
int deinit()
{
//—-
for(int i=ObjectsTotal()-1; i>=0; i–)
{
string Name=ObjectName(i);
if(StringSubstr(Name,0,3)==”Lab”)
ObjectDelete(Name);
}
Comment(“”);
//—-
return(0);
}
//+——————————————————————+
void DrawLabel(datetime TimeL,double Price,string NameF,color Col,int Code)
{
string Name=NameF;
if(ObjectCreate(Name,OBJ_ARROW,0,TimeL,Price))
{
ObjectSet(Name,OBJPROP_ARROWCODE,Code);
ObjectSet(Name,OBJPROP_COLOR,Col);
}
}
//+——————————————————————+
//| Custom indicator iteration function |
//+——————————————————————+
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<=1)
return(0);
//— last counted bar will be recounted
int limit=rates_total-prev_calculated;
if(prev_calculated>0)
limit=limit+3;
LastDay=0;
for(int i=limit-1; i>=0; i–)
{
if(TimeHour(Time[i])>=EndHour)
{
if(AlertFL){ RealAlertFL=true; }else{ RealAlertFL=false; }
datetime BeginDay= iTime(Symbol(),PERIOD_D1,iBarShift(Symbol(),PERIOD_D1,Time[i]));
datetime NextDay = BeginDay+86400;
if(LastDay>=BeginDay) continue;
int StartBar=iBarShift(Symbol(),0,BeginDay+StartHour*3600);
int FinishBar=iBarShift(Symbol(),0,BeginDay+EndHour*3600)+1;
if(StartBar==FinishBar || StartBar>=(Bars-1) || FinishBar>=(Bars-1)){ continue; }
double LowV=Low[iLowest(Symbol(),0,MODE_LOW,StartBar-FinishBar+1,FinishBar)];
double HighV=High[iHighest(Symbol(),0,MODE_HIGH,StartBar-FinishBar+1,FinishBar)];
double TargetU = (HighV-LowV)*(TargetLevel1-100)/100+HighV;
double TargetD = LowV-(HighV-LowV)*(TargetLevel1-100)/100;
double TargetU2 = (HighV-LowV)*(TargetLevel2-100)/100+HighV;
double TargetD2 = LowV-(HighV-LowV)*(TargetLevel2-100)/100;
if(LowV>_Point)
{
lastFlatMin=LowV;
}
if(HighV>_Point)
{
lastFlatMax=HighV;
}
// flat channel
for(int j=StartBar; j>=FinishBar; j–)
{
Up[j]=HighV;
Down[j]=LowV;
}
// ———————–
for(int j=FinishBar; j>=0 && Time[j]<NextDay; j–)
{
TargetUp[j] = TargetU;
TargetDn[j] = TargetD;
TargetUp2[j] = TargetU2;
TargetDn2[j] = TargetD2;
}
// ——————-
DrawLabel(Time[iBarShift(Symbol(),0,BeginDay)],HighV,”Lab”+DoubleToStr(Time[iBarShift(Symbol(),0,BeginDay)],0)+”U0″,UpColor,5);
DrawLabel(Time[iBarShift(Symbol(),0,BeginDay)],LowV,”Lab”+DoubleToStr(Time[iBarShift(Symbol(),0,BeginDay)],0)+”D0″,DnColor,5);
DrawLabel(Time[FinishBar],TargetU,”Lab”+DoubleToStr(Time[FinishBar],0)+”U”,TargetUpColor,5);
DrawLabel(Time[FinishBar],TargetD,”Lab”+DoubleToStr(Time[FinishBar],0)+”D”,TargetDnColor,5);
DrawLabel(Time[FinishBar],TargetU2,”Lab”+DoubleToStr(Time[FinishBar],0)+”U2″,TargetUpColor,5);
DrawLabel(Time[FinishBar],TargetD2,”Lab”+DoubleToStr(Time[FinishBar],0)+”D2″,TargetDnColor,5);
LastDay=BeginDay;
}else{
if(TimeHour(Time[i])>StartHour)
{
RealAlertFL=false;
}
}
}
if(AlertTL && lastAlertTarget<Time[0])
{
if(Low[0]<TargetUp[0] && High[0]>TargetUp[0])
{
if(AlertTL)
{
Alert(_Symbol+” MF +”+(string)TargetLevel1+” Breaking “+DoubleToString(TargetUp[0],_Digits));
}
lastAlertTarget=Time[0];
}
if(Low[0]<TargetDn[0] && High[0]>TargetDn[0])
{
if(AlertTL)
{
Alert(_Symbol+” MF -“+(string)TargetLevel1+” Breaking “+DoubleToString(TargetDn[0],_Digits));
}
lastAlertTarget=Time[0];
}
}
if(AlertTL && lastAlertTarget2<Time[0])
{
if(Low[0]<TargetUp2[0] && High[0]>TargetUp2[0])
{
if(AlertTL)
{
Alert(_Symbol+” MF +”+(string)TargetLevel2+” Breaking “+DoubleToString(TargetUp2[0],_Digits));
}
lastAlertTarget2=Time[0];
}
if(Low[0]<TargetDn2[0] && High[0]>TargetDn2[0])
{
if(AlertTL)
{
Alert(_Symbol+” MF -“+(string)TargetLevel2+” Breaking “+DoubleToString(TargetDn2[0],_Digits));
}
lastAlertTarget2=Time[0];
}
}
if(RealAlertFL && lastAlertFlat<Time[0])
{
if(Low[0]<lastFlatMax && High[0]>lastFlatMax)
{
if(RealAlertFL)
{
Alert(_Symbol+” MF Max level Flat Breaking “+DoubleToString(lastFlatMax,_Digits));
}
lastAlertFlat=Time[0];
}
if(Low[0]<lastFlatMin && High[0]>lastFlatMin)
{
if(RealAlertFL)
{
Alert(_Symbol+” MF Min level Flat Breaking “+DoubleToString(lastFlatMin,_Digits));
}
lastAlertFlat=Time[0];
}
}
WindowRedraw();
//—-
return(0);
}
//+——————————————————————+