How to close two orders of different pairs with the same magic number at a certain profit without touching the other orders?
Hi,
Below is the modified version,
Regards.
Hi,
Thank you for your quick reply.
I have tried your corrected version but all close in negative profit, attached the history of closed orders.
If you can review the code and try it on a demo account to see if it really works.
Still remains the reopening of orders after their closures
Thank you my brother.
would love to see this working!
I hope he will be well corrected, I trust our friend "Mehrdad Jeddi" and all the other friends!!
Since you are categorizing groups of trades by magic number then there is no reason to hard-code symbols. Just iterate over each magic number and select orders based on that magic number.
#property strict input double inpProfitPercent=0.001; const int MAGIC_NUMBERS[] = {100, 101, 102}; #include <stdlib.mqh> #include <arrays/arrayint.mqh> //+------------------------------------------------------------------+ int OnInit() { EventSetTimer(1); return(INIT_SUCCEEDED); } void OnTick() { } void OnTimer() { check_and_close(MAGIC_NUMBERS, profit_target()); } double profit_target() { return AccountBalance()* inpProfitPercent; } void print_last_error(string error) { printf("%s: %s", error, ErrorDescription(GetLastError())); } void check_and_close(const int &magic_numbers[], const double min_profit) { for (int i=ArraySize(magic_numbers)-1; i>=0; --i) { int magic_number = magic_numbers[i]; double profit = 0.0; CArrayInt tickets; RefreshRates(); for (int j=OrdersTotal()-1; j>=0; --j) { if (OrderSelect(j, SELECT_BY_POS) && OrderType() < 2 && OrderMagicNumber() == magic_number ){ profit += (OrderProfit() + OrderSwap() + OrderCommission()); tickets.Add(OrderTicket()); } } if (profit >= min_profit) { for (int j=tickets.Total()-1; j>=0; --j) { if (OrderSelect(tickets[j], SELECT_BY_TICKET)) { RefreshRates(); if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100)) { print_last_error("OrderCloseError"); return; } } else { print_last_error("OrderSelectError"); } } } } }
OK,I improved it,it's working well on my side,
Thank you for what you do for me and for the community of traders, I tried your new correction but still his close in loss by pair is bizard and I do not know why it does not work ... !!
So you should increase the "ProfitPercent",0.001 is very low on your low amount balance,
Since you are categorizing groups of trades by magic number then there is no reason to hard-code symbols. Just iterate over each magic number and select orders based on that magic number.
Thank you "Mr nicholi shen" to you all too wonderful, I adapted the code that you posted and apparently it works well because it closed on a first win with two pairs, it's true that this a good idea of simplify the code, but the work that you sent us frankly I could never do it, because I am new in the programming mq4.
I have a second small service to ask you if possible, I want orders that close and reopen just after closing.
In the meantime I check again and again if your code works well and I'll tell you tomorrow.
thank you so much!!
So you should increase the "ProfitPercent",0.001 is very low on your low amount balance,
ok I'll try to increase it and I'll give you the answer
Thank you so much :-)
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I develop a simple little strategy that consists of opening 6 orders at a time from different pairs, and I want that when two pairs with the same magic number reach a certain profit on money together they close without touching the 4 remaining orders, after closing both first i also want them to open just after closing, so on for all 3 pairs, so the strategy works by 2 pairs * 3!!
The part of the code I want you to help me close two orders of different pairs at a certain profit together:
attached mq4 file
I know my code is wrong, if someone can help me, thanks in advance!!
or
if someone can download the mq4 code and corrects it will make me a lot of fun :-)