fxDreema

    • Register
    • Login
    • Search
    • Back to the main page
    • Categories
    • Recent
    • Tags
    • Popular
    • Search

    PositionCloseBy function

    Questions & Answers
    3
    6
    3192
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      moreirajbn last edited by moreirajbn

      Hello,

      I would like to implement the function PositionCloseBy on a custom block (Custom MQL code). This function closes a position with an opposite position. For example: if there is one buy and one sell position open, the function will be able to close the buy with the sell position (or vice-versa), without the need of executing two new orders to exit both positions. Here is the MQL information: https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctradepositioncloseby

      I know this is done automatically in MT5 "netting" accounts, but I need to use it on a regular "hedging" account.

      I have tried very hard to make it work, but couldn't. I was hoping I could get some help.

      @miro1360 , would you be so kind to have a try? I have seen you do the impossible with fxdreema blocks 🙂

      You could perhaps use this simple project, that will open 2 buys + 1 sell position: https://fxdreema.com/builder/shared/dfo4SBX2e

      My goal is to be able to leave only one buy position open (the newest).

      Thanks a lot

      1 Reply Last reply Reply Quote 0
      • fxDreema
        fxDreema last edited by

        This is what I made as an example: https://fxdreema.com/shared/kUibWj44 It works... kinda. I personally don't really see the need of this function if we want to close all trades into one. Because in the process we actually create new trades and it gets messy. I'm probably wrong, but I think that this function is to be used for 2 trades only, when they are known and there is a reason to turn them into one. I don't like what happens when we want to turn more than one trade into one.

        1 Reply Last reply Reply Quote 0
        • M
          miro1360 last edited by miro1360

          The function PositionCloseBy is from the Trade.mqh library and belongs under CTrade class and therefore an object must be created.

          https://fxdreema.com/shared/E5T9pyT8e

          0_1549295696958_28f6c6a3-6c4f-4ca3-a347-cae4da2c1c93-image.png

          from FxDreema generate only mql5 code
          open generated code in the MetaEditor, in your computer

          add this include in the top of code:
          0_1549294949609_244f06f3-53de-43d4-bb68-5f67d95efaae-image.png

          Above the OnInit() function add this CTrade object Trade definition:
          0_1549295003446_150516d8-63f7-4112-96f4-32c465d926eb-image.png
          Click on the Compile button in the MetaEditor, it generates ex5 file and done 😄

          Between trading and gambling is a very small gap, be careful.

          M 1 Reply Last reply Reply Quote 0
          • M
            moreirajbn @miro1360 last edited by

            @miro1360

            Awesome miro... that's exactly what I needed.

            Thank you so much.

            1 Reply Last reply Reply Quote 0
            • M
              miro1360 last edited by miro1360

              you can change the code like this:
              0_1549339596228_8c312092-01cb-477b-9051-746577bf62f9-image.png
              which will run that function only once, when both ticket numbers are recorded

              ... or you can create another rearrangement of blocks too, like this one:
              0_1549339236243_f80b7faa-0f6d-4c8a-995a-af41393ce2d0-image.png
              where the custom code block happens after all the cycles are finished
              but in this case, custom code block must have its ID number larger (yellow circle) than any number from the green circle

              ... each for loop can go through all the trades (can happens more than 1x) and condition blocks as filters can be added (and record only that trade, which will match the filter) ... but thats your job 😄

              Between trading and gambling is a very small gap, be careful.

              M 1 Reply Last reply Reply Quote 0
              • M
                moreirajbn @miro1360 last edited by

                @miro1360

                Excellent Miro...

                I got the concept... my problem was with the initial code. 🙂

                Thanks a lot again.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post

                Online Users

                J
                T
                J
                B

                18
                Online

                146.7k
                Users

                22.4k
                Topics

                122.6k
                Posts

                Powered by NodeBB Forums | Contributors