Download MetaTrader 5
To add comments, please log in or register
1000 developers are involved in Freelance service activity. Sign up!
Patricia_T
268
Patricia_T 2014.09.07 20:43 
int init()
  {
//----
         ticketOpp=-1;
        if (Point == 0.00001 || Point == 0.001)
           pip = Point*10;
           else pip = Point;
   if (Digits==3 || Digits==5) slipFactor=10; else slipFactor=1;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if(OpenOppositeOrder && ticketOpp>0) CheckTicket();
   
   if(OpenOrdersThisPair(Symbol())>=1)
   {
      CheckForProfitLock();
      TrailStops(TrailSLPips);
  {
     if (UseServerTime){
      datetime time = TimeCurrent();
   } else {
      time = TimeLocal();
  }
  
   } 
    if (OrderSymbol()==Symbol() && OrderMagicNumber () == MagicNumber)
   {double profit = OrderProfit();}
   }
Keith Watford
Moderator
8016
Keith Watford 2014.09.07 23:26  
There is no OrderSend() in your code - How do you expect it to open a trade??
Patricia_T
268
Patricia_T 2014.09.08 00:05  
//--- input parameters
input int       StopLoss=50;
input int       TakeProfit=100;
input int       MoveToBE=25;
input bool      TrailStops=false;
input int       TrailSLPips=15;
input bool      SL_Indi=true;
input bool      InInterval=true;
input int       StartHour1=8;
input int       EndHour1=11;
input int       StartHour2=8;
input int       EndHour2=11;
input int       CloseTradesHour=17;
input bool      UseServerTime=true;
input bool      CheckForProfitLock=false;
input int       LockProfitLevel_1=100;
input int       LockProfitClosePercentage_1=25;
input int       LockProfitPips_1=25;
input int       LockProfitLevel_2=200;
input int       LockProfitClosePercentage_2=33;
input int       LockProfitPips_2=150;
input int       LockProfitLevel_3=300;
input int       LockProfitClosePercentage_3=50;
input int       LockProfitPips_3=275;
input bool      Use_Static_Lot=true;
input double    Risk=10;
input double    Static_Lot=0.10;
input bool      OpenOppositeOrder=false;
input int       MagicNumber = 4325;
input int       Slippage = 3;

int rslt;
int ticketOpp;
double pip;
int slipFactor;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
         ticketOpp=-1;
        if (Point == 0.00001 || Point == 0.001)
           pip = Point*10;
           else pip = Point;
   if (Digits==3 || Digits==5) slipFactor=10; else slipFactor=1;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
    Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if(OpenOppositeOrder && ticketOpp>0) CheckTicket();
   
   if(OpenOrdersThisPair(Symbol())>=1)
   {
      CheckForProfitLock();
      TrailStops(TrailSLPips);
  {
     if (UseServerTime){
      datetime time = TimeCurrent();
   } else {
      time = TimeLocal();
  }
  
   } 
    if (OrderSymbol()==Symbol() && OrderMagicNumber () == MagicNumber)
   {double profit = OrderProfit();}
   }
   
   
   
    if (SL_Indi)
    {
      
      double rsi = iRSI(NULL,0,9,PRICE_CLOSE,0);
      
     if(OrderType() == OP_BUY)
     if(rsi>=70)
     CloseAllOrders();
    
     
    
     if(OrderType() == OP_SELL)
     if(rsi<=30)
     CloseAllOrders();
      
    } 
  
  
  
  
  
  
  
   if(IsNewCandle() && (InInterval(time, StartHour1, 0, EndHour1, 0) || InInterval(time, StartHour2, 0, EndHour2, 0)))
   {
      double high = iHighest(NULL,0,MODE_CLOSE,5,0);
      double prevhigh = iHighest(NULL,0,MODE_CLOSE,5,6);
      double low = iLowest(NULL,0,MODE_CLOSE,5,0);
      double prevlow = iLowest(NULL,0,MODE_CLOSE,5,6);
      double RSI = iRSI(NULL,0,9,PRICE_CLOSE,1); 
      double prevRSI = iRSI(NULL,0,9,PRICE_CLOSE,6);
      double nowRSI = iRSI(NULL,0,9,PRICE_CLOSE,0); 
     
      if(OpenOrdersThisPair(Symbol()) == 0)
     
      if ( low < prevlow && RSI > prevRSI && nowRSI < 70 ) {
       OpenBuy(); 
      }
      else if (high > prevhigh && RSI < prevRSI && nowRSI > 30 ) {
        OpenSell();
      }
    }
      
      if (InInterval(time, CloseTradesHour, 0, CloseTradesHour+1, 0)){
         CloseAllOrders();
      }
//----
   return(0);
  }


//+------------------------------------------------------------------+
//checks to see if any orders open on this currency pair.
//+------------------------------------------------------------------+
int OpenOrdersThisPair(string pair, int type=-1)
{
   int total=0;
   for(int i=OrdersTotal()-1; i >= 0; i--)
   {
        if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
        if(OrderSymbol() == pair && OrderMagicNumber() == MagicNumber) 
           if(OrderType()==type || type==-1) total++;
   }
   return (total);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool OpenBuy()
{
   if(OpenOrdersThisPair(Symbol(),OP_BUY)==0){
      double bsl=0;
      double btp=0;
      double  Lot;
      if(StopLoss!=0)bsl=Ask-(StopLoss*pip);
      if(TakeProfit!=0)btp=Ask+(TakeProfit*pip);
      Lot = LOT(Risk,OrdersTotal()+1);
      if(Lot==0.0) return(false);
      int buyticket = OrderSend(Symbol(),OP_BUY,Lot,Ask,3,0,0,"",MagicNumber,0,Green);
      if(buyticket>0)
      {
         ticketOpp=buyticket;
         return (OrderModify(buyticket,OrderOpenPrice(),bsl,btp,0,CLR_NONE));
      }
   }
   return(false);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool OpenSell()
{
   if(OpenOrdersThisPair(Symbol(),OP_SELL)==0){
      double ssl=0;
      double stp=0;
      double  Lot;
      if(StopLoss!=0)ssl=Bid+(StopLoss*pip);
      if(TakeProfit!=0)stp=Bid-(TakeProfit*pip);
      Lot = LOT(Risk,OrdersTotal()+1);
      if(Lot==0.0) return(false);
      int sellticket = OrderSend(Symbol(),OP_SELL,Lot,Bid,3,0,0,"",MagicNumber,0,Red);
      if(sellticket>0)
      {
         ticketOpp=sellticket;
         return (OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,CLR_NONE));
      }
      
   }
   return(false);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double  LOTS;

double LOT(int risk,int ord)
{  
if (Use_Static_Lot==true&&Static_Lot>=MarketInfo(Symbol(),MODE_MINLOT))LOTS=Static_Lot;
  else if(Use_Static_Lot==false&& (risk>0))  {
  
   double MINLOT = MarketInfo(Symbol(),MODE_MINLOT);
   LOTS = AccountFreeMargin()*risk/100/MarketInfo(Symbol(),MODE_MARGINREQUIRED)/ord;
  
   if (LOTS>MarketInfo(Symbol(),MODE_MAXLOT)) LOTS = MarketInfo(Symbol(),MODE_MAXLOT);
   if (LOTS<MINLOT) LOTS = MINLOT;
   if (MINLOT<0.1) LOTS = NormalizeDouble(LOTS,2); else LOTS = NormalizeDouble(LOTS,1);
   
}

   if(MarketInfo(Symbol(),MODE_MARGINREQUIRED)*LOTS>AccountFreeMargin()) LOTS=0.0;
   
return(LOTS);
   
}
//===================================================================================================
//|                                                                  |
//+------------------------------------------------------------------+

bool IsNewCandle()
{
   static datetime timeLastCandle=0;
        if (Time[0] == timeLastCandle)
      return (false);
        timeLastCandle = Time[0];
           return(true);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckForProfitLock(){
  if(CheckForProfitLock)
  for(int i=OrdersTotal()-1; i >= 0; i--)
   {
        if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
        if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber){
           if(OrderType() == OP_BUY){
              double buySL;
              double closePercentage;
              if (Bid >= NormalizeDouble(OrderOpenPrice() + MoveToBE * pip, Digits)){
                 buySL = OrderOpenPrice();
                 closePercentage = 0;
              }
              if (Bid >= NormalizeDouble(OrderOpenPrice() + LockProfitLevel_1 * pip,Digits)){
                 buySL = NormalizeDouble(OrderOpenPrice() + pip * LockProfitPips_1, Digits);
                 closePercentage = LockProfitClosePercentage_1;
              }
              if (Bid >= NormalizeDouble(OrderOpenPrice() + LockProfitLevel_2 * pip, Digits)){
                 buySL = NormalizeDouble(OrderOpenPrice() + pip * LockProfitPips_2, Digits);
                 closePercentage = LockProfitClosePercentage_2;
              }   
              if (Bid >= NormalizeDouble(OrderOpenPrice() + LockProfitLevel_3 * pip, Digits)){
                 buySL = NormalizeDouble(OrderOpenPrice() + pip * LockProfitPips_3, Digits);
                 closePercentage = LockProfitClosePercentage_3;
              }   
              if (buySL > OrderStopLoss()){
                 rslt=OrderModify(OrderTicket(),OrderOpenPrice(),buySL,OrderTakeProfit(),0);
                 double lotsToClose = LotSizePercentageNormalized(OrderLots(),closePercentage,false);
                 if (lotsToClose > 0){
                    rslt=OrderClose(OrderTicket(),lotsToClose,Bid,Slippage);
                 }
              }
           }
           if(OrderType() == OP_SELL){
              double sellSL;
              if (Ask <= NormalizeDouble(OrderOpenPrice() - MoveToBE * pip, Digits)){
                 sellSL = OrderOpenPrice();
                 closePercentage = 0;
              }
              if (Ask <= NormalizeDouble(OrderOpenPrice() - LockProfitLevel_1 * pip,Digits)){
                 sellSL = NormalizeDouble(OrderOpenPrice() - pip * LockProfitPips_1, Digits);
                 closePercentage = LockProfitClosePercentage_1;
              }
              if (Ask <= NormalizeDouble(OrderOpenPrice() - LockProfitLevel_2 * pip, Digits)){
                 sellSL = NormalizeDouble(OrderOpenPrice() - pip * LockProfitPips_2, Digits);
                 closePercentage = LockProfitClosePercentage_2;
              }   
              if (Ask <= NormalizeDouble(OrderOpenPrice() - LockProfitLevel_3 * pip, Digits)){
                 sellSL = NormalizeDouble(OrderOpenPrice() - pip * LockProfitPips_3, Digits);
                 closePercentage = LockProfitClosePercentage_3;
              }   
              if (sellSL < OrderStopLoss() && sellSL != 0){
                 rslt=OrderModify(OrderTicket(),OrderOpenPrice(),sellSL,OrderTakeProfit(),0);
                 lotsToClose = LotSizePercentageNormalized(OrderLots(),closePercentage,false);
                 if (lotsToClose > 0){
                    rslt=OrderClose(OrderTicket(),lotsToClose,Ask,Slippage);
                 }
              }
           }
        }
   }
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double LotSizePercentageNormalized(double lotSize, double percentage, bool allowRoundUp=true)
{
   double step=MarketInfo(Symbol(), MODE_LOTSTEP);
   double minLot=MarketInfo(Symbol(), MODE_MINLOT);
   
   double shouldBe=MathRound((lotSize*percentage/100)/step)*step;
   
   double extra=shouldBe - minLot;
   if (extra<0) extra=0;
   
   if (!allowRoundUp && extra + minLot > shouldBe){
      if (extra + minLot - step >= minLot && extra + minLot - step <= shouldBe){
         return (extra + minLot - step);
      } else {
         return (0);
      }
   } else {
      return (extra+minLot);
   }
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void TrailStops(int trailPips){  
   if(TrailStops)
   for(int i=OrdersTotal()-1; i >= 0; i--)
   {
        int tries=0;
        if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))continue;
        if(OrderMagicNumber() == MagicNumber){
            string pair = OrderSymbol();
            double point=MarketInfo(pair,MODE_POINT);
            int digits=MarketInfo(pair,MODE_DIGITS);
            double bid=MarketInfo(pair,MODE_BID);
            double ask=MarketInfo(pair,MODE_ASK);
            double pip=point*slipFactor;
            
            double slBuy = bid-trailPips*pip;
            double slSell = ask+trailPips*pip;
                  
              if (OrderType()==OP_BUY){
                  while (tries<10){
                 
                 
                     RefreshRates();
                     if (slBuy!=0 && slBuy>MarketInfo(pair,MODE_BID)-MarketInfo(pair,MODE_STOPLEVEL)*point) slBuy=MarketInfo(pair,MODE_BID)-MarketInfo(pair,MODE_STOPLEVEL)*point;
                   
                     if (slBuy<=OrderStopLoss() && OrderStopLoss()!=0) slBuy=OrderStopLoss();
                     if (slBuy!=OrderStopLoss()){
                        if (OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(slBuy,digits),OrderTakeProfit(),0)) 
                           break;
                     } else {
                        break;
                     }
                     tries++;
                  }
              }  if(OrderType()==OP_SELL){
              string sl = slSell;
                  while (tries<10){
                  
                      RefreshRates();
                   
                      if (slSell!=0 && slSell<MarketInfo(pair,MODE_ASK)+MarketInfo(pair,MODE_STOPLEVEL)*point) slSell=MarketInfo(pair,MODE_ASK)+MarketInfo(pair,MODE_STOPLEVEL)*point;
            
                      if (slSell>=OrderStopLoss() && OrderStopLoss()!=0) slSell=OrderStopLoss();
                      if (slSell!=OrderStopLoss()){
                         if (OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(slSell,digits),OrderTakeProfit(),0)) break;
                      } else {
                         break;
                      }
                      tries++;
                   } 
              }
        }   
   }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckTicket()
{
   int ticket,type;
   datetime dtc1;
   double profit1;
   bool slt;
   
   dtc1=0;
   ticket=-1;
   profit1=0.0;
   
   slt=OrderSelect(ticketOpp,SELECT_BY_TICKET,MODE_HISTORY);   dtc1=OrderCloseTime();
   if (dtc1<=0) return;
   if(OrderProfit()>=0.0)
   {
      ticketOpp=-1;
      return;
   }
   
   type = OP_BUY;
   double ssl=0;
   double stp=0;
   double  Lot;
   double dir=1.0;
   double price=Ask;
   color clr=clrGreen;
   
   if(OrderType()==OP_BUY) { dir=-1.0;   price=Bid;   type=OP_SELL;   clr=clrRed; }
   if(StopLoss!=0)ssl=price-dir*(StopLoss*pip);
   if(TakeProfit!=0)stp=price+dir*(TakeProfit*pip);
   Lot = LOT(Risk,OrdersTotal()+1);
   if(Lot==0.0) return;
   ticket = OrderSend(Symbol(),type,Lot,price,3,0,0,"Opposite Order",MagicNumber,0,clr);
   if(ticket>0)
   {
      ticketOpp=-1;
      rslt=OrderModify(ticket,OrderOpenPrice(),ssl,stp,0,CLR_NONE);
   }
   
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void WaitTradeContext(){
   datetime tstart = TimeLocal();
   Print("enter WaitTradeContext");
   while (!IsTradeAllowed() && TimeLocal()-tstart<60){
      Sleep(100);
   }
   if (!IsTradeAllowed()){
      Print("Trade not allowed");
   }else{
      Print("Trade allowed!");
   }
   
   while (IsTradeContextBusy() && TimeLocal()-tstart<60){
      Sleep(100);
   }
   if (IsTradeContextBusy()){
      Print("Trade context busy");
   }else{
      Print("Trade context free!");
   }
   Print("leave WaitTradeContext");
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+


bool InInterval(datetime time, int beginHour, int beginMinute, int endHour, int endMinute)
{
  if(   (TimeHour(time) > beginHour || ( TimeHour(time) == beginHour && TimeMinute(time) >= beginMinute ))
   && (TimeHour(time) < endHour   || ( TimeHour(time) == endHour   && TimeMinute(time) <= endMinute   ))  )
  {
   return (true);
  }
  else
  {
   return (false);
  }
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseAllOrders(){
for(int b=OrdersTotal()-1; b >= 0; b--)
     
     
     {
        if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
           if(OrderMagicNumber()==MagicNumber)
              if(OrderSymbol()==Symbol()){
                 if(OrderType()==OP_BUY){
                   rslt=OrderClose(OrderTicket(),OrderLots(),Bid,3,Lime);
                 }
                 if(OrderType()==OP_SELL){
                   rslt=OrderClose(OrderTicket(),OrderLots(),Ask,3,Lime);
                 }
              }
     }         
}
//+------------------------------------------------
Keith Watford
Moderator
8016
Keith Watford 2014.09.08 00:26  

Did you mean this

double high = iHighest(NULL,0,MODE_CLOSE,5,0);

 

 or

double high = iHigh(NULL,0,iHighest(NULL,0,MODE_CLOSE,5,0));
Patricia_T
268
Patricia_T 2014.09.08 00:59  
thank you for helping me,  I changed it.  still not working.

 

 

 

 

ffoorr
985
ffoorr 2014.09.08 11:19  
  if (OrderSymbol()==Symbol() && OrderMagicNumber () == MagicNumber)
   {double profit = OrderProfit();}

OrderSelect is missing , you will have to do again all the code with OrderSelect, -> select what is needed, -> store it in variables, -> close OrderSelect. -> review variable

 

ffoorr
985
ffoorr 2014.09.08 11:21  

This one code do work, it draw arrow on chart, and do open orders buy :


 // if(IsNewCandle() && (InInterval(time, StartHour1, 0, EndHour1, 0) || InInterval(time, StartHour2, 0, EndHour2, 0)))
  // {
     
      double high = High[iHighest(NULL,0,MODE_CLOSE,5,0)];
      double prevhigh = High[iHighest(NULL,0,MODE_CLOSE,5,6)];
      double low = Low[ iLowest(NULL,0,MODE_CLOSE,5,0)];
      double prevlow =Low[ iLowest(NULL,0,MODE_CLOSE,5,6)];
      double RSI = iRSI(NULL,0,9,PRICE_CLOSE,1); 
      double prevRSI = iRSI(NULL,0,9,PRICE_CLOSE,6);
      double nowRSI = iRSI(NULL,0,9,PRICE_CLOSE,0); 
     
    //  if(OpenOrdersThisPair(Symbol()) == 0)
     
      if ( low < prevlow && RSI > prevRSI && nowRSI < 70 )
       {
       arrow(0);
       OpenBuy(); 
       }
      else if (high > prevhigh && RSI < prevRSI && nowRSI > 30 ) {
        OpenSell();
      }
  //  }
Patricia_T
268
Patricia_T 2014.09.08 11:42  
Oh dear   OK  Thank you so much
/
To add comments, please log in or register