I NEED TO LOGIC
-
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. -
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
-
@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
-
@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.

-
how i can SET the new balance after opening the transaction?
-
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
-
@jstap I honestly did not understand, but my question is: how do I store the balance in a variable after each SELLOR BUY PLEASE?
-
Put that code in one of the variable boxes (white, orange, gray), put the variable in a comment and watch what happens.