//+------------------------------------------------------------------+
//|                                             MTF-HAS-SCALPING.mq4 |
//|                                                 steinitz@cox.net |
//|                                                 steinitz@cox.net |
//+------------------------------------------------------------------+
#property copyright "steinitz@cox.net"
#property link      "steinitz@cox.net"
#define MagicID   822031

//extern variables
extern string EnterSet ="--------------------------";
extern bool Mtf1Change  =false;
extern bool Mtf2Accord  =true;
extern bool Mtf3Accord  =false;
extern bool Mtf4Accord  =false;
extern double Lots=1;
extern int TakeProfit=30;
extern int StopLoss=30;
extern string ExitSet ="--------------------------";
extern bool exitMtf1Change  =false;
extern bool exitMtf2Accord  =true;
extern bool exitMtf3Accord  =false;
extern bool exitMtf4Accord  =false;

extern int MaMetod  = 2;
extern int MaPeriod = 6;
extern int MaMetod2  = 3;
extern int MaPeriod2 = 2;
//Global variables
int signal=0;//0=no singal;1=long singal;-1=short singal
int cc1,cc2,cc3,cc4;
int c1,c2,c3,c4;
int pre_c1,pre_c2,pre_c3,pre_c4;
//       index   0          1         2        3          4          5         6        7          8          9         10            11
//int allTF[12]={PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1,PERIOD_MN1,PERIOD_MN1,PERIOD_MN1};
//int tf1,tf2,tf3,tf4;



int init()
  {
   
  //for(int i=0;i<=8;i++) //get tf1,tf2,tf3,tf4
  // {if(allTF[i]==Period())  
  //    {tf1=allTF[i];tf2=allTF[i+1];tf3=allTF[i+2];tf4=allTF[i+3];break;} 
 //  }
    
   return(0);
  }

int deinit()
  {

   return(0);
  }

int start()
{
   if((Year()==2007 && Month()>6)||(Year()>2007)   ) return;//**************************
   if(Volume[0]>1) return; else CalColor();
   //if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   //else                                    CheckForClose();
   CheckForClose();
   CheckForOpen();

   return(0);
}
//+------------------------------------------------------------------+
double GetLots()//count the lots to open a new position
{
   return(Lots);
}

int CalculateCurrentOrders(string symbol)//count and return the open positions of the EA on the symbol
{
   int buys=0,sells=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicID)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
   if(buys>0) return(buys);else return(-sells);
}

int OriginalMethod()
{
   
   int sign=0;
   if (Mtf1Change==true) {sign=cc1;} 
   if (Mtf2Accord==true) {if((cc1!=0 || cc2!=0 ) && (c1==c2)) sign=c1 ;}
   if (Mtf3Accord==true) {if((cc1!=0 || cc2!=0 || cc3!=0) && (c1==c2)&& (c2==c3)) sign=c1 ;}
   if (Mtf4Accord==true) {if((cc1!=0 || cc2!=0 || cc3!=0 || cc4!=0) && (c1==c2)&& (c2==c3) && (c3==c4)) sign=c1 ; }
   
   
   return(sign);   
}

int isChangeColor(int TFnum)
{
   int  pre_c,current_c;
   switch (TFnum)
   {  case 1: pre_c=pre_c1; current_c=c1;break;
      case 2: pre_c=pre_c2; current_c=c2;break;
      case 3: pre_c=pre_c3; current_c=c3;break;
      case 4: pre_c=pre_c4; current_c=c4;break;
      }
   if (pre_c==0) return(0);
   if (current_c==1 && pre_c ==-1) return(1);
   else if (current_c==-1 && pre_c==1) return(-1);
   else return(0);
   
}

int getColor(int TFnum,int index)//return 1 is blue ;-1 is red
{
   int xx;
   switch (TFnum)
   {  case 1: xx=0; break;
      case 2: xx=2; break;
      case 3: xx=4; break;
      case 4: xx=6; break;
      }
   if ( iCustom(NULL,Period(),"N4 TF HAS Bar2",xx,index) == EMPTY_VALUE ) return(-1); else return(1);
}
 
int CalTPSL()
{ 
  /* if(TPMethodUseATR==true) 
      TakeProfit=MathRound( iATR(Symbol(),tf,TPAtrPeriod,1)* TPAtrPercentage/Point);
   else 
   {
      switch (tf)
      {
         //case PERIOD_MN1: TakeProfit=TakeProfitMN; break;  //2007.5.10
         case PERIOD_W1: TakeProfit=TakeProfitW1 ; break;
         case PERIOD_D1: TakeProfit=TakeProfitD1 ; break;
         case PERIOD_H4: TakeProfit=TakeProfitH4 ; break;
         case PERIOD_H1: TakeProfit=TakeProfitH1 ; break;
         case PERIOD_M30: TakeProfit=TakeProfitM30 ; break;
         case PERIOD_M15: TakeProfit=TakeProfitM15 ; break;
         case PERIOD_M5: TakeProfit=TakeProfitM5 ; break;
         //case PERIOD_M1: TakeProfit=TakeProfitM1  ; break; //2007.5.10
      }
   }
   */
   return(0);
}

void CheckForOpen()
{  
  int res; 
  double TPprice,STprice;
  
  //if(Volume[0]>1) return; 
  signal=OriginalMethod();
  
  CalTPSL();

  if (signal==1) 
   {if(TakeProfit!=0) TPprice=Ask+TakeProfit*Point; else TPprice=0;
    if(StopLoss!=0) STprice=Ask-StopLoss*Point;else STprice=0;
    } 
  else if (signal==-1) 
   {if(TakeProfit!=0) TPprice=Bid-TakeProfit*Point; else TPprice=0;
    if(StopLoss!=0) STprice=Bid+StopLoss*Point; else STprice=0;
    }

  if (signal==1) 
      {res=OrderSend(Symbol(),OP_BUY,GetLots(),Ask,3,STprice,TPprice,"1",MagicID,0,Blue);} 
  else if (signal==-1) 
      {res=OrderSend(Symbol(),OP_SELL,GetLots(),Bid,3,STprice,TPprice,"-1",MagicID,0,Red);}
      
  if(res<0) {Print("OrderSend failed with error #",GetLastError());}
  return;
}


void CheckForClose()
{  
   //if(Volume[0]>1) return;
   for(int i=0;i<OrdersTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MagicID || OrderSymbol()!=Symbol()) continue;/////////////////////////
     /* if(BreakEvenOn==true)
      {
         if(OrderType()==OP_BUY  && (Bid-OrderOpenPrice()>Point*BreakEvenTrigger) &&(OrderStopLoss()<OrderOpenPrice()) ) 
            {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);}
         if(OrderType()==OP_SELL && (OrderOpenPrice()-Ask >Point*BreakEvenTrigger)&&(OrderStopLoss()>OrderOpenPrice()) )  
            {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);}   
       }
      
      if(TrailingStopOn==true)
      {
         if(OrderType()==OP_BUY  && (Bid-OrderOpenPrice()>Point*TrailingStopTrigger) &&(OrderStopLoss()<Bid-Point*TrailingStop) ) 
            {OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Blue);}
         if(OrderType()==OP_SELL && (OrderOpenPrice()-Ask >Point*TrailingStopTrigger)&&(OrderStopLoss()>Ask+Point*TrailingStop) )  
            {OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);}   
       } */  
      if(OrderType()==OP_BUY && ExitMethod()== -1 )  OrderClose(OrderTicket(),OrderLots(),Bid,3,Black);
      if(OrderType()==OP_SELL&& ExitMethod()== 1)  OrderClose(OrderTicket(),OrderLots(),Ask,3,Black);
  }
   return;
}

int ExitMethod()
{

   int sign=0;
   if (exitMtf1Change==true) {sign=cc1;} 
   if (exitMtf2Accord==true) {if((cc1!=0 || cc2!=0 ) && (c1==c2)) sign=c1 ;}
   if (exitMtf3Accord==true) {if((cc1!=0 || cc2!=0 || cc3!=0) && (c1==c2)&& (c2==c3)) sign=c1 ;}
   if (exitMtf4Accord==true) {if((cc1!=0 || cc2!=0 || cc3!=0 || cc4!=0) && (c1==c2)&& (c2==c3) && (c3==c4)) sign=c1 ; }

   return(sign);   
}

void CalColor()
{
   c1=getColor(1,1); c2=getColor(2,1);//get current color
   c3=getColor(3,1); c4=getColor(4,1);
   
   cc1=isChangeColor(1);   cc2=isChangeColor(2);//get whether color changed
   cc3=isChangeColor(3);   cc4=isChangeColor(4);
   
   pre_c1=c1;      pre_c2=c2;
   pre_c3=c3;      pre_c4=c4;
}