Download MetaTrader 5
To add comments, please log in or register
Have you developed a profitable strategy? Launch your trading signal and make profit from it!
barrymet
17
barrymet 2014.04.02 08:03 

Hi People

I have MT4 build 625 and have written a small EA which checks for buy and sell signals and then once just one trade has been opened checks for a signal to close it. The EA is supposed to check for all of these signals once per Bar.

The problem is sometimes the EA responds to the signals and sometimes not ?? Could you please look over my code to see what may be the problem/s

void OnTick()
{

if(LastBarOpenAt == Time[0]) {IsNewBar=false;}
else {LastBarOpenAt = Time[0];IsNewBar = true;}

if (IsNewBar) {calculateIndicators();} /// indicators are evaluated 

// Control and open trades ///////////////////////////////////////////////     
RefreshRates(); 

///buy signal
if (OrdersTotal()==0 &&IsNewBar&& buysignal) {ticketbuy = OrderSend(Symbol(), OP_BUY, Lots, Ask, slippage, stoploss, takeprofit,nameEA, 0,0,clrBlue);  
if(ticketbuy < 0){Print("OrderSend (",nameEA,") failed with error #", GetLastError());return;}}

// sell signal 
if(OrdersTotal()==0 && IsNewBar && sellsignal) {ticketsell = OrderSend(Symbol(), OP_SELL, Lots, Bid, slippage, stoploss, takeprofit,nameEA, 0,0,clrRed); 
if(ticketsell < 0){Print("OrderSend (",nameEA,") failed with error #", GetLastError());return;}}


//If an order has been placed then call function 'closeorders' which is supposed to check to close an open order once per bar
if (OrdersTotal()>0 && IsNewBar) {closeorders();}
}

My 'closeorder' function is as follows

void closeorders()
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()==OP_SELL && closesell) {OrderClose(OrderTicket(),OrderLots(),Ask,slippage,clrRed);}
            if(OrderType()==OP_BUY && closebuy) {OrderClose(OrderTicket(),OrderLots(),Bid,slippage,clrBlue);}
}

Your help is greatly appreciated

Thanks

Barry

Tjipke de Vries
6276
Tjipke de Vries 2014.04.02 10:33  
if (OrdersTotal()==0 &&IsNewBar&& buysignal) 

if at first tick opening trade fails your code is not trying to open at next tick

Print("OrderSend (",nameEA,") failed with error #", GetLastError());return;

what did you try to open Sell or Buy and what errors do you get if it fails ??

if (OrdersTotal()>0 && IsNewBar) {closeorders();}  //placed after OrderSend

if at newbar a trade is open it has to close that trade first then there won't be done a OrderSend same bar

Alain Verleyen
Moderator
28542
Alain Verleyen 2014.04.02 10:36  
barrymet:

Hi People

I have MT4 build 625 and have written a small EA which checks for buy and sell signals and then once just one trade has been opened checks for a signal to close it. The EA is supposed to check for all of these signals once per Bar.

The problem is sometimes the EA responds to the signals and sometimes not ?? Could you please look over my code to see what may be the problem/s

My 'closeorder' function is as follows

Your help is greatly appreciated

Thanks

Barry

Welcome to mql4.com forum,

I suppose that you mean your EA doesn't open a trade when you got a buysignal or sellsignal ? But you don't show how these values are obtained, so it's difficult to help you. You have to print the value of buysignal/sellsignal and check it.

barrymet
17
barrymet 2014.04.02 12:59  

de Vries

I have never received any errors when trying to open a trade . The program either opens a trade or it doesn't sometimes. Same with closing, sometimes it closes and then sometimes it doesn't.

I get the feeling that it seems to be 'brushing' past some of the code ??

It is also not necessary for me to immediately open a new trade when I have just closed an open trade (buy or sell).

The presumed program flow is :

(1).On signal buy or sell if there are no open orders (2). If an order is open then close it when the correct signal is received. (3) Wait for next signal to buy/sell.

I do not have more than 1 trade open at a time .

angevoyageur

Thanks for input, as above sometimes a trade will go through correctly and then sometimes it doesn't . Thus I must presume that the correct value is being read, but sometimes MT4 ignores it.

Tjipke de Vries
6276
Tjipke de Vries 2014.04.02 13:50  
barrymet:

de Vries

I have never received any errors when trying to open a trade .

Do you look for Print output at your Terminal (Ctrl+T) ==>kolom Journal // Experts ??

The program either opens a trade or it doesn't sometimes. Same with closing, sometimes it closes and then sometimes it doesn't.

I get the feeling that it seems to be 'brushing' past some of the code ??

It is also not necessary for me to immediately open a new trade when I have just closed an open trade (buy or sell).

With the code you made it is necessary

if (OrdersTotal()==0 &&IsNewBar&& buysignal)

second tick you have IsNewBar=false;

if(LastBarOpenAt == Time[0]) {IsNewBar=false;}

the next signal you get with IsNewBar

MT4 is not ignoring your code
barrymet
17
barrymet 2014.04.02 14:18  

de Vries

The journal shows no sign of any error message.

Could you explain a bit more about your explanation ? Do you mean the code I have written checks for my entry signal on the 'second tick' which is not the start of the new bar and therefore is not being executed ??

I should perhaps encapsulate all code that needs to be executed at the start of the new bar within the 'if' statement. ?

Tjipke de Vries
6276
Tjipke de Vries 2014.04.02 14:36  
barrymet:

de Vries

The journal shows no sign of any error message.

Could you explain a bit more about your explanation ? Do you mean the code I have written checks for my entry signal on the 'second tick' which is not the start of the new bar and therefore is not being executed ??

I should perhaps encapsulate all code that needs to be executed at the start of the new bar within the 'if' statement. ?


no your ordersend will only be done if there is no open trade,

you can have open trades also if you have manually opend one or more or if another EA is trading

also pendingtrades are counted in OrdersTotal()

if your EA sends an order and it fails you have to wait for opening newbar will it try again to do a trade

Investigate the situations you did think the EA had to trade but it didn't.

barrymet
17
barrymet 2014.04.02 14:46  
Ok thanks ...will get back to you when it's working ....this is just a small error
Alain Verleyen
Moderator
28542
Alain Verleyen 2014.04.02 15:19  
barrymet:

de Vries

The journal shows no sign of any error message.

Could you explain a bit more about your explanation ? Do you mean the code I have written checks for my entry signal on the 'second tick' which is not the start of the new bar and therefore is not being executed ??

I should perhaps encapsulate all code that needs to be executed at the start of the new bar within the 'if' statement. ?

If nothing is printed and no trade is open when executing the code you posted here, that can only means OrderSend() is not executed.

As your conditions are executed on each new bar if there is no open trade, if OrderSend() is not executed that can only means that buysignal and sellsignal are false.

As Devries said you, MT4 can't ignore your code, so either your code is buggy or you are wrong when you are thinking a trade must be executed. As you don't see the code where buysignal/sellsignal are set, it's not possible to decide.

barrymet
17
barrymet 2014.04.02 15:34  

I think the problem was what de Vries alluded to.

There were no trades being executed because my code is faulty. I was doing a faulty 'double check' for the New Bar which in fact placed my signals in 'no-mans' land

Thanks for the help thus far

/
To add comments, please log in or register