iI was wondering if any one could add a function to this code below so it could close PENDING orders with the rest of "live" orders, once the net profit is reached.
CloseAll() is supposed to close all orders including pending ones. Doesn't it do so?
That can happen if current price is too close to pending order entry price.
You could've figured that yourself if instead of littering log file with Print("Close on ", ...) there was a proper error handling code after every trade function call that prints error messages in the log.
"Doesn't it do so?" - No it doesnt
"You could've figured that yourself" - I am asking for your help, since I am still learning MT code.
Thanks.
"You could've figured that yourself" - I am asking for your help, since I am still learning MT code.
Thanks.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
This little ea below takes net profit on all of your open positions. Once the profit lever is reached, it closes all open positions.
I was wondering if any one could add a function to this code below so it could close PENDING orders with the rest of "live" orders, once the net profit is reached.
Thanks.
//+------------------------------------------------------------------+ //| CloseByPL.mq4 | //| | //+------------------------------------------------------------------+ extern int Profit=10; double myProfit=0; int EnableClose=0; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- myProfit=CalcProfit(); ShowInit(); if (EnableClose==1 && CntOrdAll(OP_BUY)==0 && CntOrdAll(OP_SELL)==0) { EnableClose=0; // Before Debug: EnableClose=1 } if ( (Profit>0 && myProfit>Profit) || (Profit<0 && myProfit<Profit)) { EnableClose=1; } CloseAll(); //---- return(0); } //+------------------------------------------------------------------+ void CloseAll () { // #function of CloseAll if (EnableClose==1) { // #if Close is yes int _total=OrdersTotal(); // number of lots or trades ???? int _ordertype;// order type if (_total==0) {return;} // if total==0 int _ticket; // ticket number double _priceClose;// price to close orders; for(int _i=_total-1;_i>=0;_i--) { //# for loop if (OrderSelect(_i,SELECT_BY_POS)) { //# if _ordertype=OrderType(); _ticket=OrderTicket(); switch(_ordertype) { //# switch case 0: // close buy _priceClose=MarketInfo(OrderSymbol(),MODE_BID); Print("Close on ",_i," position order with ticket ¹",_ticket); OrderClose(_ticket,OrderLots(),_priceClose,3,Red); break; case 1: // close sell _priceClose=MarketInfo(OrderSymbol(),MODE_ASK); Print("Close on ",_i," position order with ticket ¹",_ticket); OrderClose(_ticket,OrderLots(),_priceClose,3,Red); break; default: // values from 1 to 5, deleting pending orders // Print("Delete on ",_i," position order with ticket ¹",_ticket); // OrderDelete(_ticket); break; } //# switch } // # if } // # for loop } // #if Close yes return; } // #function of CloseAll void ShowInit() { string sComment = ""; string sp = "-----------------------------------------------------\n"; string NL = "\n"; sComment = sp; sComment = sComment + "CloseByPL will close all trades at: " + Profit + NL; sComment = sComment + sp; sComment = sComment + "Current P/L="+DoubleToStr(myProfit,0)+ NL; Comment(sComment); return; } double CalcProfit() { double _sum=0; int _total=OrdersTotal(); // number of lots int _ordertype;// order type if (_total==0) {return (0);} int _ticket; // ticket number double _priceClose;// price to close orders; for(int _i=_total-1;_i>=0;_i--) { if (OrderSelect(_i,SELECT_BY_POS)) { _ordertype=OrderType(); _ticket=OrderTicket(); switch(_ordertype) { case OP_BUY: // buy _priceClose=MarketInfo(OrderSymbol(),MODE_BID); _sum=_sum+(_priceClose-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT); break; case OP_SELL: // sell _priceClose=MarketInfo(OrderSymbol(),MODE_ASK); _sum=_sum+(OrderOpenPrice()-_priceClose)/MarketInfo(OrderSymbol(),MODE_POINT); break; default: break; } } } return(_sum); } int CntOrdAll(int Type) { //return number of orders with specific parameters int _CntOrd; _CntOrd=0; for(int i=0;i<OrdersTotal();i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderType()==Type) _CntOrd++; } return(_CntOrd); }