Warm tip: This article is reproduced from serverfault.com, please click

How to open multiple OrderSend() in MQL4 Metatrader 4?

发布于 2016-09-07 17:54:13

I am trying to simultaneously open multiple trades in MQL4 with an ExpertAdvisor or Script. However when I drag the EA to a chart, it only opens one Order, not the the second (or third, fouth, etc)

extern double lots = 0.01;
extern int takeprofit = 40;
extern int stoploss = 40;

void OnStart(){

double profitlvl_buy;
double losslvl_buy;

profitlvl_buy = Ask+takeprofit*Point;
losslvl_buy = Ask-stoploss*Point;

int ticket1;
int ticket2;

ticket1 = OrderSend("EURUSD", OP_BUY, lots, MarketInfo("EURUSD", MODE_ASK), 10, losslvl_buy, profitlvl_buy, NULL);
ticket2 = OrderSend("GBPUSD", OP_BUY, lots, MarketInfo("GBPUSD", MODE_ASK), 10, losslvl_buy, profitlvl_buy, NULL);

}

What do I need to do to make it execute those multiple OrderSend() calls?

Questioner
rainerbrunotte
Viewed
0
1 2016-09-08 15:03:46

Ok. When you deploy your EA/script on chart, it uses information about chart(symbol, timeframe and others - they are pink as a rule in editor)

So, when you deploy an EA on, suppose EURUSD, chart, it goes to line:

profitlvl_buy = Ask+takeprofit*Point;

and thinks that "Ask" is definitely Ask of EURUSD (or in other words, Ask price of the chart _Symbol ) then you request to send trades on GBPUSD, having takeprofit and stoploss calculated for EURUSD, so I suppose takeprofit is incorrect. I.e. Ask is 1.12 and takeprofit is 1000 ticks so tp = 1.13 and price of GBP is 1.33, takeprofit cannot be ( for orders at Market ) below a trade entry.

In order to fix:

double ask  = MarketInfo( "EURUSD", MODE_ASK );
int ticket1 = OrderSend(  "EURUSD", OP_BUY, lots, ask, 10, ask-stoploss*Point, ask+takeprofit*Point, NULL );

       ask  = MarketInfo( "GBPUSD", MODE_ASK );
int ticket2 = OrderSend(  "GBPUSD", OP_BUY, lots, ask, 10, ask-stoploss*Point, ask+takeprofit*Point, NULL );

also it is usually a good practice to make sure ticket returns int number ( in case of success ) or -1 and in such case prints code of an error, in this case - error#130