//===========================================================================================================================//
// Author VOLDEMAR227 site [url=http://WWW.TRADING-GO.RU]WWW.TRADING-GO.RU[/url]      SKYPE: TRADING-GO          e-mail: TRADING-GO@List.ru
//===========================================================================================================================//
extern int Bezubitok = 20;
extern int TrailingStop = 15;
extern int     PeriodEN     = 10          ;
extern double  Rashirenie   = 0.2         ;
extern int     shift        = 1           ;
extern int     Plus         = 30          ;
extern int     Distanciya   = 30         ;
extern int RSIPeriod = 14;
extern int RSIshift = 1;
double LevelUp = 80;
double LevelDw = 20;
extern int ExtDepth = 12;
extern int ExtDeviation = 5;
extern int ExtBackstep = 3;
extern int ZigShift = 0;
extern double  ShagDist     = 30          ;
extern double  Lots         = 0.00        ;
extern double M = 1.3;
extern double  Percent      = 1           ;
extern int     Slip         = 2           ;
extern int     Magic        = 1           ;
//===========================================================================================================================//
int start() 
  {
   double Lots_New=0;
   string Symb   =Symbol();               
   double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);
   double Min_Lot=MarketInfo(Symb,MODE_MINLOT);
   double Step   =MarketInfo(Symb,MODE_LOTSTEP);
   double Free   =AccountFreeMargin();        

   if (Lots>0) {                                        
      double Money=Lots*One_Lot;          
      if(Money<=AccountFreeMargin())         
         Lots_New=Lots;   else                                
         Lots_New=MathFloor(Free/One_Lot/Step)*Step; }
   else        {                                       
      if (Percent > 100)    Percent=100;                    
      if (Percent==0)       Lots_New=Min_Lot;                  
      else  Lots_New=MathFloor(Free*Percent/100/One_Lot/Step)*Step; }

   if (Lots_New < Min_Lot)  Lots_New=Min_Lot;                  
   if (Lots_New*One_Lot > AccountFreeMargin()) { return(false);}
//===========================================================================================================================//
   ObjectCreate("R",OBJ_LABEL,0,0,0);
   ObjectSet("R",OBJPROP_CORNER,2);
   ObjectSet("R",OBJPROP_XDISTANCE,10);
   ObjectSet("R",OBJPROP_YDISTANCE,10);
   ObjectSetText("R","WWW.TRADING-GO.RU  full version free",8,"Verdana",Red);

//===========================================================================================================================//
   double opB=2000; double opS=0; double orderProfitbuy=0; double Sum_Profitbuy=0; double orderProfitsel;  double Sum_Profitsel; int orderType;
   double LotB=Lots_New;
   double LotS=Lots_New;
   int total=OrdersTotal();
   int b=0,s=0,n=0,bs=0,ss=0,bl=0,sl=0;
   for(int i=total-1; i>=0; i--)
      if(OrderSelect(i, SELECT_BY_POS))
         if(OrderSymbol()==Symbol()      )
         if (OrderMagicNumber()==Magic)
            {
            if(OrderType()==OP_BUY)
              {n++;
               b++;
               
               double ProfitB=OrderTakeProfit(); 
               double openB  =OrderOpenPrice ();
               if(openB<opB) {opB=openB;LotB=OrderLots();}
              }     
            if(OrderType()==OP_SELL)
              {n++;
               s++;
               
              double ProfitS=OrderTakeProfit(); 
              double openS  =OrderOpenPrice ();
               if(openS>opS) {opS=openS;LotS=OrderLots();}
              }
              if (OrderType()==OP_BUYSTOP ) bs++;
              if (OrderType()==OP_SELLSTOP) ss++;
              if (OrderType()==OP_BUYLIMIT ) bl++;
              if (OrderType()==OP_SELLLIMIT) sl++;
           }
//============================================================================================================================//
   double dis   =NormalizeDouble((Distanciya+(ShagDist*n)) *Point,Digits);
   double spred =NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD)*Point,Digits);
   double  CORR =NormalizeDouble(Plus                            *Point,Digits);
//============================================================================================================================//
double lotb=0, lots=0;

if (b==0)lotb=Lots_New;
if (s==0)lots=Lots_New;

if (b>0)lotb=LotB*M;
if (s>0)lots=LotS*M;
//============================================================================================================================//
double up = 0;
double dw = 0;
double RSI=iRSI(NULL,0,RSIPeriod,PRICE_MEDIAN,RSIshift);
double ZigZag=iCustom(NULL, 0, "ZigZag", ExtDepth, ExtDeviation, ExtBackstep, 0, ZigShift);

up = iEnvelopes(Symbol(), 0, PeriodEN,MODE_SMA,0,PRICE_CLOSE,Rashirenie,MODE_UPPER,shift);
dw = iEnvelopes(Symbol(), 0, PeriodEN,MODE_SMA,0,PRICE_CLOSE,Rashirenie,MODE_LOWER,shift); 


if (s==0 && ss==0 && up>0)
if (Ask>NormalizeDouble(up+10*Point,Digits)&&Ask<ZigZag)
openorders("",5,lots,up);

if (b==0 && bs==0 && dw>0)
if (Bid<NormalizeDouble(dw-10*Point,Digits)&&Bid>ZigZag)
openorders("",4,lotb,dw);

if (b>0 && bl == 0 )
openorders("",2,lotb,opB-dis);

if (s>0 && sl == 0 )
openorders("",3,lots,opS+dis);
//============================================================================================================================//
   for (int iq=total-1; iq>=0; iq--)
    if(OrderSelect(iq, SELECT_BY_POS))
      if(OrderSymbol()==Symbol())
      if(OrderMagicNumber()==Magic)
      { 
        if (b==1) 
        if (OrderType()==OP_BUY)
        if (NormalizeDouble(OrderTakeProfit(),Digits)!=NormalizeDouble(up,Digits))
        if (up>(NormalizeDouble(OrderOpenPrice(),Digits)+spred*3))
        OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(up,Digits),0,Blue); 

        if (s==1) 
        if (OrderType()==OP_SELL)
        if (NormalizeDouble(OrderTakeProfit(),Digits)!=NormalizeDouble(dw,Digits))
        if (dw<(NormalizeDouble(OrderOpenPrice(),Digits)-spred*3))
        OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(dw,Digits),0,Red );  
        
        if (b==0)
        if (bs>0)
        if (OrderType()==OP_BUYSTOP)
        if (NormalizeDouble(OrderOpenPrice(),Digits)!=NormalizeDouble(dw,Digits))
        OrderModify(OrderTicket(),NormalizeDouble(dw,Digits),OrderStopLoss(),OrderTakeProfit(),0,Red );
        
        if (s==0)
        if (ss>0)
        if (OrderType()==OP_SELLSTOP)
        if (NormalizeDouble(OrderOpenPrice(),Digits)!=NormalizeDouble(up,Digits))
        OrderModify(OrderTicket(),NormalizeDouble(up,Digits),OrderStopLoss(),OrderTakeProfit(),0,Red );
        
        if (s==0)
        if (ss==0)
        if (sl>0)
        OrderDelete(OrderTicket());
        
        if (b==0)
        if (bs==0)
        if (bl>0)
        OrderDelete(OrderTicket());        
      }
//============================================================================================================================//

//============================================================================================================================//
double nn=0,bb=0;
double nnn=0,bbb=0;
for(int ui=total-1; ui>=0; ui--)
   if(OrderSelect(ui,SELECT_BY_POS))
      if(OrderSymbol()==Symbol())
      if(OrderMagicNumber()==Magic)
        {
         if(OrderType()==OP_BUY)
           {
            double op=OrderOpenPrice();
            double llot=OrderLots();
            double itog=op*llot;
            bb=bb+itog;
            nn=nn+llot;
            double factb=bb/nn;
           }
         if(OrderType()==OP_SELL)
           {
            double ops=OrderOpenPrice();
            double llots=OrderLots();
            double itogs=ops*llots;
            bbb=bbb+itogs;
            nnn=nnn+llots;
            double facts=bbb/nnn;
           }
        }
//===========================================================================================================================//
for(int uui=total-1; uui>=0; uui--)
   if(OrderSelect(uui,SELECT_BY_POS))
      if(OrderSymbol()==Symbol())
      if(OrderMagicNumber()==Magic)
        {
         if(b>=2)
         if(OrderType()==OP_BUY)
         if(Ask<opB)
         if(NormalizeDouble(OrderTakeProfit(),Digits)!=NormalizeDouble(factb+CORR,Digits))
         OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(factb+CORR,Digits),0,Blue);
         if(s>=2)
         if(OrderType()==OP_SELL)
         if(Bid>opS)
         if(NormalizeDouble(OrderTakeProfit(),Digits)!=NormalizeDouble(facts-CORR,Digits))
         OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),NormalizeDouble(facts-CORR,Digits),0,Red);
        }
}
//============================================================================================================================//

//============================================================================================================================//

int openorders(string sym,int typ,double lot,double pri)
  {
   int tik=0; color col;
   if(sym=="") sym=Symbol();
   if(typ==0 || typ==2 || typ==4) col=Blue; else col=Red;
   if(lot>0)
   tik=OrderSend(sym,typ,NormalizeDouble(lot,2),NormalizeDouble(pri,Digits),Slip,0,0,WindowExpertName()+"  "+Magic,Magic,0,col);
   return(tik);
  }
//============================================================================================================================//
void Trailing(int b, int s, double factb, double CORR)     // Bezubitok
{
   double Bez = NormalizeDouble(Bezubitok*Point,Digits);
   double tr = NormalizeDouble(TrailingStop*Point,Digits);
   for (int i=OrdersTotal()-1; i>=0; i--)
      if (OrderSelect(i, SELECT_BY_POS)==true)
         if (OrderSymbol()==Symbol())
            if (OrderMagicNumber()==Magic)
               {
               if (OrderType()==OP_BUY) 
                 if (b==1)
                  if ((Bid-OrderOpenPrice())>Bez)
                     if ((Bid-OrderStopLoss())>tr)
                         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-tr,Digits),OrderTakeProfit(),0,Blue);

               if (OrderType()==OP_SELL) 
                   if (s==1)
                  if ((OrderOpenPrice()-Ask)>Bez)
                     if (((OrderStopLoss()-Ask)>tr) || (OrderStopLoss()==0))
                        OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+tr,Digits),OrderTakeProfit(),0,Red);
               }
}