//+------------------------------------------------------------------+
//|                                           teste-hedge-gbpjpy.mq4 |
//|                                        Copyright © 2007, LEGRUPO |
//|                                           http://www.legrupo.com |
//|                                                     Version: 1.0 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, LEGRUPO Version 1.0"
#property link      "http://www.legrupo.com"

extern double TakeProfit = 40;
extern double StopLoss = 20;
extern double StopAndReverseOnLossOf = 100;
extern int MinimumToContinue = 100; // pips the daily bar must have
extern double LotSize = 0.1;
extern double Slippage = 15;
extern string BuyComment = "20 pips per day BUY";
extern string SellComment = "20 pips per day SELL";
extern color clOpenBuy = Blue;
extern color clOpenSell = Red;
static int LASTRUN_4; // verifica se uma nova bar for aberta
int ticket_buy, ticket_sell = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
int total_orders = OrdersTotal();
/*if (total_orders>=1) {
    OrderSelect(ticket_buy,SELECT_BY_TICKET);
    if (OrderType()==OP_BUY) { // you'll need to add a code section for sell  also...
      if (Bid<OrderOpenPrice()-StopAndReverseOnLossOf) { // whatever the value of hundred pips is in your currency pair
         OrderClose(OrderTicket(), LotSize, Ask, Slippage, CLR_NONE);
         ticket_sell = OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,0,Bid-(TakeProfit*Point),SellComment,0,0,clOpenSell); 
         if (ticket_sell < 0) {
               Alert(GetLastError());
         }
      }
    }
    OrderSelect(ticket_sell,SELECT_BY_TICKET);
    if (OrderType()==OP_SELL) {
      if(Ask>OrderOpenPrice()-StopAndReverseOnLossOf) {
         OrderClose(OrderTicket(), LotSize, Ask, Slippage, CLR_NONE);
         ticket_buy = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,0,Ask+(TakeProfit*Point),BuyComment,0,0,clOpenBuy);
         if (ticket_buy < 0) {
               Alert(GetLastError());
         }      
      }
    }
}*/

if (LASTRUN_4==Time[0])
   return(0);
   LASTRUN_4 = Time[0];
    Alert("EA Start 20-pips-per-day:", LASTRUN_4);
    
   double curr_open = iOpen(NULL, 0, 0);
   double last_high = iHigh(NULL, 0, 1);
   double last_low = iLow(NULL, 0, 1);
   double last_close = iClose(NULL, 0, 1);
   
   // creio que é bigode, mas por enquanto só encontrei esta maneira
   string str_last_close = DoubleToStr(last_close, Digits);
   string str_last_high = DoubleToStr(last_high, Digits);
   string str_last_low = DoubleToStr(last_low, Digits);
   
   string substr0 = "";
   string substr1 = "";
   substr0 = StringSubstr(str_last_close, 0, 3);
   substr1 = StringSubstr(str_last_close, 4, 5);
   str_last_close = StringConcatenate(substr0, substr1);
   
   substr0 = StringSubstr(str_last_high, 0, 3);
   substr1 = StringSubstr(str_last_high, 4, 5);
   str_last_high = StringConcatenate(substr0, substr1);
   
   substr0 = StringSubstr(str_last_low, 0, 3);
   substr1 = StringSubstr(str_last_low, 4, 5);
   str_last_low = StringConcatenate(substr0, substr1);
   
   int int_last_low = StrToInteger(str_last_low);
   int int_last_high = StrToInteger(str_last_high);
   int int_last_close = StrToInteger(str_last_close);
   
   int is_able_to_continue = int_last_high - int_last_low;
   Print("is_able_to_continue: ", is_able_to_continue);
   if (is_able_to_continue < MinimumToContinue) {
      return(0);
   }

   //if (total_orders == 0) {
      StopLoss = Ask-(StopLoss*Point);
      ticket_buy = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,StopLoss,Ask+(TakeProfit*Point),BuyComment,0,0,clOpenBuy);
      if (ticket_buy < 0) {
            Alert(GetLastError());
      }
      
      StopLoss = Bid-(StopLoss*Point); 
      ticket_sell = OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,StopLoss,Bid-(TakeProfit*Point),SellComment,0,0,clOpenSell); 
      if (ticket_sell < 0) {
            Alert(GetLastError());
      }
   //}
//----
   return(0);
  }
//+------------------------------------------------------------------+