//+------------------------------------------------------------------+
//|                                                AUnNomGuiOnly.mq4 |
//|                                                             Joan |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Joan"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict



extern double lots=0.01; 
extern double space=500;
extern int spaceorders=1;
extern double space_lots=0.03;
extern double space1=500;
extern int space1orders=1;
extern double space1_lots=0.05;
extern double space2=500;
extern int space2orders=1;
extern double space2_lots=0.07;
extern double space3=500;
extern int space3orders=99;
extern double space3_lots=0.09;
extern double multiplier=1.5;
extern double profit=0.80;
extern double globalprofit=0;
extern double pairglobalprofit=0;
extern double maximaloss=0;
//extern string hello="hello";
extern int magicbuy=1;
extern string buycomment="buy";
extern int magicsell=2;
extern string sellcomment="sell";
extern bool openonnewcandle=true;
extern string TradingHours="Hour 0-24 Minute 0-59";
extern int Start_Hour=00;
extern int Start_Minute=00;
extern int Finish_Hour=24;
extern int Finish_Minute=0;
extern int tradesperday=99;
extern int cciperiod=0;
extern double ccimax=100;
extern double ccimin=-100;


extern bool suspendtrades=false;
extern bool closeallsellsnow=false;
extern bool closeallbuysnow=false;
extern bool closeallnow=false;


int BuyTicket=0;
int SellTicket=0;
double totalprofit;
double profitcalctotal=0;
double totalglobalprofit=0;
double ccivalue=0;
bool sellallowed=false;
bool buyallowed=false;
bool firebuy=true;
bool firesell=true;
string stoptrading="0";
int gvbuy=0;
int gvsell=0;
string globalvariablebuyname;
string globalvariablesellname;
bool newbarvar=0;
int tradestoday=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
   
   
   {
   
   BuyTicket=OpenOrderBuyLots();
   SellTicket=OpenOrderSellLots();
   newbarvar=newbar();
   tradestoday=TradesToday();
   
   if (GlobalVariableGet(stoptrading)==1 && OrdersTotal()==0 && CheckTradingTime()==true)
   {
      GlobalVariableSet(stoptrading,0);  
   }
   
   if (cciperiod==0)
   {
   firesell=true;
   firebuy=true;   
   }
   if (cciperiod>0 && newbarvar==true)
   {
      firebuy=false;
      firesell=false;
      ccivalue=iCCI(Symbol(),0,cciperiod,PRICE_TYPICAL,0);
      
      if(sellallowed==true && ccivalue<ccimin) 
      {
      firesell=true;
      sellallowed=false;
            Print("FireSell");

      }
      if(buyallowed==true && ccivalue>ccimax) 
      {
      firebuy=true;
      buyallowed=false;
            Print("FireBuy");
      }
      
      if (ccivalue<ccimax && ccivalue>ccimin)
      {
      buyallowed=true;
      sellallowed=true;
      }
   }
   if (tradesperday>tradestoday && CheckTradingTime()==true && BuyTicket==0&& suspendtrades==false && firebuy==true && closeallnow==false && GlobalVariableGet(stoptrading)==0)
   {
     OrderSend (Symbol(),OP_BUY,lots,Ask,3,0,0,buycomment,magicbuy,0,Blue); 
     firebuy=false; 
     globalvariablebuyname=StringConcatenate("Buy ",Symbol()," ",magicbuy);
     GlobalVariableSet(globalvariablebuyname,1);
   }         

   if ((openonnewcandle==true && newbarvar==true && BuyTicket!=0)|| (openonnewcandle==false && BuyTicket!=0))
   {

      OrderSelect(BuyTicket,SELECT_BY_TICKET);
      if( Ask<=OrderOpenPrice()-space*Point()&&GlobalVariableGet(globalvariablebuyname)<spaceorders)
      {
            if (multiplier>0)OrderSend (Symbol(),OP_BUY,NormalizeDouble(OrderLots()*multiplier,2),Ask,3,0,0,buycomment,magicbuy,0,Blue); 
            if (multiplier==0)OrderSend (Symbol(),OP_BUY,space_lots,Ask,3,0,0,buycomment,magicbuy,0,Blue);

            gvbuy=GlobalVariableGet(globalvariablebuyname)+1;
            GlobalVariableSet(globalvariablebuyname,gvbuy);
      }  
      if( Ask<=OrderOpenPrice()-space1*Point()&&GlobalVariableGet(globalvariablebuyname)<=(spaceorders+space1orders-1) && GlobalVariableGet(globalvariablebuyname)>=spaceorders)
      {
            if (multiplier>0)OrderSend (Symbol(),OP_BUY,NormalizeDouble(OrderLots()*multiplier,2),Ask,3,0,0,buycomment,magicbuy,0,Blue); 
            if (multiplier==0)OrderSend (Symbol(),OP_BUY,space1_lots,Ask,3,0,0,buycomment,magicbuy,0,Blue);

            gvbuy=GlobalVariableGet(globalvariablebuyname)+1;
            GlobalVariableSet(globalvariablebuyname,gvbuy);
      }  
      if( Ask<=OrderOpenPrice()-space2*Point()&&GlobalVariableGet(globalvariablebuyname)<=(space2orders+space1orders+spaceorders-1) && GlobalVariableGet(globalvariablebuyname)>(spaceorders+space1orders-1))
      {
            if(multiplier>0)OrderSend (Symbol(),OP_BUY,NormalizeDouble(OrderLots()*multiplier,2),Ask,3,0,0,buycomment,magicbuy,0,Blue); 
            if (multiplier==0)OrderSend (Symbol(),OP_BUY,space2_lots,Ask,3,0,0,buycomment,magicbuy,0,Blue);
            
            gvbuy=GlobalVariableGet(globalvariablebuyname)+1;
            GlobalVariableSet(globalvariablebuyname,gvbuy);
      }  
      if( Ask<=OrderOpenPrice()-space3*Point()&&GlobalVariableGet(globalvariablebuyname)<=(space3orders+space2orders+space1orders+spaceorders) && GlobalVariableGet(globalvariablebuyname)>(spaceorders+space1orders+space2orders-1))
      {
            if (multiplier>0)OrderSend (Symbol(),OP_BUY,NormalizeDouble(OrderLots()*multiplier,2),Ask,3,0,0,buycomment,magicbuy,0,Blue); 
            if (multiplier==0)OrderSend (Symbol(),OP_BUY,space3_lots,Ask,3,0,0,buycomment,magicbuy,0,Blue);

            gvbuy=GlobalVariableGet(globalvariablebuyname)+1;
            GlobalVariableSet(globalvariablebuyname,gvbuy);
      }  
   }
    
      tradestoday=TradesToday();

   if (tradesperday>tradestoday && CheckTradingTime()==true && SellTicket==0&& suspendtrades==false && firesell==true && closeallnow==false && GlobalVariableGet(stoptrading)==0)
   {
     OrderSend (Symbol(),OP_SELL,lots,Bid,3,0,0,sellcomment,magicsell,0,Red);  
     firesell=false;
     globalvariablesellname=StringConcatenate("Sell ",Symbol()," ",magicsell);
     GlobalVariableSet(globalvariablesellname,1);
   }

   if ((openonnewcandle==true && newbarvar==true && SellTicket!=0)|| (openonnewcandle==false && SellTicket!=0))
   {
      OrderSelect(SellTicket,SELECT_BY_TICKET);
      if( Bid>=OrderOpenPrice()+space*Point()&&GlobalVariableGet(globalvariablesellname)<spaceorders)
      {
            if (multiplier>0)OrderSend (Symbol(),OP_SELL,NormalizeDouble(OrderLots()*multiplier,2),Bid,3,0,0,sellcomment,magicsell,0,Red); 
            if (multiplier==0)OrderSend (Symbol(),OP_SELL,space_lots,Bid,3,0,0,sellcomment,magicsell,0,Red);

            gvsell=GlobalVariableGet(globalvariablesellname)+1;
            GlobalVariableSet(globalvariablesellname,gvsell);
      } 
      if( Bid>=OrderOpenPrice()+space1*Point()&&GlobalVariableGet(globalvariablesellname)<=(spaceorders+space1orders-1) && GlobalVariableGet(globalvariablesellname)>=spaceorders)
      
      {
            if(multiplier>0)OrderSend (Symbol(),OP_SELL,NormalizeDouble(OrderLots()*multiplier,2),Bid,3,0,0,sellcomment,magicsell,0,Red); 
            if (multiplier==0)OrderSend (Symbol(),OP_SELL,space1_lots,Bid,3,0,0,sellcomment,magicsell,0,Red);

            gvsell=GlobalVariableGet(globalvariablesellname)+1;
            GlobalVariableSet(globalvariablesellname,gvsell);
      } 
      if( Bid>=OrderOpenPrice()+space2*Point()&&GlobalVariableGet(globalvariablesellname)<=(space2orders+space1orders+spaceorders-1) && GlobalVariableGet(globalvariablesellname)>(spaceorders+space1orders-1))
      
      {
            if(multiplier>0)OrderSend (Symbol(),OP_SELL,NormalizeDouble(OrderLots()*multiplier,2),Bid,3,0,0,sellcomment,magicsell,0,Red); 
            if (multiplier==0)OrderSend (Symbol(),OP_SELL,space2_lots,Bid,3,0,0,sellcomment,magicsell,0,Red);

            gvsell=GlobalVariableGet(globalvariablesellname)+1;
            GlobalVariableSet(globalvariablesellname,gvsell);
      } 
      if( Bid>=OrderOpenPrice()+space3*Point()&&GlobalVariableGet(globalvariablesellname)<=(space3orders+space2orders+space1orders+spaceorders) && GlobalVariableGet(globalvariablesellname)>(spaceorders+space1orders+space2orders-1))
      
      {
            if (multiplier>0)OrderSend (Symbol(),OP_SELL,NormalizeDouble(OrderLots()*multiplier,2),Bid,3,0,0,sellcomment,magicsell,0,Red); 
            if (multiplier==0)OrderSend (Symbol(),OP_SELL,space3_lots,Bid,3,0,0,sellcomment,magicsell,0,Red);
            gvsell=GlobalVariableGet(globalvariablesellname)+1;
            GlobalVariableSet(globalvariablesellname,gvsell);
      } 

   } 
   
   double profitcalcbuy=0;
for(int k=OrdersTotal()-1; k>=0;k--)
{
   OrderSelect(k,SELECT_BY_POS);
   if (Symbol()==OrderSymbol()&&OrderType()==OP_BUY)
   {
   profitcalcbuy=profitcalcbuy+OrderProfit()+OrderSwap()+OrderCommission();
   }
}

 if ((profit>0 && profitcalcbuy>=profit) || closeallbuysnow==true)
   {
      CloseAllBuyOrders();
      firebuy=false;

   }  
   
   
      double profitcalcsell=0;
for(int j=OrdersTotal()-1; j>=0;j--)
{
   OrderSelect(j,SELECT_BY_POS);
   if (Symbol()==OrderSymbol()&&OrderType()==OP_SELL)
   {
   profitcalcsell=profitcalcsell+OrderProfit()+OrderSwap()+OrderCommission();
   }
}
 if ((profit>0 && profitcalcsell>=profit) || closeallsellsnow==true)
   {
      CloseAllSellOrders();
      firesell=false;

   }  
   if (pairglobalprofit>0 && profitcalcbuy+profitcalcsell>=pairglobalprofit)
   {
      CloseAllSellOrders();
      CloseAllBuyOrders();
      firebuy=false;
      firesell=false;    
   }

totalglobalprofit=TotalProfit();

if (pairglobalprofit>0)
Comment("Buy: ",profitcalcbuy,"\n","Sell: ",profitcalcsell,"\n","Pair Total: ",profitcalcbuy+profitcalcsell,"\n","Total: ",totalglobalprofit);
else 
Comment("Buy: ",profitcalcbuy,"\n","Sell: ",profitcalcsell,"\n","Total: ",totalglobalprofit);


if((globalprofit>0 && totalglobalprofit>=globalprofit)||(maximaloss<0 &&totalglobalprofit<=maximaloss))
{
GlobalVariableSet(stoptrading,1);
CloseAllOrders();
firebuy=false;
firesell=false;
}
   
   
  }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

double OpenOrderBuyLots()
{
   double issell1=0;
   double issell2=0;
   int ticket1=0;

   for (int i=0;i<OrdersTotal();i++)
   {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderType()==OP_BUY && OrderSymbol()==Symbol()&& OrderMagicNumber()==magicbuy)
      {
         
         issell1=OrderLots();  
         if (issell1>=issell2) 
         {
         issell2=issell1; 
         ticket1=(OrderTicket());
         }   
      }  
   }
   return(ticket1);
}


double OpenOrderSellLots()
{
   double issell1=0;
   double issell2=0;
   int ticket1=0;

   for (int l=0;l<=OrdersTotal();l++)
   {
      OrderSelect(l,SELECT_BY_POS);
      if(OrderType()==OP_SELL && OrderSymbol()==Symbol()&& OrderMagicNumber()==magicsell)
      {
         
         issell1=OrderLots();  
         if (issell1>=issell2) 
         {
         issell2=issell1; 
         ticket1=(OrderTicket());
         }   
      }  
   }
   return(ticket1);
}
void CloseAllBuyOrders()
{


for (int m=OrdersTotal();m>=0;m--)
   {
      OrderSelect(m,SELECT_BY_POS);
      if(OrderType()==OP_BUY && OrderSymbol()==Symbol()&& OrderMagicNumber()==magicbuy)
      {
      RefreshRates();
      OrderClose(OrderTicket(),OrderLots(),Bid,0,Blue);
      }

}
GlobalVariableSet(globalvariablebuyname,0);
}


void CloseAllSellOrders()
{
for (int h=OrdersTotal();h>=0;h--)
   {
      
      OrderSelect(h,SELECT_BY_POS);
      if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magicsell)
      {
      RefreshRates();

      OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
      }

}
GlobalVariableSet(globalvariablesellname,0);
}


void CloseAllOrders()
{
for (int i=OrdersTotal();i>=0;i--)
   {
      
      OrderSelect(i,SELECT_BY_POS);
      if(OrderType()==OP_SELL)
      {
      RefreshRates();

      OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
      }
      if(OrderType()==OP_BUY)
      {
      RefreshRates();

      OrderClose(OrderTicket(),OrderLots(),Bid,0,Blue);
      }

}
GlobalVariableSet(globalvariablesellname,0);
}


double TotalProfit()
{

profitcalctotal=0;
for (int j=OrdersTotal();j>=0;j--)
   {
      OrderSelect(j,SELECT_BY_POS);
      
      RefreshRates();
      profitcalctotal=profitcalctotal+OrderProfit()+OrderSwap()+OrderCommission();
      
   }
return(profitcalctotal);
}


bool newbar()
{
static datetime time = Time[0];

if(Time[0] > time)
{
 time = Time[0]; //newbar, update time
 return (true);
} 
else
return(false);
}

bool CheckTradingTime()
   {
   int Current_Minute= TimeMinute(TimeCurrent());
   int Current_Hour = TimeHour(TimeCurrent());
//Comment(Current_Hour," : ",Current_Minute);
   
   
   if (Start_Hour == 0 && Finish_Hour == 24)
   {
      if (Start_Minute==0 && Finish_Minute==0)
      return (true);   
   } 
   if (Start_Hour>Finish_Hour) return(true);
     
   if ( (Current_Hour < Start_Hour) || (Current_Hour > Finish_Hour) ) 
   {   
   return (false);
   }
   if ( (Current_Hour == Start_Hour) || (Current_Hour == Finish_Hour) ) 
   {
    if ((Current_Minute < Start_Minute) || (Current_Minute>=Finish_Minute) )
    return(false);     
   }
   return(true);
   }
   
double TradesToday()
{
int count=0;

for (int k=OrdersTotal();k>=0;k--)
{
   OrderSelect(k,SELECT_BY_POS);
   if(OrderLots()==lots && TimeDay(OrderOpenTime())==TimeDay(TimeCurrent()))
   {
   count=count+1;
   Print ("OT: ",count);
   }
}
for (int l=OrdersHistoryTotal();l>=0;l--)
{
   OrderSelect(l,SELECT_BY_POS,MODE_HISTORY);
   if(OrdersHistoryTotal()!=0 && OrderLots()==lots && TimeDay(OrderOpenTime())==TimeDay(TimeCurrent()))
   {
   count=count+1;
   }

}
return(count);

}