Trying to ensure only 1 open trade on my EA

 

I have the following code to prevent a new order from being generated when one is already placed. Any idea why additional trades still follow...

int CheckForOpen()
{
   bool   Result;
   int    t,Pos,Error;
   int    Total=OrdersTotal();
   int OrdersTotalMagicOpen=0;
  
   if(Total>0)
       
     for(t=OrdersTotal()-1; t>=0; t--) 
     {
     OrderSelect(t, SELECT_BY_POS, MODE_TRADES); 
    if (OrderSymbol() != Symbol() || OrderMagicNumber() != MAGICMA) continue; //MAGICMA is defined in global scope
     if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
     if (OrderType() == OP_SELL || OrderType() == OP_BUY) OrdersTotalMagicOpen++;
       {break;}
 
  1. Drop unnecessary Total and if(Total). Drop the double test (MN/Sym.) Drop the break and complete the loop. Check your return codes. Simplify:
    int CheckForOpen()
    {
       int OrdersTotalMagicOpen=0;
       for(int t=OrdersTotal()-1; t>=0; t--) if(
          OrderSelect(t, SELECT_BY_POS)
       && OrderMagicNumber() == MAGICMA
       && (OrderType()       == OP_SELL || OrderType() == OP_BUY)
       && OrderSymbol()      == Symbol() 
       ) OrdersTotalMagicOpen++;
       
       // What follows?
  2. Then test your variable. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. We can't see your broken code. The code you posted doesn't open orders.
 

here's what I have now - still opens new trades continuously:

#define MAGICMA  123456789 //global section
//---------------------------------------------------------------------------  
 int OrdersTotalMagicOpen()
{
 int OrdersTotalMagicOpen=0;
  for(int t=OrdersTotal()-1; t>=0; t--) if(
      OrderSelect(t, SELECT_BY_POS, MODE_TRADES )
   && OrderMagicNumber() == MAGICMA
   && (OrderType()       == OP_SELL || OrderType() == OP_BUY)
   && OrderSymbol()      == Symbol() 
   ) OrdersTotalMagicOpen++;
  

 if(TRUE)
 { 
 { 
 double SARM1=iSAR(NULL,1,0.02,0.2,1);
 double SARM5=iSAR(NULL,5,0.02,0.2,1);

Lots = 0.3;
Amount=AccountBalance()/100;
Sleep(3000);

 if(Ask>SARM1 && Ask>SARM5){OrderSend(Symbol(),OP_BUY,Lots,Bid,Slippage,0,0,"ALLMAGICMA",MAGICMA,0,CLR_NONE); } // very basic example of code to open trade
     
}
} 
   
 
 

Please format your code so that it is easier to read.

if(TRUE)

Why do you have this? It just means that the following block of code ( Including the OrderSend() ) will always be executed. Why is it followed by 2 {

Sleep(3000);

Why the sleep? After sleep if you are going to use Bid/Ask, you must always RefreshRates()

if(Ask>SARM1 && Ask>SARM5){OrderSend(Symbol(),OP_BUY,Lots,Bid,Slippage,0,0,"ALLMAGICMA",MAGICMA,0,CLR_NONE);

A buy is opened at Ask, not Bid

Always use

#property strict

then the compiler will warn you that the "return value of 'OrderSend' should be checked"


In future please post in the correct section

I will move this topic to the MQL4 and Metatrader 4 section.

 

Thank you Keith.  I've applied all of your suggestions/corrections.  The #property strict inclusion helped me find a few errors that I wasn't even aware of.  My code now looks as pasted below, but I am now wanting to use the OrdersTotalMagicOpen count to prevent a new trade from opening when there is already one open; I just can't figure out how to do that - please can you guide me as I have tried may different ways, but just can't get that bit to work - trades keep opening.


#property strict
#define MAGICMA  123456789 //global section
//---------------------------------------------------------------------------  
 int OrdersTotalMagicOpen()
{
 int OrdersTotalMagicOpen=0;
  for(int t=OrdersTotal()-1; t>=0; t--) 
if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber() == MAGICMA && (OrderType()== OP_SELL || OrderType()== OP_BUY)&& OrderSymbol()== Symbol()) OrdersTotalMagicOpen++;
{
  return (OrdersTotalMagicOpen);//is this correct format and placing of the return coding?
  }
If(OrdersTotalMagicOpen==0)//This is the line of code that I'm struggling to get to limit my Order placements to just one position per symbol.
{ 
 double SARM1=iSAR(NULL,1,0.02,0.2,1);
 double SARM5=iSAR(NULL,5,0.02,0.2,1);

Lots = 0.3;
Amount=AccountBalance()/100;

if(Ask>SARM1 && Ask>SARM5){OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"ALLMAGICMA",MAGICMA,0,CLR_NONE); } // very basic example of code to open trade
}
 

Please make sure that code will compile before posting it.

I don't see how the function can place any trades at all. 

return (OrdersTotalMagicOpen);//is this correct format and placing of the return coding?

This return will mean that none of the code following it will be executed.

 

Hi Keith.  Here is my current code that places orders continuously.

#property strict
#define MAGICMA  123456789 //global section
//---------------------------------------------------------------------------  
 int OrdersTotalMagicOpen()
{
 int OrdersTotalMagicOpen=0;
  for(int t=OrdersTotal()-1; t>=0; t--) 
if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES )&& OrderMagicNumber() == MAGICMA && (OrderType()== OP_SELL || OrderType()== OP_BUY)&& OrderSymbol()== Symbol()) OrdersTotalMagicOpen++;
  
if(OrdersTotalMagicOpen<1)//This is the line of code that I'm struggling to get to limit my Order placements to just one position per symbol.
{ 
 double SARM1=iSAR(NULL,1,0.02,0.2,1);
 double SARM5=iSAR(NULL,5,0.02,0.2,1);

Lots = 0.3;
Amount=AccountBalance()/100;

if(Ask>SARM1 && Ask>SARM5){OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"ALLMAGICMA",MAGICMA,0,CLR_NONE); } // very basic example of code to open trade
}
return (OrdersTotalMagicOpen);
  }
 
Peter Riley:

Hi Keith.  Here is my current code that places orders continuously.

Unless I am missing something, I don't see that the code will continuously place orders.

Check the rest of your code for OrderSend().

 

Strangely, I've discovered that my code works perfectly on most financial instruments, but just a few play up - the code is identical and the only difference is the Symbol.  I guess that i will just have to drop those ones from my trading portfolio - a pity though as I like to trade a very wide and diverse portfolio.

 

You would not believe how long I've spent on this, but I've found the problem!  Maybe posting it here will help someone else in the future...

The problem was the Magic Number; I assigned these numbers very arbitrarily (literally arbitrary clicks of the keyboard) - in doing this, I transgressed the value range limits of an integer which is :

-2147483647 to 2147483647

A simple rule going forward, just keep the Magic Number <9 digits in length.

Frustration meets Relief!

Reason: