Yu Tsai:
A new bee. I want to close last 2 orders among multi orders, but not working. Help please? Thank you very much in advance!
try this code, it will close all orders with profit:
//+------------------------------------------------------------------+ //| work.mq4 | //| Copyright 2012, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property strict //+------------------------------------------------------------------+ #include <stderror.mqh> #include <stdlib.mqh> #include <WinUser32.mqh> //+---------------------------------------------------------------------------+ input int ps32_Magic = 1; //+---------------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ int gs32_TicketToClose; int gs32_SlippagePips = 10; double gd_Lot; double gd_Price; //+------------------------------------------------------------------+ void OnTick() { CloseProfitOrders(); } //+------------------------------------------------------------------+ void CloseProfitOrders() { int s32_OrdersTotal; s32_OrdersTotal = OrdersTotal(); for (int i = 0; i < s32_OrdersTotal; i++ ) { if (!OrderSelect(i,SELECT_BY_POS)) { nt s32_LastError = GetLastError(); Print("OrderSelect( "+DoubleToStr(i,0)+", SELECT_BY_POS ) - Error #"+DoubleToStr(s32_LastError,0)); continue; } if (OrderMagicNumber() != ps32_Magic) continue; if (OrderSymbol() != Symbol()) continue; if (OrderType() != OP_BUY && OrderType() != OP_SELL) continue; if (OrderProfit() > 0) { gs32_TicketToClose = OrderTicket(); gd_Lot = OrderLots(); gd_Price = OrderType() == OP_BUY ? Bid : Ask; if (_CloseOrder(gs32_TicketToClose,gd_Lot,gd_Price)) continue; else break; } } } //+------------------------------------------------------------------+ bool _CloseOrder(int s32_TicketToClose,double d_Lot,double d_Price) { string sw_Message; while(IsTradeContextBusy()) { } ResetLastError(); if (OrderClose(s32_TicketToClose,d_Lot,d_Price,gs32_SlippagePips,clrNONE)) { return(true); } int s32_LastError = GetLastError(); sw_Message = "Order close error. Ticket ="+s32_TicketToClose+" Error code = "+s32_LastError+" : "+ErrorDescription(s32_LastError); Print(sw_Message); return(false); } //+------------------------------------------------------------------+
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
A new bee. I want to close last 2 orders among multi orders, but not working. Help please? Thank you very much in advance!