String Array Problem

 

I have a problem when using string arrays. When I drop my EA on GBPUSD it will not open and others pairs. It only opens on the 1st arrays "EURUSD" and "USDJPY". 

void BuyTrade()
 {
  int Final=0;
  
  string Base[]={"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
  for(int Loop=0;Loop<4;Loop++)
  {
   string symbol=Base[0];
  }
  
  string Base2[]={"USDJPY","USDCHF","USDCAD"};
  for(int Loop2=0;Loop2<3;Loop2++)
  {
   string symbol2=Base2[0];
  }
  
  double Price=SymbolInfoDouble(Base[0],SYMBOL_ASK);
  double Price2=SymbolInfoDouble(Base2[0],SYMBOL_BID);
  
  double TakeProfitBuy=Price+TakeProfit*Pips(Base[0]);
  double StopLossBuy=Price-StopLoss*Pips(Base[0]);
  
  double TakeProfitSell=Price2-TakeProfit2*Pips(Base2[0]);
  double StopLossSell=Price2+StopLoss2*Pips(Base2[0]);
 
  if(CountSymbol(Symbol())<1)
  {
   int BuyTrade=OrderSend(Base[0],OP_BUYSTOP,0.01,Price+(0.5*Pips(Base[0])),0,StopLossBuy,TakeProfitBuy,"Scalper8",MagicNumber,0,clrBlue); 

   int SellTrade=OrderSend(Base2[0],OP_SELLSTOP,0.01,Price2-(0.5*Pips(Base2[0])),0,StopLossSell,TakeProfitSell,"Scalper8",MagicNumber,0,clrRed);
   
   
  } 
  
 }
 
Scalper8:

I have a problem when using string arrays. When I drop my EA on GBPUSD it will not open and others pairs. It only opens on the 1st arrays "EURUSD" and "USDJPY". 

this looks wrong - you are assigning the 1st element every time:

   string symbol=Base[0];

Try this (in both loops)

   string symbol=Base[loop];

Also in the later statements you are accessing the [0] element only:

double Price=SymbolInfoDouble(Base[0],SYMBOL_ASK);
  double Price2=SymbolInfoDouble(Base2[0],SYMBOL_BID);
  
  double TakeProfitBuy=Price+TakeProfit*Pips(Base[0]);
  double StopLossBuy=Price-StopLoss*Pips(Base[0]);
  
  double TakeProfitSell=Price2-TakeProfit2*Pips(Base2[0]);
  double StopLossSell=Price2+StopLoss2*Pips(Base2[0]);
 
Scalper8:

I have a problem when using string arrays. When I drop my EA on GBPUSD it will not open and others pairs. It only opens on the 1st arrays "EURUSD" and "USDJPY". 

You can also do the SELL part.


 string Base[]={"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
    string Base2[]={"USDJPY","USDCHF","USDCAD"};
    
  
    for(int Loop=0;Loop<4;Loop++)
  {
  if(CountSymbol(Symbol())<1)
  {
  
       
   double EntryPrice=NormalizeDouble((MarketInfo(Base[Loop],MODE_ASK)+50*MarketInfo(Base[Loop],MODE_POINT)),MarketInfo(Base[Loop],MODE_DIGITS));
   double EntrySL=NormalizeDouble((EntryPrice-StopLoss*MarketInfo(Base[Loop],MODE_POINT)),MarketInfo(Base[Loop],MODE_DIGITS));
   double EntryTP=NormalizeDouble((EntryPrice+TakeProfit*MarketInfo(Base[Loop],MODE_POINT)),MarketInfo(Base[Loop],MODE_DIGITS));
   bool BuyTrade=OrderSend(Base[Loop],OP_BUYSTOP,0.01,EntryPrice,3,EntrySL,EntryTP,"Scalper8",MagicNumber,0,clrBlue); 
  // Print(GetLastError()," ",Base[Loop]," ",EntryPrice," ",EntrySL," ",EntryTP);
   
   }
   
     }
Files:
BUY.JPG  294 kb
 
R4tna C #:

this looks wrong - you are assigning the 1st element every time:


Try this (in both loops)

Also in the later statements you are accessing the [0] element only:

Minor mistakes, Thank You  R4tna!

 
Mehmet Bastem #:

You can also do the SELL part.


Thank You Mehmet, it works now!

 
Scalper8 #:

Minor mistakes, Thank You  R4tna!

Glad it got sorted - I recommend using the debugger in such cases. By inspecting variables during runtime you can often identify semantic issues which are not apparent when coding/compiling.

In case it helps, here is a ref:

https://www.mql5.com/en/articles/654

Debugging MQL5 Programs
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.