Simple EA doesn't make a trade

 

Hi everyone, I am just beginning to learn how to code my own EA.

The idea of the EA is very simple, it is just a London breakout.

I want to check the highest and low of candles between 05:00 - 08:00 GMT+1 (my local time), and place 1 buy and sell stop on either side of this range.

For now I am just coding it in baby steps to ensure that everything works as it should.

I have started with just coding the time entry so that my EA will place a buystop 50 points over the current ask when the time hits 08:00 local time.

Later I will change so that the price at which the stops should be placed is based on the highest and lowest price in the defined range, but for now I just want to make sure I can even place a buystop based on my local time.

My code looks like this

#include<Trade\Trade.mqh>
CTrade   trade;

void OnTick()
  {	
     double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);		//Get price for buystop
     datetime time=TimeLocal();								//Get local time
     string hoursAndMinutes=TimeToString(time,TIME_MINUTES);				//Convert it into HH:MM format
     if ((PositionsTotal()==0)&&(StringSubstr(hoursAndMinutes,0,5)=="08:15"))		//If I have no positions and the time is 08:15
     {
        trade.BuyStop(0.10,Ask+50*_Point,0,0,ORDER_TIME_GTC,0,0);			//Open a buy stop
     }
     
     Comment(hoursAndMinutes);  							//Show the current local time as a comment
  }

It compiles with no errors but when I test it in the strategy tester nothing happens.


Any ideas?

 
  1. During testing in the strategy tester, TimeLocal() is always equal to TimeCurrent() simulated server time
              Date and Time / TimeLocal - Reference on algorithmic/automated trading language for MetaTrader 5

  2. Use the debugger or print out your variables, including _LastError and find out why.
 
whroeder1:
  1. Use the debugger or print out your variables, including _LastError and find out why.

Thank you for the quick reply, it is much apprecieated!


1. This shouldnt be a problem then since the EA should still place a buystop at 08:15 except it is just server time instead of my local time.

2. Do you mean I should just add this line of code?

int GetLastError = _LastError;
Print(GetLastError);

At the end of my code?


The debugger doesn't write anything, and nothing pops up when I add this code aswell.

 
niklasnegri:

Thank you for the quick reply, it is much apprecieated!


1. This shouldnt be a problem then since the EA should still place a buystop at 08:15 except it is just server time instead of my local time.

2. Do you mean I should just add this line of code?

At the end of my code?


The debugger doesn't write anything, and nothing pops up when I add this code aswell.


GetLastError is a function

Print(GetLastError());
 
nicholishen:

GetLastError is a function


Thank you, I still get no errors printed or anything.

When I run it in the strategy tester it just runs through the time specified and then exits.

This is what the journal says

"2017.12.06 16:15:58.098 Core 1 agent process started

2017.12.06 16:15:58.546 Core 1 connecting to 127.0.0.1:3000

2017.12.06 16:15:58.547 Core 1 connected

2017.12.06 16:15:58.554 Core 1 authorized (agent build 1690)

2017.12.06 16:15:58.558 Tester EURUSD,M15 (MetaQuotes-Demo): visual testing of Experts\SimpleBuyStop.ex5 from 2017.12.01 00:00 to 2017.12.05 00:00

2017.12.06 16:15:58.685 Core 1 common synchronization completed

2017.12.06 16:15:59.011 Tester quality of analyzed history is 43%

2017.12.06 16:16:13.929 Core 1 connection closed"


I have also tested running already coded EAs through it and they work so its nothing wrong with the automation of the EA or the strategy tester.

 
niklasnegri:

Thank you, I still get no errors printed or anything.

When I run it in the strategy tester it just runs through the time specified and then exits.

This is what the journal says

[...]

It appears you are using MT5.

So, check the [Journal] tab on the Strategy Tester Visualization window, not on the main MetaTrader window.

 
Anthony Garot:

It appears you are using MT5.

So, check the [Journal] tab on the Strategy Tester Visualization window, not on the main MetaTrader window.


Thank you, for some reason my strategy tester visualization window hadnt enabled the toolbox window so I was under the impression that the main MT5 window journal tab was showing the correct information.

Now I can clearly see the reason why which is


"2017.12.06 17:40:01.423 2017.12.01 08:15:00   CTrade::OrderOpen: Error: Unable to place order without explicitly specified expiration time"


I will try to crack this nut and come back if I need more help, thanks for everything in the mean time!

 

Okay so I have found out how to place a pending buy stop order. Now I just need to base the price on the highest high and lowest low of the range between 05:00 - 08:00.

This is my code so far.


#include<Trade\Trade.mqh>
CTrade   trade;

void OnTick()
   {
   //Get local time
   datetime time=TimeLocal();
   
   //Convert it into HH:MM format                                                               
   string hoursAndMinutes=TimeToString(time,TIME_MINUTES);
        
   //If I have no positions and the time is 08:15                       
   if ((OrdersTotal()==0)&&(StringSubstr(hoursAndMinutes,0,5)=="08:15"))                
   {
      //Create variable to store information of which candles are the highest and lowest in range
      int HighestCandle,LowestCandle;
      
      //Create array for highest and lowest candle
      double High[],Low[];
      
      //Sort array so they count backwards from current candle
      ArraySetAsSeries(High,true);
      ArraySetAsSeries(Low,true);
      
      //Fill array with data from candles
      //If the time is 08:15 and I dont want to use the 08:15 candle I start the count from candle 1 which is the 08:00 candle
      //I then need to count backwards from 08:00 to 05:00
      //08:00 = 1, 07:45 = 2, 07:30 = 3, 07:15 = 4, 07:00 = 5, 06:45 = 6, 06:30 = 7, 06:15 = 8, 06:00 = 9, 05:45 = 10
      //05:30 = 11, 05:15 = 12, 05:00 = 13      
      CopyHigh(_Symbol,PERIOD_M15,1,13,High);
      CopyLow(_Symbol,PERIOD_M15,1,13,Low);
      
      //Fill HighestCandle variable with data of the highest high in the array
      HighestCandle= ArrayMaximum(High,1,13);
      
      //Fill LowestCandle variable with data of the lowest low in the array
      LowestCandle= ArrayMinimum(Low,1,13);
      
      //Now that I know which number candle is the highest and the lowest I create another array to store the actual prices of those candles     
      MqlRates PriceInformation[];
      
      //Also set this array to countbackwards
      ArraySetAsSeries(PriceInformation,true);
      
      //Now I copy the data of the price from the same 13 candles
      int Data=CopyRates(_Symbol,PERIOD_M15,1,13,PriceInformation);
      
      //And print the prices of the highest and lowest candle
      Print(PriceInformation[HighestCandle].high);
      Print(PriceInformation[LowestCandle].low);      
      
     
   }
     
   }

My problem here is that it only counts the candles from 07:45 - 05:00 and leaves out the 08:00 candle, is my logic flawed somehow?

I have tried changing around with the numbers of candles that get counted.

For example if I change 

int Data=CopyRates(_Symbol,PERIOD_M15,1,13,PriceInformation);

to


int Data=CopyRates(_Symbol,PERIOD_M15,0,13,PriceInformation);

It now counts the 08:00 candle but leaves out the 05:00 candle and only counts from 08:00 - 05:15.


Any ideas?

 
niklasnegri:

My problem here is that it only counts the candles from 07:45 - 05:00 and leaves out the 08:00 candle, is my logic flawed somehow?

CopyHigh() and CopyLow() you grab 13 values into arrays. There's no problem starting at a shift of 1. That's all good.

But when you use ArrayMaximum() and ArrayMinimum() on those arrays, you want to look at every value stored in those arrays. Your starting value should be 0, e.g.

HighestCandle= ArrayMaximum(High,0,13);
 
Anthony Garot:

CopyHigh() and CopyLow() you grab 13 values into arrays. There's no problem starting at a shift of 1. That's all good.

But when you use ArrayMaximum() and ArrayMinimum() on those arrays, you want to look at every value stored in those arrays. Your starting value should be 0, e.g.

Ofcourse, makes perfect sense! Thank you!

I have now finished coding the entry and exit part of my EA and I am getting some pretty good results in backtesting with minimal optimization.

Now I have to start coding up my money/risk management and position sizing strategies.

But I am wondering if there is any good guides on how to scrutinize your system to minimize errors when you actually use the system?

Since I have very low experience in coding it seems strange to me that I could manage to code a system that could return up to 60% in a year this easily and have it actually work for real.

Reason: