Counting open orders for a specific magic number issue

 

I'm getting the below 2 warnings when I compile my expert advisor. Can someone please point me in the right direction to get the 2 functions working

implicit conversion from 'number' to 'string' TrendBotV2.mq5 68 27

implicit conversion from 'number' to 'string' TrendBotV2.mq5 116 25

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;

input int MA_Period = 50;  // Period for the moving average
input double LotSize = 0.2;  // Lot size for each trade
input int Slippage = 10;  // Slippage
input int TakeProfitPoints = 500; // Take profit in points
input int MaxOpenTradesPerSymbol = 1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Initialization code here
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
  // Define symbols to trade
  string symbols[4] = {"Crash 500 Index", "Crash 1000 Index", "Boom 500 Index", "Boom 1000 Index"};
  
  for(int i = 0; i < ArraySize(symbols); i++)
  {
     string symbol = symbols[i];
     double trend = CheckTrend(symbol);
     string trendStr = DoubleToString(trend, 2); // Convert trend to string

     // Manage orders based on the symbol type and trend
     if(StringFind(symbol, "Boom") != -1)
     {
        ManageBoomOrders(symbol, trend);
     }
     else if(StringFind(symbol, "Crash") != -1)
     {
        ManageCrashOrders(symbol, trend);
     }
  }
}

//+------------------------------------------------------------------+
//| Check the trend of the given symbol                              |
//+------------------------------------------------------------------+
double CheckTrend(string symbol)
  {
   double ma = iMA(symbol, PERIOD_H1, MA_Period, 0, MODE_SMA, PRICE_CLOSE);
   double price = SymbolInfoDouble(symbol, SYMBOL_BID);

   if(price > ma) return(1); // Uptrend
   if(price < ma) return(-1); // Downtrend
   return(0); // No clear trend
  }

//+------------------------------------------------------------------+
//| Count the number of open trades for a given symbol               |
//+------------------------------------------------------------------+
int CountOpenTrades(string symbol) {
    int count = 0;
    for(int i = PositionsTotal() - 1; i >= 0; i--) {
        ulong ticket = PositionGetTicket(i);
        if(PositionSelect(ticket)) {
            if(PositionGetString(POSITION_SYMBOL) == symbol) {
                count++;
            }
        }
    }
    return count;
}

//+------------------------------------------------------------------+
//| Manage orders for Boom symbols                                   |
//+------------------------------------------------------------------+
void ManageBoomOrders(string symbol, double trend) {
    ulong position_ticket = PositionGetTicketBySymbol(symbol, POSITION_TYPE_BUY);
    if(trend == 1 && position_ticket == 0) {
        if(CountOpenTrades(symbol) < MaxOpenTradesPerSymbol) {
            double takeProfitPrice = SymbolInfoDouble(symbol, SYMBOL_ASK) + TakeProfitPoints * _Point;
            trade.Buy(LotSize, symbol, Slippage, 0, takeProfitPrice, "Boom Buy Order");
        }
    }
    else if(trend == -1 && position_ticket > 0) {
        trade.PositionClose(position_ticket);
    }
}

//+------------------------------------------------------------------+
//| Manage orders for Crash symbols                                  |
//+------------------------------------------------------------------+
void ManageCrashOrders(string symbol, double trend) {
    ulong position_ticket = PositionGetTicketBySymbol(symbol, POSITION_TYPE_SELL);
    if(trend == -1 && position_ticket == 0) {
        if(CountOpenTrades(symbol) < MaxOpenTradesPerSymbol) {
            double takeProfitPrice = SymbolInfoDouble(symbol, SYMBOL_BID) - TakeProfitPoints * _Point;
            trade.Sell(LotSize, symbol, Slippage, 0, takeProfitPrice, "Crash Sell Order");
        }
    }
    else if(trend == 1 && position_ticket > 0) {
        trade.PositionClose(position_ticket);
    }
}
//+------------------------------------------------------------------+
//| Get the ticket of an open position for a specific symbol         |
//+------------------------------------------------------------------+
ulong PositionGetTicketBySymbol(string symbol, ENUM_POSITION_TYPE type)
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      ulong ticket = PositionGetTicket(i); // Get the ticket of the position at index i
      if(PositionSelect(ticket)) // Select the position by its ticket for further work
        {
         string pos_symbol = PositionGetString(POSITION_SYMBOL);
         ENUM_POSITION_TYPE pos_type = ENUM_POSITION_TYPE(PositionGetInteger(POSITION_TYPE));
         if(pos_symbol == symbol && pos_type == type)
           {
            return ticket;
           }
        }
     }
   return 0;
  }
 

We are not going to count lines for you.

Please identify the lines by highlighting them in your post.

 

Assuming it is the lines ...

if(PositionSelect(ticket)) {
if(PositionSelect(ticket)) // Select the position by its ticket for further work

... then please note that you are using the wrong function.

It should be PositionSelectByTicket and not PositionSelect.

Documentation on MQL5: Trade Functions / PositionSelectByTicket
Documentation on MQL5: Trade Functions / PositionSelectByTicket
  • www.mql5.com
PositionSelectByTicket - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5