StringGetCharacter is preventing my ea from trailing

 

Hi all, I'm having a problem as to why StringGetCharacter is preventing my ea from trailing.

#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Trade\DealInfo.mqh>

CTrade trade;
CPositionInfo m_position;
CSymbolInfo m_symbol;
COrderInfo m_order;
CDealInfo  m_deal;
CAccountInfo m_account;

input double TakeProfit=260;
input double StopLoss=500;

int MagicNumber;
int MaxTrades;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
 {
  ResetLastError();
  
  if(!m_symbol.Name(Symbol()))
  {
   return(INIT_FAILED);
  }
  
  trade.SetExpertMagicNumber(MagicNumber);
  trade.SetMarginMode();
  trade.SetTypeFillingBySymbol(m_symbol.Name());
  
  MagicNumber=(StringGetCharacter(Symbol(),0)*1
             +StringGetCharacter(Symbol(),2)*3
             +StringGetCharacter(Symbol(),4)*5
             +StringGetCharacter(Symbol(),6)*7
             +StringGetCharacter(Symbol(),8)*9
             +StringGetCharacter(Symbol(),10)*11
             +StringGetCharacter(Symbol(),12)*13
             +StringGetCharacter(Symbol(),14)*15
             +StringGetCharacter(Symbol(),16)*17);
  
  Print(MagicNumber);
  
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int OrdersPending()
 {
  int Pending=0;
  for(int a=OrdersTotal()-1;a>=0;a--)
  {
   bool Select=OrderSelect(OrderGetTicket(a));
   string OrderSymbol=OrderGetString(ORDER_SYMBOL);
   int Magic=(int)OrderGetInteger(ORDER_MAGIC);
   if(OrderSymbol==Symbol()&&Magic==MagicNumber)
   {
    ENUM_ORDER_TYPE Type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
    if(Type==ORDER_TYPE_BUY_STOP||Type==ORDER_TYPE_SELL_STOP)
    Pending++;
   }
  }
  return(Pending);
 }
 
void Buy()
 {
  double Pips=0;
  double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  if(TickSize==0.00001||TickSize==0.001)
  Pips=TickSize*10;
  else Pips=TickSize;
  
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  double TakeProfitBuy=Ask+TakeProfit*Pips;
  double StopLossBuy=Ask-StopLoss*Pips;
  
  datetime Expire=TimeCurrent()+(60*15);
  
  double Close[];
  ArraySetAsSeries(Close,true);
  CopyClose(Symbol(),0,0,Bars(Symbol(),Period()),Close);
  
  int WholeArray=0;
  int TimeFrame=Period();
  switch(TimeFrame)
  {
   case PERIOD_M1:WholeArray=600;break;
   case PERIOD_M5:WholeArray=600;break;
   case PERIOD_M15:WholeArray=200;break;
   case PERIOD_M30:WholeArray=100;break;
   case PERIOD_H1:WholeArray=10;break;
   case PERIOD_H4:WholeArray=5;break;
   case PERIOD_D1:WholeArray=2;break;
  }
  
  double Lowest=iLow(Symbol(),0,iLowest(Symbol(),0,MODE_LOW,WholeArray,1));
  double Lowest2=iLow(Symbol(),0,iLowest(Symbol(),0,MODE_LOW,WholeArray,2));

  if(MaxTrades!=1)
  {
  if(Close[1]>Lowest&&Close[1]<Lowest2)
  {
   MaxTrades=1;
   trade.BuyStop(0.01,Ask+0.5*Pips,NULL,StopLossBuy,TakeProfitBuy,ORDER_TIME_SPECIFIED,Expire,"JackBuda");
  }
  }
  
 }
 
void Sell()
 {
  double Pips=0;
  double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  if(TickSize==0.00001||TickSize==0.001)
  Pips=TickSize*10;
  else Pips=TickSize;
  
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  double TakeProfitSell=Bid-TakeProfit*Pips;
  double StopLossSell=Bid+StopLoss*Pips;
  
  datetime Expire=TimeCurrent()+(60*15);
  
  double Close[];
  ArraySetAsSeries(Close,true);
  CopyClose(Symbol(),0,0,Bars(Symbol(),Period()),Close);
  
  int WholeArray=0;
  int TimeFrame=Period();
  switch(TimeFrame)
  {
   case PERIOD_M1:WholeArray=600;break;
   case PERIOD_M5:WholeArray=600;break;
   case PERIOD_M15:WholeArray=200;break;
   case PERIOD_M30:WholeArray=100;break;
   case PERIOD_H1:WholeArray=10;break;
   case PERIOD_H4:WholeArray=5;break;
   case PERIOD_D1:WholeArray=2;break;
  }
  
  double Highest=iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,WholeArray,1));
  double Highest2=iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,WholeArray,2));

  if(MaxTrades!=-1)
  {
  if(Close[1]<Highest&&Close[1]>Highest2)
  {
   MaxTrades=-1;
   trade.SellStop(0.01,Bid-0.5*Pips,NULL,StopLossSell,TakeProfitSell,ORDER_TIME_SPECIFIED,Expire,"JackBuda");
  }
  }

 }    
 
void TrailingStop()
 {
  double Pips=0;
  double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  if(TickSize==0.00001||TickSize==0.001)
  Pips=TickSize*10;
  else Pips=TickSize;
  
  double TrailAmount=0;

  double WhenToTrail=1.5;
  int TimeFrame=Period();
  switch(TimeFrame)
  {
   case PERIOD_M1:TrailAmount=1;break;
   case PERIOD_M5:TrailAmount=1;break;
   case PERIOD_M15:TrailAmount=2;break;
   case PERIOD_M30:TrailAmount=2;break;
   case PERIOD_H1:TrailAmount=1.5;break;
   case PERIOD_H4:TrailAmount=8;break;
   case PERIOD_D1:TrailAmount=8;break;
  }
  
  double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  
  for(int c=PositionsTotal()-1;c>=0;c--)
   {
    if(m_position.SelectByIndex(c))
    if(m_position.Symbol()==Symbol()&&m_position.Magic()==MagicNumber)
    if(m_position.PositionType()==POSITION_TYPE_BUY)
    if(Bid-m_position.PriceOpen()>WhenToTrail*Pips)
    if(m_position.StopLoss()<Bid-Pips*TrailAmount)
     {
      trade.PositionModify(m_position.Ticket(),Bid-(Pips*TrailAmount),m_position.TakeProfit());
     }
   } 
   for(int d=PositionsTotal()-1;d>=0;d--)
   {
    if(m_position.SelectByIndex(d))
    if(m_position.Symbol()==Symbol()&&m_position.Magic()==MagicNumber)
    if(m_position.PositionType()==POSITION_TYPE_SELL)
    if(m_position.PriceOpen()-Ask>WhenToTrail*Pips)
    if(m_position.StopLoss()>Ask+TrailAmount*Pips||m_position.StopLoss()==0)
     {
      trade.PositionModify(m_position.Ticket(),Ask+(Pips*TrailAmount),m_position.TakeProfit());
     }
   } 
 }

void OnTick()
 {
  int Spread=(int)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD);
  double MaxLot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
  int Leverage=(int)AccountInfoInteger(ACCOUNT_LEVERAGE);
  
  Comment("Spread is "+DoubleToString(Spread/10,1)+
  "\nMax Lot For Broker is "+DoubleToString(MaxLot/1,1)+
  "\nLeverage For Broker is "+DoubleToString(Leverage/1,1)+
  "\nMagicNumber is "+IntegerToString(MagicNumber/1,1));
  
  TrailingStop();
 
  if(OrdersPending()<1){Buy();}
   
  if(OrdersPending()<1){Sell();}
 
 }

When I disable the functions:

MagicNumber=(StringGetCharacter(Symbol(),0)*1
             +StringGetCharacter(Symbol(),2)*3
             +StringGetCharacter(Symbol(),4)*5
             +StringGetCharacter(Symbol(),6)*7
             +StringGetCharacter(Symbol(),8)*9
             +StringGetCharacter(Symbol(),10)*11
             +StringGetCharacter(Symbol(),12)*13
             +StringGetCharacter(Symbol(),14)*15
             +StringGetCharacter(Symbol(),16)*17);

My ea will trailing again. I would like to add StringGetCharacter functions above & my ea to trail.

Please help with the following.

 
  1. First, you set Trade to a random magic number.
    int MagicNumber;     
    ⋮ 
      trade.SetExpertMagicNumber(MagicNumber);
    Then in trailing, you use a different selection filter.
      MagicNumber=(StringGetCharacter(Symbol(),0)*1
                 +StringGetCharacter(Symbol(),2)*3
                 +StringGetCharacter(Symbol(),4)*5
                 +StringGetCharacter(Symbol(),6)*7
                 +StringGetCharacter(Symbol(),8)*9
                 +StringGetCharacter(Symbol(),10)*11
                 +StringGetCharacter(Symbol(),12)*13
                 +StringGetCharacter(Symbol(),14)*15
                 +StringGetCharacter(Symbol(),16)*17);
    ⋮
        if(m_position.Symbol()==Symbol()&&m_position.Magic()==MagicNumber)


  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 2011

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 
William Roeder #:
  1. First, you set Trade to a random magic number.
    Then in trailing, you use a different selection filter.


  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 2011

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

Thank you William it works perfectly now

 

Just re-order the statements:

  MagicNumber=(StringGetCharacter(Symbol(),0)*1
             +StringGetCharacter(Symbol(),2)*3
             +StringGetCharacter(Symbol(),4)*5
             +StringGetCharacter(Symbol(),6)*7
             +StringGetCharacter(Symbol(),8)*9
             +StringGetCharacter(Symbol(),10)*11
             +StringGetCharacter(Symbol(),12)*13
             +StringGetCharacter(Symbol(),14)*15
             +StringGetCharacter(Symbol(),16)*17);
  
  Print(MagicNumber);

  trade.SetExpertMagicNumber(MagicNumber);
  trade.SetMarginMode();
  trade.SetTypeFillingBySymbol(m_symbol.Name());