Download MetaTrader 5
To add comments, please log in or register
Have you developed a trading robot? Publish it in the Market and make profit!
Morteza Khorasani
88
Morteza Khorasani 2016.12.01 09:44 

Hi

I wrote an expert to open two position at the same time. and close them when the total profit/loss of the two order reach the profit/koss target.

User can enter the symbol names and type of two orders as input.

I did this:

input string   FirstSymbol="GBPAUD";

input string   firstaction="BUY";

input string   SecondSymbol="EURUSD";

input string   secondaction="SELL"; 

==================

I have a function that open orders:

void setorders()

{//setorders

if(firstaction=="BUY")

int ticket1=OrderSend(FirstSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);

else if(firstaction=="SELL")

int ticket1=OrderSend(FirstSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);



if(secondaction=="BUY")

int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);

else if(secondaction=="SELL")

int ticket2=OrderSend(SecondSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);

}//setorders

=========================

The expert opens two orders but both of them are GBPAUD/BUY....

honest_knave
1349
honest_knave 2016.12.01 09:52  
void setorders()

{//setorders

if(firstaction=="BUY")

int ticket1=OrderSend(FirstSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);

else if(firstaction=="SELL")

int ticket1=OrderSend(FirstSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);



if(secondaction=="BUY")

int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);

else if(secondaction=="SELL")

int ticket2=OrderSend(SecondSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);

}//setorders

You should check your return codes on the OrderSend.

I'm guessing this EA is running on GBPAUD chart? If so, here is what I think is happening:

Your code is running twice, hence 2 orders opening on GBPAUD.

The OrderSend on EURUSD is failing because you are trying to open it at the Ask price on GBPAUD, which is a long way away from the Ask price on EURUSD! 

You will find information on both checking the returned value and about opening trades on other symbols in the OrderSend documentation.

HTH

OrderSend - Trade Functions - MQL4 Reference
OrderSend - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderSend - Trade Functions - MQL4 Reference
Morteza Khorasani
88
Morteza Khorasani 2016.12.01 09:59  
honest_knave:
void setorders()

{//setorders

if(firstaction=="BUY")

int ticket1=OrderSend(FirstSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);

else if(firstaction=="SELL")

int ticket1=OrderSend(FirstSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);



if(secondaction=="BUY")

int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);

else if(secondaction=="SELL")

int ticket2=OrderSend(SecondSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);

}//setorders

You should check your return codes on the OrderSend.

I'm guessing this EA is running on GBPAUD chart? If so, here is what I think is happening:

Your code is running twice, hence 2 orders opening on GBPAUD.

The OrderSend on EURUSD is failing because you are trying to open it at the Ask price on GBPAUD, which is a long way away from the Ask price on EURUSD! 

You will find information on both checking the returned value and about opening trades on other symbols in the OrderSend documentation.

HTH

Thank you Knave
No. The ea is running once... It doesn't matter I am running theis on any chart. It will open its orders for the given symbols.

Because I did not use Symbol()  in the ordersend(). 

honest_knave
1349
honest_knave 2016.12.01 10:10  

Not using Symbol is only half the solution. You also need to use the correct price too. 

Maybe this will help explain what I mean better. Please check your Expert tab for messages after you run the code.

void setorders()
  {
   Print("setorders() running");
   if(firstaction=="BUY")
     {
      int ticket1=OrderSend(FirstSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);
      if(ticket1<0) Print("OrderSend on FirstSymbol Buy failed with error #",GetLastError());
     }
   else if(firstaction=="SELL")
     {
      int ticket1=OrderSend(FirstSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);
      if(ticket1<0) Print("OrderSend on FirstSymbol Sell failed with error #",GetLastError());
     }
      
   if(secondaction=="BUY")
     {
      int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);
      if(ticket2<0) Print("OrderSend on SecondSymbol Buy failed with error #",GetLastError());
     }
   else if(secondaction=="SELL")
     {
      int ticket2=OrderSend(SecondSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);
      if(ticket2<0) Print("OrderSend on SecondSymbol Sell failed with error #",GetLastError());
     }
  }//setorders
Morteza Khorasani
88
Morteza Khorasani 2016.12.01 10:27  
honest_knave:

Not using Symbol is only half the solution. You also need to use the correct price too. 

Maybe this will help explain what I mean better. Please check your Expert tab for messages after you run the code.

void setorders()
  {
   Print("setorders() running");
   if(firstaction=="BUY")
     {
      int ticket1=OrderSend(FirstSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);
      if(ticket1<0) Print("OrderSend on FirstSymbol Buy failed with error #",GetLastError());
     }
   else if(firstaction=="SELL")
     {
      int ticket1=OrderSend(FirstSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);
      if(ticket1<0) Print("OrderSend on FirstSymbol Sell failed with error #",GetLastError());
     }
      
   if(secondaction=="BUY")
     {
      int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,Ask,slippage,0,0,NULL,magicnumber,0,clrBlue);
      if(ticket2<0) Print("OrderSend on SecondSymbol Buy failed with error #",GetLastError());
     }
   else if(secondaction=="SELL")
     {
      int ticket2=OrderSend(SecondSymbol,OP_SELL,lotsize,Bid,slippage,0,0,NULL,magicnumber,0,clrRed);
      if(ticket2<0) Print("OrderSend on SecondSymbol Sell failed with error #",GetLastError());
     }
  }//setorders
I encountered something strange.
when I use GBPAUD/BUY and GBPJPY/SELL as input. I have two GBPAUD/BUY orders.
But when I use GBPAUD/BUY and EURUSD/SELL . Two orders open correctly.
Files:
ee.jpg 294 kb
honest_knave
1349
honest_knave 2016.12.01 10:35  

You really won't be able to get anywhere fixing this unless you check your OrderSend return values.

Also, I hate to labour the point but trying to open orders on other symbols using the predefined variables Bid and Ask is simply wrong.

From the documentation I linked you to:

  • At opening of a market order (OP_SELL or OP_BUY), only the latest prices of Bid (for selling) or Ask (for buying) can be used as open price. If operation is performed with a security differing from the current one, the MarketInfo() function must be used with MODE_BID or MODE_ASK parameter for the latest quotes for this security to be obtained.

Your code should be looking more like this:

   if(firstaction=="BUY")
     {
      double price = SymbolInfoDouble(FirstSymbol,SYMBOL_ASK);
      int ticket1=OrderSend(FirstSymbol,OP_BUY,lotsize,price,slippage,0,0,NULL,magicnumber,0,clrBlue);
      if(ticket1<0) Print("OrderSend on FirstSymbol Buy failed with error #",GetLastError());
     }
(You could use MarketInfo() if you prefer)
Morteza Khorasani
88
Morteza Khorasani 2016.12.01 10:45  

 

This works, Thank you. 

 

void setorders()
  {
   Print("setorders() running");
   if(firstaction=="BUY")
     {
      int ticket1=OrderSend(FirstSymbol,OP_BUY,lotsize,MarketInfo(FirstSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);
      if(ticket1<0) Print("OrderSend on FirstSymbol Buy failed with error #",GetLastError());
     }
   else if(firstaction=="SELL")
     {
      int ticket1=OrderSend(FirstSymbol,OP_SELL,lotsize,MarketInfo(FirstSymbol,MODE_BID),slippage,0,0,NULL,magicnumber,0,clrRed);
      if(ticket1<0) Print("OrderSend on FirstSymbol Sell failed with error #",GetLastError());
     }
      
   if(secondaction=="BUY")
     {
      int ticket2=OrderSend(SecondSymbol,OP_BUY,lotsize,MarketInfo(SecondSymbol,MODE_ASK),slippage,0,0,NULL,magicnumber,0,clrBlue);
      if(ticket2<0) Print("OrderSend on SecondSymbol Buy failed with error #",GetLastError());
     }
   else if(secondaction=="SELL")
     {
      int ticket2=OrderSend(SecondSymbol,OP_SELL,lotsize,MarketInfo(SecondSymbol,MODE_BID),slippage,0,0,NULL,magicnumber,0,clrRed);
      if(ticket2<0) Print("OrderSend on SecondSymbol Sell failed with error #",GetLastError());
     }
  }//setorders
honest_knave
1349
honest_knave 2016.12.01 10:51  
Morteza Khorasani:

 

This works, Thank you. 

I'm glad you got it sorted.
whroeder1
13959
whroeder1 2016.12.01 13:37  
honest_knave: From the documentation I linked you to:

  • At opening of a market order (OP_SELL or OP_BUY), only the latest prices of Bid (for selling) or Ask (for buying) can be used as open price. If operation is performed with a security differing from the current one, the MarketInfo() function must be used with MODE_BID or MODE_ASK parameter for the latest quotes for this security to be obtained.
This is why I recommend
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.
/
To add comments, please log in or register