GoldenCross





//+------------------------------------------------------------------+
//|                                                          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); } 



Sample





Analysis



Market Information Used:

Series array that contains close prices for each bar


Indicator Curves created:


Indicators Used:

Moving average indicator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders
It can change open orders parameters, due to possible stepping strategy
It automatically opens orders when conditions are reached
It Closes Orders by itself

Other Features:


It plays sound alerts

BackTest : EURUSD on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.29 Total Net Profit:-23.90

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.00 Total Net Profit:-12.97

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.47 Total Net Profit:-11.20

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.12 Total Net Profit:-66.82

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:0.28 Total Net Profit:-26.30

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.84 Total Net Profit:-3.80

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.49 Total Net Profit:-78.16

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:1.40 Total Net Profit:4.30

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:0.00 Total Net Profit:0.00

Request Backtest for GoldenCross


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: