//+------------------------------------------------------------------+
//|                               JIVA Dragon EA DSS T3MA MAs v7.mq4 |
//|                                                jiva34, Taiyakixz |
//|                                               thanks to geoffb65 |
//|              http://www.forexfactory.com/showthread.php?t=242490 |
//+------------------------------------------------------------------+
//
// v4.1 - no trailing stop when TrailingStop = 0
//        take order immediately when PendingPips = 0
//        some error messages changed when order failed
// v5.0 - delete pending order when conditions change
// v6.0 - close order when candle closes out of opposite side of
//        channel even if loss trade
// v7.0 - function added with v5.0 is deleted
//      - delete pending order when new open condition

#property copyright "jiva34, Taiyakixz"
#property link      "http://www.forexfactory.com/showthread.php?t=242490"

extern int       iMagicNumber = 8435701;
extern int       GMTOffset = 2;
extern int       TradeStartHour = 0;
extern int       TradeEndHour = 24;
extern double    Lots = 0.1;
extern int       TakeProfit1 = 30;
extern int       TrailingStop1 = 80;
extern int       TakeProfit2 = 1000;
extern int       TrailingStop2 = 100;
extern int       StopLoss = 30;
extern int       PendingPips = 30;
extern int       ExpireCandles = 10;
extern int       DelayOrder2 = 20;
extern bool      CheckSlope = true;
extern bool      USE_DSS_name = false;
extern string    DSS_name = "Robby DSS Bressert Colored with alert v1.1";
extern int       DSS_EMA=8;
extern int       DSS_stoch=13;
extern bool      Use_T3MA = false;
extern string    T3MA_name = "T3MA";
extern int       T3MA_Periods = 8;
extern double    T3MA_VolumeFactor = 0.7;
extern int       Channel_EMAPeriod = 38;
extern int       Channel_EMAShift = 0;
extern bool      ShowComment = true;


int ServerTradeStartHour, ServerTradeEndHour;
datetime prevBarTime;
double Poin;
int pipsDigits;
int order1Ticket;
bool order1Finished = false;
bool order1Pending = false;
bool order2BuyStart = false;
bool order2SellStart = false;
datetime order1StartTime;
double dss1, dss2;
double t3ma1, t3ma2;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   ServerTradeStartHour = TradeStartHour + GMTOffset;
   if( ServerTradeStartHour >= 24 ) ServerTradeStartHour = ServerTradeStartHour - 24;
   ServerTradeEndHour = TradeEndHour + GMTOffset;
   if( ServerTradeEndHour >= 24 ) ServerTradeEndHour = ServerTradeEndHour - 24;
   if( Digits == 2 || Digits == 4 ) Poin = Point;
   if( Digits == 3 || Digits == 5 ) Poin = 10*Point;
   if( Digits == 6 ) Poin = 100*Point;

   if( Digits == 4 || Digits == 5 || Digits == 6 ) pipsDigits = 4;
   if( Digits == 2 || Digits == 3 ) pipsDigits = 2;

   prevBarTime = Time[0];
   order1Ticket = -1;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

int CountOpenOrders()
  {
   int total = 0;
//----
   for(int i=0;i<OrdersTotal();i++)
   {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
     {
       if(OrderSymbol()==Symbol() && (OrderMagicNumber()==iMagicNumber || OrderMagicNumber()==(iMagicNumber+1) ) )
       {
         if(OrderType()==OP_BUY || OrderType()==OP_SELL) total++;
       }
     }
   }
   return( total );
  }

int CountPendingOrders( int buySellTotal )
  {
   // buySellTotal : 0=buy, 1=sell, 2=total
   int total = 0;
//----
   for(int i=0;i<OrdersTotal();i++)
   {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
     {
       if(OrderSymbol()==Symbol() && (OrderMagicNumber()==iMagicNumber || OrderMagicNumber()==(iMagicNumber+1) ) )
       {
         if( buySellTotal == 0 && OrderType()==OP_BUYSTOP ) total++;
         if( buySellTotal == 1 && OrderType()==OP_SELLSTOP ) total++;
         if( buySellTotal == 2 && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) ) total++;
       }
     }
   }
   return( total );
  }

int CountBuyOrSellOrders( int buySell )
  {
   int total = 0;
//----
   for(int i=0;i<OrdersTotal();i++)
   {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
     {
       if(OrderSymbol()==Symbol() && (OrderMagicNumber()==iMagicNumber || OrderMagicNumber()==(iMagicNumber+1) ) )
       {
         if( (buySell == OP_BUY && OrderType()==OP_BUY) || (buySell == OP_SELL && OrderType()==OP_SELL) ) total++;
       }
     }
   }
   return( total );
  }

void CheckForMovingStopLoss()
  {
   int i, iTotal;
   bool order1Find;
   double order1OpenPrice=0, order1ClosePrice=0;
   
   iTotal = OrdersTotal();

   order1Find = false;
   for(i=0;i<iTotal;i++)
   {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if( OrderSymbol()==Symbol() && OrderMagicNumber() == iMagicNumber && OrderTicket() == order1Ticket )
       order1Find = true;
   }
   
   if( order1Find == false )
   {
     if( OrderSelect( order1Ticket, SELECT_BY_TICKET ) == true )
     {
       order1OpenPrice = OrderOpenPrice();
       order1ClosePrice = OrderClosePrice();
     }
   }
   
   for(i=0;i<iTotal;i++)
   {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if( OrderSymbol()==Symbol() && OrderMagicNumber() == iMagicNumber+1)
       switch(OrderType()){
         case OP_BUY:
           if( ((order1Find == false && order1OpenPrice < order1ClosePrice) || (Bid-OrderOpenPrice())>Poin*TakeProfit1) && Bid > OrderOpenPrice()){
             if( OrderStopLoss() < OrderOpenPrice() || OrderStopLoss() == 0 ){
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Aqua);
               order1Finished = true;
             }
           }
           break;
         case OP_SELL:
           if( ((order1Find == false && order1OpenPrice > order1ClosePrice) || (OrderOpenPrice()-Ask)>Poin*TakeProfit1) && Bid < OrderOpenPrice()){
             if( OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0 ){
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Orange);
               order1Finished = true;
             }
           }
           break;
       }
   }

  }

void TrailingStop()
  {
   int i, total;
   total = OrdersTotal();
   
   for(i=0; i < total; i++)
   {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if( TrailingStop1 != 0 )
     {
       if( OrderSymbol()==Symbol() && OrderMagicNumber() == iMagicNumber)
         switch(OrderType()){
           case OP_BUY:
             if(Bid-OrderOpenPrice() > Poin*TrailingStop1){
               if((OrderStopLoss()<(Bid-Poin*TrailingStop1)) || OrderStopLoss()==0){
                 OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Poin*TrailingStop1,OrderTakeProfit(),0,Aqua);
               }
             }
             break;
           case OP_SELL:
             if(OrderOpenPrice()-Ask > Poin*TrailingStop1){
                if((OrderStopLoss()>(Ask+Poin*TrailingStop1)) || OrderStopLoss()==0){
                   OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Poin*TrailingStop1,OrderTakeProfit(),0,Orange);
                }
             }
             break;
         }
     }
     if( TrailingStop2 != 0 )
     {
       if( OrderSymbol()==Symbol() && OrderMagicNumber() == iMagicNumber+1)
         switch(OrderType()){
           case OP_BUY:
             if(Bid-OrderOpenPrice() > Poin*TrailingStop2){
               if((OrderStopLoss()<(Bid-Poin*TrailingStop2)) || OrderStopLoss()==0){
                 OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Poin*TrailingStop2,OrderTakeProfit(),0,Aqua);
               }
             }
             break;
           case OP_SELL:
             if(OrderOpenPrice()-Ask > Poin*TrailingStop2){
                if((OrderStopLoss()>(Ask+Poin*TrailingStop2)) || OrderStopLoss()==0){
                   OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Poin*TrailingStop2,OrderTakeProfit(),0,Orange);
                }
             }
             break;
         }
     }
   }
  }

void CheckDSS()
  {
   dss1 = iCustom(NULL, 0, DSS_name, false, DSS_EMA, DSS_stoch, 0, 1);
   dss2 = iCustom(NULL, 0, DSS_name, false, DSS_EMA, DSS_stoch, 0, 2);
  }

void CheckT3MA()
  {
   t3ma1 = iCustom(NULL, 0, T3MA_name, T3MA_Periods, T3MA_VolumeFactor, 0, 1);
   t3ma2 = iCustom(NULL, 0, T3MA_name, T3MA_Periods, T3MA_VolumeFactor, 0, 2);
  }

int CheckMA( int maNo )
  {
   double ma1, ma2;
   if( !( maNo >= 1 && maNo <= 5 ) ) return(0);
   switch( maNo )
   {
     
  }


  }

void CheckForOpen()
  {
   int retries, res, err;
   string errOrderMes;
   double emah1, emah2, emal1, emal2;
   datetime tradeExpired;

   emah1 = iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_HIGH, 1);
   emah2 = iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_HIGH, 2);
   emal1 = iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_LOW, 1);
   emal2 = iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_LOW, 2);

   if(  true && dss1 > dss2
   && ( Use_T3MA == false || t3ma1 > t3ma2 )
   && ( CheckSlope == false || NormalizeDouble( emah1, pipsDigits ) > NormalizeDouble( emah2, pipsDigits ) )
   && Close[1] > emah1 && Close[2] <= emah2
   && CountPendingOrders(0) == 0 )
   {
     if( CountPendingOrders(1) > 0 ) DeletePending(false);
     tradeExpired = Time[0] + ExpireCandles * Period() * 60;
     retries =10;
     while(retries > 0)
     {
       RefreshRates();
       if( PendingPips == 0 )
         order1Ticket = OrderSend(Symbol(), OP_BUY, Lots, NormalizeDouble(Ask,Digits), 3*Poin/Point, NormalizeDouble(Ask-StopLoss*Poin,Digits), NormalizeDouble(Ask+TakeProfit1*Poin,Digits), "", iMagicNumber, 0, Blue);
         
       else
         order1Ticket = OrderSend(Symbol(), OP_BUYSTOP, Lots, NormalizeDouble(Ask+PendingPips*Poin,Digits), 3*Poin/Point, NormalizeDouble(Ask+(PendingPips-StopLoss)*Poin,Digits), NormalizeDouble(Ask+(PendingPips+TakeProfit1)*Poin,Digits), "", iMagicNumber, tradeExpired, Blue);
       err = GetLastError();
       if(err != 0)
       {
         Print("Buy OrderSend 1 failed with error #", err);
         errOrderMes = StringConcatenate("Lots=",Lots,", Price=",DoubleToStr(Ask+PendingPips*Poin,Digits),", StopLoss=",DoubleToStr(Ask+(PendingPips-StopLoss)*Poin,Digits),", TakeProfit=",DoubleToStr(Ask+(PendingPips+TakeProfit1)*Poin,Digits));
         if( PendingPips != 0 ) errOrderMes = StringConcatenate( errOrderMes, ", ExpireCandles=",ExpireCandles);
         Print(errOrderMes);
         Sleep(500);
       }
       else
       {
         order1Finished = false;
         break;
       }
       retries--;
     }//end while
   }
   if(  true && dss1 < dss2
   && ( Use_T3MA == false || t3ma1 < t3ma2 )
   && ( CheckSlope == false || NormalizeDouble( emal1, pipsDigits ) < NormalizeDouble( emal2, pipsDigits ) )
   && Close[1] < emal1 && Close[2] >= emal2
   && CountPendingOrders(1) == 0 )
   {
     if( CountPendingOrders(0) > 0 ) DeletePending(true);
     tradeExpired = Time[0] + ExpireCandles * Period() * 60;
     retries =10;
     while(retries > 0)
     {
       RefreshRates();
       if( PendingPips == 0 )
         order1Ticket = OrderSend(Symbol(), OP_SELL, Lots, NormalizeDouble(Bid,Digits), 3*Poin/Point, NormalizeDouble(Bid+StopLoss*Poin,Digits), NormalizeDouble(Bid-TakeProfit1*Poin,Digits), "", iMagicNumber, 0, Red);
       else
         order1Ticket = OrderSend(Symbol(), OP_SELLSTOP, Lots, NormalizeDouble(Bid-PendingPips*Poin,Digits), 3*Poin/Point, NormalizeDouble(Bid-(PendingPips-StopLoss)*Poin,Digits), NormalizeDouble(Bid-(PendingPips+TakeProfit1)*Poin,Digits), "", iMagicNumber, tradeExpired, Red);
       err = GetLastError();
       if(err != 0)
       {
         Print("Sell OrderSend 1 failed with error #", err);
         errOrderMes = StringConcatenate("Lots=",Lots,", Price=",DoubleToStr(Bid-PendingPips*Poin,Digits),", StopLoss=",DoubleToStr(Bid-(PendingPips-StopLoss)*Poin,Digits),", TakeProfit=",DoubleToStr(Bid-(PendingPips+TakeProfit1)*Poin,Digits));
         if( PendingPips != 0 ) errOrderMes = StringConcatenate( errOrderMes, ", ExpireCandles=",ExpireCandles);
         Print(errOrderMes);
         Sleep(500);
       }
       else
       {
         order1Finished = false;
         break;
       }
       retries--;
     }//end while
   }
  }

void Order2Buy()
  {
   int retries, res, err;

   if( order1Ticket != -1 )
   {
     OrderSelect( order1Ticket, SELECT_BY_TICKET );
     if( TimeCurrent() - order1StartTime >= DelayOrder2 )
     {
       retries =10;
       while(retries > 0 && order1Ticket != -1)
       {
         RefreshRates();
         res = OrderSend(Symbol(), OP_BUY, Lots, NormalizeDouble(Ask,Digits), 3*Poin/Point, NormalizeDouble(Ask - StopLoss * Poin,Digits), NormalizeDouble(Ask + TakeProfit2 * Poin,Digits), "", iMagicNumber+1, 0, Blue);
         err = GetLastError();
         if(err != 0)
         {
           Print("Buy OrderSend 2 failed with error #", err);
           Print("Lots=",Lots,", Price=",DoubleToStr(Ask,Digits),", StopLoss=",DoubleToStr(Ask - StopLoss * Poin,Digits),", TakeProfit=",DoubleToStr(Ask + TakeProfit2 * Poin,Digits));
           Sleep(500);
         }
         else
         {
           order2BuyStart = false;
           break;
         }
         retries--;
       }//end while
     }
   }
  }

void Order2Sell()
  {
   int retries, res, err;

   if( order1Ticket != -1 )
   {
     OrderSelect( order1Ticket, SELECT_BY_TICKET );
     if( TimeCurrent() - order1StartTime >= DelayOrder2 )
     {
       retries =10;
       while(retries > 0 && order1Ticket != -1)
       {
         RefreshRates();
         res = OrderSend(Symbol(), OP_SELL, Lots, NormalizeDouble(Bid,Digits), 3*Poin/Point, NormalizeDouble(Bid + StopLoss * Poin,Digits), NormalizeDouble(Bid - TakeProfit2 * Poin,Digits), "", iMagicNumber+1, 0, Red);
         err = GetLastError();
         if(err != 0)
         {
           Print("Sell OrderSend 2 failed with error #", err);
           Print("Lots=",Lots,", Price=",DoubleToStr(Bid,Digits),", StopLoss=",DoubleToStr(Bid + StopLoss * Poin,Digits),", TakeProfit=",DoubleToStr(Bid - TakeProfit2 * Poin,Digits));
           Sleep(500);
         }
         else
         {
           order2SellStart = false;  
           break;
         }
         retries--;
       }//end while
     }
   }
  }

void DeletePending( bool buySell )
  {
   bool   res;
   int    i, pos, err, iTotal;

   while( (buySell == true && CountPendingOrders(0) > 0)
    || (buySell == false && CountPendingOrders(1) > 0) )
   {
     iTotal = OrdersTotal();
     if( iTotal > 0 )
     {
       for(i=0; i<iTotal; i++)
       {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
         {
           if(OrderSymbol()==Symbol() && ( OrderMagicNumber()==iMagicNumber || OrderMagicNumber()==(iMagicNumber+1) ) )
           {
             pos = OrderType();
             if( (buySell == true && pos == OP_BUYSTOP) || (buySell == false && pos == OP_SELLSTOP) )
             {
               res = OrderDelete(OrderTicket(), CLR_NONE);
               if( res != true)
               {
                 err = GetLastError();
                 Print("OrderDelete failed with error ", err );
               }
             }
           }
         }
       }
     }
   }
  }

void CheckForClose()
  {
   int i, iTotal;
   int retries, err;
   bool res;
   
   iTotal = OrdersTotal();
   
   for(i=0;i<iTotal;i++)
   {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if( OrderSymbol()==Symbol() && OrderMagicNumber() == iMagicNumber+1)
     {
       if(OrderType() == OP_BUY && Close[1] < iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_LOW, 1) )
       {
         CloseOrders(OP_BUY);
         break;
       }
       if(OrderType() == OP_SELL && Close[1] > iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_HIGH, 1) )
       {
         CloseOrders(OP_SELL);
         break;
       }
     }
   }
  }

void CloseOrders( int buySell )
  {
   int i, iTotal;
   int retries, err;
   bool res;


   if( buySell == OP_BUY )
   {
     while( CountBuyOrSellOrders(OP_BUY) >= 0 )
     {
       iTotal = OrdersTotal();
   
       for(i=0;i<iTotal;i++)
       {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if( OrderType() == OP_BUY && OrderSymbol()==Symbol() && (OrderMagicNumber() == iMagicNumber || OrderMagicNumber() == iMagicNumber+1))
         {
           retries =10;
           while(retries > 0)
           {
             RefreshRates();
             res = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid,Digits), 3*Poin/Point, Green);
             err = GetLastError();
             if(err != 0)
             {
               Print("Buy OrderClose failed with error #", err);
               Sleep(500);
             }
             else                 
               break;
             retries--;
           }//end while retries
         }
       }
     }//end while CountBuyOrSellOrders
   }
   
   if( buySell == OP_SELL )
   {
     while( CountBuyOrSellOrders(OP_SELL) >= 0 )
     {
       iTotal = OrdersTotal();
   
       for(i=0;i<iTotal;i++)
       {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if( OrderType() == OP_SELL && OrderSymbol()==Symbol() && (OrderMagicNumber() == iMagicNumber || OrderMagicNumber() == iMagicNumber+1))
         {
           retries =10;
           while(retries > 0)
           {
             RefreshRates();
             res = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask,Digits), 3*Poin/Point, Yellow);
             err = GetLastError();
             if(err != 0)
             {
               Print("Sell OrderClose failed with error #", err);
               Sleep(500);
             }
             else                 
               break;
             retries--;
           }//end while retries
         }
       }
     }//end while CountBuyOrSellOrders
   }
  }

void WriteComment()
  {
   string sText;
   
   sText = "";
   sText = StringConcatenate( sText, "GMT Offset : ", GMTOffset, "\n" );
   sText = StringConcatenate( sText, "Trade Start(Server Time) : ", ServerTradeStartHour, "\n" );
   sText = StringConcatenate( sText, "Trade End(Server Time) : ", ServerTradeEndHour, "\n" );
   if( ( ServerTradeStartHour < ServerTradeEndHour && Hour() >= ServerTradeStartHour && Hour() < ServerTradeEndHour )
   || ( ServerTradeStartHour > ServerTradeEndHour && ( Hour() >= ServerTradeStartHour || Hour() < ServerTradeEndHour ) ))
     sText = StringConcatenate( sText, "ooooo TRADE HOUR ooooo\n" );
   else
     sText = StringConcatenate( sText, "xxxxx NOT TRADE HOUR xxxxx\n" );
   sText = StringConcatenate( sText, "Lots : ", DoubleToStr( Lots, 2), "\n" );
   sText = StringConcatenate( sText, "TakeProfit1 : ", TakeProfit1, "\n" );
   sText = StringConcatenate( sText, "TrailingStop1 : ", TrailingStop1, "\n" );
   sText = StringConcatenate( sText, "TakeProfit2 : ", TakeProfit2, "\n" );
   sText = StringConcatenate( sText, "TrailingStop2 : ", TrailingStop2, "\n" );
   sText = StringConcatenate( sText, "StopLoss : ", StopLoss, "\n" );
   sText = StringConcatenate( sText, "PendingPips : ", PendingPips, "\n" );
   sText = StringConcatenate( sText, "ExpireCandles : ", ExpireCandles, "\n" );
   sText = StringConcatenate( sText, "DelayOrder2 : ", DelayOrder2, "\n" );
   sText = StringConcatenate( sText, "CheckSlope : " );
   if( CheckSlope ) sText = StringConcatenate( sText, "true\n" );
   else sText = StringConcatenate( sText, "false\n" );
   sText = StringConcatenate( sText, "DSS(", DSS_EMA, ",", DSS_stoch, ") : " );
   if( dss1 > dss2 ) sText = StringConcatenate( sText, "Up\n" );
   if( dss1 < dss2 ) sText = StringConcatenate( sText, "Down\n" );
   if( dss1 == dss2 ) sText = StringConcatenate( sText, "Flat\n" );
   if( Use_T3MA )
   {
     sText = StringConcatenate( sText, "use T3MA(", T3MA_Periods, ",", DoubleToStr( T3MA_VolumeFactor, 1), ") : " );
     if( t3ma1 > t3ma2 ) sText = StringConcatenate( sText, "Up\n" );
     if( t3ma1 < t3ma2 ) sText = StringConcatenate( sText, "Down\n" );
     if( t3ma1 == t3ma2 ) sText = StringConcatenate( sText, "Flat\n" );
   }
   else
   {
     sText = StringConcatenate( sText, "uee T3MA : false\n" );
   }
   if( Bid > iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_HIGH, 1) )
     sText = StringConcatenate( sText, "Price higher than ", Channel_EMAPeriod, "EMA High\n" );
   if( Bid < iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_LOW, 1) )
     sText = StringConcatenate( sText, "Price lower than ", Channel_EMAPeriod, "EMA Low\n" );
   if( Bid >= iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_LOW, 1)
    && Bid <= iMA(NULL, 0, Channel_EMAPeriod, Channel_EMAShift, MODE_EMA, PRICE_HIGH, 1) )
     sText = StringConcatenate( sText, "Price between ", Channel_EMAPeriod, "EMA High and Low\n" );
   
   {
    
  
   {
     sText = StringConcatenate( sText, "use ma1 : false\n" );
   }
   
  
   {
     sText = StringConcatenate( sText, "use ma2 : false\n" );
   }
   
   }
   
   {
     sText = StringConcatenate( sText, "use ma3 : false\n" );
   }
  

  

  }



//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   CheckDSS();
   CheckT3MA();
   if( (( ServerTradeStartHour < ServerTradeEndHour && Hour() >= ServerTradeStartHour && Hour() < ServerTradeEndHour )
   || ( ServerTradeStartHour > ServerTradeEndHour && ( Hour() >= ServerTradeStartHour || Hour() < ServerTradeEndHour ))
   || ServerTradeStartHour == ServerTradeEndHour )
   && ( CountOpenOrders() == 0 && prevBarTime != Time[0] ) ) CheckForOpen();
   
   if( CountPendingOrders(2) > 0 ) order1Pending = true;
   if( CountPendingOrders(2) == 0 )
   {
     if( order1Pending == true )
     {
       if( CountBuyOrSellOrders(OP_BUY) > 0 )
       {
         order1StartTime = TimeCurrent();
         order2BuyStart = true;
       }
       if( CountBuyOrSellOrders(OP_SELL) > 0 )
       {
         order1StartTime = TimeCurrent();
         order2SellStart = true;
       }
     }
     order1Pending = false;
   }
   
   if( order2BuyStart == true) Order2Buy();
   if( order2SellStart == true ) Order2Sell();
   
   if( CountOpenOrders() >= 1)
   {
     CheckForMovingStopLoss();
     TrailingStop();
     if( prevBarTime != Time[0] ) CheckForClose();
   }

   if( ShowComment ) WriteComment();
   
   prevBarTime = Time[0];
//----
   return(0);
  }
//+------------------------------------------------------------------+