input int MagicNumber=1982;  //Magic Number
input double Lots=0.1;      //Fixed Lots
input double StopLoss=50;    //Fixed Stop Loss (in Points)
input double TakeProfit=150; //Fixed Take Profit (in Points)
input int TrailingStop=15;   //Trailing Stop (in Points)
input int Slippage=3;
//+------------------------------------------------------------------+
//|   expert OnTick function                                         |
//+------------------------------------------------------------------+
void OnTick()
 {
   double MyPoint=Point;
   if(Digits==3 || Digits==5) MyPoint=Point*10;
   double TheStopLoss=0;
   double TheTakeProfit=0;
   if(TotalOrdersCount()==0)
     {
      int result=0;
      if((iRSI(NULL,PERIOD_H1,14,PRICE_CLOSE,0)<50) && (iRSI(NULL,PERIOD_H4,14,PRICE_CLOSE,0)<50) && (iRSI(NULL,PERIOD_D1,14,PRICE_CLOSE,0)<50)) // Here is your open buy rule
        {
         result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage  ,0,0,"RAMI V2_5M BUY AUTO",MagicNumber,0,Blue);
         if(result>0)
           {
            TheStopLoss=0;
            TheTakeProfit=0;
            if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
            if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
            int MyOrderSelect=OrderSelect(result,SELECT_BY_TICKET)  ;
            int MyOrderModify=OrderModify(OrderTicket(),OrderOpenP  rice(),NormalizeDouble(TheStopLoss,Digits),Normali  zeDouble(TheTakeProfit,Digits),0,Green);
           }
        }
      if((iRSI(NULL,PERIOD_H1,14,PRICE_CLOSE,0)>50) && (iRSI(NULL,PERIOD_H4,14,PRICE_CLOSE,0)>50) && (iRSI(NULL,PERIOD_D1,14,PRICE_CLOSE,0)>50)) // Here is your open buy rule
        {
         result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippag  e,0,0,"RAMI V2_5M SELL AUTO",MagicNumber,0,Red);
         if(result>0)
           {
            TheStopLoss=0;
            TheTakeProfit=0;
            if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
            if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
            int MyOrderSelect=OrderSelect(result,SELECT_BY_TICKET)  ;
            int MyOrderModify=OrderModify(OrderTicket(),OrderOpenP  rice(),NormalizeDouble(TheStopLoss,Digits),Normali  zeDouble(TheTakeProfit,Digits),0,Green);
           }
        }
     }
   for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      int MyOrderSelect=OrderSelect(cnt,SELECT_BY_POS,MODE_T  RADES);
      if(OrderType()<=OP_SELL && 
         OrderSymbol()==Symbol() && 
         OrderMagicNumber()==MagicNumber
         )
         if(OrderType()==OP_BUY)
           {
            if((iRSI(NULL,0,14,PRICE_CLOSE,0)>800)) //here is the close buy condition
              {
               int MyOrderClose=OrderClose(OrderTicket(),OrderLots(),  OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     int MyOrderModify=OrderModify(OrderTicket(),OrderOpenP  rice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                    }
                 }
              }
           }
         else
           {
            if((iRSI(NULL,0,14,PRICE_CLOSE,0)<-800)) // here is the close sell condition
              {
               int MyOrderClose=OrderClose(OrderTicket(),OrderLots(),  OrderClosePrice(),Slippage,Red);
              }
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     int MyOrderModify=OrderModify(OrderTicket(),OrderOpenP  rice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),  0,Red);
                    }
                 }
              }
           }
        }
     }
//+------------------------------------------------------------------+
//|   expert TotalOrdersCount function                               |
//+------------------------------------------------------------------+
int TotalOrdersCount()
  {
   int result=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      int MyOrderSelect=OrderSelect(i,SELECT_BY_POS,MODE_TRA  DES);
      if(OrderMagicNumber()==MagicNumber) result++;
     }
   return (result);
  }
  
//+------------------------------------------------------------------+
//|   expert Start function                               
//+------------------------------------------------------------------+
void drawshap(string name,color LineColor,double Price1,double Price2,datetime Time1,datetime Time2) 
    { 
     if(ObjectFind(name) == -1) 
        { 
         ObjectCreate(name, OBJ_RECTANGLE, 0, Time1,Price1,Time2,Price2); 
         ObjectSet(name, OBJPROP_COLOR, LineColor); 
         ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID); 
          ObjectSet(name, OBJPROP_WIDTH, 2); 
           } 
         else if(ObjectGet(name,OBJPROP_TIME1)!=Time1||ObjectGet  (name,OBJPROP_PRICE1)!=Price1){ 
         ObjectDelete(name); 
          } 
         }  
         
  
      
      
      
    
      
//+------------------------------------------------------------------+
double lastOpenPrice(){
   datetime d =0;
  double xt =0;
  int total  = OrdersTotal();
 
  for (int cnt = 0 ; cnt <= total ; cnt++)
  {
    if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
    if (OrderSymbol()==Symbol()&&OrderMagicNumber()==Magi  cNumber)
    {
    if (OrderOpenTime()>d) {d=OrderOpenTime(); xt=OrderOpenPrice();}
    }
  }
  return(xt);
}