EA for multiple pairs won't initiate trades

 

Hi everyone,


Just got an EA running for 1 pair based on RSI.  It works when I use the pair on the chart in MT4.  However, when I try to apply it to multiple pairs, it doesn't seem to work.  I was hoping one of you nice folks could help me!  The codes are below for single and multiple pairs.  I've gotten the multiple code to attempt purchases before tweaking some more.  MT4 gave the error that TP and SL were out of range.  I realized that I hadn't used the correct bid and ask prices for the appropriate pairs (it was using the bid/ask for the chart pair).  After adding those with MarketInfo(), it no longer even attempts to buy or sell.


Here's the "meat" of the single pair:

if(StopLoss>0){SL_B=Bid - StopLoss*vPoint;}else{SL_B=0;}

if(StopLoss>0){SL_S=Ask + StopLoss*vPoint;}else{SL_S=0;}

 
if(TakeProfit>0){TP_B=Ask+TakeProfit*vPoint;}else{TP_B=0;}

if(TakeProfit>0){TP_S=Bid-TakeProfit*vPoint;}else{TP_S=0;}

if(RSI_Current < 30 && OrdersTotal() == 0){ticket = OrderSend(Symbol(),OP_BUY,NormalizeDouble(Lots,LotDigits), Ask,vSlippage,SL_B,TP_B,EAName, MagicNumber, 0, Green);

return;
}

if(RSI_Current > 70 && OrdersTotal() == 0){ticket = OrderSend(Symbol(),OP_SELL,NormalizeDouble(Lots,LotDigits), Bid,vSlippage,SL_S,TP_S,EAName, MagicNumber, 0, Green);

return;

}



And here's the modification for the code with multiple pairs:

//-------------------------------------------------------------------------------------------------------------
// EURUSD

int ord_EURUSD,EURUSD;

total = OrdersTotal();

for(EURUSD=0;EURUSD<total;EURUSD++)

{

OrderSelect(EURUSD,SELECT_BY_POS);

//if(OrderSymbol() == Symbol())ord++;
if(OrderSymbol() == "EURUSD")ord_EURUSD++;

}

if(ord_EURUSD>0) return (0); //Abort! A Position For This Pair is Already Open

//the rest of my program code



double Spread_EURUSD = MarketInfo("EURUSD", MODE_SPREAD);
Print("Spread_EURUSD = ",Spread_EURUSD);

double Bid_EURUSD = MarketInfo("EURUSD", MODE_BID);
double Ask_EURUSD = MarketInfo("EURUSD", MODE_ASK);

double RSI_Current_EURUSD = iRSI("EURUSD",TimeFrame,14,PRICE_CLOSE,Current);

double RSI_Previous_EURUSD = iRSI("EURUSD",TimeFrame,14,PRICE_CLOSE,Current + 1);


if(StopLoss>0){SL_B_EURUSD=Bid_EURUSD - StopLoss*vPoint;}else{SL_B_EURUSD=0;}

if(StopLoss>0){SL_S_EURUSD=Ask_EURUSD + StopLoss*vPoint;}else{SL_S_EURUSD=0;}

 
if(TakeProfit>0){TP_B_EURUSD=Ask_EURUSD+TakeProfit*vPoint;}else{TP_B_EURUSD=0;}

if(TakeProfit>0){TP_S_EURUSD=Bid_EURUSD-TakeProfit*vPoint;}else{TP_S_EURUSD=0;}

if(RSI_Current_EURUSD < 30   && Spread_EURUSD < 4){ticket = OrderSend("EURUSD",OP_BUY,NormalizeDouble(Lots,LotDigits), Ask_EURUSD,vSlippage,SL_B_EURUSD,TP_B_EURUSD,EAName, MagicNumber, 0, Green);

return;
}

if(RSI_Current_EURUSD > 70   && Spread_EURUSD < 4){ticket = OrderSend("EURUSD",OP_SELL,NormalizeDouble(Lots,LotDigits), Bid_EURUSD,vSlippage,SL_S_EURUSD,TP_S_EURUSD,EAName, MagicNumber, 0, Green);

return;
}
//-------------------------------------------------------------------------------------------------------------
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  3. Do not trade multiple currencies in one EA
    • You can't use any predefined variables, can't use the tester, must poll (not OnTick,) and usually other problems.
    • Code it to trade the chart pair only. Look at the others if you must.
    • Then put it on other charts to trade the other pairs. Done.
  4. If your chart isn't exactly "EURUSD" then it can't work. Broker's use a variety of naming patterns: EURUSD, EURUSDm, EURUSDi, "EURUSD.", "EURUSD..", "EUR.USD", "EUR/USD", "EURUSD.stp", EURUSDct, "EURUSD.G", "EURUSD+", and EURUSDpro at least. Don't hard code things; just use the predefined _Symbol.
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  3. Do not trade multiple currencies in one EA
    • You can't use any predefined variables, can't use the tester, must poll (not OnTick,) and usually other problems.
    • Code it to trade the chart pair only. Look at the others if you must.
    • Then put it on other charts to trade the other pairs. Done.
  4. If your chart isn't exactly "EURUSD" then it can't work. Broker's use a variety of naming patterns: EURUSD, EURUSDm, EURUSDi, "EURUSD.", "EURUSD..", "EUR.USD", "EUR/USD", "EURUSD.stp", EURUSDct, "EURUSD.G", "EURUSD+", and EURUSDpro at least. Don't hard code things; just use the predefined _Symbol.

1. Put into SRC mode, thanks.


2 and 4.  I did indicate in the original post that MT4 attempted to open positions for a few pairs (I saw on the Journal tab) but ran into the error that TP and SL were out of the boundaries.  So after adding:

double Bid_EURUSD = MarketInfo("EURUSD", MODE_BID);
double Ask_EURUSD = MarketInfo("EURUSD", MODE_ASK);

it no longer even attempted to purchase them.  So, I believe that this is one of the main issues.  Since it attempted to purchase some, the symbols aren't the main culprit.


3.  Thanks for the suggestion, it seems like it would work better with an EA for each pair.  Didn't realize that could be done.  Sorry for my ignorance.  I'll have to read more into making that work.  How many charts with the same EA can I have running at once?  I don't mind a "go read up on it" answer.  I'm at work and will give a try on multiple when I get home.  (EDIT:  Did my due dilligence.  https://www.ea-coder.com/attach-multiple-expert-advisors-on-mt4/)


The main concern is that I still want only one trade open for each pair.  If I had the same exact EA for each chart and used Symbol(), would the following piece of code (as a part of the EA) constrain it to one open position for each chart?  Basically, is OrdersTotal() global?  If it is, I think that should work.  If not, I think I'd use Orderstotal() == 0 in my "if" statement.  I can always look into Magic numbers too.


int total,ord,i;

total = OrdersTotal();

for(i=0;i<total;i++)

{

OrderSelect(i,SELECT_BY_POS);

if(OrderSymbol() == Symbol())ord++;

}

if(ord>0) return (0); //Abort! A Position For This Pair is Already Open

//the rest of my program code


Not using the tester was a pain.  I just tried to "go live" with a demo account.


 

Reason: