//+------------------------------------------------------------------+ //| close-all-ordersByMargin.mq4 | //| Copyright © 2005, Matias Romeo. | //| Custom Metatrader Systems. | //| History: | //| Modify to close order by Margin Used | | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, Matias Romeo." #property link "mailto:matiasDOTromeoATgmail.com" extern double MarginMultiplier = 0.7; // For the purpose of Multiplying MarginUsed to determine profit target int start() { if(OrdersTotal() > 0 ) Comment("MarginUsed",(AccountMargin())," Multiplier",MarginMultiplier," Fraction",AccountProfit()/AccountMargin()," |||ProfitTarget: $",(AccountMargin())*MarginMultiplier," Less AccountProfit: ",AccountProfit()," = RemainingTarget: $",((AccountMargin())*MarginMultiplier)-AccountProfit()); if(OrdersTotal() > 0 && AccountProfit()>(AccountMargin())*MarginMultiplier) int total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; // if(OrdersTotal() > 0 && AccountProfit()>(AccountMargin())*MarginMultiplier) switch(type) { //Close opened long positions case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); break; //Close opened short positions case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); break; //Close pending orders case OP_BUYLIMIT : case OP_BUYSTOP : case OP_SELLLIMIT : case OP_SELLSTOP : result = OrderDelete( OrderTicket() ); } if(result == false) { // Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() ); // Sleep(3000); } } return(0); }
Sample
Analysis
Market Information Used:
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It Closes Orders by itself
Other Features: