#ifndef LIB_MQLPLUS_MQL4_COMPATIBILITY_POSITION_MQH_INCLUDED #define LIB_MQLPLUS_MQL4_COMPATIBILITY_POSITION_MQH_INCLUDED #property version "1.0"; #property strict /********************************************************************************** * Copyright (C) 2010-2022 Dominik Egert * * This file is the MQL4 backward compatibility include file. * * MQLplus, including this file may not be copied and/or distributed * without explecit permit by the author. * Author Dominik Egert / Freie Netze UG. ********************************************************************************** * * Version: 1.0 * State: development * * File information * ================ * * * */ #ifdef DBG_MSG_TRACE_FILE_LOADER DBG_MSG_TRACE_FILE_LOADER; #endif //*********************************************************************************************************************************************************/ // // BEGIN MQL4 compatibility additions // /////////////////////////////////////////////////////////////////////////// // // Add required MQL5 enumerations // enum ENUM_POSITION_PROPERTY_INTEGER { POSITION_TICKET, POSITION_TIME, POSITION_TIME_MSC, POSITION_TIME_UPDATE, POSITION_TIME_UPDATE_MSC, POSITION_TYPE, POSITION_MAGIC, POSITION_IDENTIFIER }; enum ENUM_POSITION_PROPERTY_DOUBLE { POSITION_VOLUME, POSITION_PRICE_OPEN, POSITION_SL, POSITION_TP, POSITION_PRICE_CURRENT, POSITION_SWAP, POSITION_PROFIT }; enum ENUM_POSITION_PROPERTY_STRING { POSITION_SYMBOL, POSITION_COMMENT, POSITION_EXTERNAL_ID }; enum ENUM_POSITION_TYPE { POSITION_TYPE_BUY, POSITION_TYPE_SELL }; enum ENUM_POSITION_REASON { POSITION_REASON_CLIENT, POSITION_REASON_MOBILE, POSITION_REASON_WEB, POSITION_REASON_EXPERT }; // ///////////////////////////////////////////////////////////////////////////////////////////////////// //+------------------------------------------------------------------+ //| MQL4: MQL5-compatible PositionsTotal() | //+------------------------------------------------------------------+ const int PositionsTotal() { int ptr = OrdersTotal(); int cnt = NULL; while( (ptr >= NULL) && (!_StopFlag) ) { cnt += (OrderSelect(ptr, SELECT_BY_POS, MODE_TRADES)) && (OrderType() <= OP_SELL); } // Return return(cnt); } //+------------------------------------------------------------------+ //| MQL4: MQL5-compatible PositionGetSymbol() | //+------------------------------------------------------------------+ #define PositionGetSymbol(x) (mt4_OrderSelect(x, SELECT_BY_POS, MODE_TRADES) ? OrderSymbol() : "") //+------------------------------------------------------------------+ //| MQL4: MQL5-compatible PositionSelect() | //+------------------------------------------------------------------+ const bool PositionSelect(const string symbol) { int total = OrdersTotal(); int cnt = NULL; while( (cnt <= total) && (!_StopFlag) ) { if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) { continue; } if(OrderSymbol() == symbol) { return(true); } cnt++; } // Return return(false); } //+------------------------------------------------------------------+ //| MQL4: MQL5-compatible PositionSelectByTicket() | //+------------------------------------------------------------------+ #define PositionSelectByTicket(x) mt4_OrderSelect((int)x, SELECT_BY_TICKET, MODE_TRADES) //+------------------------------------------------------------------+ //| MQL4: MQL5-compatible PositionGetDouble() | //+------------------------------------------------------------------+ const double PositionGetDouble(const ENUM_POSITION_PROPERTY_DOUBLE property_id) { switch(property_id) { case POSITION_VOLUME: return(OrderLots()); case POSITION_PRICE_OPEN: return(OrderOpenPrice()); case POSITION_SL: return(OrderStopLoss()); case POSITION_TP: return(OrderTakeProfit()); case POSITION_PRICE_CURRENT: return(SymbolInfoDouble(OrderSymbol(), (OrderType() == OP_SELL) ? SYMBOL_BID : SYMBOL_ASK)); case POSITION_SWAP: return(OrderSwap()); case POSITION_PROFIT: return(OrderProfit()); } // Return return(DBL_MIN); } //+------------------------------------------------------------------+ //| MQL4: MQL5-compatible PositionGetInteger() | //+------------------------------------------------------------------+ const long PositionGetInteger(const ENUM_POSITION_PROPERTY_INTEGER property_id) { switch(property_id) { case POSITION_TICKET: return(OrderTicket()); case POSITION_TIME: return(OrderOpenTime()); case POSITION_TIME_MSC: return(OrderOpenTime() * 1000); case POSITION_TYPE: switch(OrderType()) { case OP_BUY: return((long)POSITION_TYPE_BUY); case OP_SELL: return((long)POSITION_TYPE_SELL); } break; case POSITION_MAGIC: return(OrderMagicNumber()); case POSITION_TIME_UPDATE: case POSITION_TIME_UPDATE_MSC: case POSITION_IDENTIFIER: #ifdef _DEBUG printf("Feature not supported in MQL4: %s; %s", __FUNCTION__, EnumToString(property_id)); #else break; #endif } // Return return(LONG_MIN); } //+------------------------------------------------------------------+ //| MQL4: MQL5-compatible PositionGetString() | //+------------------------------------------------------------------+ const string PositionGetString(const ENUM_POSITION_PROPERTY_STRING property_id) { switch(property_id) { case POSITION_SYMBOL: return(OrderSymbol()); case POSITION_COMMENT: return(OrderComment()); case POSITION_EXTERNAL_ID: #ifdef _DEBUG printf("Feature not supported in MQL4: %s; %s", __FUNCTION__, EnumToString(property_id)); #else break; #endif } // Return return(""); } //+------------------------------------------------------------------+ //| MQL4: MQL5-compatible PositionGetTicket() | //+------------------------------------------------------------------+ #define PositionGetTicket(x) (OrderSelect(x, SELECT_BY_POS, MODE_TRADES) && (OrderType() <= OP_SELL) ? (ulong)OrderTicket() : (ulong)0) // // END MQL compatibility */ //*********************************************************************************************************************************************************/ #endif // LIB_MQLPLUS_MQL4_COMPATIBILITY_POSITION_MQH_INCLUDED