Limiting the opening of multiple orders on new bar

 

Hi there,

I have written an EA which I run on several charts simultaneously. In order to keep the margin usage within a reasonable level, I want to prevent having more than 5 orders open at a given time. I use an order counter and compare its result with the value of OrdersTotal() before allowing a new order to open.  This works fine when the orders are opened intra-bar. However, when I make the EA to open trades on the appearance of a new bar, several orders open simultaneously in different charts exceeding my max limit.

Can someone guide me to finding a solution to this issue please.

Thanks in advance

 

Whats it going on, chart event? Just add some logic that makes it wait a random amount of time (so each EA doesnt buy at the same time) before it checks to see if it wants to buy. Then obviously have it check OrdersTotal() < 6 {SendOrder}; 

There is no bar event, you can have a timer set to the bar but no actual bar event to my knowledge.

 If you give me a snippet of your code i can show what to change. 

 
Keelan:
Whats it going on, chart event? Just add some logic that makes it wait a random amount of time (so each EA doesnt buy at the same time) before it checks to see if it wants to buy. Then obviously have it check OrdersTotal() < 6 {SendOrder}; 

Thanks Keelan. I will try that idea. Can you give me an idea of a suitable delay. Secs or milli Secs ? Does OrdersTotal() get updated in between ticks?

 
Tony227:

Thanks Keelan. I will try that idea. Does OrdersTotal() get updated in between ticks?

 

You are working on ticks? are you back testing? ticks and bars are different. If you want to work with bars use the onchart event as they they give you all the data for bars. If your working on ticks then your gona have a little bit more of an issue unless you dont mind a slight delay (missing a few ticks). I presume OrderTotal() returns the value for all order not just that EA but ive never tested. Hold on ill tell you. 

 
No this happens on a demo. I have back tested this on a single pair before putting it on demo
 
Yes, OrdersTotal() gives the total open orders in the account.  I think my logic is correct. Hopefully adding a delay, as you have suggested, should solve the problem. I will work on it. Thanks a lot. 
 
Yes the OrdersTotal() updates for all trades regardless of EA. OrdersTotal() will be updated as soon as an order is successfully sent. If your sending multiple orders at the same time the only solution i can offer is have the timer wait a random time then divide that random number by 1000 or so, or limit the return val from the rand func using a cast or such. I forget what shulgin said but there's an easy fix to get it to produce a value from 1-10 then / 100. 
 
Thanks a lot.
 

try. The rand1() is based on old code, i dont know if RAND_MAX is the same as shulgins. It works in my code, it only sleeps from 1-100 milliseconds so you can play with that.

 

int RAND_MAX = 32767;


void OnTick()
  {
//---
   MathSrand(GetTickCount());
   double newvar = rand1();
   int done = 0;
   newvar*= 100;
   done = (int)newvar;
   Sleep(done);
   if(OrdersTotal() <= 5 ){
    
      //Your code
   
   }


  }

double rand1()
{
    return (double)rand() / (double)RAND_MAX ;
}
 
That should separate each of your EA's but only by milliseconds so you might want to push that multiplier up a bit if the OrdersTotal() doesnt update faster than the Sleep() allows it. Maybe go 1000 if it doesnt work for you. 
 
Many thanks again Keelan. You have been very helpful. I will try it this weekend and let you know how it goes.