For instance, this?
#property copyright "what?" #property link "who cares?" //+------------------------------------------------------------------+ //| The fancy comment here | //+------------------------------------------------------------------+ extern double profit = 1.0; // [0.1 to 100%] extern int slippage=3; void close_all_trades() { for(int i=OrdersTotal()-1;i>=0;i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; // if(OrderSymbol()!=Symbol()) // continue; RefreshRates(); if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID), slippage,White); if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK), slippage,Red); if((OrderType()==OP_BUYSTOP) || (OrderType()==OP_BUYLIMIT)) OrderDelete(OrderTicket()); if((OrderType()==OP_SELLSTOP) || (OrderType()==OP_SELLLIMIT)) OrderDelete(OrderTicket()); } } int start() { double actual_profit = ((AccountEquity()-AccountBalance())/AccountEquity())*100.0; if(actual_profit>=profit) close_all_trades(); return(0); }
int BuyOrders=0; int SellOrders=0; int mode; for(int cnt=0;cnt<OrdersTotal();cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); mode=OrderType(); if (OrderSymbol()==Symbol()) { if (mode==OP_BUY) { BuyOrders++;//Count buyorders } if (mode==OP_SELL) { SellOrders++;//Count sellorders } } }
I dont get the Clue with the first post. But I tried the second... but I have still problems to include it into the EA.
I try to use it on the following EA: https://www.mql5.com/en/code/9125
Maybe someone can include it? I still dont get the wished result :(
Hello,
The close script in MT4 closes the first order entered. I would like it to close the most recent order entered instead. I have trudged around looking at the code snippets here and elsewhere, but I am no programmer. If anyone can do this i would love some help. Thanks! Andy
the code in first post works like an EA. It is always verifying whether profit is above a level. If so, it closes all orders and deletes all pending. The default value for level is 1%.
It is a very basic code. Try understand it to include in your ea.
Regardless of the situation in your trading account: open trades, pending trades, hedge, no hedge.... if accountEquity vs accountBalance is at or above the profit level, all orders are closed.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Does someone know how to code the following Idea:
If we have a hedged Situation (f.e.: 10 Sell and 10 Buy) AND a specific proft target is reached --> Close all orders
My Problem is that i dont know how to count the amoung of open trades.
UPDATE:
I have a first idea but it doenst work fine. The EA opens a hedget Position at the beginning, so i tried to define that the EA only closes all Positions if there were more than 5 Sell and Buy orders.
BUT: The EA closes at the beginning about 10x the first hedget Position and after that he clothes nothing even if its hedget and we have more than 5 Sell/5 Buy. Does someone know how to solve this?
CODE:
int BuyCnt = 0;
int SellCnt = 0;
int abo = OrdersTotal();
for (int i=0; i < abo; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
int type = OrderType();
if (type == OP_BUY) BuyCnt++;
if (type == OP_SELL) SellCnt++;
}
if (BuyCnt<5 && SellCnt<5 && BuyCnt-SellCnt==0)
{
GLBcloseall=true;
}