fxDreema

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

    example: Save variables into file and read them back [advanced]

    Tutorials by Users
    11
    27
    10156
    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
      miro1360 last edited by miro1360

      important note:
      ... be sure that each EA must have own variables file (1 EA >> 1 variables file), otherwise there will be a big fight between EAs πŸ˜„ .... 1.EA>>varSave1.txt ... 2.EA>>varSave2.txt ....

      Sometimes we deal with situation, when EA have few critical variables and MT4 terminal must be restarted or was restarted by incident, these variables are no longer available and it can cause problems.
      Values from history trades can be obtained from trades history, but sometimes can be calculation very complicated. For this situation exist another way how to avoid loss of variables. They can be stored into text file with every variable change. For this situation I prepared small example how to do it.

      Example EA have two buttons. Try with tester.
      https://fxdreema.com/shared/HtX9lP51c
      After click on the button trade is opened (buy or sell). Here is small management with lots. When EA is inserted into chart, variable "varLotsBuy" get value from input parameter initLots1. After button click first trade (buy) is opened with "varLotsBuy". If this trade is closed with SL, variable "varLotsBuy" get value multiplied by input parameter "Lots1Multiplier". After new button click, next trade is opened with new value from variable "varLotsBuy". When trade is closed with TP, variable "varLotsBuy" is reseted from first value (initLots1).

      1. Summary: initLots1=0.1, Lots1Multiplier=1.6
      2. Buy: Lots=0.1, closed by SL
      3. Buy: Lots=0.1*1.6=0.16, closed by SL
      4. Buy: Lots=0.16*1.6=0.25, closed by TP
      5. Buy: Lots=0.1

      Example EA in chart:
      0_1506722412649_upload-eef0b83c-d92b-446f-b429-9abbc47683bc

      EA is using few input parameters:
      0_1506722484698_upload-6e5c0fd2-25b5-4241-92cc-331a2e1a02e5

      and few variables: (used are only varLotsBuy and varLotsSell, next variables are only as example)
      0_1506722509992_upload-99db7fe3-ffe9-4267-b01e-01e3a30d99ed

      on Init section draw two buttons and with Condition block (File exist) it check existence of "file with variables", if this file does not exist, EA continue with normal start (with nulled variables), but if file exist, custom code read and initialize variables from this file:
      0_1506722696116_upload-d207c06d-c088-4806-9793-b802c390d3a5

      this is how code looks like:
      0_1506722725210_upload-af7242e9-c719-4e80-a38f-229a9b98bcd2
      strArr[20] is simple temporary array where values are readed from file, each line is one variable (size of array 20 can be increased to any higher size, depends on how much variables you need, it can by much more higher as count of variables, there is no strict rule)
      at bottom of code are variables one after another, in my case they are:
      varLotsBuy = (double)strArr[0];
      varLotsSell = (double)strArr[1];
      var0 = strArr[2];
      var1 = strArr[3];
      var2 = strArr[4];
      var3 = strArr[5];
      var4 = strArr[6];
      var5 = strArr[7];
      important are order and type of variables (order must be same as in file-write code)

      on Tick section just open trades after button click:
      0_1506723117024_upload-6475960e-08e9-4340-b31e-ba7d7003bb9f

      and in on Trade section is calculation where variables are calculated/changed and re-saved into file:
      (this saving into file can be done everywhere you need, in on Tick section, on Timer, on Chart ...)
      0_1506723172046_upload-50cdb367-bc34-415a-9a0c-ff151cb593f3
      and this how code for save into file looks like:
      0_1506723258821_upload-944a12fd-7849-40c6-8fb4-15f696b3cd13
      you see, pretty easy, nothing complicated .. variables are writed one after another with FileWrite function into new lines in file ... (once again, order of values in file is important, it must be compatible with file-read code)

      File with variables is saved into MQL folder:
      0_1506723422647_upload-1d9bad30-b0b1-43ee-9bab-e414cbefedfb
      in case of tester it is file: tester\files\yourFileName.txt
      in case of normal EA in chart: MQL4\files\yourFileName.txt

      Note, that this file can be opened and edited also manually:
      0_1506723663570_upload-224652b6-5c75-4523-8320-ba33c1ba01bf

      And when you need full reset of variables, before you place EA into chart, delete this file (or create input parameter for delete function, function for this is simple:
      FileDelete(fileName,0);

      I hope my explanation was not very complicated ... have a fun ...

      Cheers ...

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

      J M 2 Replies Last reply Reply Quote 5
      • J
        Jennifer @miro1360 last edited by

        @miro1360
        hey thanks for the tutorial
        can you help me with this : https://fxdreema.com/forum/topic/5162/any-way-to-do-this

        ===>Jennifer Syn<===

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

          @miro1360 you re a genius! I work with EAs that depend greatly on variables and you don't know how many times I had a blackout hampering the workings of said EA. I had been thinking in a workaround to this but this is THE solution. Many thanks!πŸ˜€

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

            @Andermaiden2nd perfect! try if is it working because I did it "on the fly" without some serious testing πŸ˜‰

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

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

              @miro1360

              Let us know when you have finished writing an FX Dreema course Miro.....πŸ‘

              I'll sign up now...

              A,

              1 Reply Last reply Reply Quote 2
              • Andermaiden2nd
                Andermaiden2nd @miro1360 last edited by

                @miro1360 I tested it and it works well (at least haven't found any bug so far and tested it with different types of variables). This effectively adds more value to variables as they can now be relied upon to store (and manipulate) more vital information.

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

                  @Andermaiden2nd thanks for info ... if you find something not working, give reply and I (or we πŸ˜„ ) try to find solution πŸ˜‰
                  ... be sure that each EA must have own variables file (1 EA >> 1 variables file), otherwise there will be a big fight between EAs πŸ˜„

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

                  Andermaiden2nd 1 Reply Last reply Reply Quote 1
                  • Andermaiden2nd
                    Andermaiden2nd @miro1360 last edited by

                    @miro1360 yes, I'm aware that when using more than one EA on an account it is needed to give a different name to the constant "tempFileName" like varSave2.txt instead of varsave1.txt (or any other name). It is still important to leave that point clarified for anyone who tries this, so thaks again! :simple_smile:

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

                      @Andermaiden2nd note added into first post πŸ™‚

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

                      1 Reply Last reply Reply Quote 1
                      • U
                        UnicornMadness last edited by UnicornMadness

                        Just wanted to say: "You are awesome!!".

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

                          I have not tried it yet, but with variables file it can be done (or maybe also with terminal variables) ... problem still to find perfect strategy πŸ˜„

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

                          1 Reply Last reply Reply Quote 0
                          • P
                            padletut last edited by

                            An old post, but Thank you again @miro1360 ❀

                            1 Reply Last reply Reply Quote 1
                            • CedieTraderMax
                              CedieTraderMax last edited by

                              Nice tutorial on Storing Variables...
                              Perfect!
                              Thank you again @miro1360 ❀

                              1 Reply Last reply Reply Quote 1
                              • MrDaisyBates
                                MrDaisyBates last edited by

                                Thanks for the post. I know it's old, but if you were using MT5 instead of MT4, is this a more feasible route than using the "Global Variables" in MT5? Are there pros and cons to weighing each approach? I've never tried either, but a lot of criteria I like to use relies on variables and it's super aggravating when they're lost due to resetting something.

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

                                  They are both very similar, especially when it comes to FX.

                                  Learn fxDreema Without the Wait!

                                  My comprehensive book, available on Amazon, is packed with examples and invaluable insights to help you fast-track your learning journey.

                                  The paperback and hardback editions include MT4 & MT5 QR codes for easy access to all prebuilt projects and robots, including my latest gold trading robot!

                                  Don’t miss out

                                  Click here➑️ https://mybook.to/fxDreema to get your copy today!

                                  Enjoy! 😊

                                  MrDaisyBates 1 Reply Last reply Reply Quote 1
                                  • MrDaisyBates
                                    MrDaisyBates @jstap last edited by

                                    @jstap Thanks!

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

                                      I've still not learned any actual "coding" yet really (hence why I'm using Dreema).

                                      How similar is this in MQL5? I don't really know how different their syntax is, but it seems like these functions would be so basic that they might possibly be the same in both languages?

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

                                        The code is about 98% the same, a little different though.

                                        Learn fxDreema Without the Wait!

                                        My comprehensive book, available on Amazon, is packed with examples and invaluable insights to help you fast-track your learning journey.

                                        The paperback and hardback editions include MT4 & MT5 QR codes for easy access to all prebuilt projects and robots, including my latest gold trading robot!

                                        Don’t miss out

                                        Click here➑️ https://mybook.to/fxDreema to get your copy today!

                                        Enjoy! 😊

                                        1 Reply Last reply Reply Quote 0
                                        • E
                                          edro77 last edited by

                                          Hi is it possible to save bool variables instead of double variables?

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

                                            Just create type bool

                                            Learn fxDreema Without the Wait!

                                            My comprehensive book, available on Amazon, is packed with examples and invaluable insights to help you fast-track your learning journey.

                                            The paperback and hardback editions include MT4 & MT5 QR codes for easy access to all prebuilt projects and robots, including my latest gold trading robot!

                                            Don’t miss out

                                            Click here➑️ https://mybook.to/fxDreema to get your copy today!

                                            Enjoy! 😊

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

                                            Online Users

                                            S
                                            A
                                            R
                                            B
                                            N
                                            M
                                            A
                                            T
                                            V
                                            M
                                            A
                                            A
                                            R

                                            25
                                            Online

                                            146.7k
                                            Users

                                            22.4k
                                            Topics

                                            122.6k
                                            Posts

                                            Powered by NodeBB Forums | Contributors