Strange Order Behaviour

 

Hi All,


I am a MQL4 newbie and don't really code so I need some guidance.


I am working on an EA and for testing purposes I would like to fire off a buy and sell order every minute (in this example) so I can watch the behaviour of my code.


I have two snippets below; One being from the timer function and the other from the trailing stoploss function.


Both of the debug Print statements are printed but the orders are doing weird things.


In the case of the OrderSend(). Sometimes only one order will get placed, sometimes two and sometimes none. Never will orders be placed on a per minute basis (although "Hit" is printed to the log every minute). For example I ran it all night and only 3 trades where processed.


In the case of the trailing stoploss. The Print statement is getting hit, but the modify order is not being placed. Its like its not there at all.


There are no errors in the logs.


Anyone have any ideas?


Thanks

if(Seconds() % 60 == 0)
   {
      Print("Hit");
      OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),Comment,MagicNumber,0,Green);
      OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),Comment,MagicNumber,0,Red);
      return(0);
   }
}



if(OrderType() == OP_BUY)
{
      Print("Modify!!!");
      OrderModify(OrderTicket(),OrderOpenPrice(),MinStoploss,OrderTakeProfit(),0,Orange);
      return(0);
}
 
Try this:
if(Seconds() % 60 == 0)
   {
      Print("Hit");
      OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),Comment,MagicNumber,0,Green);
      Sleep(1000);
      RefreshRates();
      OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),Comment,MagicNumber,0,Red);
      return(0);
   }
}
 

Hi Roger


Thanks for the reply.


I have placed the Sleep(1000); and RefreshRates(); lines into the code however they had no effect. I am still getting "hits" printed in the log (6 as I type this) and not a single trade has been placed...

 
Show me your order price, stoploss and takeprofit. And try this:
if(Seconds() % 60 == 0)
   {
      Print("Hit");
      if(!OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),Comment,MagicNumber,0,Green)Print("Error - ",GetLastError());
      Sleep(1000);
      RefreshRates();
      if(!OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),Comment,MagicNumber,0,Red)Print("Error - ",GetLastError());
      return(0);
   }
}
 

I just had to change the first if statement to 1 == 1 just to get it in there. 5 mins of waiting and nothing was happening.


I changed my code as per yours above. it was missing an ")" so I put it in after the colour. Hope that was the right spot.


logs with requested info:


18:10:21 Compiling 'TimerTrade'
18:10:21 TimerTrade EURUSD,M5: uninit reason 2
18:10:21 TimerTrade EURUSD,M5: loaded successfully
18:10:21 TimerTrade EURUSD,M5: initialized
18:10:21 TimerTrade EURUSD,M5 inputs: Lots=0.1; slip=100; BuyStoploss=50; SellStoploss=50; TakeProfit=5000;
18:10:21 TimerTrade EURUSD,M5: Hit
18:10:21 TimerTrade EURUSD,M5: Ask: 1.2651 Bid: 1.2648 Ask Stoploss: 1.2646 Bid Stoploss: 1.2653 TakeProfit: 1.3151
18:10:31 TimerTrade EURUSD,M5: Hit
18:10:31 TimerTrade EURUSD,M5: Ask: 1.2651 Bid: 1.2648 Ask Stoploss: 1.2646 Bid Stoploss: 1.2653 TakeProfit: 1.3151
 

If this works:

if(Seconds() % 60 == 0)
   {
      Print("Hit");
      if(!OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,0,Ask+(TakeProfit*Point),Comment,MagicNumber,0,Green)Print("Error - ",GetLastError()));
      Sleep(1000);
      RefreshRates();
      if(!OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,0,Bid-(TakeProfit*Point),Comment,MagicNumber,0,Red)Print("Error - ",GetLastError()));
      return(0);
   }
}
your stops are too short.
 
I won't be able to test this until later tonight, but I can tell you that the demo broker has a min stoploss of 40 and I am using 50. Previously when i have tried to run under this number I would get log entries saying "OrderSend error 130" I am getting nothing in the logs at all with this problem.
 
AlwayzLearnin wrote >>
I won't be able to test this until later tonight, but I can tell you that the demo broker has a min stoploss of 40 and I am using 50. Previously when i have tried to run under this number I would get log entries saying "OrderSend error 130" I am getting nothing in the logs at all with this problem.

It's wrong. I don't know your logic, but if you use so short stops, you should calculate another way, because of spread.

OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Bid-(Stoploss*Point),Bid+(TakeProfit*Point),Comment,MagicNumber,0,Green);
 
AlwayzLearnin wrote >>

Hi All,

I am a MQL4 newbie and don't really code so I need some guidance.

I am working on an EA and for testing purposes I would like to fire off a buy and sell order every minute (in this example) so I can watch the behaviour of my code.

I have two snippets below; One being from the timer function and the other from the trailing stoploss function.

Both of the debug Print statements are printed but the orders are doing weird things.

In the case of the OrderSend(). Sometimes only one order will get placed, sometimes two and sometimes none. Never will orders be placed on a per minute basis (although "Hit" is printed to the log every minute). For example I ran it all night and only 3 trades where processed.

In the case of the trailing stoploss. The Print statement is getting hit, but the modify order is not being placed. Its like its not there at all.

There are no errors in the logs.

Anyone have any ideas?

Thanks

I had the same issue, when i was placing orders for a hedge. The issue is there when you try to open double orders, and close double orders. I found a way out!

You need to write another function, which you call to basically check if the order was opened, if it is not opened, then open the order. Do the same for closing, after you run your closing function, call another function to check that the orders were closed, if they were not closed, then close them. Use ordermagicnumber(), ordertype(), and symbol() to track the orders in your live order pool.

Below is a function for verifying that the orders were closed, and this function is called, when your exit conditions are met. e.g. if ma>60 closeorders();


void closeorders()
{
int close_ord1=OrdersTotal();
for (int qe1=0;qe1<close_ord1;qe1++)
{
OrderSelect(qe1, SELECT_BY_POS, MODE_TRADES);
if( OrderMagicNumber()==MagicNumber1)
{
if(OrderType()==OP_BUY) {RefreshRates();OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3);Sleep(2000);}
if(OrderType()==OP_SELL){RefreshRates();OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3);Sleep(2000);}
}
}
closeordersverify();
return;
}


void closeordersverify()
{
int close_ord11=OrdersTotal();
for (int qe11=0;qe11<close_ord11;qe11++)
{
OrderSelect(qe11, SELECT_BY_POS, MODE_TRADES);
if( OrderMagicNumber()==MagicNumber1)
{closeorders();}
}
return;
} 
 

Thanks for your help guys.


I put a GetLastError() after the OrderSend() which showed an error. Fixing the stops/takeprofits and putting in some time between the orders has fixed the issue.


Although the timer still doesn't fire every 60 seconds, but I have changed it to be every X points instead.