//+------------------------------------------------------------------+
//|                                      Enveloppement des cours.mq4 |
//|                                                         Nickleus |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Nickleus"
#property link      ""

extern double lotStart=0.1;
extern int sl=100;
extern int tp=100;
extern int HourStart=14;
extern int HourEnd=18;

extern int magicNumber = 123;

double lot;
double orderType;
int opertion;
int typeOrder = OP_SELL;
int ticket;

bool revenge = false;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   lot = lotStart;
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
   double profit = 0;
   double SLA = NormalizeDouble(Ask-sl*Point, Digits);//NormalizeDouble(Ask-10*Point, Digits);
   double TPA = NormalizeDouble(Ask+tp*Point, Digits);//NormalizeDouble(Ask+10*Point, Digits);
   double SLB = NormalizeDouble(Bid+sl*Point, Digits);//NormalizeDouble(Ask-10*Point, Digits);
   double TPB = NormalizeDouble(Bid-tp*Point, Digits);//NormalizeDouble(Ask+10*Point, Digits);
   int hourActually = TimeHour(Time[0]);
   
   double mm1 = iMA(NULL,0,50,0,MODE_EMA,PRICE_MEDIAN,0);
   double mm2 = iMA(NULL,0,50,0,MODE_EMA,PRICE_MEDIAN,0);
   
   if(OrdersTotal() == 0 && hourActually > HourStart && hourActually < HourEnd) {
      if(OrderSelect(ticket - 1, SELECT_BY_POS, MODE_HISTORY)) {         
         if(OrderType() == OP_SELL) {
            typeOrder = OP_SELL;
         } else if(OrderType() == OP_BUY) {
            typeOrder = OP_SELL;
         }
      }
      
      if(OrderProfit() < 0) {
          lot = lot*2;
      } else if(OrderProfit() > 0) {
          lot = lotStart;
      }
      
      if(typeOrder == OP_SELL) {
         //Comment("Achat");
         ticket = OrderSend(Symbol(),OP_BUY,lot,Ask,3,SLA,TPA,"Enveleppement des cours",magicNumber,0,Green);
      } else if(typeOrder == OP_BUY) {
         //Comment("Vente");
         ticket = OrderSend(Symbol(),OP_SELL,lot,Bid,3,SLB,TPB,"Enveleppement des cours",magicNumber,0,Green);
     }
     
   }
   return(0);
}
//+------------------------------------------------------------------+