//+------------------------------------------------------------------+
//|                                                  ArtFX_v1.07.mq4 |
//|                                                     Yuri Mazurin |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Yuri Mazurin"
#property link      ""
#property version   "1.07"
#property strict
extern string        comment     = "ArtFX";
extern int           magic       = 777;        //Уникальный номер
extern double        lot         = 0.01;       //Размер лота
extern double        maxlot      = 2;          //Макс.размер лота
extern int           MM          = 20;         //Авторазмер лота
extern double        MaxSpread   =  2;         //Макс.спред 
extern double        sl          = 12;         //Стоп лосс
extern double        nl1         = 7;          //Безубыток старт
extern double        nl2         = 3;          //Пункты прибыли безубытка
extern int           CCIper      = 12;         //Период CCI
extern int           CCIclose    = 120;        //Уровень закрытия CCI
extern double        shift       = 15;         //Мин. расстояние от экстремума
extern string        timeEnd     = "23:55";     //Время удаления отложенных ордеров
int p;

int OnInit()
  {
if (MarketInfo(Symbol(), MODE_DIGITS) == 5.0 || MarketInfo(Symbol(), MODE_DIGITS) == 3.0) p = 10;
else p = 1;
  sl=sl*Point*p;
  nl1=nl1*Point*p;
  nl2=nl2*Point*p;
  MaxSpread = MaxSpread*p;
  shift =shift*Point*p;
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
  {
if (MM>0) lot = NormalizeDouble (AccountBalance()*MM*Point,2);
if (lot<0.01)lot = 0.01;
if (lot>maxlot)lot = maxlot;   
double cci = iCCI(NULL,0,CCIper,PRICE_CLOSE,0);  
double cci1 = iCCI(NULL,0,CCIper,PRICE_CLOSE,1);  
double Spread=Ask-Bid;
double ph = iHigh (NULL,PERIOD_D1,1);
double pl = iLow (NULL,PERIOD_D1,1);  
double ph1 = iHigh (NULL,PERIOD_D1,0);
double pl1 = iLow (NULL,PERIOD_D1,0);  
    
  

      for (int i = OrdersTotal()-1; i>=0;i--)
      {
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {  
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
            {
               if(Spread > MaxSpread*Point||TimeCurrent()>StrToTime(timeEnd)) OrderDelete(OrderTicket());      
               if (OrderType() == OP_BUYSTOP&&OrderOpenPrice()!=ph)OrderDelete(OrderTicket());
               if (OrderType() == OP_SELLSTOP&&OrderOpenPrice()!=pl)OrderDelete(OrderTicket());            
               if (OrderType() == OP_BUY&&OrderStopLoss()==0)OrderModify(OrderTicket(),Bid,OrderOpenPrice()-sl,0,0);
               if (OrderType() == OP_SELL&&OrderStopLoss()==0)OrderModify(OrderTicket(),Ask,OrderOpenPrice()+sl,0,0);            
               if (OrderType() == OP_BUY&&cci<CCIclose&&cci1>CCIclose&&Bid-OrderOpenPrice()>0)OrderClose (OrderTicket(),OrderLots(),Bid,0,clrGreen);
               if (OrderType() == OP_SELL&&cci>-CCIclose&&cci1<-CCIclose&&OrderOpenPrice()-Ask>0)OrderClose (OrderTicket(),OrderLots(),Ask,0,clrRed);
               if (OrderType() == OP_BUY&&nl1!=0&&Bid-OrderOpenPrice()>nl1&&OrderStopLoss()!=NormalizeDouble (OrderOpenPrice()+nl2,Digits))
               OrderModify(OrderTicket(),Bid,NormalizeDouble (OrderOpenPrice()+nl2,Digits),0,0);
               if (OrderType() == OP_SELL&&nl1!=0&&OrderOpenPrice()-Ask>nl1&&OrderStopLoss()!=NormalizeDouble (OrderOpenPrice()-nl2,Digits))
               OrderModify(OrderTicket(),Ask,NormalizeDouble(OrderOpenPrice()-nl2,Digits),0,0);
               if (OrderType() == OP_BUY&&OrderStopLoss()==0&&OrderOpenPrice()-Bid>sl+0.0001)OrderClose (OrderTicket(),OrderLots(),Bid,0,clrGreen);
               if (OrderType() == OP_SELL&&OrderStopLoss()==0&&Ask-OrderOpenPrice()>sl+0.0001)OrderClose (OrderTicket(),OrderLots(),Ask,0,clrRed);
            }   
         }
      }
   
    if(Spread<MaxSpread*Point&&TimeCurrent()<StrToTime(timeEnd))
    {
      if (CountBuystop  ()==0 &&CountBuy  ()==0 && ph-Ask>shift&&ph>ph1)OrderSend (NULL,OP_BUYSTOP,lot,ph,3,0,0,"ArtFX",magic,0,clrGreen);
      if (CountSellstop ()==0 &&CountSell ()==0 && Bid-pl>shift&&pl<pl1)OrderSend (NULL,OP_SELLSTOP,lot,pl,3,0,0,"ArtFX",magic,0,clrRed);  
    }
    
   }
  
//+------------------------------------------------------------------+
int CountSellstop ()
{
int count = 0;
for (int j=OrdersTotal (); j>=0;j--)
{
OrderSelect (j,SELECT_BY_POS,MODE_TRADES);
if (OrderType () == OP_SELLSTOP&&OrderMagicNumber()==magic) count++;
}
return (count);
}
//--------------------------------------------------------------------+
int CountBuystop ()
{
int count = 0;
for (int k=OrdersTotal ()-1; k>=0;k--)
{
OrderSelect (k,SELECT_BY_POS,MODE_TRADES);
if (OrderType () == OP_BUYSTOP&&OrderMagicNumber()==magic) count++;
}
return (count);
}
//+------------------------------------------------------------------+
int CountSell ()
{
int count = 0;
for (int j=OrdersTotal (); j>=0;j--)
{
OrderSelect (j,SELECT_BY_POS,MODE_TRADES);
if (OrderType () == OP_SELL&&OrderMagicNumber()==magic) count++;
}
return (count);
}
//--------------------------------------------------------------------+
int CountBuy ()
{
int count = 0;
for (int k=OrdersTotal ()-1; k>=0;k--)
{
OrderSelect (k,SELECT_BY_POS,MODE_TRADES);
if (OrderType () == OP_BUY&&OrderMagicNumber()==magic) count++;
}
return (count);
}