//+------------------------------------------------------------------+
//|                                                  10points 3B.mq4 |
//|                              Copyright © 2005, Alejandro Galindo |
//|                                              http://elCactus.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Alejandro Galindo"
#property link      "http://elCactus.com"

// Peter Wu's Notes: A 15-pips grid system on M5/M15 Charts, trend direction sensing, profit trailing
//                   EURUSD, GBPUSD, USDCHF, USDJPY
//                   Now is fractional pips compatible and ECN compliant
//                   Works with symbol+suffix brokers

extern double        TakeProfit =      30; //40;
extern double        Lots =            0.01;
extern double        InitialStop =     0;
extern double        TrailingStop =    15; //20;
extern string        sq="--------MACD SETTING-------";
extern int           FastEMA=          5;
extern int           SlowEMA=          13;
extern int           SignalSMA=        4;
extern int           MACD_TF =         1;
extern double        multiply=         2.0;
extern int           MaxTrades=        10;
extern int           Pips=             20;
extern int           SecureProfit=     10;
extern int           AccountProtection=1;
extern int           OrderstoProtect=  3;
extern int           ReverseCondition= 0;
extern double        EURUSDPipValue=   10;
extern double        GBPUSDPipValue=   10;
extern double        USDCHFPipValue=   10;
extern double        USDJPYPipValue=   9.715;
//extern int StartYear=2005;
//extern int StartMonth=1;
//extern int EndYear=2005;
//extern int EndMonth=12;
//extern int EndHour=22;
//extern int EndMinute=30;
extern int           mm=               0;
extern int           risk=             2; //12;
extern int           AccountisNormal=  0;

int                  OpenOrders=       0,           cnt=        0;
int                  slippage=         5;
double               sl=               0,           tp=         0;
double               BuyPrice=         0,           SellPrice=  0;
double               lotsi=            0,           mylotsi=    0;
int                  mode=             0,           myOrderType=0;
bool                 ContinueOpening=  True;
double               LastPrice=        0;
int                  PreviousOpenOrders=0;
double               Profit=           0;
int                  LastTicket=       0,           LastType=   0;
double               LastClosePrice=   0,           LastLots=   0;
double               Pivot=            0;
double               PipValue=         0;
string text="", text2="";
int    xP = 1;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   if(Digits == 5 || Digits == 3) {
      slippage = slippage * 10;
      xP = 10;
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//**************ADDED FOR ACCOUNT SENTRY ***************
  
      if (GlobalVariableGet("GV_CloseAllAndHalt") > 0){
      return (0);
      } else
      
//**************END ACCOUNT SENTRY HOOK ****************
   

   //--- Set Margin counter ---------------------------- WJ Begin --//
   //--- Init Code ---//
   string GlobValMargin = AccountNumber()+"_Stoch-Power-EA-5h_Margin";
   if(!GlobalVariableCheck(GlobValMargin)) GlobalVariableSet(GlobValMargin,0);
   double AcctMargin = GlobalVariableGet(GlobValMargin);

   //--- Start Code ---//
   double AcctMarginCurr = AccountMargin();
   if(AcctMargin < AcctMarginCurr){
      AcctMargin = AcctMarginCurr;
      GlobalVariableSet(GlobValMargin,AcctMargin);
   }
   //--------------------------------------------------- WJ End ----//

  
//---- 
   int Ticket;
   if (AccountisNormal==1)
   {
	  if (mm!=0) { lotsi=MathCeil(AccountBalance()*risk/10000); }
		else { lotsi=Lots; }
   } else {  // then is mini
    if (mm!=1) { lotsi=MathCeil(AccountBalance()*risk/10000)/10; }
		else { lotsi=Lots; }
   }
   if (lotsi>100){ lotsi=100; }
   
   OpenOrders=0;
   for(cnt=0;cnt<OrdersTotal();cnt++)   
   {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
	  if (OrderSymbol()==Symbol())
	  {				
	  	  OpenOrders++;
	  }
   }     
   
   /*
   if (OpenOrders<1) 
   {
	  if (TimeYear(CurTime())<StartYear) { return(0);  }
	  if (TimeMonth(CurTime())<StartMonth) { return(0); }
	  if (TimeYear(CurTime())>EndYear) { return(0); }
	  if (TimeMonth(CurTime())>EndMonth ) { return(0); }
   }
   */
   
   if (StringSubstr(Symbol(),0,6)=="EURUSD") { PipValue=EURUSDPipValue; }
   if (StringSubstr(Symbol(),0,6)=="GBPUSD") { PipValue=GBPUSDPipValue; }
   if (StringSubstr(Symbol(),0,6)=="USDJPY") { PipValue=USDJPYPipValue; }
   if (StringSubstr(Symbol(),0,6)=="USDCHF") { PipValue=USDCHFPipValue; }
   if (PipValue==0) { PipValue=5; }
   
   if (PreviousOpenOrders>OpenOrders) 
   {	  
	  for(cnt=OrdersTotal();cnt>=0;cnt--)
	  {
	     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
	  	  mode=OrderType();
		  if (OrderSymbol()==Symbol()) 
		  {
			if (mode==OP_BUY) { OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Blue); }
			if (mode==OP_SELL) { OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,Red); }
			return(0);
		 }
	  }
   }

   PreviousOpenOrders=OpenOrders;
   if (OpenOrders>=MaxTrades) 
   {
	  ContinueOpening=False;
   } else {
	  ContinueOpening=True;
   }

   if (LastPrice==0) 
   {
	  for(cnt=0;cnt<OrdersTotal();cnt++)
	  {	
	    OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
		 mode=OrderType();	
		 if (OrderSymbol()==Symbol()) 
		 {
			LastPrice=OrderOpenPrice();
			if (mode==OP_BUY) { myOrderType=2; }
			if (mode==OP_SELL) { myOrderType=1;	}
		 }
	  }
   }

   if (OpenOrders<1) 
   {
	  myOrderType=3;
	  //if (iMACD(MACD_TF,FastEMA,SlowEMA,SignalSMA,MODE_MAIN,0)>0 and iMACD(MACD_TF,FastEMA,SlowEMA,SignalSMA,MODE_MAIN,0)>iMACD(MACD_TF,FastEMA,SlowEMA,SignalSMA,MODE_MAIN,1)) then OrderType=2;
	  if (iMACD(NULL,MACD_TF,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,MACD_TF,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,1)) { myOrderType=2; }
	  //if (iMACD(MACD_TF,FastEMA,SlowEMA,SignalSMA,MODE_MAIN,0)<0 and iMACD(MACD_TF,FastEMA,SlowEMA,SignalSMA,MODE_MAIN,0)<iMACD(MACD_TF,FastEMA,SlowEMA,SignalSMA,MODE_MAIN,1)) then OrderType=1;
	  if (iMACD(NULL,MACD_TF,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,0)<iMACD(NULL,MACD_TF,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,1)) { myOrderType=1; }
	  if (ReverseCondition==1)
	  {
	  	  if (myOrderType==1) { myOrderType=2; }
		  else { if (myOrderType==2) { myOrderType=1; } }
	  }
   }

   // if we have opened positions we take care of them
   for(cnt=OrdersTotal();cnt>=0;cnt--)
   {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
	  if (OrderSymbol() == Symbol()) 
	  {	
	  	  if (OrderType()==OP_SELL) 
	  	  {			
	  	  	  if (TrailingStop>0) 
			  {
				  if (OrderOpenPrice()-Ask>=(TrailingStop+Pips)*(Point*xP)) 
				  {						
					 if (OrderStopLoss()>(Ask+(Point*xP)*TrailingStop))
					 {			
					    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(Point*xP)*TrailingStop,OrderClosePrice()-TakeProfit*(Point*xP)-TrailingStop*(Point*xP),800,Purple);
	  					 return(0);	  					
	  				 }
	  			  }
			  }
	  	  }
   
	  	  if (OrderType()==OP_BUY)
	  	  {
	  		 if (TrailingStop>0) 
	  		 {
			   if (Bid-OrderOpenPrice()>=(TrailingStop+Pips)*(Point*xP)) 
				{
					if (OrderStopLoss()<(Bid-(Point*xP)*TrailingStop)) 
					{					   
					   OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(Point*xP)*TrailingStop,OrderClosePrice()+TakeProfit*(Point*xP)+TrailingStop*(Point*xP),800,Yellow);
                  return(0);
					}
  				}
			 }
	  	  }
   	}
   }
   
   Profit=0;
   LastTicket=0;
   LastType=0;
	LastClosePrice=0;
	LastLots=0;	
	for(cnt=0;cnt<OrdersTotal();cnt++)
	{
	  OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
	  if (OrderSymbol()==Symbol()) 
	  {			
	  	   LastTicket=OrderTicket();
			if (OrderType()==OP_BUY) { LastType=OP_BUY; }
			if (OrderType()==OP_SELL) { LastType=OP_SELL; }
			LastClosePrice=OrderClosePrice();
			LastLots=OrderLots();
			if (LastType==OP_BUY) 
			{
				//Profit=Profit+(Ord(cnt,VAL_CLOSEPRICE)-Ord(cnt,VAL_OPENPRICE))*PipValue*Ord(cnt,VAL_LOTS);				
				if (OrderClosePrice()<OrderOpenPrice()) 
					{ Profit=Profit-(OrderOpenPrice()-OrderClosePrice())*OrderLots()/(Point*xP); }
				if (OrderClosePrice()>OrderOpenPrice()) 
					{ Profit=Profit+(OrderClosePrice()-OrderOpenPrice())*OrderLots()/(Point*xP); }
			}
			if (LastType==OP_SELL) 
			{
				//Profit=Profit+(Ord(cnt,VAL_OPENPRICE)-Ord(cnt,VAL_CLOSEPRICE))*PipValue*Ord(cnt,VAL_LOTS);
				if (OrderClosePrice()>OrderOpenPrice()) 
					{ Profit=Profit-(OrderClosePrice()-OrderOpenPrice())*OrderLots()/(Point*xP); }
				if (OrderClosePrice()<OrderOpenPrice()) 
					{ Profit=Profit+(OrderOpenPrice()-OrderClosePrice())*OrderLots()/(Point*xP); }
			}
			//Print(Symbol,":",Profit,",",LastLots);
	  }
   }
	
	Profit=Profit*PipValue;
	text2="Profit: $"+DoubleToStr(Profit,2)+" +/-";
   if (OpenOrders>=(MaxTrades-OrderstoProtect) && AccountProtection==1) 
   {	    
	     //Print(Symbol,":",Profit);
	     if (Profit>=SecureProfit) 
	     {
	        OrderClose(LastTicket,LastLots,LastClosePrice,slippage,Yellow);		 
	        ContinueOpening=False;
	        return(0);
	     }
   }

      if (!IsTesting()) 
      {
	     if (myOrderType==3) { text="No conditions to open trades"; }
	     else { text="                         "; }
	     Comment("LastPrice=",LastPrice," Previous open orders=",PreviousOpenOrders,"\nContinue opening=",ContinueOpening," OrderType=",myOrderType,"\n",text2,"\nLots=",lotsi,"\n",text);
      }

      if (myOrderType==1 && ContinueOpening) 
      {	
	     if ((Bid-LastPrice)>=Pips*(Point*xP) || OpenOrders<1) 
	     {		
		    SellPrice=Bid;				
		    LastPrice=0;
		    if (TakeProfit==0) { tp=0; }
		    else { tp=SellPrice-TakeProfit*(Point*xP); }	
		    if (InitialStop==0) { sl=0; }
		    else { sl=SellPrice+InitialStop*(Point*xP);  }
		    if (OpenOrders!=0) 
		    {
			      mylotsi=lotsi;			
			      for(cnt=1;cnt<=OpenOrders;cnt++)
			      {
				     if (MaxTrades>12) { mylotsi=NormalizeDouble(mylotsi*1.5,2); }
				     else { mylotsi=NormalizeDouble(mylotsi*2,2); }
			      }
		    } else { mylotsi=lotsi; }
		    if (mylotsi>100) { mylotsi=100; }
//		    OrderSend(Symbol(),OP_SELL,mylotsi,SellPrice,slippage,sl,tp,NULL,0,0,Red);		    		    
          Ticket = OrderSend(Symbol(), OP_SELL, mylotsi, Bid, slippage, 0, 0, NULL, 0, 0, Red);
             if (Ticket > 0) {
             OrderSelect(Ticket, SELECT_BY_TICKET);
             OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0, Red);
             }
		    return(0);
	     }
      }
      
      if (myOrderType==2 && ContinueOpening) 
      {
	     if ((LastPrice-Ask)>=Pips*(Point*xP) || OpenOrders<1) 
	     {		
		    BuyPrice=Ask;
		    LastPrice=0;
		    if (TakeProfit==0) { tp=0; }
		    else { tp=BuyPrice+TakeProfit*(Point*xP); }	
		    if (InitialStop==0)  { sl=0; }
		    else { sl=BuyPrice-InitialStop*(Point*xP); }
		    if (OpenOrders!=0) {
			   mylotsi=lotsi;			
			   for(cnt=1;cnt<=OpenOrders;cnt++)
			   {
				  if (MaxTrades>12) { mylotsi=NormalizeDouble(mylotsi*multiply,2); }
				  else { mylotsi=NormalizeDouble(mylotsi*multiply,2); }
			   }
		    } else { mylotsi=lotsi; }
		    if (mylotsi>100) { mylotsi=100; }
//		    OrderSend(Symbol(),OP_BUY,mylotsi,BuyPrice,slippage,sl,tp,NULL,0,0,Blue);		   
          Ticket = OrderSend(Symbol(), OP_BUY, mylotsi, Ask, slippage, 0, 0, NULL, 0, 0, Red);
             if (Ticket > 0) {
             OrderSelect(Ticket, SELECT_BY_TICKET);
             OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0, Red);
             }
		    return(0);
	     }
      }   

//----
   return(0);
  }
//+------------------------------------------------------------------+