What is fastest way for closing all my positions?
https://www.mql5.com/en/docs/trading/ordersendasync
Documentation on MQL5: Trade Functions / OrderSendAsync
- www.mql5.com
Trade Functions / OrderSendAsync - Reference on algorithmic/automated trading language for MetaTrader 5
thanks for your Help.
I wrote this code for closing position but its not work why?
bool PositionCloseali(const ulong ticket,const ulong deviation,ulong m_magic,ulong m_deviation) { MqlTradeRequest m_request; // request data MqlTradeResult m_result; // result data //--- check stopped if(IsStopped()) return(false); //--- check position existence if(!PositionSelectByTicket(ticket)) return(false); string symbol=PositionGetString(POSITION_SYMBOL); //--- check if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { //--- prepare request for close BUY position m_request.type =ORDER_TYPE_SELL; m_request.price=SymbolInfoDouble(symbol,SYMBOL_BID); } else { //--- prepare request for close SELL position m_request.type =ORDER_TYPE_BUY; m_request.price=SymbolInfoDouble(symbol,SYMBOL_ASK); } //--- setting request m_request.action =TRADE_ACTION_DEAL; m_request.position =ticket; m_request.symbol =symbol; m_request.volume =PositionGetDouble(POSITION_VOLUME); m_request.magic =m_magic; m_request.deviation=(deviation==ULONG_MAX) ? m_deviation : deviation; //--- close position return(OrderSendAsync(m_request,m_result)); }
and this code for use that function
while(PositionsTotal()>0) { for (int i=PositionsTotal()-1;i>=0; i--) { PositionSelect(PositionGetSymbol(i)); PositionCloseali(PositionGetInteger(POSITION_TICKET),deviation_,MAGIC,deviation_); } } }
Ali Poormomen:
The market is closed.
I wrote this code for closing position but its not work why?
I can't test it on tester ?
Ali Poormomen:
I can't test it on tester ?
If this code works
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006 bool CloseAllPositionsAsync( const bool AllSymbols = true, const int Slippage = 0 ) { bool Res = true; for (int i = OrdersTotal() - 1; i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS) && (OrderType() <= OP_SELL) && (AllSymbols ? true : (OrderSymbol() == Symbol()))) Res &= OrderCloseAsync(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage); return(Res); }
try this
Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий
Особенности языка mql5, тонкости и приёмы работы
fxsaber, 2017.02.25 16:12
// Возвращает тип исполнения ордера, равный Type, если он доступен на символе Symb, иначе - корректный вариант.
ENUM_ORDER_TYPE_FILLING GetFilling( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);
return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
(((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
(ENUM_ORDER_TYPE_FILLING)Type);
}
ПрименениеENUM_ORDER_TYPE_FILLING GetFilling( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);
return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
(((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
(ENUM_ORDER_TYPE_FILLING)Type);
}
Request.type_filling = GetFilling(Request.symbol);
Hello
My server's ping is 2 ms When I want to close all my positions, the difference between each closing is at least 300 to 400 ms.
I used:
Why is there a difference? and What is fastest way for closing all my positions?