//+------------------------------------------------------------------+
//|                                                          CCI.mq4 |
//|                                         Lowphat Copyright � 2005 |
//|                                              Golden Cross        |
//+------------------------------------------------------------------+
#property copyright "Simple code by Lowphat  Copyright � 2005"
#property link      "lazersatan@yahoo.com msnger only"

#define MAGIC 654654

extern double smallma = 50;
extern double bigma = 200;
extern double MinutesBetweenOrders = 120;
extern double MAperiod = 50;
extern double StopLoss = 97;
extern double TakeProfit = 50;
extern double TrailingStop = 0;
extern color clOpenBuy = Blue;
extern color clCloseBuy = Aqua;
extern color clOpenSell = Red;
extern color clCloseSell = Violet;
extern color clModiBuy = Blue;
extern color clModiSell = Red;
extern string Name_Expert = "MA Cross";
extern int Slippage = 5;
extern bool UseSound = True;
extern string NameFileSound = "alert.wav";
extern double Lots = 0.10;

void deinit() {
   Comment("");
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   if(Bars<100){
      Print("bars less than 100");
      return(0);
   }
   if(StopLoss<10){
      Print("StopLoss less than 10");
      return(0);
   }
   if(TakeProfit<10){
      Print("TakeProfit less than 10");
      return(0);
   }
   if(StopLoss<10){
      Print("StopLoss less than 10");
      return(0);
   }
   if(TakeProfit<10){
      Print("TakeProfit less than 10");
      return(0);
   }

   double xma;
   double xma1;
   double xma2;
   double xma3;
   double yma;
   double yma1;
   double yma2;
   double yma3;
   
   xma=iMA(NULL,0,smallma,MODE_EMA,0,PRICE_CLOSE,0);  //If this is not in use its for future use
   xma1=iMA(NULL,0,smallma,MODE_EMA,0,PRICE_CLOSE,1); //If this is not in use its for future use  
   xma2=iMA(NULL,0,smallma,MODE_EMA,0,PRICE_CLOSE,2); //If this is not in use its for future use
   xma3=iMA(NULL,0,smallma,MODE_EMA,0,PRICE_CLOSE,3); //If this is not in use its for future use
   
   yma=iMA(NULL,0,bigma,MODE_EMA,0,PRICE_CLOSE,0);  //If this is not in use its for future use
   yma1=iMA(NULL,0,bigma,MODE_EMA,0,PRICE_CLOSE,1); //If this is not in use its for future use 
   yma2=iMA(NULL,0,bigma,MODE_EMA,0,PRICE_CLOSE,2); //If this is not in use its for future use
   yma3=iMA(NULL,0,bigma,MODE_EMA,0,PRICE_CLOSE,3); //If this is not in use its for future use
  
static datetime lastordertime= 0;
int sometimelimit= MinutesBetweenOrders * 60;  // don't re-order within 10 minutes

   if(AccountFreeMargin()<(1000*Lots)){
      Print("We have no money. Free Margin = ", AccountFreeMargin());
      return(0);
   }
   if (!ExistPositions()){

      if ((xma2<yma2 && xma1>yma1 && Close[1]<Close[0] && CurTime()-lastordertime > sometimelimit)){
         OpenBuy();
         lastordertime= CurTime();
         return(0);
      }

      if ((xma2>yma2 && xma1<yma1 && Close[1]>Close[0] && CurTime()-lastordertime > sometimelimit)){
         OpenSell();
         lastordertime= CurTime();
         return(0);
      }
   }
   if (ExistPositions()){
      if(OrderType()==OP_BUY){

         if ((Bid!=Bid)){  //To be determined
            CloseBuy();
            return(0);
         }
      }
      if(OrderType()==OP_SELL){

         if ((Bid!=Bid)){//To be determined
            CloseSell();
            return(0);
         }
      }
   }
   TrailingPositionsBuy(TrailingStop);
   TrailingPositionsSell(TrailingStop);
   return (0);
}

bool ExistPositions() {
	for (int i=0; i<OrdersTotal(); i++) {
		if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
			if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {
				return(True);
			}
		} 
	} 
	return(false);
}
void TrailingPositionsBuy(int trailingStop) { 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) { 
            if (OrderType()==OP_BUY) { 
               if (Bid-OrderOpenPrice()>trailingStop*Point) { 
                  if (OrderStopLoss()<Bid-trailingStop*Point) 
                     ModifyStopLoss(Bid-trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void TrailingPositionsSell(int trailingStop) { 
   for (int i=0; i<OrdersTotal(); i++) { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { 
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) { 
            if (OrderType()==OP_SELL) { 
               if (OrderOpenPrice()-Ask>trailingStop*Point) { 
                  if (OrderStopLoss()>Ask+trailingStop*Point || OrderStopLoss()==0)  
                     ModifyStopLoss(Ask+trailingStop*Point); 
               } 
            } 
         } 
      } 
   } 
} 
void ModifyStopLoss(double ldStopLoss) { 
   bool fm;
   fm = OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE); 
   if (fm && UseSound) PlaySound(NameFileSound); 
} 

void OpenBuy() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 
   ldLot = GetSizeLot(); 
   ldStop = GetStopLossBuy(); 
   ldTake = GetTakeProfitBuy(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenBuy); 
   if (UseSound) PlaySound(NameFileSound); 
} 

void CloseBuy() { 
   bool fc; 
   fc=OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clCloseBuy); 
   if (fc && UseSound) PlaySound(NameFileSound); 
} 
void CloseSell() { 
   bool fc; 
   fc=OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell); 
   if (fc && UseSound) PlaySound(NameFileSound); 
} 
void OpenSell() { 
   double ldLot, ldStop, ldTake; 
   string lsComm; 

   ldLot = GetSizeLot(); 
   ldStop = GetStopLossSell(); 
   ldTake = GetTakeProfitSell(); 
   lsComm = GetCommentForOrder(); 
   OrderSend(Symbol(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenSell); 
   if (UseSound) PlaySound(NameFileSound); 
} 
string GetCommentForOrder() { 	return(Name_Expert); } 
double GetSizeLot() { 	return(Lots); } 
double GetStopLossBuy() { 	return (Bid-StopLoss*Point);} 
double GetStopLossSell() { 	return(Ask+StopLoss*Point); } 
double GetTakeProfitBuy() { 	return(Ask+TakeProfit*Point); } 
double GetTakeProfitSell() { 	return(Bid-TakeProfit*Point); }