//+------------------------------------------------------------------+
//|                                              Reversal_EA_MT4.mq4 |
//|                         Copyright © 2007, Metex Investments Inc. |
//|                                       http://www.metexinvest.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Metex Investments Inc."
#property link      "http://www.metexinvest.com"

#include <stdlib.mqh>

//+------------------------------------------------------------------+
//| Common External variables                                        |
//+------------------------------------------------------------------+
extern double Lots = 1.00;
extern double StopLoss = 99.00;
extern double TakeProfit = 15.00;
extern double TrailingStop = 7.00;

//+------------------------------------------------------------------+
//| External variables                                               |
//+------------------------------------------------------------------+
extern double Slippage = 3;
extern double risklevel = 3;

//+------------------------------------------------------------------+
//| Special Convertion Functions                                     |
//+------------------------------------------------------------------+

int LastTradeTime;

bool MOrderModify( int ticket, double price, double stoploss, double 
takeprofit, datetime expiration, color arrow_color=CLR_NONE)
{
  LastTradeTime = TimeCurrent();
  price = MathRound(price*10000)/10000;
  stoploss = MathRound(stoploss*10000)/10000;
  takeprofit = MathRound(takeprofit*10000)/10000;
  return ( OrderModify( ticket, price, stoploss, takeprofit, 
expiration, arrow_color) );
}

int MOrderSend( string symbol, int cmd, double volume, double price, 
int slippage, double stoploss, double takeprofit, string comment="", int 
magic=0, datetime expiration=0, color arrow_color=CLR_NONE)
{
  LastTradeTime = TimeCurrent();
  price = MathRound(price*10000)/10000;
  stoploss = MathRound(stoploss*10000)/10000;
  takeprofit = MathRound(takeprofit*10000)/10000;
  return ( OrderSend( symbol, cmd, volume, price, slippage, stoploss, 
takeprofit, comment, magic, expiration, arrow_color ) );
}

int OrderValueTicket(int index)
{
  OrderSelect(index, SELECT_BY_POS);
  return(OrderTicket());
}

int OrderValueType(int index)
{
  OrderSelect(index, SELECT_BY_POS);
  return(OrderType());
}

double OrderValueOpenPrice(int index)
{
  OrderSelect(index, SELECT_BY_POS);
  return(OrderOpenPrice());
}

double OrderValueStopLoss(int index)
{
  OrderSelect(index, SELECT_BY_POS);
  return(OrderStopLoss());
}

double OrderValueTakeProfit(int index)
{
  OrderSelect(index, SELECT_BY_POS);
  return(OrderTakeProfit());
}

string OrderValueSymbol(int index)
{
  OrderSelect(index, SELECT_BY_POS);
  return(OrderSymbol());
}

//+------------------------------------------------------------------+
//| End                                                              |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+

int init()
{
   return(0);
}
int start()
{
//+------------------------------------------------------------------+
//| Local variables                                                  |
//+------------------------------------------------------------------+
double temp = 0;
double lotss = 0;
int i = 0;
double OpenTrades = 0;
int cnt = 0;

lotss=MathCeil((AccountBalance()*risklevel*Ask)/100000);

if ((Open[2]>Close[2] && Close[1]>Open[1] && Open[2]-Close[2]>0 && Close[1]-Open[1]>1*Point) )
  
  {
 OpenTrades = 0;
  	for(i =1;i <=OrdersTotal();i ++){
        if( OrderValueSymbol(i) == Symbol() ) 
             OpenTrades++;
  		}
   if( OpenTrades<1 ) 
	    {
MOrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",16384,0,Blue);
	    }
  }

if( TrailingStop>0 ) 
{
for(cnt=1;cnt<=OrdersTotal();cnt++){
   if( OrderValueType(cnt) == OP_BUY && OrderValueSymbol(cnt) == Symbol() ) 
     {
      if( (Bid-OrderValueOpenPrice(cnt))>(TrailingStop*Point) ) 
      	{
         if( OrderValueStopLoss(cnt)<(Bid-TrailingStop*Point) ) 
           {
            
MOrderModify(OrderValueTicket(cnt),OrderValueOpenPrice(cnt),         
Bid-TrailingStop*Point,OrderValueTakeProfit(cnt),0,Blue);
            return(0); 
           }
        }
     }
  }

if (Close[2]>Open[2] && Open[1]>Close[1] && Close[2]-Open[2]>0 && Open[1]-Close[1]>1*Point) 
 
  {
	 OpenTrades = 0;
  	for(i =1;i <=OrdersTotal();i ++){
        if( OrderValueSymbol(i) == Symbol() ) 
             OpenTrades++;
  		}
  		
   if( OpenTrades<1 ) 
    	{
MOrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",16384,0,Red);
	    }
  }

   for(cnt=1;cnt<=OrdersTotal();cnt++){
     if( OrderValueType(cnt) == OP_SELL && OrderValueSymbol(cnt) == Symbol() )    
     {
      if( (OrderValueOpenPrice(cnt)-Ask)>(TrailingStop*Point) ) 
     	{
         if( OrderValueStopLoss(cnt)>(Ask+TrailingStop*Point) ||
            OrderValueStopLoss(cnt) == 0 ) 
           {
            
MOrderModify(OrderValueTicket(cnt),OrderValueOpenPrice(cnt),Ask+TrailingStop*Point,OrderValueTakeProfit(cnt),0,Red);
            return(0);
           }
     	}
  	 }  	 
   }
}

  return(0);
}

