Warm tip: This article is reproduced from serverfault.com, please click

IsTradeAllowed not returning what I would expect

发布于 2021-02-28 15:03:23

Please see the script below:

void OnStart()
{
   Alert(IsTradeAllowed()); //alert 1
   Alert(IsTradeAllowed(NULL, TimeGMT())); //alert 2
   Alert(IsTradeAllowed(Symbol(), TimeGMT())); //alert 3
   Alert(IsTradeAllowed("GBPUSD", TimeGMT())); //alert 4
}

This returns:

true //for alert 1
true //for alert 2
false //for alert 3
false //for alert 4

As alert 2 returns: true, then I would expect alert 3 and alert 4 to return true.

I have tried running the code at multiple times of the day on weekdays. The code returns the same result at weekends. I have also tried putting the code in a script and an EA. Every time I get the same result. Is there an explanation for this? I have tried what is suggested here: https://www.mql5.com/en/docs/runtime/tradepermission

Symbol() returns: "GBPUSD". Each alert should return true in my mind, however this does not appear to be the case here. Incidentally I have notices that Symbol() returns the symbol at the top of the demo account watchlist if the script is run inside MetaEditor, however it returns the symbol displayed on the chart if run inside the demo account.

The broker is Oanda.

Update 04/03/21 at 19:55

I have now discovered that if I right click on the Market Watch and select: show all, then more symbols appear. I can then see that some symbols are greyed out and some symbols are not. The symbols that are not greyed out e.g. USDGBP-g return what I would expect when running the program above i.e. alert 1-alert 4 =true. The symbols that are greyed out e.g. USDGBP returns true; true; false; false in the program above. I now have two questions:

  1. Why does: IsTradeAllowed(NULL, TimeGMT()); //alert 2 return true for symbols that are greyed out?

  2. What does -g mean in GBPUSD-g?

Questioner
w0051977
Viewed
0
PaulB 2021-03-05 03:01:52

IsTradeAllowed checks if the Expert Advisor is allowed to trade and trading context is not busy.

The version of the function without any arguments will check if the EA has been applied with the correct permissions ("Allow live trading" ticked and "AutoTrading" enabled).

The second form of the function:

bool IsTradeAllowed(const string symbol, datetime tested_time);

checks if the EA would be allowed to trade according to the specifications for the chart selected (to view this, from the Market Watch window right click a symbol, from the menu that pops up select "Specification").

For example

IsTradeAllowed(Symbol(), D'2021.03.06 12:00');

would check if the current symbol can be traded this coming Saturday (which should be false).

If you are getting undesirable results you should check that your broker has set the "Specifications" correctly.

EDIT

I've tested the command in OANDA which is the broker you are using and the command functions as expected.

NULL is not valid for a Symbol and its use makes the command function in its first form (ie timedate is ignored).

I would suggest rather than use Alerts to examine output, try the following.

   string cmnt;

   cmnt=StringConcatenate("Alert 1: ",IsTradeAllowed());
   cmnt=StringConcatenate(cmnt+"\r\n","Alert 2: ",IsTradeAllowed(NULL, TimeGMT()));
   cmnt=StringConcatenate(cmnt+"\r\n","Alert 3: ",IsTradeAllowed(Symbol(), TimeGMT()));
   cmnt=StringConcatenate(cmnt+"\r\n","Alert 4: ",IsTradeAllowed("GBPUSD", TimeGMT()));

   Comment(cmnt);