Opening Trades for Any MetaTrader Symbol
In the last module we created our first user function and used it in our Expert Advisor. If you’d like to review that module, click here: User Functions in MQL.
In this module we are going to add to our user function “fnOpenTrade”. We are going to change the function so it can open a trade for any MetaTrader symbol. The current MQL code can only open a trade for the symbol of the chart that the EA is running on.
We are going to use the MQL programming function MarketInfo. The MarketInfo function allows you to get information about any currency symbol supported on the platform. This is the definition of the MarketInfo function:
double MarketInfo( | string symbol, int type) |
The symbol parameter is the name of the currency symbol that you want information about.
The type parameter is the type of information you are requesting. There are about 30 different types available. They can be viewed in the MetaEditor help window. We’ll be using the types MODE_BID, MODE_ASK and MODE_POINT to request the bid, ask and point prices for any symbol.
The “fnOpenTrade” function will use MarketInfo to get the correct information. We will also change the function so it requires a “symbol” parameter. The definition of our “fnOpenTrade” function is now:
void fnOpenTrade(string symbol, int type)
This is the MQL code for our complete Expert Advisor with the new “fnOpenTrade” function.
// these are all externs so they can be changed when the EA is attached to a chart
// the values set are default values
extern int stoploss=200;
extern int takeprofit=200;
extern double lots = 1.0;
extern int magic_number=12345;
extern int rsi_period=12;
extern double rsi_buy_level=75.0;
extern double rsi_sell_level=25.0;
int start()
{
// get the rsi value
double rsi_value = iRSI(Symbol(), Period(), rsi_period, PRICE_CLOSE, 0);
// this variable will hold the number of trades open for this EA (as defined by magic number)
int my_trades=0;
// this variable will holds the total number of trades for the entire account
int all_trades=OrdersTotal();
// use a for loop to cycle through all of the trades, from 0 up to all_trades
for( int cnt=0;cnt<all_trades;cnt++ )
{
// use OrderSelect to get the info for each trade, cnt=0 the first time, then 1, 2, .., etc
// if OrderSelect fails it returns false, so we just continue
if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false )
continue;
// compare the magic_number of our EA (as passed in as an input parameter) to the order’s magic number
// if they are equal, increment my_trades
if( magic_number == OrderMagicNumber() )
{
my_trades++;
if( OrderType() == OP_BUY )
{
// this is the explicit close logic for a buy order
if(rsi_value < rsi_sell_level)
{
// use the ticket info to close
// note: the price should be Bid for a Buy order
// using OrderLots()will close the entire order
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Green);
}
}
if( OrderType() == OP_SELL )
{
// this is the explicit close logic for a sell order
if(rsi_value > rsi_buy_level)
{
// use the ticket info to close
// note: the price should be Ask for a Sell order
// using OrderLots()will close the entire order
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Green);
}
}
}
}
// my_trades should either be 1 or 0. if it is greater than zero, then we just exit
if( my_trades > 0 )
return(0);
// if the rsi_value is larger than rsi_buy_level, we open a buy
if(rsi_value > rsi_buy_level)
{
// call our user function with a SELL parameter
fnOpenTrade(Symbol(), OP_BUY);
// exit after trying to open a trade
return(0);
}
// if the rsi_value is larger than rsi_buy_level, we open a buy
if(rsi_value < rsi_sell_level)
{
// call our user function with a SELL parameter
fnOpenTrade(Symbol(), OP_SELL);
// exit after trying to open a trade
return(0);
}
return(0);
}
void fnOpenTrade(string symbol, int type)
{
// notice that the symbol is passed in as a parameter
// get the bid value for whatever symbol was sent in
double my_bid = MarketInfo(symbol, MODE_BID);
// get the ask value for whatever symbol was sent in
double my_ask = MarketInfo(symbol, MODE_ASK);
// get the point value for whatever symbol was sent in
double my_point = MarketInfo(symbol, MODE_POINT);
// now instead of using the Bid, Ask and Point values that give the value for the
// symbol that the EA is running, use our values from above
int status =
OrderSend( symbol, // the synbol for this chart
type, // a buy order
lots, // number of lots
my_bid, // use the ask price for a BUY
3, // allow the price up to move 3 points
my_bid + (stoploss*my_point), // stop
my_ask – (takeprofit*my_point), // limit
“My Simple EA”, // comment to see in Terminal
magic_number, // a unique # to id this trade
0, // expiration, doesn’t work
Red // a blue arrow
);
if( status < 0 )
Comment(“OrderSend Failed!! Error=”, GetLastError());
}
Copy and paste this code into your MetaEditor and experiment with it.
In our next module, we are going to use day and time information to control our Expert Advisor.