How to run EA for multiple pairs? - page 2

 
mittalpa:

I think both approaches are good.

Only thing I would like to add is that option #2 has performance merit due to less overhead. Everything will be in memory which obviously is faster than file operation.

One advantage for option #3 is if you want to use the file data for something which MT4 cannot do.

Also consider that if you need to build an EA which can recover from power outages etc. without user intervention to update market conditions etc. you will find yourself building file access functionality anyway.

 
cloudbreaker wrote >>

Also consider that if you need to build an EA which can recover from power outages etc. without user intervention to update market conditions etc. you will find yourself building file access functionality anyway.

In my experience you’re going to run into a lot of trouble trying to build an EA that trades multiple pairs.

1. Each pair needs its own custom logic, optimization and for some pairs maybe even a different trading strategy. I've wrote EA's that were optimized for a particular pair to the point where it was trading well. Then when I tried using it on another pair that has the most correlation to the first pair, I was amazed that many things had to be changed to get the EA working and optimized for the second pair. I've found that each pair needs it's own complete set of custom settings, indicator values and many times even changes in the basic logic and strategy. To me it makes much more sense to create one very flexible EA that has several different strategies and branches of logic built in. Then it's just a simple matter of creating a new optimal .set file for each pair.

2. The EA you want to make will not be able to be back tested and optimized in Strategy Tester. Back testing and optimization is essential in my experience. There are optimizations that greatly improved my EA performance that I never could have discovered if I did not use the Strategy Tester. It gave me things like indicator settings that were so far away from what is considered by many to be the most useful and optimal values for a particular indicator, that I would have never thought of trying values anywhere near what the optimal values turned out to be. Think how many parameters you have in your EA. Each one needs to be optimized, and each optimization of one param may require a change on another param. That's why you can't just optimize each param by itself. The goal in optimization is to get all your params optimized in relation to each other as best as you can. That can take a long time and a lot of work using the tester, but it's pretty much impossible to do these optimizations manually just by changing the params one by one yourself.

On another note, whichever way you decide to code your EA, saving your EA state or other info to file is not mandatory in most cases, and is not the only option. The best, most efficient and easiest way to do this is to save your state to Global Variables. After all, that is the very reason the Global Variables feature was added to MT. Also some of you talked about having an extra EA created for the sole purpose of exchanging data stored on the file system allowing data exchange and interaction between the two EA’s. This is also unnecessary. Data exchange and even conditional logic capability between multiple EA’s is another feature of Global Variables. This allows multiple EA’s on different charts to gain access to the data of any EA and use that data to make decisions that can effect a change in another EA. That data is safe and secure even in the event of a computer crash or power outage. But what happens in a crash or power outage when you are in the process of saving, reading or exchanging data between EA’s and have one or two files open? You may very likely end up with missing data, data corruption and the worst- zero byte files or no files at all. With Global Variables none of these problems can happen. Your EA state will be exactly as it was the millisecond before your system went down. Now the downside to GV's that everyone talks about is that they cannot store strings, but there are some good ways around that.

First of all GV’s can store strings if you use the GV name AS the string value, for example GlobalVariableSet(Symbol() + "LastUptime=" + TimeLocal(), -1); One problem with this is that there is a limit on the length of GV names so you can't use this way to save long strings. Another great workaround that I use all the time is storing strings in the text fields of graphical objects. You can work with them just you work with orders, and GV’s. There are mql functions for getting the total amount of both GV’s and graphical Objects so you can then loop through all of them and find the one you’re looking for, again the exact same way you loop through your orders.

WARNING- I’m now going on a bit of a rabbit trail here about other cool ways graphic objects can enhance your trading functions… getting off topic a bit but may be useful info…

There are much more useful things you can do with graphical objects. For example, one of my EA’s has an optional hedging feature. When hedging is enabled there is of course no real stop loss on any new orders placed, because when hedging on the same symbol the point is to open an opposite order if the first order goes past what the normal stop loss would be and the first order needs to stay open. So your code would need to know what the stop loss is and monitor the trade so it can open the hedge when the first order hits the stop loss price, and in case there is any misunderstanding the reason we can't use an actual stop loss on the order is because then the order would be closed which of course would make for no hedge at all. But with graphic objects you can make this work better and make it look exactly the same to the user as a real stop loss. What you do is this: When you place the order, at the same time you create a horizontal line object with the price param = to the stop loss price. The name of the line object is "Order #" + orderTicket; the description is “StopLoss @ " + SLPrice. Set the line style to STYLE_DASHDOT, color to red and you have a stop loss line that looks exactly like the real thing. The code to make it work is easy too. All the info you need is already stored in the line object- the order ticket# and the SL price, which is the value of the line object. Then you make a function that checks whether the current price reaches or goes beyond the line. When that happens you get the ticket# that was saved in the line’s name field. Then you find the open order that has the same ticket. Select the order to see if it is a Buy or Sell order and also get the Lot size. Now you have all the info you need to open your hedge order. You open a new order opposite from the first one and with the same lot size. The final step is to delete the stop loss line. When you watch it work it’s pretty cool because now you can see when you’re getting close to being “Hedged”.

Another plus is if you think your order just might make it, if the price seems like it may go back into your favor but you just need a bit more room, then just drag the SL/Hedge line up a bit. Don’t you wish you could do that with the standard SL and TP lines? That would be a great new feature if they made the stop loss and take profit lines movable so we could make quick adjustments when the market is really moving. Of course you can do that today in your EA’s using the steps above and one good benefit is that the broker would never see your SL or TP. The down side is if your computer goes off- you have no server side SL or TP. But the same is true for Robots like FapTurbo. Their ‘stealth’ method is putting fake SL and TP values on the orders and either closing the orders from code or modifying the order with the correct SL and TP values at the last minute when price is getting close.

 
Jacques366:

Hi there,

I prefer to keep in mind we are still working in real time processing so I just forget about using while loop or wait function to keep the hand on the communication!

Attaching your EA to a pair like EURUSD provides you enough signals to manage all the other pairs, ticks are very frequent. It's not a matter of minute but of second (running a loop for 2 minutes sounds really crazy for me). If it is not a matter of second, just think why or see with another broker.

if you really need more than what you will get in attaching your EA to eurusd, think of running separate instance of your EA attached to each currency. Sorry but I tend to think "or rethink your system".

Sorry if feel that this post is a bit abrupt. Wanted to share with you my point of view.

Good luck.

I'm a novice programmer, so please consider this a "what-if" scenario for discussion purposes:


I realize it is bad programming to create an endless While loop with nothing productive in the loop, or if it prevents you from executing the remainder of your EA, but what if you create an endless While loop containing the body of your EA. I don't understand MQL4 programming well enough to fully understand your "keep the hand on the communication" comment. If you launch trade orders via a standalone script will you have communication problems if the EA continues to run in an endless loop?


I am continuing to toy with the idea of using an endless While loop and issue trade orders via standalone scripts because relying on incoming EURUSD ticks to run the EA can lead to some lengthy delays. For example between 0700and 0800 GMT today, the longest wait would have been 31 seconds, but later in the day near the end of the New York session the wait can take up to 2 minutes for an incoming tick --- I have not checked the Asian session yet, but I suspect that it too has some lengthy intervals between ticks.


If you put the body of your EA in an endless loop you could easily control the frequency of updating all the currencies you are trading and not sacrifice any delay. In fact, you may have to put a 100-250 millisecond sleep statement in the loop to slow it down a bit if 50 passes through the EA in 1 second is mind numbing.


I appreciate all feedback.

 
vangrosh wrote >>

WARNING- I’m now going on a bit of a rabbit trail here about other cool ways graphic objects can enhance your trading functions… getting off topic a bit but may be useful info…

brilliant!

 
vangrosh:

In my experience you’re going to run into a lot of trouble trying to build an EA that trades multiple pairs.

This is really cool stuff and I will surely introduce most of it to my EA.

Couple of things I have already decided upon -
1. Only one pair per EA.
2. Only one order per pair. (This may change later but I will stick to this till I become proficient enough).

Thank you very much for sharing your golden experiences.

Pankaj
 

This graph shows the time interval between ticks for EURUSD on 29 April 2009. I do not know what time zone this data represents. The tick data was downloaded from Gain Capital.

As you can see there are some periods during the day when the interval between ticks frequently exceeds one minute and occasionally two minutes.



 
vangrosh:

Then it's just a simple matter of creating a new optimal .set file for each pair.

vangrosh: How do you create and use a .set file? I couldn't find any reference for that type of file.

 
cloudbreaker wrote >>

Also consider that if you need to build an EA which can recover from power outages etc. without user intervention to update market conditions etc. you will find yourself building file access functionality anyway.

Please take a look here: https://book.mql4.com/special/index

General Characteristics of Complex Programs


The way to go if you would like to create multi cirrency EAs.

 
StraightTrader:

Please take a look here: https://book.mql4.com/special/index

General Characteristics of Complex Programs


The way to go if you would like to create multi cirrency EAs.

Thanks for the reference.

 
FXtrader2008 wrote >>

This graph shows the time interval between ticks for EURUSD on 29 April 2009. I do not know what time zone this data represents. The tick data was downloaded from Gain Capital.

As you can see there are some periods during the day when the interval between ticks frequently exceeds one minute and occasionally two minutes.

Thanks for the graph. It would be nice to match it with the one of some other currencies, just to make sure you don't have the same delays. (I'll apreciate if you could easilly present charts for lets say 4 pairs)

I have just remarked, when I started to build my EA, that when I do not have ticks for 1 or 2 minutes on eurusd, then it's the same for other currencies. May be it was just a hazard at that time or a bad assomption I made but I kept this idea and my EA is running for months now with no problem. The factual experience compensate the lack of logic and I kept my EA working like this. If i had encountered problems I would have implanted your solution, I mean your idea of keeping your EA awake with just a delay inbetween 2 runs. Logically that's how it should be done, as far as I know : but I don't know how ticks are generated by the brokers, so it's difficult to go further.

It's also 'normal' or 'better' to code according to the system within your program is running.

So allowing multi currencies treatment, we normally should be able to attach our EA to a channel providing all the ticks. That's a lack in the system logic we have to deal with in one way or another.

Regards

Reason: