- Enter in the search field (top right the lens) "open positions" press enter.
- Choose on the left side CodeBase.
- Open one of the provided option, the third one should be yours: "Display open positions total stoploss and takeprofit value MT5 - expert for MetaTrader 5"
- Study to understand the provided code.
- Bare in mind: There is eventually nothing that ha not been programmed fpr Mt4/5 yet.
I use this code but is dos not stop adding total_open_positions !
double check_total_positions () { for(int v = PositionsTotal() - 1; v >= 0; v--) { ulong positionticket = PositionGetTicket(v); ulong magic=PositionGetInteger(POSITION_MAGIC); if(PositionSelectByTicket(positionticket)) { if(magic ==EXPERT_MAGIC ) { total_open_positions ++ ; } } } return total_open_positions; }
??
Have you gone through your code with the debugger?
read this: https://www.mql5.com/en/articles/654
Debugging MQL5 Programs
- www.mql5.com
This article is intended primarily for the programmers who have already learned the language but have not fully mastered the program development yet. It reveals some debugging techniques and presents a combined experience of the author and many other programmers.
sorry it's complete code:
double check_total_positions () { int total_open_positions =0; for(int v = PositionsTotal() - 1; v >= 0; v--) { ulong positionticket = PositionGetTicket(v); ulong magic=PositionGetInteger(POSITION_MAGIC); if(PositionSelectByTicket(positionticket)) { if(magic ==EXPERT_MAGIC ) { total_open_positions ++ ; } } } return total_open_positions; }
Michael Quinatadcan Macasil Macasil #:
when I try the code NO ERRORS FOUND. what is the real problem?
refer attached jpeg
Forum on trading, automated trading systems and testing trading strategies
Vladimir Karputov, 2020.12.30 07:56
Calculate Positions and Pending Orders
Code: Calculate Positions and Pending Orders.mq5
//+------------------------------------------------------------------+ //| Calculate Positions and Pending Orders.mq5 | //| Copyright © 2019-2020, Vladimir Karputov | //+------------------------------------------------------------------+ #property copyright "Copyright © 2019-2020, Vladimir Karputov" #property version "1.003" //--- #include <Trade\PositionInfo.mqh> #include <Trade\OrderInfo.mqh> //--- CPositionInfo m_position; // object of CPositionInfo class COrderInfo m_order; // object of COrderInfo class //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- int buys=0,sells=0; int buy_limits=0,sell_limits=0,buy_stops=0,sell_stops=0; CalculateAllPositions(buys,sells); CalculateAllPendingOrders(buy_limits,sell_limits,buy_stops,sell_stops); string text="BUY: "+IntegerToString(buys)+", SELL: "+IntegerToString(sells)+"\n"+ "Buy limits "+IntegerToString(buy_limits)+", Sell limits "+IntegerToString(sell_limits)+ ", Buy stops "+IntegerToString(buy_stops)+", Sell stops "+IntegerToString(sell_stops); Comment(text); //--- } //+------------------------------------------------------------------+ //| Calculate all positions Buy and Sell | //+------------------------------------------------------------------+ void CalculateAllPositions(int &count_buys,int &count_sells) { count_buys=0; count_sells=0; for(int i=PositionsTotal()-1; i>=0; i--) if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties //if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic) { if(m_position.PositionType()==POSITION_TYPE_BUY) count_buys++; if(m_position.PositionType()==POSITION_TYPE_SELL) count_sells++; } //--- return; } //+------------------------------------------------------------------+ //| Calculate all pending orders | //+------------------------------------------------------------------+ void CalculateAllPendingOrders(int &count_buy_limits,int &count_sell_limits,int &count_buy_stops,int &count_sell_stops) { count_buy_limits = 0; count_sell_limits = 0; count_buy_stops = 0; count_sell_stops = 0; for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders if(m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties //if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==InpMagic) { if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT) count_buy_limits++; else if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT) count_sell_limits++; else if(m_order.OrderType()==ORDER_TYPE_BUY_STOP) count_buy_stops++; else if(m_order.OrderType()==ORDER_TYPE_SELL_STOP) count_sell_stops++; } } //+------------------------------------------------------------------+
Open positions: GBPUSD BUY 0.03 lot and USDPLN SELL 0.01
Pending orders are placed: USDPLN Sell limit 0.01 and GBPUSD Buy limit 0.03
Maybe also intersting for you how to count long and short positions. To get all open positions just add them both up. :)
//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int countLongPositions(int magic) { int counterBuy = 0; for(int i = PositionsTotal() - 1; i>=0; i--) { string symbol = PositionGetSymbol(i); int PositionMagicNumber = (int)PositionGetInteger(POSITION_MAGIC); //Comment(symbol) ; if(PositionMagicNumber == magic && _Symbol==symbol) { long pos_type =(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); if(pos_type == 0) // 0 = buy; 1 = sell { counterBuy = counterBuy +1; } } } return counterBuy; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int countShortPositions(int magic) { int counterSell = 0; for(int i = PositionsTotal() - 1; i>=0; i--) { string symbol = PositionGetSymbol(i); int PositionMagicNumber = (int)PositionGetInteger(POSITION_MAGIC); //Comment(symbol) ; if(PositionMagicNumber == magic && _Symbol==symbol) { long pos_type =(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); if(pos_type == 1) // 0 = buy; 1 = sell { counterSell = counterSell +1; } } } return counterSell; }
Can someone modify this indicator to have open positions by Magic number as alternative to Symbol?
columns would be only:
- Magic
- Buy lots
- Sell lots
- Net profit
Files:
iExposure.zip
17 kb
I know I'm late to the game, but here:
int CountOrdersMagic(int magicnumber) { int count = 0; for(int i = 0; i < PositionsTotal(); i++) { if(PositionSelectByTicket(PositionGetTicket(i))) { if(PositionGetInteger(POSITION_MAGIC) == magicnumber) { count++; } } } return (count); }
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
Hi guys
would you please guide me that How to get total open positions with a magic number in mql5 ?
thanks in advance for you help