Download MetaTrader 5

Need help to finish my EA

To add comments, please log in or register
Publish your article and thousands of people all over the world will read it!
mjstock78
62
mjstock78 2016.11.17 21:24 

I've created an EA which is supposed to open trades base on RSI values and should work on 15min time frame. But it doesn't recognize the 3AM candle on everyday so doesn't open any trade. I appreciate if someone kindly debug this code for me.

If the RSI is above 50 at 3:00AM ET (15Min. candle close), it should open a Pending Buy 10 PIPs above the High of past 40 candles, and if the RSI is below 50 at 3:00AM ET (15Min. candle close), it should open a Pending Sell 10 PIPs below the Low of past 40 candles. Also it should close the trade at 11 AM ET.

 Below is the code: 

#property copyright "MJ"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


input double Lots             =0.1;
input double TrailingStop     =0;
input int    RSIPeriod        =14;
input int    Shift            =0;
input int    Shifthours       =5;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   double Stop,Limit,boxhigh,boxlow;
   double RSImain;        //RSI value
   double MaxRisk=0.015;  //Maximum Risk = 150 PIPs

   int    cnt,ticket,total;
//---

//---
   if(Bars<100)
     {
      Print("bars less than 100");
      return;
     }
//   if(TakeProfit<10)
//     {
//      Print("TakeProfit less than 10");
//      return;
//     }
//--- to simplify the coding and speed up access data are put into internal variables
    RSImain=iRSI(NULL,PERIOD_M15,RSIPeriod,PRICE_CLOSE,1);

//--------------------------------------------------------------------------      
  
   total=OrdersTotal();
   if(total<1)
     {
      //--- no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ",AccountFreeMargin());
         return;
        }
      
      if(TimeHour(TimeCurrent())==3+Shifthours)
        { boxhigh=High[iHighest(NULL,PERIOD_M15,MODE_HIGH,41,1)];
         boxlow=Low[iLowest(NULL,PERIOD_M15,MODE_LOW,41,1)];
      
        
      //--- check for long position (BUY) possibility        
         if(MaxRisk>=boxhigh-boxlow+0.0010+0.0010 && RSImain>=50 )
        {
         Limit= boxhigh+0.0010+MaxRisk;
         Stop= boxlow-0.0010;
         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask,3,Bid-Stop*Point,Ask+Limit*Point," ",16003,0,Green);
        
         //Ask+TakeProfit*Point
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               { Print("BUY order opened : ",OrderOpenPrice());
                 ticket=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Stop*Point,Ask+Limit*Point,0,Yellow);
               }
           }
         else
            Print("Error opening BUY order : ",GetLastError());
         return;
        }
      //--- check for short position (SELL) possibility
         if(MaxRisk>=boxhigh-boxlow+0.0010+0.0010 && RSImain<50 )
        {
         Limit= boxlow-0.0010-MaxRisk;
         Stop= boxhigh+0.0010;
         ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid,3,Bid+Stop*Point,Bid-Limit*Point," ",16003,0,Red);
         //Bid-TakeProfit*Point        
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               { Print("SELL order opened : ",OrderOpenPrice());
                 ticket=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Stop*Point,Bid-Limit*Point,0,Yellow);
               }
           }
         else
            Print("Error opening SELL order : ",GetLastError());
        }
       }      
      //--- exit from the "no opened orders" block
      return;
     }
    
//--- it is important to enter the market correctly, but it is more important to exit it correctly...  
   for(cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderType()<=OP_SELL &&   // check for opened position
         OrderSymbol()==Symbol()&&   // check for symbol
         Hour()==11+Shifthours)     // Check if it is 11AM ET
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
               //--- close order and exit
               ticket=OrderModify(OrderTicket(),OrderOpenPrice(),0,MarketInfo(Symbol(),MODE_BID),0,Green);
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
           }
         else // go to short position
           {
               //--- close order and exit
               ticket=OrderModify(OrderTicket(),OrderOpenPrice(),0,MarketInfo(Symbol(),MODE_ASK),0,Red);
               //TakeProfit==(MarketInfo(Symbol(),MODE_ASK)-OrderOpenPrice());
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;

           }
        }
     }
//---
  }
//+------------------------------------------------------------------+


 

Keith Watford
Moderator
9603
Keith Watford 2016.11.17 22:20  

How do you know that it is not recognising the time?


Are you checking the log?


         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask,3,Bid-Stop*Point,Ask+Limit*Point," ",16003,0,Green);

You cannot usually open a BuyStop at the current price, your SL and TP make no sense at all, Stop and Limit are lkely to be 1.xxxx, why are you multiplinh them by Point?

mjstock78
62
mjstock78 2016.11.18 17:06  
Keith Watford:

How do you know that it is not recognising the time?


Are you checking the log?


         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask,3,Bid-Stop*Point,Ask+Limit*Point," ",16003,0,Green);

You cannot usually open a BuyStop at the current price, your SL and TP make no sense at all, Stop and Limit are lkely to be 1.xxxx, why are you multiplinh them by Point?

Hi Keith ,

Thanks for looking into my EA.

Back to your question, I didn't check the log but when I ran the EA, no order been opened.

About BuyStop, you're right, I forgot to add 10 PIPS to the current price. Also could you please amend the SL and TP the way you think is right?

Thanks. 

Keith Watford
Moderator
9603
Keith Watford 2016.11.18 20:38  
, it should open a Pending Buy 10 PIPs above the High of past 40 candles,



        { boxhigh=High[iHighest(NULL,PERIOD_M15,MODE_HIGH,41,1)];

That gets the high of the past 41 candles, not 40.



         //Limit= boxlow-0.0010-MaxRisk;     //Why are you deducting MaxRisk??????
         Limit= boxlow-0.0010;               //Only good for 4 or 5 digit Symbols
         Stop= boxhigh+0.0010;


I assume that your entry is intended to besomething like


         double TP = ??;
         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Limit,3,Stop,TP,NULL,16003,0,Green);


If the order is opened, why do youi then try to modify it with the same values?

mjstock78
62
mjstock78 2016.11.21 17:34  
Keith Watford:

        { boxhigh=High[iHighest(NULL,PERIOD_M15,MODE_HIGH,41,1)];

That gets the high of the past 41 candles, not 40. You're correct, that was a typo.



         //Limit= boxlow-0.0010-MaxRisk;     //Why are you deducting MaxRisk?????? Cause I would like to have 1 to 1 profit (TP=Risk) also I don't wanna risk more than 150 PIPs.
         Limit= boxlow-0.0010;               //Only good for 4 or 5 digit Symbols
         Stop= boxhigh+0.0010;


I assume that your entry is intended to besomething like


         double TP = ??;
         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Limit,3,Stop,TP,NULL,16003,0,Green);


If the order is opened, why do youi then try to modify it with the same values? I tried the syntax you are mentioning at first place but it didn't work, so I tried to modify the order with the same values.

Keith Watford
Moderator
9603
Keith Watford 2016.11.21 23:30  

I think that I made a typo


I assume that your entry is intended to besomething like


double TP = ??;
         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Limit,3,Stop,TP,NULL,16003,0,Green);

More likely

double TP = ??;
         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Stop,3,Limit,TP,NULL,16003,0,Green);

.
Keith Watford
Moderator
9603
Keith Watford 2016.11.21 23:34  

/Limit= boxlow-0.0010-MaxRisk;     //Why are you deducting MaxRisk?????? Cause I would like to have 1 to 1 profit (TP=Risk) also I don't wanna risk more than 150 PIPs.

By doing that you are risking more than 150 pips

mjstock78
62
mjstock78 2016.11.22 20:03  
Keith Watford:

/Limit= boxlow-0.0010-MaxRisk;     //Why are you deducting MaxRisk?????? Cause I would like to have 1 to 1 profit (TP=Risk) also I don't wanna risk more than 150 PIPs.

By doing that you are risking more than 150 pips

Keith, do you know why my EA doesn't recognize the 3:00 AM candle? 

I even inserted a line to draw a vertical line at 3:00 AM everyday, but the EA draws the line only at the first day 3:00AM.

 

if(TimeHour(iTime(NULL,PERIOD_M15,0))==3+Shifthours)
        {
         boxhigh=High[iHighest(NULL,PERIOD_M15,MODE_HIGH,41,1)];
         boxlow=Low[iLowest(NULL,PERIOD_M15,MODE_LOW,41,1)];
         ObjectCreate(0,"box",OBJ_VLINE,0,TimeCurrent(),boxhigh);
        }


 

honest_knave
Moderator
2159
honest_knave 2016.11.22 20:18  
mjstock78:

Keith, do you know why my EA doesn't recognize the 3:00 AM candle? 

I even inserted a line to draw a vertical line at 3:00 AM everyday, but the EA draws the line only at the first day 3:00AM.

 

if(TimeHour(iTime(NULL,PERIOD_M15,0))==3+Shifthours)
        {
         boxhigh=High[iHighest(NULL,PERIOD_M15,MODE_HIGH,41,1)];
         boxlow=Low[iLowest(NULL,PERIOD_M15,MODE_LOW,41,1)];
         ObjectCreate(0,"box",OBJ_VLINE,0,TimeCurrent(),boxhigh);
        }


 

I haven't looked through all your code (or indeed this thread) but:

1. Give each line you create a unique name. The first line will be called "box"... but that is the same name your code will try to use for subsequent lines too. I suggest adding the datetime into the name.

2. Will this code snippet be called more than once in an hour? If this code gets called every tick, think about how many times it is going to try to create a 3AM line.

Hope that helps 

mjstock78
62
mjstock78 2016.12.06 22:16  
Keith Watford:

/Limit= boxlow-0.0010-MaxRisk;     //Why are you deducting MaxRisk?????? Cause I would like to have 1 to 1 profit (TP=Risk) also I don't wanna risk more than 150 PIPs.

By doing that you are risking more than 150 pips

Hi Keith

Could you please do me a favor and give me a line of code which can find the 3AM candle (in 15 Min. time frame) for each day?

 

Thanks in advance. 

Keith Watford
Moderator
9603
Keith Watford 2016.12.10 01:56  

Globals


bool FirstTick;
string Name=WindowExpertName();


Init


   FirstTick=true;


DeInit


   ObjectsDeleteAll(0,Name);


OnCalculate/OnTick   note if in an EA there will be no lines displayed until a tick is received


   static datetime day_bar_time=0;
   if(FirstTick)
     {
      datetime today=iTime(Symbol(),PERIOD_D1,0);
      int bars15m=iBars(Symbol(),PERIOD_M15);
      datetime earliest_time=iTime(Symbol(),PERIOD_M15,bars15m-1);
      if(GetLastError()==4066)
         Sleep(15000);
      bars15m=iBars(Symbol(),PERIOD_M15);
      earliest_time=iTime(Symbol(),PERIOD_M15,bars15m-1);
      int x=0;
      while(true)
        {
         datetime threeAM=iTime(Symbol(),PERIOD_D1,x)+180*60;
         if(threeAM<earliest_time)
            break;
         ObjectCreate(0,Name+TimeToStr(threeAM),OBJ_VLINE,0,threeAM,0);
         x++;
        }
      day_bar_time=iTime(Symbol(),PERIOD_D1,0);
      FirstTick=false;
     }

   datetime today=iTime(Symbol(),PERIOD_D1,0);
   if(day_bar_time!=today)
     {
      day_bar_time=today;
      datetime threeAM=today+180*60;
      ObjectCreate(0,Name+TimeToStr(threeAM),OBJ_VLINE,0,threeAM,0);
     }


Hopefully that will give you some ideas to work with.
To add comments, please log in or register