//+------------------------------------------------------------------+
//|                                             OverlayEAControl.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
string ButtonSetLong = "Set_Long";
string ButtonSetShort = "Set_Short";
string ButtonSetSL="Set_SL";
double stop_loss=15;
double take_profit=0.0040;
double lots=0.01;
bool LongSet=false;
bool ShortSet=false;
bool LongButtonInit = false; 
bool ShortButtonInit = false; 
int ticket;
int LongButtonX=500;
int LongButtonY=000;
int ShortButtonX=600;
int ShortButtonY=000;
int SLButtonX=700;
int SLButtonY=0;
double prev_short=0.0000;
double prev_long=0.0000;
bool iscalm=false;
int left_margin=10;
int font_size=8;
color font_colour=clrWhite;
double ask,bid;
int period;
int YposBUY=400;
int YposSELL=450;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   iscalm=false;
Comment("");
 period=ObjectGetString(0,"OLTFLabel",OBJPROP_TEXT,0);
 LongSet=false;
 ShortSet=false;
 LongButtonInit = false; 
 ShortButtonInit = false; 
   EventSetMillisecondTimer(100);
set_askbid();
   CreateSetLongButton();
   CreateSetShortButton();
   if(IsTesting())
   {
   CreateSetSLButton();
   }
   LongButtonInit = true; 
   ShortButtonInit = true; 
   label("BUYLABEL","NO",YposBUY);
   label("SELLLABEL","NO",YposSELL);
   label("SETSL","No set",300);
   
    stop_loss=stop_loss*Point()*10;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
// Comment("ticked");
 //Comment("tick");
   period=ObjectGetString(0,"OLTFLabel",OBJPROP_TEXT,0);
   set_askbid();
  
   //Print(DoubleToString(stop_loss,8), "Piont ",Point());
   if(period<240)
   {
     Create_Level("LevelTop",clrMagenta,ask+(stop_loss));
     Create_Level("Levelask",clrMagenta,ask);
     Create_Level("Levelbid",clrAliceBlue,bid);
     Create_Level("LevelBottom",clrDodgerBlue,bid-stop_loss);
   //  Create_Level("StopLine",clrDodgerBlue,bid);
   }
   else
   {
     ObjectDelete("LevelTop");
     ObjectDelete("Levelask");
     ObjectDelete("Levelbid");
     ObjectDelete("LevelBottom");
     ObjectDelete("StopLine");
   }
   double Levelask=ObjectGetDouble(0,"Levelask",OBJPROP_PRICE);
   double Levelbid=ObjectGetDouble(0,"Levelbid",OBJPROP_PRICE);
   double LevelTop=ObjectGetDouble(0,"LevelTop",OBJPROP_PRICE);
   double LevelBottom=ObjectGetDouble(0,"LevelBottom",OBJPROP_PRICE);
   double StopLine=ObjectGetDouble(0,"StopLine",OBJPROP_PRICE);
   string objName="StopLine";
   ObjectCreate(NULL,objName,OBJ_TEXT,NULL,Time[WindowFirstVisibleBar()-30],StopLine);
   ObjectSetText(objName,"Stop Line",10,"Arial",clrWhite);
   ObjectSetDouble(0,objName,OBJPROP_PRICE,StopLine);
   ObjectSetInteger(0,"StopLine",OBJPROP_COLOR,clrYellow);
   ObjectSetInteger(0,objName,OBJPROP_TIME1,Time[WindowFirstVisibleBar()-30]);
 

   //Comment("SL",StopLine);
   if(prev_long!=LevelTop)
   {
    set_level("Levelask",LevelTop-stop_loss);
    prev_long=LevelTop;
   }
   if(prev_short!=LevelBottom)
   {
   set_level("Levelbid",LevelBottom+stop_loss);
   prev_short=LevelBottom;
   }
 
 
 //Comment("Short Set ",ShortSet, "  Long Set ",LongSet);
   set_askbid();
   Levelask=ObjectGetDouble(0,"Levelask",OBJPROP_PRICE);
   Levelbid=ObjectGetDouble(0,"Levelbid",OBJPROP_PRICE);
 //  Comment("bid ",bid,"  Levelask ",Levelask,"  ",bid>Levelask);
   //Comment("");
   iscalm=true;
   if(LongSet)// && bid>Levelask)
   {
     if(!iscalm)
     {
       iscalm=checklist();
     }
     if(!iscalm)
     {
      LongSet=false; 
      SetButtonLongGreen(ButtonSetLong,LongButtonX,LongButtonY,50,20);
     }
     if(iscalm && ask<Levelbid)
     {
     // Comment("long");
      LongSet=false;
      buy();
    }
   }
   if(ShortSet)// && bid>Levelask)
   {
     if(!iscalm)
     {
       iscalm=checklist();
     }
     if(!iscalm)
     {
      ShortSet=false; 
      SetButtonShortGreen(ButtonSetShort,ShortButtonX,ShortButtonY,50,20);
     }
     Print("CalmSell"," calm bool",iscalm,"  bid ",bid,"   asklevel ",Levelask);
     if(iscalm &&  bid>Levelask)
     {
       Comment("short");
       ShortSet=false;
       sell();
     }
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
  // Print("OnTimer");
   
  }
  void OnTick()
  {
  
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
set_askbid();
    if(sparam==ButtonSetLong )
   {
     if(LongSet)
     {
      LongSet=false;
      SetButtonLongGreen(ButtonSetLong,LongButtonX,LongButtonY,50,20);
     }
     else
     {
      LongSet=true;
      SetButtonLongRed(ButtonSetLong,LongButtonX,LongButtonY,50,20);
     }
   }
   
   if(sparam==ButtonSetShort) 
   {
     if(ShortSet)
     {
       ShortSet=false;
       SetButtonShortGreen(ButtonSetShort,ShortButtonX,ShortButtonY,50,20);
     }
     else
     {
      ShortSet=true;
      SetButtonShortRed(ButtonSetShort,ShortButtonX,ShortButtonY,50,20);
     }
  }
  if(sparam==ButtonSetSL) 
   {
    label("SETSL","SETSL",300);
   }
  }
//+------------------------------------------------------------------+
void CreateSetLongButton()
{
 SetButtonLongGreen(ButtonSetLong,LongButtonX,LongButtonY,50,20);
}
void CreateSetShortButton()
 {
  SetButtonShortGreen(ButtonSetShort,ShortButtonX,ShortButtonY,50,20);
 }
 void CreateSetSLButton()
 {
 SetButtonSL(ButtonSetSL,SLButtonX,SLButtonY,65,20);
 }
void SetButtonSL(string ButtonName,int XD,int YD,int XS,int YS)
{


   CreateButton(ButtonName,clrGreen,"Arial","Set SL",XD,YD,XS,YS);
}

void SetButtonLongRed(string ButtonName,int XD,int YD,int XS,int YS)
{
   iscalm=false;
   LongSet=true;
   //  Comment("Short Set ",ShortSet, "  Long Set ",LongSet);
   CreateButton(ButtonName,clrRed,"Arial","Long",XD,YD,XS,YS);
}
void SetButtonShortRed(string ButtonName,int XD,int YD,int XS,int YS)
{
    iscalm=false;
    ShortSet=true;
  //  Comment("Short Set ",ShortSet, "  Long Set ",LongSet);
    CreateButton(ButtonName,clrRed,"Arial","Short",XD,YD,XS,YS);
 }
  void SetButtonLongGreen(string ButtonName,int XD,int YD,int XS,int YS)
{
   iscalm=false;
   LongSet=false;
    // Comment("Short Set ",ShortSet, "  Long Set ",LongSet);
   CreateButton(ButtonName,clrLimeGreen,"Arial","Long",XD,YD,XS,YS);
}
void SetButtonShortGreen(string ButtonName,int XD,int YD,int XS,int YS)
{
    iscalm=false;
    ShortSet=false;
  //  Comment("Short Set ",ShortSet, "  Long Set ",LongSet);
    CreateButton(ButtonName,clrLimeGreen,"Arial","Short",XD,YD,XS,YS);
 }

void CreateButton(string sName, color cColor, string sFont, string sText,int XD,int YD,int XS,int YS){
// create a button on the chart

// many of these settings are hard coded below but you can easily have them as paramaters and pass them to the function
// just like I've done already with the color, font and text

   if(ObjectFind(sName)< 0){
      ObjectCreate(0,sName,OBJ_BUTTON,0,0,0);
   }
   
   // these could be external valiables to allow the user to create the button wherever they wanted
   ObjectSetInteger(0,sName,OBJPROP_XDISTANCE,XD);
   ObjectSetInteger(0,sName,OBJPROP_YDISTANCE,YD);
   ObjectSetInteger(0,sName,OBJPROP_XSIZE,XS);
   ObjectSetInteger(0,sName,OBJPROP_YSIZE,YS);
   ObjectSetInteger(0,sName,OBJPROP_CORNER,CORNER_LEFT_UPPER);
   
   ObjectSetString(0,sName,OBJPROP_TEXT,sText);
   ObjectSetInteger(0,sName,OBJPROP_COLOR, cColor);
   // I'm setting the background and border to the same as the chart background
   // as I'm just using a wingding arrow
   long result;
   ChartGetInteger(0,CHART_COLOR_BACKGROUND,0,result);
   color cBack = (color) result;
   ObjectSetInteger(0,sName,OBJPROP_BGCOLOR, cBack);
   ObjectSetInteger(0,sName,OBJPROP_BORDER_COLOR,cBack);
   ObjectSetInteger(0,sName,OBJPROP_HIDDEN, false);
   ObjectSetString(0,sName,OBJPROP_FONT,sFont);
   ObjectSetInteger(0,sName,OBJPROP_FONTSIZE,10);
   
}

void  Create_Level(string objName,color level_color,double price)
{
if(ObjectFind(objName)<0)
  {
  ObjectCreate(objName,OBJ_HLINE,NULL,0,price);
  ObjectSetInteger(0,objName,OBJPROP_COLOR,level_color);
   ObjectSetInteger(0,objName,OBJPROP_WIDTH,1);
  if(objName=="LevelTop" || objName=="LevelBottom")
  {
   ObjectSetInteger(0,objName,OBJPROP_WIDTH,3);
  }
 
  ObjectSetDouble(0,objName,OBJPROP_PRICE,price);
  if(objName=="Levelask" || objName=="Levelbid")
  {
    ObjectSetInteger(0,objName,OBJPROP_STYLE,STYLE_DASHDOT);
  }
  else
  {
    ObjectSetInteger(0,objName,OBJPROP_STYLE,STYLE_SOLID);
   }

 }

  }
void set_level(string objName,double price)

{

  ObjectSetDouble(0,objName,OBJPROP_PRICE,price);

}

void buy()
{
set_askbid();
double SL=ObjectGetDouble(0,"LevelBottom",OBJPROP_PRICE);
 //ticket=OrderSend(Symbol(),OP_BUY,lots,ask,3,SL,ask+take_profit,"Buy",16384,0,DeepPink);
// Alert("Buy");
 ticket=100;
 label("BUYLABEL","YES",YposBUY);
  iscalm=false;
 if(ticket>0 )
 {
     SetButtonLongGreen(ButtonSetLong,LongButtonX,LongButtonY,50,20);
     iscalm=false;
 }
}
void sell()
{
set_askbid();
double SL=ObjectGetDouble(0,"LevelTop",OBJPROP_PRICE);
 //ticket=OrderSend(Symbol(),OP_SELL,lots,bid,3,SL,bid-take_profit,"Sell",16384,0,DeepPink);
// Alert("Sell");
 ticket=100;
 label("SELLLABEL","YES",YposSELL);
  iscalm=false;
 if(ticket>0)
 {
   SetButtonShortGreen(ButtonSetShort,ShortButtonX,ShortButtonY,50,20);
  
  }
}

bool checklist()
{
     bool checked=true;
  //   Print("Checklist");
     int yesno;
    // if(1==2)
     {
    // Print("YesNo");
      yesno = MessageBox("Are You CALM ??","Are You CALM??",MB_YESNO);
    // Print("YesNoExec");
     if(yesno==7) //7= No
     {
      checked=false;
     }
     if(checked)
     {
     //yesno =MessageBox("4 x 15M wicks ?","4 x 15M wicks ?",MB_YESNO);
     if(yesno==7)
     {
      checked=false;
     }
     }
     }
     Print(yesno);
     return(checked);
}

void label(string obj_name,string text,int ypos)

{
  ObjectDelete(obj_name);
  ObjectCreate(obj_name,OBJ_LABEL,NULL,Time[WindowFirstVisibleBar()-5],ypos);
  //   ObjectSet(obj_name,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
  //if(int(obj_name)==0)
  {
   ObjectSetText(obj_name,text);
  }
  ObjectSet(obj_name,OBJPROP_XDISTANCE,left_margin);
  ObjectSet(obj_name,OBJPROP_COLOR,font_colour);
  ObjectSet(obj_name,OBJPROP_FONTSIZE,font_size);
  ObjectSet(obj_name,OBJPROP_YDISTANCE,ypos);
}

void set_askbid()
{
if(IsTesting())
{
bid=iClose(Symbol(),period,0);
ask=iClose(Symbol(),period,0)+0.0001;
}
else
{
bid=Bid;
//Print(" Set bid ",bid);
ask=Ask;
}
}

