Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

First independent community of traders

First independent community of traders Logo First independent community of traders Logo

First independent community of traders Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • Buy Theme
  • Home
  • About Us
  • Blog
  • Contact Us
Home/ Questions/Q 2312
Next
In Process
vvebus
vvebus

vvebus

  • 3 Questions
  • 6 Answers
  • 0 Best Answers
  • 20 Points
View Profile
  • 0
vvebus
Asked: December 26, 20202020-12-26T12:01:52+00:00 2020-12-26T12:01:52+00:00In: Forex Expert Advisors

Netting MT5, how to catch the closing moment by stop loss.

  • 0

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!

  • 14 14 Answers
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    14 Answers

    • Voted
    • Oldest
    • Recent
    1. alexeyvik

      alexeyvik

      • 1 Question
      • 74 Answers
      • 0 Best Answers
      • 172 Points
      View Profile
      alexeyvik
      2020-12-26T12:03:35+00:00Added an answer on December 26, 2020 at 12:03 pm

      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?

      Start by carefully reading the documentation. There is such a thing

      The type of the trade transaction is passed in the type parameter of the MqlTradeTransaction structure.
      Possible types of trading transactions are described by the following enumeration:

      ENUM_TRADE_TRANSACTION_TYPE

      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.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. fxsaber

      fxsaber

      • 1 Question
      • 54 Answers
      • 0 Best Answers
      • 134 Points
      View Profile
      fxsaber
      2020-12-26T12:03:27+00:00Added an answer on December 26, 2020 at 12:03 pm

      vvebus:

      technically, the position remained open – but there was a close on SL and I can’t yet figure out how to catch this close in this case.

      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!");
      }

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    3. tara

      tara

      • 0 Questions
      • 9 Answers
      • 0 Best Answers
      • 38 Points
      View Profile
      tara
      2020-12-26T12:03:16+00:00Added an answer on December 26, 2020 at 12:03 pm

      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?

      Closing an order is not an event. It happens not on the MQL side, but on the server. MQL doesn’t see it. 

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    4. vvebus

      vvebus

      • 3 Questions
      • 6 Answers
      • 0 Best Answers
      • 20 Points
      View Profile
      vvebus
      2020-12-26T12:03:09+00:00Added an answer on December 26, 2020 at 12:03 pm

      Alexey Viktorov:

      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 a stop position and activating a pending order will be separated, even if these events occurred simultaneously.

      Yes, I did not mention multidirectional orders, but they are there.. But not the point.

      Alexey Viktorov:

      And the fact that vvebus did not understand the possibilities at all
      OnTradeTransaction() are obvious. 

      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?

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    5. alexeyvik

      alexeyvik

      • 1 Question
      • 74 Answers
      • 0 Best Answers
      • 172 Points
      View Profile
      alexeyvik
      2020-12-26T12:03:03+00:00Added an answer on December 26, 2020 at 12:03 pm

      Andrey Barinov:

      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.

      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.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    6. wahoo

      wahoo

      • 0 Questions
      • 7 Answers
      • 0 Best Answers
      • 34 Points
      View Profile
      wahoo
      2020-12-26T12:02:55+00:00Added an answer on December 26, 2020 at 12:02 pm

      Alexey Viktorov:

      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?

      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.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    7. alexeyvik

      alexeyvik

      • 1 Question
      • 74 Answers
      • 0 Best Answers
      • 172 Points
      View Profile
      alexeyvik
      2020-12-26T12:02:48+00:00Added an answer on December 26, 2020 at 12:02 pm

      Andrey Barinov:

      Did you read the first post of the branch?

      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.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    8. vvebus

      vvebus

      • 3 Questions
      • 6 Answers
      • 0 Best Answers
      • 20 Points
      View Profile
      vvebus
      2020-12-26T12:02:42+00:00Added an answer on December 26, 2020 at 12:02 pm

      Andrey Barinov:

      I wrote a program logic and not the logic of the strategy. These are different things.

      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.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    9. alexeyvik

      alexeyvik

      • 1 Question
      • 74 Answers
      • 0 Best Answers
      • 172 Points
      View Profile
      alexeyvik
      2020-12-26T12:02:31+00:00Added an answer on December 26, 2020 at 12:02 pm

      Andrey Barinov:

       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 (set to pending
      order). 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 postponement
      a larger volume 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.

      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?

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    10. wahoo

      wahoo

      • 0 Questions
      • 7 Answers
      • 0 Best Answers
      • 34 Points
      View Profile
      wahoo
      2020-12-26T12:02:25+00:00Added an answer on December 26, 2020 at 12:02 pm

      vvebus:

      Unfortunately, the logic of the strategy cannot be changed.

      I wrote a program logic and not the logic of the strategy. These are different things.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    11. vvebus

      vvebus

      • 3 Questions
      • 6 Answers
      • 0 Best Answers
      • 20 Points
      View Profile
      vvebus
      2020-12-26T12:02:19+00:00Added an answer on December 26, 2020 at 12:02 pm

      Did so:

      • at the beginning and end of each tick, I push all transactions that were executed by DEAL_ENTRY_OUT into arrays.
      • I compare the number of tickets at the end of the last tick and the beginning of a new tick. 
      • If the number is different, it means that there was just some kind of closure.

      so far, everything is working as it should.

      Thank you all!

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    12. wahoo

      wahoo

      • 0 Questions
      • 7 Answers
      • 0 Best Answers
      • 34 Points
      View Profile
      wahoo
      2020-12-26T12:02:09+00:00Added an answer on December 26, 2020 at 12:02 pm

      vvebus:

      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: 

      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 accounts
      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
      according to SL, it was and I can’t yet figure out how to catch this closure in this case.

      Can anyone have any ideas? Thank you in advance!

       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.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    13. vvebus

      vvebus

      • 3 Questions
      • 6 Answers
      • 0 Best Answers
      • 20 Points
      View Profile
      vvebus
      2020-12-26T12:02:03+00:00Added an answer on December 26, 2020 at 12:02 pm

      Alexey Tarabanov:

      Vladimir Karputov:

      I will add the previous answer: OnTradeTransaction and ENUM_DEAL_REASON

      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.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    14. barabashkakvn

      barabashkakvn

      • 7 Questions
      • 162 Answers
      • 0 Best Answers
      • 314 Points
      View Profile
      barabashkakvn
      2020-12-26T12:01:58+00:00Added an answer on December 26, 2020 at 12:01 pm

      vvebus:

      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: 

      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 accounts
      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
      according to SL, it was and I can’t yet figure out how to catch this closure in this case.

      Can anyone have any ideas? Thank you in advance!

      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

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    Leave an answer

    Leave an answer
    Cancel reply

    Browse

    Sidebar

    Ask A Question

    Stats

    • Questions 504
    • Answers 2k
    • Posts 5
    • Comments 0
    • Best Answers 0
    • Users 681
    • Popular
    • Comments
    • Tags
    • forexcommunity

      Highlighting what’s important about questions & Answers on Discy Community!

      • 0 Comments
    • forexcommunity

      Introducing Keyboard Shortcuts, our first Labs feature

      • 0 Comments
    • forexcommunity

      Defining quality on Discy Engine — what a helpful answer ...

      • 0 Comments
    • forexcommunity

      Organizational and company accounts on Discy Engine the next step

      • 0 Comments
    • forexcommunity

      Hello world!

      • 0 Comments

    Related Questions

    • Brus Agell

      Правельный выбор

      • 0 Answers
    • pinupcasinoo

      Pin Up Casino

      • 0 Answers
    • c00l777

      During optimization, indicators that are not present in the robot ...

      • 2 Answers
    • c00l777

      During optimization, indicators that are not present in the robot ...

      • 0 Answers
    • c00l777

      Optimization results differ on different accounts

      • 3 Answers

    Users

    Leonardcig

    Leonardcig

    • 0 Questions
    • 0 Answers
    DennisByday

    DennisByday

    • 0 Questions
    • 0 Answers
    Haroldmeary

    Haroldmeary

    • 0 Questions
    • 0 Answers

    Footer

    First independent community of traders

    About

    An independent community of forex traders. This is where traders communicate. You can ask your questions and you will receive an answer to your question.
    • Terms of Use
    • Privacy Policy
    • Cookie Policy
    • Knowledge Base
    • Support

    © 2021 Forexcommunity.net. All Rights Reserved