EA making mysterious trades error

 

I have strategy that I wrote an EA for and it is supposed to place an OCO for the instrument it is attached.

2011.10.11 03:00:03 DailyBreakOut AUDUSD,H1: open #57394810 buy stop 4.20 EURUSD at 1.36901 sl: 1.36401 tp: 1.37901 ok

2011.10.11 03:00:04 DailyBreakOut EURUSD,H1: open #57394823 buy stop (instead of sell stop) 3.30 EURJPY at 104.93200 sl: 104.43200 tp: 105.93200 ok

2011.10.11 03:00:04 DailyBreakOut EURUSD,H1: open #57394814 sell stop (instead of buy stop) 4.20 AUDUSD at 0.98217 sl: 0.98717 tp: 0.97217 ok

2011.10.11 03:00:05 DailyBreakOut EURJPY,H1: open #57394830 sell stop 3.30 GBPJPY at 119.541 sl: 120.041 tp: 118.541 ok

when the EA is attached to the AUD it opens a buy stop for EURUSD.

same thing happens when the EA is attached to the EURUSD and the EURJPY. The funny thing is that there will be a buystop and sellstop order for all the pairs but it will not necessarily be an OCO because the EA attached to AUDUSD will open one of the orders for the EURUSD and the the EA attached to some other instrument places an order for the EURUSD for example. How is this possible when the EA is not even nearly designed this way to place orders for pairs it is not attached to.

this is the code in the EA to place the orders

         PendingPrice = pHigh;
         SL1 = pHigh - UsePoint*StopLoss;
         TP1 = pHigh + UsePoint*TakeProfit;
         OTicket1 = OpenBuyStopOrder(Symbol(), LotSize, PendingPrice, SL1 ,TP1, Slippage, MagicNumber, 0, "Buy Stop Order");
         
         PendingPriceS = pLow;
         SL2 = pLow + UsePoint*StopLoss;
         TP2 = pLow - UsePoint*TakeProfit;
         OTicket2 = OpenSellStopOrder(Symbol(), LotSize, PendingPriceS, SL2 ,TP2, Slippage, MagicNumber, 0, "Sell Stop Order");
         
         OCOset = true;

HELP!!

 
jeemba2012:

this is the code in the EA to place the orders

HELP!!

No it isn't . . . there is no OrderSend() in that code.
 
jeemba2012:

HELP!!

Hmmm. I've seen something similar in the past under very extreme conditions (https://www.mql5.com/en/forum/115837), and I've seen one other report on this forum of what may the same phenomenon. Assuming, of course, that your code really, definitely, absolutely can't possibly place orders for a different symbol under any circumstances, i.e. categorically always and only uses Symbol() as a parameter for OrderSend(). Normally I'd say that this was simply a bug in your code, but it's about the only such area where I've definitely seen an MT4 problem.

I'd make sure that you're using a method such as the one described at https://www.mql5.com/en/articles/1412 to prevent multiple EAs trying to trade at once. That fixed the problem for me.

 
jjc:

Hmmm. I've seen something similar in the past under very extreme conditions (https://www.mql5.com/en/forum/115837), and I've seen one other report on this forum of what may the same phenomenon. Assuming, of course, that your code really, definitely, absolutely can't possibly place orders for a different symbol under any circumstances, i.e. categorically always and only uses Symbol() as a parameter for OrderSend(). Normally I'd say that this was simply a bug in your code, but it's about the only such area where I've definitely seen an MT4 problem.

I'd make sure that you're using a method such as the one described at https://www.mql5.com/en/articles/1412 to prevent multiple EAs trying to trade at once. That fixed the problem for me.


Just to make sure I am correct...

I place the functions in my EA in the appropriate place and at the top I include the directive #include <TradeContext.mq4>. This way when my EA is attached to 5 different instruments they will be sharing the same information from the TradeContext file and will know if it is allowed

to trade. Is this how it works precisely?

 
jeemba2012:


Just to make sure I am correct...

I place the functions in my EA in the appropriate place and at the top I include the directive #include <TradeContext.mq4>. This way when my EA is attached to 5 different instruments they will be sharing the same information from the TradeContext file and will know if it is allowed

to trade. Is this how it works precisely?

I've not actually used the TradeContext.mq4 mentioned in section 7 of https://www.mql5.com/en/articles/1412, as opposed to sections 5 and 6, but the answer should be yes.

An an alternative, if you are happy with a requirement for "Allow DLL imports" to be turned on, then I'd consider something like the following. It's an adapted version of the code from https://www.mql5.com/en/forum/130970/page2, with the same benefits versus use of global variables.

If you go down this route then your EA calls GetEALock() before trading, and then ReleaseEALock() when it has finished. If the call to GetEALock() fails, because another EA is already trading, then it's up to you whether you wait and retry, or whether you give up etc.

#define  LOCKNAME                "myea"

// Kernel constants and imports
#import "kernel32.dll"
   int CreateFileA(string Filename, int AccessMode, int ShareMode, int PassAsZero, int CreationMode, int FlagsAndAttributes, int AlsoPassAsZero);
   int CloseHandle(int);
   string DeleteFile(string);
#import

// Global variables holding the file handle and file name. It's not actually
// necessary to record and then delete the file.
int glbLockFileHandle = -1;
string glbLockFileName = "";


bool GetEALock()
{
   // Single-instance solution, therefore we can just create the file in experts\files
   glbLockFileName = TerminalPath() + "\\experts\\files\\" + LOCKNAME + ".dat";

   // Try opening the file for writing, with exclusive access. This will fail, returning -1,
   // if the file is already in use by another EA.
   glbLockFileHandle = CreateFileA(glbLockFileName, 1073741824, 0, 0, 2, 0, 0);

   return (glbLockFileHandle != -1);
}

void ReleaseEALock()
{
   // Close the handle to the file, releasing the lock
   CloseHandle(glbLockFileHandle);
   
   // Delete the file (not actually required; just included for general cleanliness)
   DeleteFile(glbLockFileName);
}
 
jjc:

I've not actually used the TradeContext.mq4 mentioned in section 7 of https://www.mql5.com/en/articles/1412, as opposed to sections 5 and 6, but the answer should be yes.

An an alternative, if you are happy with a requirement for "Allow DLL imports" to be turned on, then I'd consider something like the following. It's an adapted version of the code from https://www.mql5.com/en/forum/130970/page2, with the same benefits versus use of global variables.

If you go down this route then your EA calls GetEALock() before trading, and then ReleaseEALock() when it has finished. If the call to GetEALock() fails, because another EA is already trading, then it's up to you whether you wait and retry, or whether you give up etc.


I like this. Where do I paste this code or what file do I create to paste it. I have visualC++, do I create a cpp file and put it in a specific folder and go to metatrader and allow dll imports?
 
jeemba2012:

I like this. Where do I paste this code or what file do I create to paste it. I have visualC++, do I create a cpp file and put it in a specific folder and go to metatrader and allow dll imports?

It's just MQL4 code which you copy and paste anywhere into your EA, or put in a .mqh header if you prefer. When placing trades, you then do something like the following:

if (!GetEALock()) {
   // Failed to get lock. Could Sleep() and retry, or just give up

} else {
   // Send an order, or close an order, or modify an order etc

   // Release the lock so other EAs can trade
   ReleaseEALock();
}

If needs "Allow DLL imports" turned on not because there's a DLL involved in the usual sense of a bepoke DLL, but because it's using Win32 kernel functions.

 
jjc:

It's just MQL4 code which you copy and paste anywhere into your EA, or put in a .mqh header if you prefer. When placing trades, you then do something like the following:

If needs "Allow DLL imports" turned on not because there's a DLL involved in the usual sense of a bepoke DLL, but because it's using Win32 kernel functions.


Is this correct.

my header

//+------------------------------------------------------------------+
//|                                                   wildthingh.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
// #define MacrosHello   "Hello, world!"
// #define MacrosYear    2005

//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
// #import "user32.dll"
//   int      SendMessageA(int hWnd,int Msg,int wParam,int lParam);

// #import "my_expert.dll"
//   int      ExpertRecalculate(int wParam,int lParam);
// #import

//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
// #import "stdlib.ex4"
//   string ErrorDescription(int error_code);
// #import
//+------------------------------------------------------------------+
#define  LOCKNAME                "wildthing"

// Kernel constants and imports
#import "kernel32.dll"
   int CreateFileA(string Filename, int AccessMode, int ShareMode, int PassAsZero, int CreationMode, int FlagsAndAttributes, int AlsoPassAsZero);
   int CloseHandle(int);
   string DeleteFile(string);
#import

// Global variables holding the file handle and file name. It's not actually
// necessary to record and then delete the file.
int glbLockFileHandle = -1;
string glbLockFileName = "";


bool GetEALock()
{
   // Single-instance solution, therefore we can just create the file in experts\files
   glbLockFileName = TerminalPath() + "\\experts\\files\\" + LOCKNAME + ".dat";

   // Try opening the file for writing, with exclusive access. This will fail, returning -1,
   // if the file is already in use by another EA.
   glbLockFileHandle = CreateFileA(glbLockFileName, 1073741824, 0, 0, 2, 0, 0);

   return (glbLockFileHandle != -1);
}

void ReleaseEALock()
{
   // Close the handle to the file, releasing the lock
   CloseHandle(glbLockFileHandle);
   
   // Delete the file (not actually required; just included for general cleanliness)
   DeleteFile(glbLockFileName);
}

was I supposed to replace "myea" with my EA named wildthing i.e #define LOCKNAME "wildthing"

here is my mq4

//+------------------------------------------------------------------+
//|                                                    wildthing.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
       if (!GetEALock()) {
   // Failed to get lock. Could Sleep() and retry, or just give up  

      } else {
       // Send an order, or close an order, or modify an order etc
       PendingPrice = pHigh;
         SL1 = pHigh - UsePoint*StopLoss;
         TP1 = pHigh + UsePoint*TakeProfit;
         OTicket1 = OpenBuyStopOrder(Symbol(), LotSize, PendingPrice, SL1 ,TP1, Slippage, MagicNumber, 0, "Buy Stop Order");
         
         PendingPriceS = pLow;
         SL2 = pLow + UsePoint*StopLoss;
         TP2 = pLow - UsePoint*TakeProfit;
         OTicket2 = OpenSellStopOrder(Symbol(), LotSize, PendingPriceS, SL2 ,TP2, Slippage, MagicNumber, 0, "Sell Stop Order");

       // Release the lock so other EAs can trade
       ReleaseEALock();
      }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

What actually goes into the if block if a trade is not made because its busy and I want it to try again until success?

 
jeemba2012:

was I supposed to replace "myea" with my EA named wildthing i.e #define LOCKNAME "wildthing"

[...]

What actually goes into the if block if a trade is not made because its busy and I want it to try again until success?

It doesn't matter what you define LOCKNAME as provided that, if have you different EAs which you want to work together, they all use the same value. The only restriction is that LOCKNAME can't use characters which are invalid in filenames, such as < or > or |

You could repeatedly retry until success using something along the lines of the following:

while (!GetEALock()) {
   // EA potentially enters an infinite loop here if another EA is buggy and never calls ReleaseEALock()
   Sleep(1000);
}

// Typically need to make sure that market prices are fresh after a potentially lengthy delay.
// Could also use MarketInfo(Symbol(), MODE_ASK) rather than Ask etc, which has the same effect
RefreshRates();

// Carry out trading activity here

// Release the EA lock
ReleaseEALock();
 

I am trying to use the code in the article mentioned above. I downloaded the mql4 file provided in the link under item 7 and put the file in my experts folder and included it in the directive of my EA. For

some reason when I compile the code I get an error saying "can not open the program file". How do I fix this error.

 
jeemba2012:

I am trying to use the code in the article mentioned above. I downloaded the mql4 file provided in the link under item 7 and put the file in my experts folder and included it in the directive of my EA. For

some reason when I compile the code I get an error saying "can not open the program file". How do I fix this error.

Why don't you just do as already suggested and ADD the code to your own ? ?