//+------------------------------------------------------------------+
//|                                                MA Line EA V1.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
extern string  tmp1              = "Short MA params";
extern double  S_ma_period       = 0;
extern int     S_ma_shift        = 0;

extern string  tmp2              = "Long MA params";
extern double  L_ma_period       = 0;
extern int     L_ma_shift        = 0;

extern string  tmp3              = "General params";
extern int     TimeFrame         = PERIOD_H4;
extern int     ma_method         = MODE_SMA;
extern int     ma_price          = PRICE_CLOSE;

extern string  tmp4              = "--------------";
extern bool    Buy_Only          = true;
extern bool    Sel_only          = true;

extern string  tmp5              = "--------------";
extern bool    UseMACross        = true;
extern bool    UseTrendLine      = true;
extern bool    UseHorizontalLine = true;    
extern bool    AlertAllow        = true;
//------
extern string  tmp6              = "--------------";
extern double  Lots              = 0.1;
extern double  StopLoss          = 200;
extern double  TakeProfit        = 400;

extern string  tmp7              = "if Trailling=0, trail will not work.";
extern double  Trailling         = 0; // if =0, trail do not work. 

int    mg =20140803;
bool   mo;
int    t0,t1,t2;
//+------------------------------------------------------------------+
int init() {
   int objt=0, objv=0;
   string obj_name;
   int obj_type;
   
   for(int i=ObjectsTotal()-1; i >= 0; i--)  {
       obj_name = ObjectName(i);
       obj_type = ObjectType(obj_name);
       if(obj_type == OBJ_TREND)
          objt ++;
       if(obj_type == OBJ_HLINE)
          objv ++;
      }
   if(AlertAllow && (objt==0 || objv==0))
      Alert("First of all, you Must Draw Trend Line and horizontal Line");
   
   return(0);
}
//+------------------------------------------------------------------+
int start()
  {
   double
   ms1 =iMA(Symbol(),TimeFrame,S_ma_period,S_ma_shift,ma_method,ma_price,1),
   ms2 =iMA(Symbol(),TimeFrame,S_ma_period,S_ma_shift,ma_method,ma_price,2),
   ml1 =iMA(Symbol(),TimeFrame,L_ma_period,L_ma_shift,ma_method,ma_price,1),
   ml2 =iMA(Symbol(),TimeFrame,L_ma_period,L_ma_shift,ma_method,ma_price,2);
   
   int Sigma=0;
   if(ms1>ml1 && ms2<ml2)
      Sigma=1;
   if(ms1<ml1 && ms2>ml2)
      Sigma=2;
   
   if(op()==0) OBJ_TLCheck(Sigma);
   
   if(op()>0 && Trailling>0) ModiPos();
   
   return(0);
  }
  
void OBJ_TLCheck(int mr) {
  int mabuy =1;
  int masel =1;
  
  if(UseMACross) {
     if(mr==0) {
        mabuy =0;
        masel =0;
       }
     if(mr==1) {
        mabuy =1;
        masel =0;
       }
     if(mr==2) {
        mabuy =0;
        masel =1;
       }
    }
  
  if(UseTrendLine) {
     int TLcount =0;
     string obj_name;
     int obj_type;
     int total = ObjectsTotal(); 
  
     for(int i=total-1; i >= 0; i--)  {
         obj_name = ObjectName(i);
         obj_type = ObjectType(obj_name);
         if(ObjectFind(obj_name)<0) continue;
         if(obj_type != OBJ_TREND) continue;
            TLcount ++;
        }
     if(AlertAllow && TLcount==0 && TimeCurrent()-t0>300) {
        Alert("you Must Draw Trend Line");
        t0=TimeCurrent();
       }
     if(TLcount>0) {
        if(mabuy==1 && Buy_Only) LTR(obj_name);
        if(masel==1 && Sel_only) STR(obj_name);
       }
    }
    
  if(!UseTrendLine) {
     if(mabuy==1 && Buy_Only)
        LTR("");
     if(masel==1 && Sel_only)
        STR("");
    }
}

void LTR(string obj_name) {
  int Vbuy=0;
  double TL = ObjectGetValueByShift(obj_name, 0);
  double C  = iClose(Symbol(), TimeFrame, 0);
  
  bool BuySig =true;
  bool Hline  =true;
  
  if(UseTrendLine) 
     BuySig = C > TL;
  
  if(UseHorizontalLine)
     Hline  = C > VlineBM();
  
  if(BuySig && Hline)
     Vbuy=1;
     
  double sl=0, tp=0;
  int ticket=-99;

  if(BuySig && Vbuy==1 && op()==0) {
     double slb=0, tpb=0;
     if(StopLoss > 0) 
        slb = NormalizeDouble((Ask - StopLoss*Point), Digits);
     if(TakeProfit > 0)
        tpb = NormalizeDouble((Ask + TakeProfit*Point), Digits);
     ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, slb, tpb, "", mg, 0, Blue);
     Sleep(2000);
     if(ticket < 0) 
        Print("              buy "+Symbol()+" information "+GetLastError());
    }
}

double VlineBM() {
  int VHcount =0;
  string obj_name;
  int obj_type;  
  int total = ObjectsTotal();  
  for(int i=total-1; i >= 0; i--)  {
      obj_name = ObjectName(i);
      obj_type = ObjectType(obj_name);
      if(ObjectFind(obj_name)<0) continue;
      if(obj_type != OBJ_HLINE) continue;
         VHcount ++;
     }
  if(AlertAllow && VHcount==0 && TimeCurrent()-t1>300) {
     Alert("you Must Draw horizontal Line");
     t1=TimeCurrent();
    }
  if(VHcount>0)
     double Vh =ObjectGet(obj_name, OBJPROP_PRICE1);
  return(Vh);
}

void STR(string obj_name) {
  int Vsel=0;
  double TL = ObjectGetValueByShift(obj_name, 0);
  double C  = iClose(Symbol(), TimeFrame, 0);
  
  bool SelSig =true;
  bool Hline  =true;
  
  if(UseTrendLine) 
     SelSig = C < TL;
  
  if(UseHorizontalLine)
     Hline  = C < VlineSM();
  
  if(SelSig && Hline)
     Vsel=1;
       
  double sl=0, tp=0;
  int ticket=-99;

  if(SelSig && Vsel==1 && op()==0) {
     double sls=0, tps=0;
     if(StopLoss > 0) 
        sls = NormalizeDouble((Bid + StopLoss*Point), Digits);
     if(TakeProfit > 0)
        tps = NormalizeDouble((Bid - TakeProfit*Point), Digits);
     ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, sls, tps, "", mg, 0, Red);
     Sleep(2000);
     if(ticket < 0) 
        Print("              sell "+Symbol()+" information "+GetLastError());
    }
}


double VlineSM() {
  int VHcount =0;
  string obj_name;
  int obj_type;  
  int total = ObjectsTotal();  
  for(int i=total-1; i >= 0; i--)  {
      obj_name = ObjectName(i);
      obj_type = ObjectType(obj_name);
      if(ObjectFind(obj_name)<0) continue;
      if(obj_type != OBJ_HLINE) continue;
         VHcount ++;
     }
  if(AlertAllow && VHcount==0 && TimeCurrent()-t2>300) {
     Alert("you Must Draw horizontal Line");
     t2=TimeCurrent();
    }
  if(VHcount>0)
     double Vl =ObjectGet(obj_name, OBJPROP_PRICE1);
  return(Vl);
}

int op() {
  int otn=0;
  for(int i=0; i<OrdersTotal(); i++) {
      if(OrderSelect(i, SELECT_BY_POS))
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=mg) continue;  
         otn ++;
     }
  return(otn);
}

void ModiPos() {
  for(int i=0; i<OrdersTotal(); i++) {
      if(OrderSelect(i, SELECT_BY_POS))
      if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=mg) continue;  
      if(OrderType()==OP_BUY) {
         if(Bid-OrderOpenPrice()>=Trailling*Point && Bid-OrderStopLoss()>=Trailling*Point) {
            mo=OrderModify(OrderTicket(), OrderOpenPrice(),NormalizeDouble(Bid-Trailling*Point,Digits), OrderTakeProfit(), 0, Yellow);
            if(!mo)
               Print("              modify "+Symbol()+" information "+GetLastError());
           }
        }
      if(OrderType()==OP_SELL) {
         if(OrderOpenPrice()-Ask>=Trailling*Point && (OrderStopLoss()-Ask>=Trailling*Point || OrderStopLoss()==0)) {
            mo=OrderModify(OrderTicket(), OrderOpenPrice(),NormalizeDouble(Ask+Trailling*Point,Digits), OrderTakeProfit(), 0, Yellow);
            if(!mo)
               Print("              modify "+Symbol()+" information "+GetLastError());
           }
        }
     }
  }