Download MetaTrader 5

Limiting the opening of multiple orders on new bar

To add comments, please log in or register
treasure1949
39
treasure1949  

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

ThreadHandle
105
ThreadHandle  

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. 

treasure1949
39
treasure1949  
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?

ThreadHandle
105
ThreadHandle  
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. 

treasure1949
39
treasure1949  
No this happens on a demo. I have back tested this on a single pair before putting it on demo
treasure1949
39
treasure1949  
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. 
ThreadHandle
105
ThreadHandle  
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. 
treasure1949
39
treasure1949  
Thanks a lot.
ThreadHandle
105
ThreadHandle  

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 ;
}
ThreadHandle
105
ThreadHandle  
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. 
treasure1949
39
treasure1949  
Many thanks again Keelan. You have been very helpful. I will try it this weekend and let you know how it goes.
12
To add comments, please log in or register