Close Trade when a condition is verify

Hello,
I’m looking for a script or indicator who can close all position on one chart when the Drawdown on it’s chart is 10% for exemple.

I have already the code for a script who close all trade when i put it on a chart.

Know i’m looking for add an condition like
If DrawDown == -500 or 10% for example, the script while be execute and close all positions.

I’m going to try to put the code in this post.

If the script or indicator can also delete the Expert Advisor on this chart to stopped it it’s good :wink:

So that is all that i have for that, i try on metaEditor but it’s difficult.

Thanks for your help :slight_smile:

sinput int RTOTAL = 4; // Число повторов при неудачных сделках
sinput int SLEEPTIME = 1; // Время паузы между повторами в секундах
sinput int Deviation_ = 10; // Отклонение цены
sinput bool exAllSymbols = false; // false - только текущий, true - все символы

#define _CS(A) ((!IsStopped()) && (A))

bool CloseAllPositions( const bool AllSymbols = true, const int Slippage = 0 )

{
bool Res = true;

for (int i = OrdersTotal() - 1; _CS(i >= 0); i–)
if (OrderSelect(i, SELECT_BY_POS) && (OrderType() <= OP_SELL) &&
(AllSymbols ? true : (OrderSymbol() == Symbol())))
Res &= OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage);

return(Res);

}

void OnStart()
{

for (int i = 0; CS((i < RTOTAL) && (!CloseAllPositions(exAllSymbols, Deviation))); i++)
Sleep(SLEEPTIME * 1000);

}

I’ve mixed my previous code with an indicator that gives the max drowdawn of the account. For the test, if this max drawdown is less than or equal to -1 it should close all the positions but not.

I think I am not far, can someone help me?

#property indicator_chart_window

double CurrentFloatingProfit=0.0;
double LowestFloatingProfit=0.0;
//±-----------------------------------------------------------------+
//| Custom indicator initialization function |
//±-----------------------------------------------------------------+
int init()
{
//---- indicators
//----
return(0);
}
//±-----------------------------------------------------------------+
//| Custom indicator deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//±-----------------------------------------------------------------+
//| Custom indicator iteration function |
//±-----------------------------------------------------------------+

sinput int RTOTAL = 4; // Число повторов при неудачных сделках
sinput int SLEEPTIME = 1; // Время паузы между повторами в секундах
sinput int Deviation_ = 10; // Отклонение цены
sinput bool exAllSymbols = false; // false - только текущий, true - все символы

#define _CS(A) ((!IsStopped()) && (A))

bool CloseAllPositions( const bool AllSymbols = true, const int Slippage = 0 )

{
bool Res = true;

for (int i = OrdersTotal() - 1; _CS(i >= 0); i–)
if (OrderSelect(i, SELECT_BY_POS) && (OrderType() <= OP_SELL) &&
(AllSymbols ? true : (OrderSymbol() == Symbol())))
Res &= OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage);

return(Res);

}

int start()
{
int counted_bars=IndicatorCounted();
CurrentFloatingProfit = AccountProfit();

if (CurrentFloatingProfit < 0.0 && CurrentFloatingProfit < LowestFloatingProfit)
{
LowestFloatingProfit = CurrentFloatingProfit;
}

if (LowestFloatingProfit <= -1)
{
for (int i = 0; CS((i < RTOTAL) && (!CloseAllPositions(exAllSymbols, Deviation))); i++)
Sleep(SLEEPTIME * 1000);
}

Comment("Max Account Drawdown = " + LowestFloatingProfit + “\n” + "Current Profit/Loss = " + CurrentFloatingProfit);

//----
return(0);
}
//±-----------------------------------------------------------------+

When it comes to coding, I like to talk to my coder who is an expert in all this. Frankly, I don’t understand coding at all and I am happy to pay an expert to get work done properly.