CDealInfo -> Magic() not returning magic number on profitable deal
Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий
fxsaber, 2024.07.21 08:39
The magic for IN trades and OUT trades may not be the same for the same position.
- **Order**: An instruction to execute a trade (e.g., buy or sell).
- **Deal**: The result of executing an order, either opening or closing a position.
- **Position**: The current open trade resulting from one or more deals.
// Setting the magic number for an order int magicNumber = 123456; double lotSize = 0.1; trade.SetExpertMagicNumber(magicNumber); trade.Buy(lotSize, _Symbol);
void PrintOrdersAndDeals() { for(int i = OrdersHistoryTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) { if(OrderMagicNumber() == magicNumber) { Print("Order #", OrderTicket(), ": Magic Number = ", OrderMagicNumber()); } } } for(int i = DealsTotal() - 1; i >= 0; i--) { if(DealSelect(i)) { if(DealMagic() == magicNumber) { Print("Deal #", DealTicket(), ": Magic Number = ", DealMagic()); } } } }
- **Orders**: Check the magic number associated with orders to ensure they are set correctly.
- **Orders**: Check the magic number associated with orders to ensure they are set correctly.
- **Deals**: Check the deals resulting from these orders to confirm the magic number is propagated.
Found the reason why some deals and orders were returning zero for profit.
For the EA I had the following:
#include <Trade\Trade.mqh> #include <Trade\PositionInfo.mqh> CTrade trade; CPositionInfo position; // ... and later in the code I was calling the following function which closed trades when in profit: closeProfitablePositions(); // ... void closeProfitablePositions() { CPositionInfo p; CTrade t; for (int i = PositionsTotal() - 1; i >= 0; i--) { if (p.SelectByIndex(i) && p.Profit() > 0) { ulong ticket = p.Ticket(); if (!t.PositionClose(ticket)) { Print("Failed to close position with ticket: ", ticket, " Error: ", t.ResultRetcode(), " - ", t.ResultRetcodeDescription()); } } } }
This resulted in a new CPositionInfo object being created with a zero magic number.
Solution was to use the parent CPositionInfo/CTrade objects:
// ... closeProfitablePositions(position, trade); // ... void closeProfitablePositions(CPositionInfo &p, CTrade &t) {...}
Greets,
This has been working for a while now, but today I noticed it's not working as expected. Not sure what's changed (maybe nothing has changed and this has never worked).
I basically want to trigger some code when a deal is in profit/loss. It works when the deal is in loss (ie, hits the SL), but not when in profit.
Perhaps I'm not understanding:
...which works when a position hits the SL, but not when I want to programmatically trigger a TP.
Here's the code:
I'd appreciate any pointers on what I'm missing.
Thanks