fxDreema

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

    I NEED TO LOGIC

    Questions & Answers
    2
    8
    448
    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.
    • khalids222
      khalids222 last edited by

      In this example, I wanted to design an expert who opens a deal when the Equity drops ten dollars. Every time the Equity drops ten dollars, he opens a deal, but I could not create the logic for this method. I do not want to open a deal at every ten points through the pink blocks. I want to open a deal. When the balance drops to only ten dollars
      https://fxdreema.com/shared/fedtKqgR
      I used GPT chat, and it was explained, but I did not understand it in the BLOCKS method. Can someone explain it in the BLOCKS method?

      double initialEquity;
      double threshold = 10;

      int OnInit() {
      initialEquity = AccountEquity();
      return INIT_SUCCEEDED;
      }

      void OnTick() {
      double currentEquity = AccountEquity();

      if (initialEquity - currentEquity >= threshold) {
          Print("Opening a trade because equity decreased by ", threshold, " dollars.");
          
          // Open a buy or sell trade randomly
          if (MathRand() % 2 == 0) {
              if (OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0, "Opening a buy trade", 0, 0, clrGreen)) {
                  Print("Buy trade opened successfully");
              } else {
                  Print("Failed to open buy trade");
              }
          } else {
              if (OrderSend(Symbol(), OP_SELL, 0.1, Bid, 3, 0, 0, "Opening a sell trade", 0, 0, clrRed)) {
                  Print("Sell trade opened successfully");
              } else {
                  Print("Failed to open sell trade");
              }
          }
          
          // Update initial equity
          initialEquity = currentEquity;
      }
      

      }

      In this code:

      We define initialEquity to store the initial equity when the script starts.
      We define threshold as 10 dollars.
      In the OnInit function, we set initialEquity to the current equity.
      In the OnTick function, we compare the current equity with initialEquity.
      If the equity has decreased by threshold or more, a trade is opened randomly (buy or sell).
      After opening the trade, we update initialEquity to the current equity to ensure the condition applies the next time equity decreases by another 10 dollars.
      This way, a trade is opened each time the equity decreases by 10 dollars, whether it's a buy or sell trade, randomly.

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

        You can add this code to custom code. You do not need on init or on tick because it is in FX , you don't need to create variables on FX unless you want to view or use the result elsewhere, in which case you will need to delete them from the code

        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! 😊

        khalids222 2 Replies Last reply Reply Quote 0
        • khalids222
          khalids222 @jstap last edited by

          @jstap First of all, thank you for the response, but I tried more than once and it did not work in all cases. I do not know what the problem is

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

            @jstap https://fxdreema.com/shared/5WxWi9tad
            I have tried all the methods, but the problem is how to SET the new balance after opening the transaction. I put a zero number in the balance box after the buying and selling process, but I do not know whether it is correct or not.
            image.png

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

              how i can SET the new balance after opening the transaction?

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

                The balance only changes on a trade closing. this code in the box will put a value into the variable:
                AccountBalance()
                AccountEquity()

                There are many codes you can add to these boxes

                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! 😊

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

                  @jstap I honestly did not understand, but my question is: how do I store the balance in a variable after each SELLOR BUY PLEASE?

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

                    Put that code in one of the variable boxes (white, orange, gray), put the variable in a comment and watch what happens.

                    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
                    • 1 / 1
                    • First post
                      Last post

                    Online Users

                    L
                    F
                    S
                    F
                    N
                    S
                    E
                    H
                    E

                    20
                    Online

                    146.7k
                    Users

                    22.4k
                    Topics

                    122.6k
                    Posts

                    Powered by NodeBB Forums | Contributors