Closing Trades if floating balace is in X% In negative

shanmugapradeep

Corporal
Messages
84
Hello,

Logic i am looking for :

If floating Balance is less than 1% in negative of account balance => Close the trade (OR) if floating balance is less than $100 in negative => Close the trade


Code :

Code:
extern int negative_equity_percentage = 1;
extern int negative_equity_balance = 50;

double equity = AccountEquity();

double Account_balance = AccountBalance();

double floatingBalance = equity - Account_balance;

void CheckAndCloseTrade()
{
        double NewspercentageThreshold = 0.01 * negative_equity_percentage * Account_balance;

        if ((floatingBalance < NewspercentageThreshold) || (floatingBalance < -negative_equity_balance)) {

            closetrade();
        }
    }



void closetrade() {

//Closing Trade Logic

}

int start()
{

CheckAndCloseTrade(;

return(0);

}




Error : Balance, Equity and Floating balance do not change in Real time. Instead it takes the initial value when installing the EA and the values remains same.
 
Last edited:
The main purpose of your code is to automatically close trading positions if the floating balance (the difference between equity and account balance) falls below a certain percentage of the account balance or a specific dollar amount.

To fix the issue with the values not updating in real-time, you can make a small change to your code. You should move the calculation of balance, equity, and floating balance into the start() function. By doing this, you ensure that these values are updated every time the start() function is executed, providing you with current data to base your trading decisions on.
 
Here's an updated version of your code that addresses this issue by recalculating the values in the start() function:

extern int negative_equity_percentage = 1;
extern int negative_equity_balance = 50;

double equity;
double Account_balance;
double floatingBalance;

void CheckAndCloseTrade()
{
double NewspercentageThreshold = 0.01 * negative_equity_percentage * Account_balance;

if ((floatingBalance < NewspercentageThreshold) || (floatingBalance < -negative_equity_balance)) {
closetrade();
}
}

void closetrade()
{
// Closing Trade Logic
}

int start()
{
// Refresh account values
equity = AccountEquity();
Account_balance = AccountBalance();
floatingBalance = equity - Account_balance;

CheckAndCloseTrade();

return (0);
}
 
Back
Top