The problem is this:
you need to catch the moment of closing the position (by sl, tp or manual closing) in the netting version. I catch it with a simple check:
OnTick(){
......
int market_trades = TM.GetMarketPositions();
if (!market_trades && last_market_trades > 0){
UpdatePendingOrders();
}
last_market_trades = TM.GetMarketPositions();
......
}
That is, I check every tick if there was a position on the last tick, but there is no position on this tick – it has just closed.
The essence of the problem is as follows:
EA opens three pending orders, for example, SellStop. One of them becomes a market one. A little later, the position is closed on SL and on
at the same tick, one of the pending orders becomes a market order. Thus, technically, the position remained open – but closing on SL
it was and I still can’t figure out how to catch this closure in this case.
Can anyone have any ideas? Thank you in advance!
alexeyvik
Start by carefully reading the documentation. There is such a thing
After reading the table of these types, you can experiment in the debugger, which will speed up understanding.
If you are only interested in opening or closing positions, you can only stop at studying the TRADE_TRANSACTION_DEAL_ADD type
I start analyzing the closing of a position by saying that there is a transaction, its type is TRADE_TRANSACTION_DEAL_ADD, but it is no longer possible to select a position.
if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
{
if(!PositionSelectByTicket(trans.position))/ / & & PositionGetInteger(POSITION_MAGIC) = = magick) & & check the symbol and whatever your heart desires
{
// Here it is already necessary to go into history and
if(HistoryDealSelect(trans.deal))
{
long hdr = HistoryDealGetInteger(trans.deal, DEAL_REASON);
if(hdr = = DEAL_REASON_SL)
;/ / here are actions provided that the position is closed by SL
}
}
}
BUT! this is for working on a hadge account and there is no check for ENUM_DEAL_ENTRY entering the market or exiting the market, in my case
, this is not necessary in this particular expert Advisor. This is already your own decision, whether it is necessary or not.
fxsaber
Forum on trading, automatic trading
systems and testing of trading strategies
Questions from
beginners MQL5 MT5 MetaTrader 5
fxsaber, 2018.08.02
17:09
#include //
void OnTrade()
{
static int PrevTotal = OrdersHistoryTotal();
const int Total = OrdersHistoryTotal();
for (int i = Total - 1; i >= PrevTotal; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderCloseReason() == DEAL_REASON_SL))
Alert("Stop!");
PrevTotal = Total;
}
Or
void OnTradeTransaction( const MqlTradeTransaction &Trans, const MqlTradeRequest&, const MqlTradeResult& )
{
if (HistoryDealSelect(Trans.deal) && (HistoryDealGetInteger(Trans.deal, DEAL_REASON) == DEAL_REASON_SL))
Alert("Stop2!");
}
tara
Closing an order is not an event. It happens not on the MQL side, but on the server. MQL doesn’t see it.
vvebus
Yes, I did not mention multidirectional orders, but they are there.. But not the point.
Can you hint how to solve my question on this event (check whether the position was closed, the reason is not important)?
Will it all come down to ENUM_DEAL_ENTRY?
alexeyvik
How does it not matter? If unidirectional, then the position size will increase, otherwise it will decrease down to zero and even to a change in
direction.
Only here I have never checked how the stop loss will be worked out on netting, if the stop is set when the position size was, the path is 0.1 a in
when the stop was triggered, the position size became 0.2, 0.5 or even larger. The entire position will be closed, or only the initial size, that lot size
which was at the time of setting the stop loss?
Here, to prevent such incomprehensible cases on netting accounts, I would prefer to put orders of the opposite
direction instead of stops.
I wasn’t going to apologize.
wahoo
It doesn’t matter if they are unidirectional or multidirectional.
If logically they are executed on the same tick, as described in the first post, then the order of execution is not guaranteed. This means that
the delay can work earlier than the stop and this will lead to undesirable consequences.
Your apology is accepted.
alexeyvik
Well. I read it a little inattentively the first time. How many times have you read it inattentively? Where does it say about multidirectional orders?
In fact, the answer was given immediately the most correct. All this is caught when processing the OnTradeTransaction() event, where in the case of closing
the position of the foot and activation of a pending order will be divided, even if such events occurred at the same time.
And the fact that vvebus did not understand the possibilities at all
OnTradeTransaction() are obvious.
vvebus
The fact is that in this case, the terms “program logic” and “strategy logic” coincide. The level of pending orders is determined from
indicators – I can’t interfere with this logic (not my strategy).
The bug described by you will be solved as soon as it manifests itself, the essence of it is understood. It will be interesting to play it.
alexeyvik
And if the strategy does not provide for opening pending orders at all? Why give advice, as well as go to someone else’s monastery with your
own Charter?
wahoo
I wrote a program logic and not the logic of the strategy. These are different things.
vvebus
Did so:
so far, everything is working as it should.
Thank you all!
wahoo
If I were You, I would change the logic of the program a little. The way it is now, on a real account is fraught with problems.
The situation. There is a sell position and it has a stop loss. At the same stop loss level, you want to open in buy with the same lot (a pending order is set). In
reality, it may happen that the delay will work BEFORE the stop and the position will collapse. And there won’t be a new one. If the Deposit is larger than
the current position, then the result will be an INOUT transaction and a new position of small volume, and even without stops.
In the tester, this is not enough to play, but on a live account it can be easy.
vvebus
Should help, thanks!
So far, I do not see a manual deletion, but I will conduct tests, maybe throw the necessary reason into the log. Then I’ll unsubscribe.
barabashkakvn
I will add the previous answer: OnTradeTransaction and ENUM_DEAL_REASON
ENUM_DEAL_REASON
ID
Description
DEAL_REASON_CLIENT
The transaction was carried out as a result of triggering the order placed
from the desktop terminal
DEAL_REASON_MOBILE
The transaction was carried out as a result of triggering the order placed
from the mobile app
DEAL_REASON_WEB
The transaction was carried out as a result of triggering the order placed
from the web platform
DEAL_REASON_EXPERT
The transaction was carried out as a result of triggering the order placed
from an MQL5 program-an expert Advisor or a script
DEAL_REASON_SL
The deal
carried out as a result of triggering the Stop Loss order
DEAL_REASON_TP
The deal
carried out as a result of triggering the take Profit order
DEAL_REASON_SO
The transaction was executed as a result of the Stop Out event
DEAL_REASON_ROLLOVER
The transaction was made due to the transfer of the position
DEAL_REASON_VMARGIN
The transaction was carried out due to the accrual/write-off of variational
margins
DEAL_REASON_SPLIT
The transaction was carried out due to a split (decrease in price) of the instrument
for which there was a position at the time of the split