//+------------------------------------------------------------------+
//|                                                   ShadowTrader   |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//|                                                             v1.2 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double Lots = 0.01;
extern double MaximumRisk        = 0.05;
extern double DecreaseFactor     = 3;
extern int TakeProfit  =  90;
extern int StopLoss    =  30;
extern int hour        =  12; // 12 is for GMT+2 The time, after which the order will be closed
extern int limit       =   2; // Level of setting the postponed order

static int magicNumber = 23456;


double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,100);
//---- calculate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.01) lot=0.01;
   return(lot);
  }

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}

static datetime prev_min = D'1.1.1'; // Previous minimum
static datetime prev_max = D'1.1.1'; // Previous maximum

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   double iFractals_up = 0; // Maximum
   double iFractals_lo = 0; // Minimum
   double TP = 0, SL = 0, price = 0;
   bool order_buy  = false,
        order_sell = false;

   // If there are 2 open orders - no longer it is discovered
   for (int cnt = 0; cnt < OrdersTotal(); cnt++)
   {
      if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false)
         continue;
         
      if ((OrderSymbol() != Symbol()) || (OrderMagicNumber() != magicNumber))
         continue;

      if ((OrderType() != OP_SELL) && (OrderType() != OP_BUY))
         continue;

      switch (OrderType())
      {
         case OP_BUY :
         {
            price     = Bid; 
            order_buy = true;
         } break;
         
         case OP_SELL: 
         { 
            price      = Ask; 
            order_sell = true;
         } break;
      }
            
      // The time elapsed...
      if ((CurTime() - OrderOpenTime()) > hour * 3600)
      {
         OrderClose(OrderTicket(), OrderLots(), price, 3, Red);
         return (0);
      }
   }
   
   // Free Margin remainder
   if (AccountFreeMargin() < 1000 * Lots)
      return (0);
   
   for (int i = 0; i < Bars; i++)
   {
     iFractals_up = iFractals(NULL, 0, MODE_UPPER, i);
     iFractals_lo = iFractals(NULL, 0, MODE_LOWER, i);

      if ((iFractals_up != 0) || (iFractals_lo != 0))
         break;
   }

   // New maximum ?
   if ((iFractals_up != 0) && (Time[i] != prev_max) && (i < 3) && (order_buy == false))
   {
      prev_max = Time[i];
      
      // Let us open the postponed order.
      price = High[i] + limit * Point;
      SL    = price - StopLoss   * Point;
      TP    = price + TakeProfit * Point;      
      OrderSend(Symbol(), OP_BUYSTOP, LotsOptimized(), price, 3, SL, TP, "OP_BUYSTOP", magicNumber, 0, Blue);
      
      Sleep(10000);
      
      // Let us open the basic order.
      SL = Bid + StopLoss   * Point;
      TP = Bid - TakeProfit * Point;
      OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, 3, price, TP, "OP_SELL", magicNumber, 0, Blue);
   }

   // New minimum ?
   if ((iFractals_lo != 0) && (Time[i] != prev_min) && (i < 3) && (order_sell == false))
   {
      prev_min = Time[i];

      // Let us open the postponed order.
      price = Low[i] - limit * Point;
      SL    = price + StopLoss   * Point;
      TP    = price - TakeProfit * Point;
      OrderSend(Symbol(), OP_SELLSTOP, LotsOptimized(), price, 3, SL, TP, "OP_SELLSTOP", magicNumber, 0, Blue);      

      Sleep(10000);

      SL = Ask - StopLoss   * Point;
      TP = Ask + TakeProfit * Point;
      OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, 3, price, TP, "OP_BUY", magicNumber, 0, Yellow);
   }
   return(0);
}
//+------------------------------------------------------------------+