//+------------------------------------------------------------------+
//|                                                  ArtFX_v1.01.mq4 |
//|                                                     Yuri Mazurin |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Yuri Mazurin"
#property link      ""
#property version   "1.01"
#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        sl          = 12;         //Стоп лосс
extern double        nl1         = 7;         //Безубыток старт
extern double        nl2         = 3;          //Пункты прибыли безубытка
extern int           CCIper      = 12;         //Период CCI
extern int           CCIclose    = 120;        //Уровень закрытия CCI
extern double        shift       = 15;         //Мин. расстояние от экстремума


int OnInit()
  {
sl = sl*10*Point;
shift = shift*10*Point;
nl1 = nl1*10*Point;
nl2 = nl2*10*Point;

   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 ph = iHigh (NULL,PERIOD_D1,1);
double pl = iLow (NULL,PERIOD_D1,1);  


   if (CountBuystop  ()==0 &&CountBuy  ()==0 && ph-Ask>shift)OrderSend (NULL,OP_BUYSTOP,lot,ph,3,0,0,"ArtFX",magic,0,clrGreen);
   if (CountSellstop ()==0 &&CountSell ()==0 && Bid-pl>shift)OrderSend (NULL,OP_SELLSTOP,lot,pl,3,0,0,"ArtFX",magic,0,clrRed);  

      for (int i = OrdersTotal()-1; i>=0;i--)
      {
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {  
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
            {
               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);
            }   
         }
      }
   }
  
//+------------------------------------------------------------------+
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);
}