//+------------------------------------------------------------------+
//|                                            Sovetnik GENADIY2.mq4 |
//+------------------------------------------------------------------+
#property copyright " panelektrik@yandex.ru"
//---- 
 extern int magic    =   11;
//----
 extern datetime START = D'2010.01.01 00:00';
 extern int  MinuteDelay = 100; 
//---- 
 extern double lot =1;
//+----
 extern int StartDW = 100;
 extern int StartUP = -100;
//----
 extern int EndDW = 100;
 extern int EndUP = 100;
 extern int StopLossDW = -1000; 
 extern int StopLossUP = -1000;  
//----
 extern double  KofKv = 0.5;
//----
 int MDbars;
 double Delta[15000],Otklonenie[15000],Ostatok[15000],Diapazon[15000],MiddleSpread[15000]; 
//+------------------------------------------------------------------+
int init()
 {MDbars=NormalizeDouble(MinuteDelay/Period(),0);}
//+------------------------------------------------------------------+
void info()
 {object("MinuteDelay","MinuteDelay = "+MDbars*Period()+"  мин.",9,White,25,1);
 object("Перерасчет","Перерасчет = "+DoubleToStr(Ostatok[0],0)+"  мин.",9,White,37,1);
 object("Рабочий спрэд","Рабочий спрэд = "+DoubleToStr(Otklonenie[0]/Point,0),9,White,49,1);
 object("StartDW","StartDW = "+DoubleToStr(StartDW+(Diapazon[0]/Point)*KofKv,0),9,White,61,1);
 object("StartUP","StartUP = "+DoubleToStr(StartUP-(Diapazon[0]/Point)*KofKv,0),9,White,73,1); 
 object("EndDW","EndDW = "+EndDW,9,White,85,1);   
 object("EndUP","EndUP = "+EndUP,9,White,97,1);  
 object("profitsDW","profitsDW = "+DoubleToStr(profits(1),0),9,White,109,1);
 object("profitsUP","profitsUP = "+DoubleToStr(profits(0),0),9,White,121,1);
 object("ordersDW","ordersDW = "+orders(0),9,White,133,1);
 object("ordersUP","ordersUP = "+orders(1),9,White,145,1);}
//+------------------------------------------------------------------+
void object(string z,string x,int b,color c,int v,int w)
 {ObjectCreate(z,OBJ_LABEL,0,0,0);ObjectSetText(z,x,b,"Times New Roman",c);
 ObjectSet(z,OBJPROP_CORNER,w);ObjectSet(z,OBJPROP_XDISTANCE,4);
 ObjectSet(z,OBJPROP_YDISTANCE,v);}
//+------------------------------------------------------------------+
int orders(int Type)
 {RefreshRates();int j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
 if((OrderType()==Type)&&(OrderMagicNumber()==magic))j++;}return(j);}
//+------------------------------------------------------------------+
void Send(int Type,double LOT)
 {RefreshRates();
 if((Type==0)&&(orders(0)==0)){OrderSend(Symbol(),OP_BUY,LOT,Ask,20*Point,0,0,WindowExpertName(),magic,0,Blue);Print(GetLastError());PlaySound("Wait.wav");}
 if((Type==1)&&(orders(1)==0)){OrderSend(Symbol(),OP_SELL,LOT,Bid,20*Point,0,0,WindowExpertName(),magic,0,Red);Print(GetLastError());PlaySound("Wait.wav");}}
//+------------------------------------------------------------------+
double profits(int Type)
 {RefreshRates();double j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
 if((OrderType()==Type)&&(OrderMagicNumber()==magic))j=OrderProfit();}return(j);}
//+------------------------------------------------------------------+
int ClosE(int Type)
 {while(!IsStopped()){RefreshRates();for(int i=0;i<OrdersTotal();i++){
 if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(Type==0)&&(OrderMagicNumber()==magic))
 {OrderClose(OrderTicket(),OrderLots(),Bid,(Ask-Bid));Print(GetLastError());PlaySound("Wait.wav");}
 if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(Type==1)&&(OrderMagicNumber()==magic))
 {OrderClose(OrderTicket(),OrderLots(),Ask,(Ask-Bid));Print(GetLastError());PlaySound("Wait.wav");}}
 if(orders(Type)==0){break;}Sleep(1000);}}
//+------------------------------------------------------------------+
int indikator()
 {int tm,T;double Middle,M;MDbars=NormalizeDouble(MinuteDelay/Period(),0);
//---- 
 tm=10000000;for(int i=iBarShift(NULL,0,START,false);i>=0;i--){
//---- 
 Delta[i]=Close[i]-Close[iBarShift(NULL,0,START,false)];
//---- 
 if(i<=tm-MDbars){double v=0;for(int ii=MDbars;ii>0;ii--){v=v+Delta[ii+i];}Middle=v/MDbars;tm=i;T=Time[i];M=MiddleSpread[i+1];}MiddleSpread[i]=Middle;
 if(Time[i]>START+MinuteDelay*60){Otklonenie[i]=Delta[i]-MiddleSpread[i];}}
 Ostatok[0]=((MDbars*Period()*60-(iTime(NULL,1,0)-T))/60);
 Diapazon[0]=MathAbs(MiddleSpread[0]-M);
 }
//+------------------------------------------------------------------+
int start()
 {indikator();
//----
 if((Otklonenie[0]/Point>StartDW+(Diapazon[0]/Point)*KofKv)&&(orders(1)==0)){Send(1,lot);}
 if((Otklonenie[0]/Point<StartUP-(Diapazon[0]/Point)*KofKv)&&(orders(0)==0)){Send(0,lot);}
//----
 if(profits(1)>EndDW){ClosE(1);}
 if(profits(0)>EndUP){ClosE(0);}
//----
 if(profits(1)<StopLossDW){ClosE(1);}
 if(profits(0)<StopLossUP){ClosE(0);}
 info();}
//+------------------------------------------------------------------+
int deinit()
{ObjectDelete("MinuteDelay");ObjectDelete("Перерасчет");ObjectDelete("Рабочий спрэд");ObjectDelete("StartDW");
ObjectDelete("StartUP");ObjectDelete("EndDW");ObjectDelete("EndUP");ObjectDelete("profitsDW");
ObjectDelete("profitsUP");ObjectDelete("ordersDW");ObjectDelete("ordersUP");}
//+------------------------------------------------------------------+