How to avoid multiple orders on the same bar chart ?

 

Hi:

I need a solution for this problem.

Imagine a Simple Moving Average EA running on a 15 Minutes Chart with a tight Take Profit and Stop Loss.

Imagine than a new buy order has been activated because the 15 min. bar as closed above the Moving Average.

Imagine that prices are rising fast and the Take profit level is reached 5 minutes later in the same 15 min. bar. The Buy order is closed with a profit.


The problem now is that the EA will open immediately a new Buy Order on the same bar because the open rule is still active (The 15 Min. bar that has activated the rule as not changed yet).

I want to avoid opening new orders on the same bar.


How could I do it ?

Some link with a solution or examples ?


Thank you for your help.

 

B

Use something like this

static datetime LastTradeBarTime;




int init()
  {
//----


  LastTradeBarTime = Time[1]; // initialise the variable




//----
   return(0);
  }


start()
{

  // Do <any tick> code here such as trailing stops etc





  if (LastTradeBarTime == Time[0]) return(0); // Not <first detected tick> on this bar so quit

  else LastTradeBarTime = Time[0]; // and continue

  // Do <once per bar> trading stuff here




   return (0);
  }

OTTOMH

-BB-

 
BarrowBoy:

B

Use something like this

OTTOMH

-BB-

Great! I have inserted your solution into my EA and the problem was fixed. Thank You!
The code now looks like this: 


static datetime LastTradeBarTime;

int init()
  {
  LastTradeBarTime = Time[1]; // initialise the variable
  return(0);
  }

start()
{
  // Do <any tick> code here such as trailing stops etc

  if (LastTradeBarTime == Time[0]) return(0); // Not <first detected tick> on this bar so quit

  else 

 // Start with open order instructions.
  
 // If a trade is open give this instruction

  LastTradeBarTime = Time[0]; // If a new trade is opened we give current time as new value for LastTradeBarTime.

   return (0);
  }

 
blackhawk:


Hi, I'm not sure where to add the above code into my EA? Would you be able to give me little guidance please?

 

Many thanks,

DM 

 
danielmermina:


Hi, I'm not sure where to add the above code into my EA? Would you be able to give me little guidance please?

 

You are asking a question about a 3 year old post,  maybe you will get an answer in 3 years time.