Before using the CTrade class – you need to specify magic number (also you can specify slippage etc). – this way you can later detect your trades by magic number. It can be done in OnInit:
tr_entry = new CTrade();
tr_entry.SetDeviationInPoints(Slippage);
tr_entry.SetExpertMagicNumber(MagicNo);
Then you can go in a loop of positions and find if there is one or two trades opened (by checking magic number) – if only one is remained you can modify then this position remain by Position Modify function.
Before using the CTrade class – you need to specify magic number (also you can specify slippage etc). – this way you can later detect your trades by magic number. It can be done in OnInit:
tr_entry = new CTrade();
tr_entry.SetDeviationInPoints(Slippage);
tr_entry.SetExpertMagicNumber(MagicNo);
Then you can go in a loop of positions and find if there is one or two trades opened (by checking magic number) – if only one is remained you can modify then this position remain by Position Modify function.
thank you, I do that initialization, but I never knew what it is used for :-D
and will it work if I do that:
tr_entry.SetExpertMagicNumber( 11 );
tr_entry.PositionOpen( .... TP1 );
tr_entry.SetExpertMagicNumber( 12 );
tr_entry.PositionOpen( .... TP1 );
hello,
I have more than 20 years programming experience, but now I make my first steps in MQL5 framework,
can you help me?
thank you in advance!
Thank you @amrali !
you library helped me to perfectly identify:
- when it is opened a position
- and when it hit the SL or TP of already open position
but how you come to those conditions?
was it by try and errors or there is some comprehensive documentation of the trading mechanism and
OnTradeTransaction() and OnTrade() handlers ?
Thank you @amrali !
you library helped me to perfectly identify:
- when it is opened a position
- and when it hit the SL or TP of already open position
but how you come to those conditions?
was it by try and errors or there is some comprehensive documentation of the trading mechanism and
OnTradeTransaction() and OnTrade() handlers ?

- 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 have more than 20 years programming experience, but now I make my first steps in MQL5 framework,
so far I made a simple Expert Advisor, and I read a lot of articles and passed thoroughly through the reference
but I still straggle with the following:
I am hanging on OnTick() event handler and in specific conditions I open two position:
MqlTick last_tick;
CTrade tr_entry();
// in some moment in OnTick()
double sl_size = 0.0004;
double sl = last_tick.ask - sl_size;
double tp1 = last_tick.ask + sl_size * 3;
double tp2 = last_tick.ask + sl_size * 6;
// 1. open "Buy TP1" position
tr_entry.PositionOpen( _Symbol, ORDER_TYPE_BUY, 0.5,
last_tick.ask,
sl,
tp1,
"Buy TP1: " + DoubleToString( tp1, 6 )
);
// 2. open "Buy TP2" position
tr_entry.PositionOpen( _Symbol, ORDER_TYPE_BUY, 0.5,
last_tick.ask,
sl,
tp2,
"Buy TP2: " + DoubleToString( tp2, 6 )
);
and I identified the following method to do the job:
tr_entry.PositionModify( position_ticket, sl + sl_size + sl_size, tp2 );
and I can not use the
Q2. I tried to hang on event handler:
const MqlTradeRequest &request,
const MqlTradeResult &result )
and explored the trans, request and result structures,
but I cannot recognize
- when it is opened a position after 1. or 2.
- and when it hit the SL or TP1 or TP2 of already open position
Q3. how are related request, position, deal and order?
I understand that when I open a position it have to be made some sell/buy and
when it is closed with SL or TP, it have to be made the opposite trades.
also I see that in the opening of one position there is both deals and order
and also even a request :-(
but what is the meaning of each and what is the workflow? when it come the deal, order, request?
can you help me?
thank you in advance!