Incomplete code.
CTrade m_trade; // trading object CSymbolInfo m_Asset1; // symbol info object CSymbolInfo m_Asset2; // symbol info object CSymbolInfo m_Asset3; // symbol info object CPositionInfo m_position; // trade position object CAccountInfo m_account; // account info wrapper . . . int OnInit() { m_Asset1.Name("EURUSD") m_trade.SetMarginMode(); return(INIT_SUCCEEDED); } void OnTick(){ int iTotalPosition = PositionsTotal() ; if (iTotalPosition == 0) { if (Condition) { m_Asset1.Refresh(); m_Asset1.RefreshRates(); PositionOpen(1,Lot,GetPointer(m_Asset1), ORDER_TYPE_SELL, m_Asset1.Bid()); } }else { if (Condition) { if(m_trade.PositionClose(m_Asset1.Name(),(ulong)100)) { printf("Poisition Closed"); } else { printf("Error closing position by %s : '%s' Total Positions (%d)",Symbol(),m_trade.ResultComment(),PositionsTotal()); } } } } bool PositionOpen(long _magicNumber, double _lot, CSymbolInfo *_mAsset, ENUM_ORDER_TYPE _op, double price) { m_trade.SetExpertMagicNumber(_magicNumber); m_trade.SetTypeFilling(ORDER_FILLING_IOC); m_trade.SetAsyncMode(false); //--- check for price if (price == 0) { return false; } //--- check for free money if(m_account.FreeMarginCheck(_mAsset.Name(), _op, _lot, price) < 0.0) { printf("We have no money. Free Margin = %f",m_account.FreeMarginCheck(_mAsset.Name(),_op, _lot, price)); return false; } //--- check Lots string result = ""; if (!ChecVolumeValue (_lot,result) ) { printf("Error Opening Position" + result); return false; } //--- check opening position if (!m_trade.PositionOpen(_mAsset.Name(),_op,_lot,price,0,0,"")) { printf("Error Opening Position %s, Code %d, Lot %f, Price %f", _mAsset.Name(), m_trade.ResultRetcode(), _lot, price); return false; } return true; }
Hi Marco Which part do you want me to send I've just sent the part of code that make the problem it's ot all the code but the same stucture
Any Help please !
#define EXPERT_MAGIC 123456 // MagicNumber of the expert //+------------------------------------------------------------------+ //| Closing all positions | //+------------------------------------------------------------------+ void OnStart() { //--- declare and initialize the trade request and result of trade request MqlTradeRequest request; MqlTradeResult result; int total=PositionsTotal(); // number of open positions //--- iterate over all open positions for(int i=total-1; i>=0; i--) { //--- parameters of the order ulong position_ticket=PositionGetTicket(i); // ticket of the position string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol int digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places ulong magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position double volume=PositionGetDouble(POSITION_VOLUME); // volume of the position ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); // type of the position //--- output information about the position PrintFormat("#%I64u %s %s %.2f %s [%I64d]", position_ticket, position_symbol, EnumToString(type), volume, DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),digits), magic); //--- if the MagicNumber matches if(magic==EXPERT_MAGIC) { //--- zeroing the request and result values ZeroMemory(request); ZeroMemory(result); //--- setting the operation parameters request.action =TRADE_ACTION_DEAL; // type of trade operation request.position =position_ticket; // ticket of the position request.symbol =position_symbol; // symbol request.volume =volume; // volume of the position request.deviation=5; // allowed deviation from the price request.magic =EXPERT_MAGIC; // MagicNumber of the position //--- set the price and order type depending on the position type if(type==POSITION_TYPE_BUY) { request.price=SymbolInfoDouble(position_symbol,SYMBOL_BID); request.type =ORDER_TYPE_SELL; } else { request.price=SymbolInfoDouble(position_symbol,SYMBOL_ASK); request.type =ORDER_TYPE_BUY; } //--- output information about the closure PrintFormat("Close #%I64d %s %s",position_ticket,position_symbol,EnumToString(type)); //--- send the request if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code //--- information about the operation PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order); //--- } } } //+------------------------------------------------------------------+
struct MqlTradeRequest { ENUM_TRADE_REQUEST_ACTIONS action; // Trade operation type ulong magic; // Expert Advisor ID (magic number) ulong order; // Order ticket string symbol; // Trade symbol double volume; // Requested volume for a deal in lots double price; // Price double stoplimit; // StopLimit level of the order double sl; // Stop Loss level of the order double tp; // Take Profit level of the order ulong deviation; // Maximal possible deviation from the requested price ENUM_ORDER_TYPE type; // Order type ENUM_ORDER_TYPE_FILLING type_filling; // Order execution type ENUM_ORDER_TYPE_TIME type_time; // Order expiration type datetime expiration; // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type) string comment; // Order comment ulong position; // Position ticket ulong position_by; // The ticket of an opposite position };
Thanks Marco I ll give it a try
I Use this code.
https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions#trade_action_deal
for broker XM just add this setting.
request.type_filling = ORDER_FILLING_IOC;
It work for me :)

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
- www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Trade Operation Types - Reference on algorithmic/automated trading language for MetaTrader 5
amazing!! thanks for your advice!

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
Hello my EA worked fine until i've change the broker XM The positions wont close and gives me
2017.03.13 08:24:53.670 2017.03.01 00:42:32 Error closing position by EURUSD : 'Unsupported filling mode' Total Positions (1)
The problem is that i have already filled the mode (i have already opened the position successfully )