//+------------------------------------------------------------------+
//|                                              20-pips-per-day.mq4 |
//|                                        Copyright © 2007, LEGRUPO |
//|                                           http://www.legrupo.com |
//|                                                     Version: 1.5 |
//| History:                                                         |
//| 1.0 => Release version to the public                             |
//| 1.1 =>  bugs fixed by Dave (dmieluk@exemail.com.au):             |
//|         1) StopLoss was wrong;                                   |
//|         2) StopLoss was conflicting with each other;             |
//|         3) The is_able_to_continue is now much better coded;     |
//|         3.1) The is_able_to_continue now works on any pair;      |
//|         4) Now the errors have descriptions instead of numbers.  |
//| 1.2 => LASTRUN now is display in human readable format by Dave.  |
//| 1.3 => Added these features:                                     |
//|        1) Added the StartOpenOrdersOn to open orders at a        |
//|        specific candle time. It's is good because sometimes you  |
//|        need to close the plataform and reopen, and with this     |
//|        function the EA do not open the orders again since the    |
//|        time allowed is not reached.                              |
//|        2) Added the option to use StopLoss or not.               |
//|        3) Added the option to use BuyStop and SellStop           |
//|        4) No anoying alerts anymore.                             |
//|  1.4 => Added these features:                                    |
//|        1) Magic Numbers for the orders.                          |
//|        2) Corrected StartOpenOrdersOn now works fine.            |
//|  1.5 => Added these features:                                    |
//|        1) Set ticket_buy_20PPD = 0                               |
//|        2) Reset ticket_buy_20PPD and ticket_sell_20PPD to zero   |
//|           on continuation at the end of the day.                 |
//+------------------------------------------------------------------+

#include <stderror.mqh>
#include <stdlib.mqh>

#property copyright "Copyright © 2007, LEGRUPO Version 1.4"
#property link      "http://www.legrupo.com"

extern string StartOpenOrdersOn = "FROM 00:00 UNTIL 01:00"; // look on your chart to see when your daily bar open
extern bool UseStopLoss = true;
extern bool IPreferStopOrders = true;
extern int DistanceFromOpen = 20; // Distance the Limit Orders Should be placed
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;
extern int MagicNumber = 123456789;

static int LASTRUN_v1_1; // verifica se uma nova bar for aberta
int ticket_buy_20PPD = 0, ticket_sell_20PPD = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int total_orders = OrdersTotal();

   int minute = TimeMinute(TimeCurrent());
   int hour   = TimeHour(TimeCurrent());
   //"FROM 02:14 UNTIL 03:15"
   string temp_variable = ""; // variable used for conversions
   temp_variable = StringSubstr(StartOpenOrdersOn,5,6);
   int start_allowed_time_hour   = StrToInteger(temp_variable);
   
   temp_variable = StringSubstr(StartOpenOrdersOn,8,9);
   int start_allowed_time_minute = StrToInteger(temp_variable);
   
   temp_variable = StringSubstr(StartOpenOrdersOn,17,18);
   int end_allowed_time_hour     = StrToInteger(temp_variable);
   
   temp_variable = StringSubstr(StartOpenOrdersOn,20,21);
   int end_allowed_time_minute   = StrToInteger(temp_variable);
   if (hour >= start_allowed_time_hour && 
       //minute >= start_allowed_time_minute &&
       hour <= end_allowed_time_hour
       // && minute <= end_allowed_time_minute
   ) {
      Comment("EA Start 20-pips-per-day:", TimeDay(TimeCurrent()),"/",TimeMonth(TimeCurrent()),"/",TimeYear(TimeCurrent()));
   } else {
      Comment("EA will trade only on this time: ", StartOpenOrdersOn);
      return(0);
   }
   
   ticket_buy_20PPD = 0;
   ticket_sell_20PPD = 0;
   
   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);
   
   double is_able_to_continue = (last_high - last_low)*MathPow(10,Digits);

   Comment("is_able_to_continue: ", is_able_to_continue);
   if (is_able_to_continue < MinimumToContinue) {
      Comment("EA is not able to continue previous bar moved less than ", MinimumToContinue, " pips");
      return(0);
   } else {
      Comment("EA is able to continue :). Good trade.");
   }
   double buy_stoploss,sell_stoploss, buy_price, sell_price, buy_take_profit, sell_take_profit;
   int order_buy_type,order_sell_type;
   
   // now we choose depending on what people want
   if (IPreferStopOrders) {
         buy_price        = curr_open+DistanceFromOpen*Point;
         sell_price       = curr_open-DistanceFromOpen*Point;
         order_buy_type   = OP_BUYSTOP;
         order_sell_type  = OP_SELLSTOP;
   } else {
         buy_price        = Ask;
         sell_price       = Bid;
         order_buy_type   = OP_BUY;
         order_sell_type  = OP_SELL;
   }
   if (UseStopLoss) {
      buy_stoploss     = buy_price-(StopLoss*Point);
      sell_stoploss    = sell_price+(StopLoss*Point);
   } else {
      buy_stoploss = 0;
      sell_stoploss = 0;
   }
   buy_take_profit  = buy_price+(TakeProfit*Point);
   sell_take_profit = sell_price-(TakeProfit*Point);
   
   if (OrderSelect(ticket_buy_20PPD, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_buy_20PPD = OrderSend(Symbol(),order_buy_type,LotSize,buy_price,Slippage,buy_stoploss,buy_take_profit,BuyComment,MagicNumber,0,clOpenBuy);
      if (ticket_buy_20PPD < 0) {
         Alert("Open BUY order error: ", ErrorDescription(GetLastError()));
      }
   }
   
   if (OrderSelect(ticket_sell_20PPD, SELECT_BY_TICKET, MODE_TRADES) == false) {
      ticket_sell_20PPD = OrderSend(Symbol(),order_sell_type,LotSize,sell_price,Slippage,sell_stoploss,sell_take_profit,SellComment,MagicNumber,0,clOpenSell); 
      if (ticket_sell_20PPD < 0) {
         Alert("Open SELL order error: ", ErrorDescription(GetLastError()));
      }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+