//+------------------------------------------------------------------+
//| AutoBot.mq4 |
//+------------------------------------------------------------------+
extern string symbol= "EURUSD";
extern int Magic = 84578;
extern int StartRiskDepo = 100000;
extern int BrokerZnakovLot = 2;
//+--------
extern string _="Time";
extern int Start = 19;
extern int Finish = 3;
extern int GMT = 2;
//+--------
extern string __="BUY";
extern bool BUY = true;
extern int shagBUY = 10;
extern int MaxKolenBUY = 20;
extern double ProfitBUY = 1;
extern double kofLotsBUY = 1.1;
extern double kofShagBUY = 1.6;
extern double kofProfBUY = 0.9;
//+--------
extern string ___="SELL";
extern bool SELL = true;
extern int shagSELL = 10;
extern int MaxKolenSELL = 20;
extern double ProfitSELL = 0.6;
extern double kofLotsSELL = 1.2;
extern double kofShagSELL = 1.2;
extern double kofProfSELL = 0.9;
//+--------
bool ft = true;double lotsBUY,lotsSELL,PrBUY,PrSELL;
//+------------------------------------------------------------------+
double kof;int k,Lt,time,ftp;
//+------------------------------------------------------------------+
int ordersBUY(){int j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderType()==OP_BUY)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==symbol))j++;}return(j);}
//+------------------------------------------------------------------+
int ordersSELL(){int j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderType()==OP_SELL)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==symbol))j++;}return(j);}
//+------------------------------------------------------------------+
void Send_buy(double Lot,string com){
if(OrderSend(symbol,OP_BUY,NormalizeDouble(Lot,BrokerZnakovLot),NormalizeDouble(MarketInfo(symbol,MODE_ASK),MarketInfo(symbol,MODE_DIGITS)),1*k,0,0,com,Magic,0,SteelBlue)>0){PlaySound("Wait.wav");return(0);}
Sleep(2000);
if(OrderSend(symbol,OP_BUY,NormalizeDouble(Lot,BrokerZnakovLot),NormalizeDouble(MarketInfo(symbol,MODE_ASK),MarketInfo(symbol,MODE_DIGITS)),1*k,0,0,com,Magic,0,SteelBlue)>0){PlaySound("Wait.wav");return(0);}
Sleep(2000);
if(OrderSend(symbol,OP_BUY,NormalizeDouble(Lot,BrokerZnakovLot),NormalizeDouble(MarketInfo(symbol,MODE_ASK),MarketInfo(symbol,MODE_DIGITS)),1*k,0,0,com,Magic,0,SteelBlue)>0){PlaySound("Wait.wav");return(0);}}
//+------------------------------------------------------------------+
void Send_sell(double Lot,string com){
if(OrderSend(symbol,OP_SELL,NormalizeDouble(Lot,BrokerZnakovLot),NormalizeDouble(MarketInfo(symbol,MODE_BID),MarketInfo(symbol,MODE_DIGITS)),1*k,0,0,com,Magic,0,Brown)>0){PlaySound("Wait.wav");return(0);}
Sleep(2000);
if(OrderSend(symbol,OP_SELL,NormalizeDouble(Lot,BrokerZnakovLot),NormalizeDouble(MarketInfo(symbol,MODE_BID),MarketInfo(symbol,MODE_DIGITS)),1*k,0,0,com,Magic,0,Brown)>0){PlaySound("Wait.wav");return(0);}
Sleep(2000);
if(OrderSend(symbol,OP_SELL,NormalizeDouble(Lot,BrokerZnakovLot),NormalizeDouble(MarketInfo(symbol,MODE_BID),MarketInfo(symbol,MODE_DIGITS)),1*k,0,0,com,Magic,0,Brown)>0){PlaySound("Wait.wav");return(0);}}
//+------------------------------------------------------------------+
int ALLCloseBUY(){
if(ordersBUY()>0){BUYClose();if(ordersBUY()==0){PlaySound("Wait.wav");return(0);}Sleep(500);}
if(ordersBUY()>0){BUYClose();if(ordersBUY()==0){PlaySound("Wait.wav");return(0);}Sleep(500);}
if(ordersBUY()>0){BUYClose();if(ordersBUY()==0){PlaySound("Wait.wav");return(0);}Sleep(500);}
if(ordersBUY()>0){BUYClose();if(ordersBUY()==0){PlaySound("Wait.wav");return(0);}Sleep(500);}
if(ordersBUY()>0){BUYClose();if(ordersBUY()==0){PlaySound("Wait.wav");return(0);}}}
//+------------------------------------------------------------------+
int BUYClose(){RefreshRates();for(int i=OrdersTotal(); i+1>=0; i--){
if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderType()==OP_BUY)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==symbol))
{OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(symbol,MODE_DIGITS)),MarketInfo(Symbol(),MODE_SPREAD));}}}
//+------------------------------------------------------------------+
int ALLCloseSELL(){
if(ordersSELL()>0){SELLClose();if(ordersSELL()==0){PlaySound("Wait.wav");return(0);}Sleep(500);}
if(ordersSELL()>0){SELLClose();if(ordersSELL()==0){PlaySound("Wait.wav");return(0);}Sleep(500);}
if(ordersSELL()>0){SELLClose();if(ordersSELL()==0){PlaySound("Wait.wav");return(0);}Sleep(500);}
if(ordersSELL()>0){SELLClose();if(ordersSELL()==0){PlaySound("Wait.wav");return(0);}Sleep(500);}
if(ordersSELL()>0){SELLClose();if(ordersSELL()==0){PlaySound("Wait.wav");return(0);}}}
//+------------------------------------------------------------------+
int SELLClose(){RefreshRates();for(int i=OrdersTotal(); i+1>=0; i--){
if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderType()==OP_SELL)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==symbol))
{OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(symbol,MODE_DIGITS)),MarketInfo(Symbol(),MODE_SPREAD));}}}
//+------------------------------------------------------------------+
double profitsBUY(){RefreshRates();double j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderSymbol()==symbol)&&(OrderMagicNumber()==Magic)&&(OrderType()==OP_BUY))j=j+OrderProfit();}return(j);}
//+------------------------------------------------------------------+
double profitsSELL(){RefreshRates();double j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderSymbol()==symbol)&&(OrderMagicNumber()==Magic)&&(OrderType()==OP_SELL))j=j+OrderProfit();}return(j);}
//+------------------------------------------------------------------+
double commBUY(){RefreshRates();double j=-1000;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderSymbol()==symbol)&&(OrderMagicNumber()==Magic)&&(OrderType()==OP_BUY)){if(StrToDouble(OrderComment())>j)j=StrToDouble(OrderComment());}}return(j);}
//+------------------------------------------------------------------+
double commSELL(){RefreshRates();double j=1000;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)if((OrderSymbol()==symbol)&&(OrderMagicNumber()==Magic)&&(OrderType()==OP_SELL)){if(StrToDouble(OrderComment())<j)j=StrToDouble(OrderComment());}}return(j);}
//+------------------------------------------------------------------+
int start()
{
//æææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææ+
if((ordersBUY()>0)&&(Hour()>=Finish+GMT)&&(Hour()<Start+GMT)&&(profitsBUY()>=0)){ALLCloseBUY();}
if((ordersSELL()>0)&&(Hour()>=Finish+GMT)&&(Hour()<Start+GMT)&&(profitsSELL()>=0)){ALLCloseSELL();}
if(profitsBUY()<-StartRiskDepo){ALLCloseBUY();}if(profitsSELL()<-StartRiskDepo){ALLCloseSELL();}
//æææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææ+
if(iTime(symbol,5,0)==time)return(0);time=iTime(symbol,5,0);
//æææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææææ+
for(int i=1;i<=1000;i++){if(StartRiskDepo==1000*i)
{lotsBUY=0.01*i;lotsSELL=0.01*i;
PrBUY=ProfitBUY*i;PrSELL=ProfitSELL*i;
if(Digits==5)k=10;if(Digits==4)k=1;
if(BUY == true)startBUY();
if(SELL == true)startSELL();
break;}}}
//+------------------------------------------------------------------+
int startBUY(){if(Year()<2005)return(0);if(Year()==2005)per5();if(Year()==2006)per6();if(Year()==2007)per7();
if(Year()==2008)per8();if(Year()==2009)per9();if(Year()==2010)per10();
if((ordersBUY()==0)&&(Hour()>=Start+GMT||Hour()<Finish+GMT)&&(ftp!=1)){Send_buy(lotsBUY,MarketInfo(symbol,MODE_BID));}
for(int i=1;i<MaxKolenBUY;i++){if((ordersBUY()==i)&&(profitsBUY()>PrBUY*i*kofProfBUY))ALLCloseBUY();
if((ordersBUY()==i)&&(MarketInfo(symbol,MODE_BID)<commBUY()-i*kofShagBUY*shagBUY*k*MarketInfo(symbol,MODE_POINT))&&(ftp!=1))
{if(ordersBUY()==i)Send_buy(lotsBUY*i*kofLotsBUY,MarketInfo(symbol,MODE_BID));}}}
//+------------------------------------------------------------------+
int startSELL(){if(Year()<2005)return(0);if(Year()==2005)per5();if(Year()==2006)per6();if(Year()==2007)per7();
if(Year()==2008)per8();if(Year()==2009)per9();if(Year()==2010)per10();
if((ordersSELL()==0)&&(Hour()>=Start+GMT||Hour()<Finish+GMT)&&(ftp!=2)){Send_sell(lotsSELL,MarketInfo(symbol,MODE_BID));}
for(int i=1;i<MaxKolenSELL;i++){if((ordersSELL()==i)&&(profitsSELL()>PrSELL*i*kofProfSELL))ALLCloseSELL();
if((ordersSELL()==i)&&(MarketInfo(symbol,MODE_BID)>commSELL()+i*kofShagSELL*shagSELL*k*MarketInfo(symbol,MODE_POINT))&&(ftp!=2))
{if(ordersSELL()==i)Send_sell(lotsSELL*i*kofLotsSELL,MarketInfo(symbol,MODE_BID));}}}
//+------------------------------------------------------------------+
int per5()
{
kofLotsBUY = 1.6;kofShagBUY = 1.5;kofProfBUY = 1.3;
kofLotsSELL = 1.8;kofShagSELL = 1.8;kofProfSELL = 0.9;
ftp=0;
if((Month()==2)&&(Day()==23))ftp=1;
if((Month()==3)&&(Day()==21))ftp=1;
if((Month()==6)&&(Day()==2))ftp=1;
if((Month()==5)&&(Day()==30))ftp=1;
if((Month()==5)&&(Day()==31))ftp=1;
if((Month()==6)&&(Day()==1))ftp=1;
if((Month()==7)&&(Day()==13))ftp=1;
if((Month()==1)&&(Day()==3))ftp=2;
if((Month()==2)&&(Day()==22))ftp=2;
if((Month()==4)&&(Day()==18))ftp=2;
if((Month()==8)&&(Day()==3))ftp=2;
}
int per6()
{
kofLotsBUY = 1.8;kofShagBUY = 1;kofProfBUY = 0.6;
kofLotsSELL = 1.3;kofShagSELL = 1.1;kofProfSELL = 1;
ftp=0;
if((Month()==5)&&(Day()==19))ftp=1;
if((Month()==5)&&(Day()==22))ftp=1;
if((Month()==11)&&(Day()==24))ftp=2;
}
int per7()
{
kofLotsBUY = 1.8;kofShagBUY = 1.2;kofProfBUY = 0.8;
kofLotsSELL = 2;kofShagSELL = 1;kofProfSELL = 1.2;
ftp=0;
if((Month()==10)&&(Day()==8))ftp=1;
if((Month()==4)&&(Day()==19))ftp=1;
if((Month()==4)&&(Day()==10))ftp=2;
if((Month()==3)&&(Day()==5))ftp=2;
if((Month()==7)&&(Day()==10))ftp=2;
if((Month()==11)&&(Day()==6))ftp=2;
if((Month()==11)&&(Day()==20))ftp=2;
if((Month()==11)&&(Day()==23))ftp=2;
if((Month()==12)&&(Day()==26))ftp=2;
if((Month()==6)&&(Day()==29))ftp=2;
if((Month()==2)&&(Day()==6))ftp=2;
if((Month()==9)&&(Day()==20))ftp=2;
if((Month()==9)&&(Day()==21))ftp=2;
if((Month()==11)&&(Day()==13))ftp=2;
if((Month()==12)&&(Day()==28))ftp=2;
}
int per8()
{
kofLotsBUY = 1.5;kofShagBUY = 1;kofProfBUY = 1.1;
kofLotsSELL = 1.3;kofShagSELL = 1.1;kofProfSELL = 1;
ftp=0;
if((Month()==2)&&(Day()==5))ftp=1;
if((Month()==2)&&(Day()==6))ftp=1;
if((Month()==2)&&(Day()==7))ftp=1;
if((Month()==2)&&(Day()==8))ftp=1;
if((Month()==3)&&(Day()==25))ftp=2;
if((Month()==6)&&(Day()==12))ftp=1;
if((Month()==10)&&(Day()==22))ftp=1;
if((Month()==4)&&(Day()==16))ftp=2;
if((Month()==12)&&(Day()==30))ftp=2;
}
int per9()
{
kofLotsBUY = 1;kofShagBUY = 2;kofProfBUY = 1.1;
kofLotsSELL = 1.3;kofShagSELL = 2;kofProfSELL = 0.8;
ftp=0;
if((Month()==6)&&(Day()==16))ftp=1;
if((Month()==9)&&(Day()==8))ftp=2;
if((Month()==9)&&(Day()==9))ftp=2;
}
int per10()
{
kofLotsBUY = 1.1;kofShagBUY = 1.6;kofProfBUY = 0.9;
kofLotsSELL = 1.2;kofShagSELL = 1.2;kofProfSELL = 0.9;
ftp=0;
if((Month()==1)&&(Day()==18))ftp=2;
if((Month()==9)&&(Day()==13))ftp=2;
if((Month()==3)&&(Day()==31))ftp=2;
if((Month()==1)&&(Day()==11))ftp=1;
if((Month()==5)&&(Day()==4))ftp=1;
if((Month()==5)&&(Day()==3))ftp=1;
if((Month()==5)&&(Day()==5))ftp=1;
if((Month()==9)&&(Day()==7))ftp=1;
if((Month()==11)&&(Day()==8))ftp=1;
if((Month()==11)&&(Day()==9))ftp=1;
}
//===========================================================================================================================
// Modification in order to close all trades each day at a predetermined CloseHour
bool IsTrade = False;
for (int i = 0; i< total; i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderType() <= OP_SELL && OrderSymbol()==Symbol())
{
IsTrade = True;
if(OrderType() == OP_BUY)
{
if (CloseHour==Hour())
{
switch(OrderType())
{
case OP_BUY : OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Violet); break;
case OP_SELL : OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Violet); break;
case OP_BUYLIMIT : OrderDelete(OrderTicket()); break;
case OP_SELLLIMIT: OrderDelete(OrderTicket()); break;
case OP_BUYSTOP : OrderDelete(OrderTicket()); break;
case OP_SELLSTOP : OrderDelete(OrderTicket()); break;
}
}
}
}
}