//+------------------------------------------------------------------+
//|                                                      Winner2.mq4 |
//|                                              Copyright 2021, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, AM2"
#property link      "http://www.forexsystems.biz"
#property version   "1.00"
#property strict
//--- Inputs
extern double Lots       = 0.1;      // лот
extern double KLot       = 2;        // умножение лота
extern double MaxLot     = 5;        // максимальный лот
extern int StopLoss      = 0;        // лось
extern int Numbers       =100;       // кол-во баровв для SL
extern int TakeProfit    = 5000;     // язь
extern int TrailingStop  = 25;       // трал
extern int Spread        = 15;       // спред
extern int Delta         = 250;      // расстояние от цены
extern int Slip          = 30;       // реквот
extern int Magic         = 123;      // магик
extern int Depth         = 12;
extern int Dev           = 5;
extern int Back          = 3;
int num=0;
double last=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Comment("");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
  {
   int r=0;
   color clr=Green;
   double sl=0,tp=0;
   double isl=MarketInfo(Symbol(),MODE_STOPLEVEL);
   double lo=Low[iLowest(NULL,0,MODE_LOW,Numbers,1)];
   double hi=High[iHighest(NULL,0,MODE_HIGH,Numbers,1)];
   double sl2=(hi-lo)/2;
   if(type==1 || type==3 || type==5)
     {
      clr=Red;
      if(StopLoss>0)
        {
         sl=NormalizeDouble(price+StopLoss*_Point,_Digits);
        }
      if(isl==0 || sl2>isl)
        {
          sl=NormalizeDouble(price+sl2,_Digits);
        }
      else
        {
          sl=NormalizeDouble(price+isl,_Digits);
        }
      if(TakeProfit>0)
         tp=NormalizeDouble(price-TakeProfit*_Point,_Digits);
     }
   if(type==0 || type==2 || type==4)
     {
      clr=Blue;
      if(StopLoss>0)
        {
         sl=NormalizeDouble(price-StopLoss*_Point,_Digits);
        }
      if(isl==0 || sl2>isl)
        {
          sl=NormalizeDouble(price-sl2,_Digits);
        }
      else
        {
          sl=NormalizeDouble(price-isl,_Digits);
        }
      if(TakeProfit>0)
         tp=NormalizeDouble(price+TakeProfit*_Point,_Digits);
     }
   r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,_Digits),Slip,sl,tp,"",Magic,0,clr);
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CountTrades()
  {
   int count=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()<2)
               count++;
           }
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Lot()
  {
   double lot=Lots;
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderProfit()>0)
            break;
         if(OrderProfit()<0)
           {
            lot=OrderLots()*KLot;
            break;
           }
        }
     }
   if(lot>MaxLot)
      lot=Lots;
   return(lot);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double ZZPrice(int ne=0)
  {
   double zz;
   int    i,k=iBars(NULL,0),ke=0;
   for(i=1; i<k; i++)
     {
      zz=iCustom(NULL,0,"ZigZag",Depth,Dev,Back,0,i);
      if(zz!=0)
        {
         ke++;
         if(ke>ne)
            return(zz);
        }
     }
   Print("GetExtremumZZPrice(): Экстремум ЗигЗага номер ",ne," не найден");
   return(0);
  }
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу                                          |
//+------------------------------------------------------------------+
int CountOrders(int type)
  {
   int count=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==type)
               count++;
           }
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Trailing()
  {
   bool mod;
   double sl=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY)
              {
               if(Bid-OrderOpenPrice()>TrailingStop*_Point)
                 {
                  sl=NormalizeDouble(Bid-TrailingStop*_Point,_Digits);
                  if(OrderStopLoss()<sl)
                    {
                     Print("SL: ",sl, " TP: ",OrderTakeProfit());
                     mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Yellow);
                     return;
                    }
                 }
              }
            if(OrderType()==OP_SELL)
              {
               if(OrderOpenPrice()-Ask>TrailingStop*_Point)
                 {
                  sl=NormalizeDouble(Ask+TrailingStop*_Point,_Digits);
                  if(OrderStopLoss()>sl || (OrderStopLoss()==0))
                    {
                     Print("SL: ",sl, " TP: ",OrderTakeProfit());
                     mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Yellow);
                     return;
                    }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double zz1=ZZPrice(0);
   double zz2=ZZPrice(1);
   double spread=MarketInfo(NULL,MODE_SPREAD);
   if(TrailingStop>0)
      Trailing();
   /*
   1. поступил сигнал
   2. счетчик равен +=1
   3. поставили ордера
   4. если счетчик четный не ставим
   */
   if(last!=zz1 && spread<=Spread)
     {
      num++;
      if(MathMod(num,2)==1)
        {
         if(zz1>zz2)
           {
            PutOrder(5,Low[1]-Delta*_Point);
            PutOrder(3,High[1]+Delta*_Point);
           }
         if(zz1<zz2)
           {
            PutOrder(4,High[1]+Delta*_Point);
            PutOrder(2,Low[1]-Delta*_Point);
           }
        }
      last=zz1;
     }
   Comment("\n ZZ1: ",zz1,
           "\n ZZ2: ",zz2,
           "\n Count: ",num,
           "\n Mod: ",MathMod(num,2));
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                      Winner2.mq4 |
//|                                              Copyright 2021, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, AM2"
#property link      "http://www.forexsystems.biz"
#property version   "1.00"
#property strict
//--- Inputs
extern double Lots       = 0.1;      // лот
extern double KLot       = 2;        // умножение лота
extern double MaxLot     = 5;        // максимальный лот
extern int StopLoss      = 0;        // лось
extern int Numbers       =100;       // кол-во баровв для SL
extern int TakeProfit    = 5000;     // язь
extern int TrailingStop  = 25;       // трал
extern int Spread        = 15;       // спред
extern int Delta         = 250;      // расстояние от цены
extern int Slip          = 30;       // реквот
extern int Magic         = 123;      // магик
extern int Depth         = 12;
extern int Dev           = 5;
extern int Back          = 3;
int num=0;
double last=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Comment("");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
  {
   int r=0;
   color clr=Green;
   double sl=0,tp=0;
   double lo=Low[iLowest(NULL,0,MODE_LOW,Numbers,1)];
   double hi=High[iHighest(NULL,0,MODE_HIGH,Numbers,1)];
   double sl2=(hi-lo)/2;
   if(type==1 || type==3 || type==5)
     {
      clr=Red;
      if(StopLoss>0)
        {
         sl=NormalizeDouble(price+StopLoss*_Point,_Digits);
        }
      else
        {
          sl=NormalizeDouble(price+sl2,_Digits);
        }
      if(TakeProfit>0)
         tp=NormalizeDouble(price-TakeProfit*_Point,_Digits);
     }
   if(type==0 || type==2 || type==4)
     {
      clr=Blue;
      if(StopLoss>0)
        {
         sl=NormalizeDouble(price-StopLoss*_Point,_Digits);
        }
      else
        {
          sl=NormalizeDouble(price-sl2,_Digits);
        }
      if(TakeProfit>0)
         tp=NormalizeDouble(price+TakeProfit*_Point,_Digits);
     }
   r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,_Digits),Slip,sl,tp,"",Magic,0,clr);
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CountTrades()
  {
   int count=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()<2)
               count++;
           }
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Lot()
  {
   double lot=Lots;
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderProfit()>0)
            break;
         if(OrderProfit()<0)
           {
            lot=OrderLots()*KLot;
            break;
           }
        }
     }
   if(lot>MaxLot)
      lot=Lots;
   return(lot);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double ZZPrice(int ne=0)
  {
   double zz;
   int    i,k=iBars(NULL,0),ke=0;
   for(i=1; i<k; i++)
     {
      zz=iCustom(NULL,0,"ZigZag",Depth,Dev,Back,0,i);
      if(zz!=0)
        {
         ke++;
         if(ke>ne)
            return(zz);
        }
     }
   Print("GetExtremumZZPrice(): Экстремум ЗигЗага номер ",ne," не найден");
   return(0);
  }
//+------------------------------------------------------------------+
//| Подсчет ордеров по типу                                          |
//+------------------------------------------------------------------+
int CountOrders(int type)
  {
   int count=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==type)
               count++;
           }
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Trailing()
  {
   bool mod;
   double sl=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY)
              {
               if(Bid-OrderOpenPrice()>TrailingStop*_Point)
                 {
                  sl=NormalizeDouble(Bid-TrailingStop*_Point,_Digits);
                  if(OrderStopLoss()<sl)
                    {
                     Print("SL: ",sl, " TP: ",OrderTakeProfit());
                     mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Yellow);
                     return;
                    }
                 }
              }
            if(OrderType()==OP_SELL)
              {
               if(OrderOpenPrice()-Ask>TrailingStop*_Point)
                 {
                  sl=NormalizeDouble(Ask+TrailingStop*_Point,_Digits);
                  if(OrderStopLoss()>sl || (OrderStopLoss()==0))
                    {
                     Print("SL: ",sl, " TP: ",OrderTakeProfit());
                     mod=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Yellow);
                     return;
                    }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double zz1=ZZPrice(0);
   double zz2=ZZPrice(1);
   double spread=MarketInfo(NULL,MODE_SPREAD);
   if(TrailingStop>0)
      Trailing();
   /*
   1. поступил сигнал
   2. счетчик равен +=1
   3. поставили ордера
   4. если счетчик четный не ставим
   */
   if(last!=zz1 && spread<=Spread)
     {
      num++;
      if(MathMod(num,2)==1)
        {
         if(zz1>zz2)
           {
            PutOrder(5,Low[1]-Delta*_Point);
            PutOrder(3,High[1]+Delta*_Point);
           }
         if(zz1<zz2)
           {
            PutOrder(4,High[1]+Delta*_Point);
            PutOrder(2,Low[1]-Delta*_Point);
           }
        }
      last=zz1;
     }
   Comment("\n ZZ1: ",zz1,
           "\n ZZ2: ",zz2,
           "\n Count: ",num,
           "\n Mod: ",MathMod(num,2));
  }
//+------------------------------------------------------------------+
  for (int i=0; i<10; i++)
   {
   if (OrderClose(OrderTicket(),OrderLots(),price,Slip,0);!=-1) return;
      Sleep(100);
      RefreshRates();
      if (IsStopped()) return;
   }
 
 
					        										//+------------------------------------------------------------------+
//|                                               CPsar.mq4          |
//|                                              Copyright 2021,     |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021"
#property link      "http://www.forexsystems.biz"
#property version   "1.00"
#property strict
//--- Inputs
extern double Lots       = 0.01;     // лот
extern double Profit     = 0.15;     // Суммарный профит
extern int Count         =10;        // макс.к-во ордеров
extern int Slip          = 30;       // реквот
extern int Magic         = 123;      // магик
extern string SAR        ="Настроки PSar";
extern double SarStep       =0.02;
extern double SarMax        =0.2;
extern int SarShift      =0;
extern string Comm       = "CPsar";   // коммент
double MINLOT,MAXLOT;
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
 MINLOT = MarketInfo(Symbol(),MODE_MINLOT);
 MAXLOT = MarketInfo(Symbol(),MODE_MAXLOT);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
  {
   int r=0;
   color clr=Green;
   double sl=0,tp=0;
   if(type==1)
     {
      clr=Red;
     }
   if(type==0)
     {
      clr=Blue;
     }
   r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,Digits),Slip,sl,tp,Comm,Magic,0,clr);
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Lot()
  {
   double lot=Lots;
   if (lot>MAXLOT) lot = MAXLOT;
   if (lot<MINLOT) lot = MINLOT;
   return(lot);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CountTrades(int type=-1)
  {
   int count=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==0 && (type==0 || type==-1))
              {
               count++;
              }
            if(OrderType()==1 && (type==1 || type==-1))
             {
               count++;
             }
           }
         }
       }
   return(count);
  }
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера                               |
//+------------------------------------------------------------------+
double AllProfit(int ot=-1)
  {
   double pr=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==0 && (ot==0 || ot==-1))
              {
               pr+=OrderProfit()+OrderCommission()+OrderSwap();
              }
            if(OrderType()==1 && (ot==1 || ot==-1))
              {
               pr+=OrderProfit()+OrderCommission()+OrderSwap();
              }
           }
        }
     }
   return(pr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseAll(int ot=-1)
  {
   bool cl;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==0 && (ot==0 || ot==-1))
              {
               RefreshRates();
               cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
              }
            if(OrderType()==1 && (ot==1 || ot==-1))
              {
               RefreshRates();
               cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
    double sar=iSAR(NULL,0,SarStep,SarMax,SarShift);
    double sar2=iSAR(NULL,0,SarStep,SarMax,SarShift+1);
    
    if(AllProfit()>Profit && Profit>0)
     {
      CloseAll();
     }
    if(sar2>High[1] && sar<Bid)
     {
      CloseAll(1);
     }
    if(sar2<Low[1] && sar>Bid)
     {
      CloseAll(0);
     }
    if(t!=Time[0])
     {
     if(CountTrades()<Count)
       {
      if(sar<Bid)
       {
        PutOrder(0,Ask);
       }
      if(sar>Bid)
       {
        PutOrder(1,Bid);
       }
      t=Time[0];
     } 
    }    
 //--------------------------------------
   Comment("\n Trades: ",CountTrades());
   }
  
//+------------------------------------------------------------------+if(OrderMagicNumber()==Mn)
     {
      for(int j=OrdersTotal()-1; j>=0; j--)
         if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderType()==OP_BUYSTOP && OrderSymbol()==Symbol()&&OrderMagicNumber())  // в данной строчке проверяется наличие магика, а по идее должно быть так: OrderMagicNumber()==Mn
               int del=OrderDelete(OrderTicket());
            if(OrderType()==OP_SELLSTOP && OrderSymbol()==Symbol()&&OrderMagicNumber())  // здесь тоже самое: OrderMagicNumber()==Mn
               del=OrderDelete(OrderTicket());
           }
      return(0);
     }//+------------------------------------------------------------------+
//|                                               Proboy_Candle.mq4  |
//|                                              Copyright 2021,     |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021"
#property link      "http://www.forexsystems.biz"
#property version   "1.00"
#property strict
//--- Inputs
extern ENUM_TIMEFRAMES TF  = PERIOD_H1;
extern double Lots       = 0.01;      // лот
extern double Profit     = 0.15;       // Суммарный профит
extern int Slip          = 30;       // реквот
extern int Magic         = 123;      // магик
extern string Comm       = "Proboy_Candle";       // коммент
double MINLOT,MAXLOT;
datetime t=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
 MINLOT = MarketInfo(Symbol(),MODE_MINLOT);
 MAXLOT = MarketInfo(Symbol(),MODE_MAXLOT);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
  {
   int r=0;
   color clr=Green;
   double sl=0,tp=0;
   if(type==1)
     {
      clr=Red;
     }
   if(type==0)
     {
      clr=Blue;
     }
   r=OrderSend(NULL,type,Lot(),NormalizeDouble(price,Digits),Slip,sl,tp,Comm,Magic,0,clr);
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Lot()
  {
   double lot=Lots;
   if (lot>MAXLOT) lot = MAXLOT;
   if (lot<MINLOT) lot = MINLOT;
   return(lot);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CountTrades(int type=-1)
  {
   int count=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==0 && (type==0 || type==-1))
              {
               count++;
              }
            if(OrderType()==1 && (type==1 || type==-1))
             {
               count++;
             }
           }
         }
       }
   return(count);
  }
//+------------------------------------------------------------------+
//|                   Одна  сделка за свечу                          |
//+------------------------------------------------------------------+
bool OneCandleDeal()
  {
   if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
     {
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
        {
         if(OrderOpenTime()>Time[0] && OrderOpenTime()<Time[0]+PeriodSeconds())
           {
            return(false);
           }
        }
     }
   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double signal()
{
   double sig=3;
   if(iClose(NULL,TF,1)>iOpen(NULL,TF,1))
    {
     sig=0;
    }
   if(iClose(NULL,TF,1)<iOpen(NULL,TF,1)) 
    {
     sig=1;
    }
   return(sig);
}
//+------------------------------------------------------------------+
//| Профит всех ордеров по типу ордера                               |
//+------------------------------------------------------------------+
double AllProfit(int ot=-1)
  {
   double pr=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==0 && (ot==0 || ot==-1))
              {
               pr+=OrderProfit()+OrderCommission()+OrderSwap();
              }
            if(OrderType()==1 && (ot==1 || ot==-1))
              {
               pr+=OrderProfit()+OrderCommission()+OrderSwap();
              }
           }
        }
     }
   return(pr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseAll(int ot=-1)
  {
   bool cl;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==0 && (ot==0 || ot==-1))
              {
               RefreshRates();
               cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),Slip,White);
              }
            if(OrderType()==1 && (ot==1 || ot==-1))
              {
               RefreshRates();
               cl=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),Slip,White);
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   
    if(AllProfit()>Profit && Profit>0)
     {
      CloseAll();
     }
    if(t!=Time[0])
     {
      if(CountTrades()<2 && AllProfit()>0)
        {
         CloseAll();
        } 
    if(CountTrades()>0 && AllProfit()<0)
     {
      if(signal()==0)
       {
        PutOrder(0,Ask);
       }
      if(signal()==1)
       {
        PutOrder(1,Bid);
       }
      }
      t=Time[0];
     } 
      if(signal()==0)
        {   
        if(CountTrades()<1)
         {   
          if(OneCandleDeal())
           {
            PutOrder(0,Ask);
           }
         }
        }
      if(signal()==1)
       {
        if(CountTrades()<1)
         {
          if(OneCandleDeal())
           {
            PutOrder(1,Bid);
           }
         }
       }
    
 //--------------------------------------
   Comment("\n Trades: ",CountTrades(),
           "\n Profit: ",AllProfit());
   }
  
//+------------------------------------------------------------------+
www.opentraders.ru/downloads/3038/
verta81