//+------------------------------------------------------------------+
//|                                                      SRDC v2     |
//|                                     Copyright © 2006, Skyline    |
//|                             Last version compiled on 31 Oct 2006 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Skyline"
#include <stdlib.mqh>

//---- input parameters
extern int       StartTime    = 2;   // begin of the first session; time adjust by your broker time
extern int       BORDER       = 1;   // pips we add to the high and low to get entry price
extern int       EOD          = 23;  // time for closing orders at end of day
extern int       StopLoss     = 20;
extern int       MOVE_SL_AT   = 35;  // if an order reaches this profit level...
extern int       MOVE_SL_TO   =  5;  // ...adjust SL to this profit level
extern int       TrailingStop = 50;  //if equals 0, it uses move_sl_at as breakeven
extern int       TakeProfit   = 100;  
extern double    Lots         = 0.5;
extern int       Slippage     = 0;

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   //---- 
   int i,Ticket,MN;
   
   //Setup comment
   string Text="SRDC"+Symbol();

   //Setup orders
   if(Hour()==StartTime && Minute()<59)
   {
      MN=1;
      SetOrders(Text,MN);
   }
   
   //Manage opened orders
   for (i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderComment()==Text){
         //close open positions at EOD
         if(Hour()==EOD)// || (DayOfWeek()>=5 && Hour()==FridayClosing-1 && Minute()>=50))
         { 
            switch (OrderType()){
               case OP_BUY: OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Red);
               break;
               case OP_SELL: OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
               break;
               default: OrderDelete(OrderTicket());
               break;
            }
            Sleep(10000);
         }
         else {
            //move at MOVE_SL_TO if profit>MOVE_SL_AT
            if(TrailingStop==0){ 
               if(OrderType()==OP_BUY)
               {
                  //if(High[0]-OrderOpenPrice()>=BreakEven*Point && OrderStopLoss()<OrderOpenPrice())
                  if(High[0]-OrderOpenPrice()>=MOVE_SL_AT*Point && OrderStopLoss()<OrderOpenPrice()+MOVE_SL_TO*Point)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
                     Sleep(10000);
                  }   
               }   
               if(OrderType()==OP_SELL)
               {
                  //if(OrderOpenPrice()-Low[0]>=BreakEven*Point && OrderStopLoss()>OrderOpenPrice())
                  if(OrderOpenPrice()-Low[0]>=MOVE_SL_AT*Point && OrderStopLoss()>OrderOpenPrice()-MOVE_SL_TO*Point)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
                     Sleep(10000);
                  }
               }
            }
            //use trailing stop
            else 
            {                  
               if(OrderType()==OP_BUY)
               {
                  if(High[0]-OrderStopLoss()>TrailingStop*Point)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),High[0]-TrailingStop*Point,OrderTakeProfit(),0,Green);
                     Sleep(10000);
                  }   
               }   
               if(OrderType()==OP_SELL)
               {
                  if(OrderStopLoss()-Low[0]>TrailingStop*Point)
                  {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Low[0]+TrailingStop*Point,OrderTakeProfit(),0,Green);
                     Sleep(10000);
                  }
               }
            }
         }
      }
   }
   
   return(0);
   }
//+------------------------------------------------------------------+

void SetOrders(string Text,int MN)
{
   int i,Ticket,Bought,Sold,err;
   double EntryLong,EntryShort,SLLong,SLShort,TPLong,TPShort;
   double Spread = Ask-Bid;
   //Determine range

   
   EntryLong   = iHigh(NULL,PERIOD_D1,1)+Spread+BORDER*Point;
   EntryShort  = iLow(NULL,PERIOD_D1,1)-Spread-BORDER*Point;
   SLLong      =MathMax(EntryLong-StopLoss*Point,EntryShort);
   SLShort     =MathMin(EntryShort+StopLoss*Point,EntryLong);
   TPLong      =EntryLong+TakeProfit*Point;
   TPShort     =EntryShort-TakeProfit*Point;
   //Send orders
   for (i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderComment()==Text && OrderMagicNumber()==MN)
      {
         if(OrderType()==OP_BUYSTOP || OrderType()==OP_BUY) Bought++;
         if(OrderType()==OP_SELLSTOP || OrderType()==OP_SELL) Sold++;
      }
   }
   if(Bought==0){ //no buy order
      Ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,EntryLong,Slippage,SLLong,TPLong,Text,MN,0,Blue);
      if(Ticket<0)
      {
       err=GetLastError();
       Print("error(",err,"): ",ErrorDescription(err));
      }
      if(Ticket<0 && High[0]>=EntryLong)
         Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SLLong,TPLong,Text,MN,0,Blue);
         Sleep(10000); 
   }
   if(Sold==0){ //no sell order
      Ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,EntryShort,Slippage,SLShort,TPShort,Text,MN,0,Magenta);
      if(Ticket<0)
      {
       err=GetLastError();
       Print("error(",err,"): ",ErrorDescription(err));
      }
      if(Ticket<0 && Low[0]<=EntryShort)
         Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SLShort,TPShort,Text,MN,0,Magenta);
         Sleep(10000); 
   }
   //Check orders
   for (i=0;i<OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderComment()==Text && OrderMagicNumber()==MN)
      {
         if(OrderType()==OP_BUYSTOP && (MathAbs(OrderOpenPrice()-EntryLong)>Point 
         || MathAbs(OrderStopLoss()-SLLong)>Point || MathAbs(OrderTakeProfit()-TPLong)>Point))
            OrderModify(OrderTicket(),EntryLong,SLLong,TPLong,0,Blue);
         if(OrderType()==OP_SELLSTOP && (MathAbs(OrderOpenPrice()-EntryShort)>Point 
         || MathAbs(OrderStopLoss()-SLShort)>Point || MathAbs(OrderTakeProfit()-TPShort)>Point))
            OrderModify(OrderTicket(),EntryShort,SLShort,TPShort,0,Magenta);
      }
   }
}

