Download MetaTrader 5

MT4 Mulitiple Ea's - need Maximum Trades per terminal/account

To add comments, please log in or register
Publish your programs in the Market. Make money with your knowledge!
xazarlx20
76
xazarlx20 2016.05.11 22:50 
Hey all

I want to run separate EA's on 28 currencies. Being that my broker is OANDA I am using their MT4 version build 950. I have my strategies working using an awesome EA called "Traders Gift" by David Currey. My problem is that I want to limit my trading to one open position at a time. Not one per currency pair but one open trade per account, per terminal, per whatever else. Only one open trade at a time, period.

For testing I run 28 pairs on DEMO with easy entry signals such as "1 minute chart, bearish candle followed by a bullish candle- Buy"

I thought OrdersTotal() would do the trick but apparently there is a window of time before a new trade shows up in OrderHistory().

Which lead me to use a function  called "O_R_CheckForHistory()" by Matt Kennel. This function looks through order history, sleeps, looks through again, sleeps, until the new trade is found in OrderHistory(). This led to a decrease in concurrent trades but they still happen.

Is there some easy way to achieve this limit of one trade at a time?

I will attach the mangled code for anyone who is up to deciphering spaghetti.

Thanks
Files:
Daniela Bluemel
587
Daniela Bluemel 2016.05.12 07:34  

There is no time delay on OrdersTotal function. Maybe you produce a time delay with the functions you use (sleep)

OrdersTotal is the right way to check this. 

xazarlx20
76
xazarlx20 2016.05.12 11:06  
Daniela Bluemel:

There is no time delay on OrdersTotal function. Maybe you produce a time delay with the functions you use (sleep)

OrdersTotal is the right way to check this. 

I suspect the version of MT4 that I must use has a lot to do with my problem.

This following lines of code ... allows two trades to be opened, so far three hasn't happened but two is common.:

if(OrdersTotal()>=1) return;

...OrderSend()...

Marco vd Heijden
Moderator
4708
Marco vd Heijden 2016.05.12 11:19  
xazarlx20:

I suspect the version of MT4 that I must use has a lot to do with my problem.

This following lines of code ... allows two trades to be opened, so far three hasn't happened but two is common.:

if(OrdersTotal()>=1) return;

...OrderSend()...


if(OrdersTotal()>=1) 


if orderstotal is larger or equal to 1.

what would you expect ?

how about

If OrdersTotal()>0)
Alain Verleyen
Moderator
30755
Alain Verleyen 2016.05.12 12:00  
Marco vd Heijden:


if orderstotal is larger or equal to 1.

what would you expect ?

how about

>=1 or >0 is exactly the same Marco.
Marco vd Heijden
Moderator
4708
Marco vd Heijden 2016.05.12 12:01  
Alain Verleyen:
>=1 or >0 is exactly the same Marco.

haha :)

xazarlx20
76
xazarlx20 2016.05.12 12:34  
Marco vd Heijden:


if orderstotal is larger or equal to 1.

what would you expect ?

how about

if (OrdersTotal()>0) return;


allowed 6 trades to be immediately (seemingly) opened

Marco vd Heijden
Moderator
4708
Marco vd Heijden 2016.05.12 13:36  
xazarlx20:

if (OrdersTotal()>0) return;


allowed 6 trades to be immediately (seemingly) opened

#define MAX_ORDERS 1
1240 if(OrdersTotal()>=MAX_ORDERS) return;
1329 if(OrdersTotal()>=MAX_ORDERS) return;

so i think you should investigate

if (Buys > 0 || Sells > 0) return;

by printing Buys and Sells when it happens.

xazarlx20
76
xazarlx20 2016.05.12 14:23  
Marco vd Heijden:

so i think you should investigate

by printing Buys and Sells when it happens.

This line:  

if (Buys > 0 || Sells > 0) return;

refers to variables that are available only to the individual EA. Each EA is "Allowed" to place a trade if there is no trade opened on that pair. I never get 2 orders on the same pair.

If I were getting two trades on the same pair, that line would be suspect.

I have found discussions about this delay in trades showing up in history and I really think I am hitting that wall due to the version of MT4 that OANDA clients must use.

I don't want to change brokers so I need another workaround, maybe setting a global variable.

I can set a GV when a trade is opened but I don't know how to reset it if a trade is closed by hitting the TakeProfit or StopLoss.

THANK YOU SO MUCH!

Stanislav Korotky
17941
Stanislav Korotky 2016.05.12 16:09  

I think your problem is related to the fact that you missed asynchronous nature of the platform (and this is not a feature of your specific version of MT).

When you use several copies of EA simultaneously, each of them is running in a separate thread concurrently with other instances. So, basically, you use a multi-threaded software (a set of EAs), which is known as requiring a synchronization on global objects in order to prevent collisions. First thought is to use a global variable for this, that is you should read OrdersTotal only when current instance of EA "owns" the global variable. Maybe this article could help.

Please note that synchronization will also imply some time overheads. So if you want an optimized solution, the best thing to do is to refactor exiting EA (strategies) to run in a single copy to process all pairs.

xazarlx20
76
xazarlx20 2016.05.13 00:26  
Stanislav Korotky:

I think your problem is related to the fact that you missed asynchronous nature of the platform (and this is not a feature of your specific version of MT).

When you use several copies of EA simultaneously, each of them is running in a separate thread concurrently with other instances. So, basically, you use a multi-threaded software (a set of EAs), which is known as requiring a synchronization on global objects in order to prevent collisions. First thought is to use a global variable for this, that is you should read OrdersTotal only when current instance of EA "owns" the global variable. Maybe this article could help.

Please note that synchronization will also imply some time overheads. So if you want an optimized solution, the best thing to do is to refactor exiting EA (strategies) to run in a single copy to process all pairs.

OK

I think you are right.

So the reason ...

     if (OrdersTotal()>0) return;

... doesn't 'work' for me is that any other EA that tries to place a trade during the same second will succeed?

I have been refining my strategy for 6 months and I don't want to spend another six months figuring out how to get 28 EA's to play nicely together without collisions.

Can anyone recommend a code template/structure or a programmer who can code one for me?

THANK YOU SO SO SO SO MUCH!

12
To add comments, please log in or register