I've been using these forums for a while but not posted before. I've been digging around but can't seem to find a full resolution to my query so here goes. I see this as more of a logic problem and not specific code so haven't included any coding.
I have an EA that runs on MT4 and runs multiple variations of open and close conditions at the same time, using the magic number as an identifier. It runs on a demo account currently doing forward testing for all variations, using arrays to track and record various data by MN. I then have a statistics script that runs through order history and separates orders by MN and exports a report to excel on various factors such as order time, total orders, profit orders, profit percent etc.
I'm looking to migrate this to MT5 to utilise the enhanced backtest facilities, if the broker has suitable tick data available. This EA also runs all magic numbers simultaneously in MT4 Tester, and then outputs an Excel file report.
My questions are:
- Is it possible for an MT5 position to have multiple magic numbers? Then do partial close for all parts with a specific MN.
- Or is it possible to identify which DEAL I want to close by DEAL_MAGIC?
I previously had multiple orders open at one, but only one per magic number at a time. I'm looking at partial closes in MT5, but can't work out if I'll be closing the right part of the position, can I then track the deal (DEAL_ENTRY_OUT) and the DEAL_PROFIT?
The reading I have done so far suggests that an MT5 position has one MN, so I think it'll be impossible to assign a magic number to an open deal, then isolate it once it make part of the overall position.
I think I may have to run each magic number variation in MT5 backtests individually due to the inability to assign multiple MN's to a position at any time.
Any help with this is much appreciated :)
I think you can have different magic numbers, but not for positions like you say, it would rather be for each orders. In mt5, there can only be 1 position per symbols, that's probably why there can only be 1 magic number. The position is basically the sum of each different orders, for that reason, I think each orders can have different magic numbers.
I am not 100% certain about that, so if somebody more experience than me could confirm, it would be great.
I think I now have a workaround for this, but would still appreciate any input as it definitely doesn't feel like the most efficient/effective use of code and functions!
My plan is to assign MagicNumber + 1 to each entry deal, and for 'closing' I will be sending deals with MagicNumber assigned (Example MagicNumber = 101000001 for Buy, MagicNumber = 101000000 for 'Close' (Sell)). This is so that I can later distinguish closing deals and export various data (profit/loss, no. of trades etc.). Each deal will be uniquely identified, as will the entry from the exit.
I still have a question that is causing me some confusion. If only one overall position is allowed per symbol, what is the function of close_by? It seems to be to take 2 open positions of opposite signs and use them to cancel each other out, but how is it possible to have these conditions if new deal just adjusts the volume and/or sign of the single overall position? Close_by did look like a good fit for my closing purposes, but I've so far failed to get it functioning in a simple buy/sell EA.
Is to identify which trades belong to the Expert Adviser with that specific magic number.
Not multiple magic Numbers.
If you are talking about positions, then it is Ticket Numbers, which identify each individual trade.
I do not understand why you are trying to use multiple magic numbers in one EA.
Please read here: https://www.mql5.com/en/docs/constants/tradingconstants/positionproperties
And also here: https://www.mql5.com/en/docs/constants/tradingconstants/dealproperties
Or here: https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions
For some examples.
It is the same function as having multiple EA's within one EA, the magic number identifier is used to segregate the different open and close functions used within one EA. I have 360 slight variations of open and close conditions, so to test them all simultaneously for direct comparison I found some posts on this site that suggested this method for MT4. Due to the low quality of backtest results in MT4 I needed to run them together to accurately compare performance.
It has worked well so far in MT4, and I am still running them on 4 demo account terminals to gather forward test data.
In MT4, orders were kept separate and each could have an individual magic number, and this allowed me to run them all on one EA but then isolate them by MN for data comparison when retrieving the order history.
My main aim for MT5 migration is to make use of the enhanced backtest tick data. This may mean that I don't need to run them all simultaneously if I can run one at a time but on accurate historical ticks.
Thanks again for input to this, it's still a work in progress but is great hearing other ideas about it.
I do not understand multiple EA's in one EA.
It's just one EA.
So, I have 360 possible combinations of slightly different open/close conditions. Instead of coding these to 360 EA's and having 360 charts open on the MT4 terminal, I have an EA that runs them all simultaneously, and manages one open order at a time for each MN. There may be 360 orders open at a time, but only one per MN.
At the end, I run a script on the order history, and this outputs results by MN. So the results output are similar to having 360 individual EA's operating, but not requiring 360 charts running. I have split it down to 4 terminals, but that's due to the delay in looping through open orders to close them.
That can not be the case.
Simply because running 360 logical operations in 360 timer events can never be the same as running 360 logical operations in one timer event.
All in one it's still one EA.
No it's probably not the same. It just fit my requirements at the time, which was to find a way of getting test results for multiple variations of open/close conditions at the same time for direct comparison. As I said, it's currently run on 4 terminals, and I have each EA split onto 3 charts per terminal, so only 30 variations running per chart. There is some closing delay but it serves its function well. There may be better ways of doing it, but when I read up about it on this forum it seemed the best fit at the time.
Do you have any thoughts on my question about close_by function in MT5? If you can only ever have one position per symbol (variable direction and volume), how can there ever be opposing positions open at the same time to use a close_by operation on?
Well it depends on the account type but in your case you would fire an opposing order of the matching or equal volume, to cancel out the position.
So you are either long or short but not at the same time, if you want that you have to use a Hedging account.