getting "position ticket" and identifying when open posion hit SL and TP

 

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:

// global variables
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 )
                       );
I am interesting, when the price hit tp1, to modify the SL of "Buy TP2:" position to ( sl + sl_size * 2 )

and I identified the following method to do the job:
// 3. move up SL of "Buy TP2"
tr_entry.PositionModify( position_ticket,  sl + sl_size + sl_size,  tp2 );
Q1. but I struggle to get the position_ticket, after execution of 1. and 2.,
    and I can not use the
PositionModify( _Symbol, sl, tp );
    because it could happen to have several open positions in the moment

Q2. I tried to hang on event handler:
OnTradeTransaction( const MqlTradeTransaction &trans,
                    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!

Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
  • www.mql5.com
MetaQuotes Programming Language 5 (MQL5), included in MetaTrader 5 Client Terminal, has many new possibilities and higher performance, compared to MQL4. This article will help you to get acquainted with this new programming language. The simple examples of how to write an Expert Advisor and Custom Indicator are presented in this article. We will also consider some details of MQL5 language, that are necessary to understand these examples.
 

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.

 
Marzena Maria Szmit #:

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 );

and also, how to "loop" in the opened position with specific MacgicNumber?
 
theodorei:

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!

Please check my library if it would work with you..
 

Thank you @amrali :-)

that answer some of my question - I will check it!

it seems that the trade mechanism is developed by Microsoft system engineer :-D

so complex and confusing :-D

 

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 ?

 
theodorei #:

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 ?

Hi i am happy it helped you.

I wrote that after extensive testing plus the help of available docs about the subject.