MQL4 Function Reference
MQL4 Function Reference
The include files are located in the \MQL4\Include\Mql4Book\ folder of your MetaTrader 4 installation. To include a file in
your program, use the #include directive:
#include <Mql4Book\Trade.mqh>
Trade.mqh
CTrade class
Opens a buy or sell order. Returns the order ticket number, or -1 if the order is not placed.
1
color pArrow = clrMediumSlateBlue)
Opens a pending order of the specified type. Returns the order ticket number, or -1 if the order is not placed.
Closes a single market order. Return true if the order was closed, false otherwise.
• pVolume - The trade volume to close. If pVolume is less than the order volume, the order will be partially closed if
the broker supports it. If pVolume is greater than the order volume, or pVolume is 0, the entire order will be closed.
bool CTrade::CloseAllBuyOrders()
bool CTrade::CloseAllSellOrders()
bool CTrade::CloseAllMarketOrders()
Closes all orders of the specified type that match the magic number set by the SetMagicNumber() function. Return true if
all orders were closed successfully, false otherwise.
Deletes the specified pending order. Return true if the order was deleted, false otherwise.
bool CTrade::DeleteAllBuyStopOrders()
bool CTrade::DeleteAllSellStopOrders()
bool CTrade::DeleteAllBuyLimitOrders()
bool CTrade::DeleteAllSellLimitOrders()
2
bool CTrade::DeleteAllPendingOrders()
Deletes all pending orders of the specified type that match the magic number set by the SetMagicNumber() function.
Sets the magic number to be applied to all orders opened with this expert advisor.
Returns the magic number that was set by the SetMagicNumber() function.
Sets the slippage to be used when placing market orders on instant execution brokers.
Non-Class Functions
Modifies a single order. If the order price, stop loss, take profit or expiration will not be modified, you must pass the
current value to this function. Returns true if the order was modified successfully, false otherwise.
bool ModifyStopsByPoints(int pTicket, int pStopPoints, int pProfitPoints = 0, int pMinPoints = 10)
Modifies the stop loss and/or take profit for the specified order, using the specified stop distance in points. The stop will
be automatically adjusted if invalid.
3
• pStopPoints - The new stop loss distance in points.
• pMinPoints - The minimum stop distance, in case the stop loss or take profit is automatically adjusted (optional).
Modifies the stop loss and/or take profit for the specified order, using the specified prices. The stop will be automatically
adjusted if invalid.
• pMinPoints - The minimum stop distance, in case the stop loss or take profit is automatically adjusted (optional).
Calculates and returns a stop loss price for a buy or sell order.
• pStopPoints - The distance between the stop loss and the order opening price in points.
• pOpenPrice - The order opening price. If 0, the current Bid or Ask price will be used.
Calculates and returns a take profit price for a buy or sell order.
• pProfitPoints - The distance between the take profit and the order opening price in points.
• pOpenPrice - The order opening price. If 0, the current Bid or Ask price will be used.
Verifies whether a pending order opening price, stop loss or take profit price is valid relative to the current Bid or Ask price.
If the price is within the stop level (+/- pPoints), it is invalid. Returns true if the price is valid, false otherwise.
4
• pPoints - Adds/subtracts a specified number of points to the stop level price.
Verifies whether a pending order opening price, stop loss or take profit price is valid relative to the current Bid or Ask price.
If the price is within the stop level (+/- pPoints), it is invalid and will be automatically adjusted to a valid price. Returns
pPrice if the price is valid, or an adjusted price otherwise.
CCount Class
int CCount::Buy()
int CCount::Sell()
int CCount::BuyStop()
int CCount::SellStop()
int CCount::BuyLimit()
int CCount::SellLimit()
int CCount::TotalMarket()
int CCount::TotalPending()
int CCount::TotalOrders()
Returns a count of all open orders of the specified type that match the magic number set by the SetMagicNumber()
function in the CTrade class. The TotalMarket() function returns a count of all market orders (buy and sell),
TotalPending() returns a count of all pending orders, and TotalOrders() returns a count of all orders, market and
pending.
MoneyManagement.mqh
Calculates a trade volume using a specified percentage of the current balance, as well as the stop loss in points. Returns
the calculated trade volume, or if no trade volume can be calculated, the pFixedVol value.
5
double VerifyVolume(string pSymbol, double pVolume)
Verifies a trade volume against the minimum, maximum and step values enforced by the trade server. Returns pVolume if
the trade volume is valid, or an adjusted value if not.
Calculates and returns the difference in points between a stop loss price and an order opening price.
TrailingStop.mqh
bool TrailingStop(string pTicket, int pTrailPoints, int pMinProfit = 0, int pStep = 10)
bool TrailingStop(string pTicket, double pTrailPrice, int pMinProfit = 0, int pStep = 10)
Applies a trailing stop to a single order. The first variant of the function takes a trailing stop value in points, and the second
variant takes a trailing stop price. Returns a value of true if the trailing stop was adjusted, false otherwise.
• pTicket - The ticket number of the order to adjust the trailing stop for.
• pMinProfit - The minimum profit in points required before the trailing stop will be adjusted (optional).
• pStep - Moves the trailing stop in increments. The minimum step is 10 points (optional).
Applies a trailing stop to all open market orders. The first variant of the function takes a trailing stop value in points, and
the second variant takes a trailing stop price.
• pMinProfit - The minimum profit in points required before the trailing stop will be adjusted (optional).
• pStep - Moves the trailing stop in increments. The minimum step is 10 points (optional).
6
bool BreakEvenStop(int pTicket, int pMinProfit, int pLockProfit = 0)
Applies a break even stop for a single order. The stop loss is moved to the order opening price once as soon as the
specified break even profit is reached. Returns a value of true if the break even stop is adjusted successfully, false
otherwise.
• pTicket - The ticket number of the order to adjust the break even stop for.
• pMinProfit - The break even profit in points. When the order profit in points meets or exceeds this amount, the
break even stop will trigger.
• pLockProfit - Adds or subtracts a specified number of points to the break even stop price (optional).
• pMinProfit - The break even profit in points. When the order profit in points meets or exceeds this amount, the
break even stop will trigger.
• pLockProfit - Adds or subtracts a specified number of points to the break even stop price (optional).
Timer.mqh
CTimer class
Takes a start and end time and determines whether the current time falls between them. Returns a value of true if the
current time is between the start and end time, and false otherwise.
• pLocalTime - If true, will use your computer's local time. Otherwise, the server time will be used (optional).
Calculates a start and end time using a start and end hour and minute. Returns a value of true if the current time is
between the start and end times, and false otherwise.
7
• pEndHour - The end hour.
• pLocalTime - If true, will use your computer's local time. Otherwise, the server time will be used (optional).
Takes an array that holds several start and end times, and determines whether the current time falls between any of them.
The array type uses the TimerBlock structure, described below. Returns a value of true if the current time falls within one
of the start and end times, and false otherwise.
• pBlock - An array passed by reference that contains start and end times.
• pLocalTime - If true, will use your computer's local time. Otherwise, the server time will be used (optional).
struct TimerBlock
{
bool enabled;
int start_day;
int start_hour;
int start_min;
int end_day;
int end_hour;
int end_min;
};
The TimerBlock structure is used for the BlockTimer() function. An array is declared using TimerBlock as the type, and
loaded with data representing start and end times for the current week.
• enabled - Set to true if the timer for the current array element is enabled, false if disabled.
• start_day - The start day for the current timer. 0: Sunday, 5: Friday.
• end_day - The end day for the current timer. 0: Sunday, 5: Friday.
datetime CTimer::GetStartTime()
datetime CTimer::GetEndTime()
Returns the current start time or end time for the current timer.
8
CNewBar class
Checks to see if a new bar has opened since the last tick. Returns a value of true if a new bar has opened, and false
otherwise.
Indicators.mqh
CIndicator class
Sets the symbol and timeframe for the current indicator. This function is called from the constructor of all classes derived
from the CIndicator class.
CiMA class
void CiMA::CiMA(string pSymbol, int pTimeFrame, int pMaPeriod, int pMaShift, int pMaMethod,
int pAppliedPrice)
9
• pShift - The bar to retrieve the indicator value for. 0 is the current bar.
CiRSI class
• pShift - The bar to retrieve the indicator value for. 0 is the current bar.
CiStochastic class
Returns the value for the %K or %D line for the specified bar.
10
• pShift - The bar to retrieve the indicator value for. 0 is the current bar.
CiBollinger class
void CiBands::CiBands(string pSymbol, int pTimeFrame, int pPeriod, double pDeviation, int pShift,
int pAppliedPrice)
Returns the main, lower or upper band price for the specified bar.
• pShift - The shift of the bar to retrieve the indicator value from.
CiMACD class
11
double CiMACD::Main(int pShift = 0)
double CiMACD::Signal(int pShift = 0)
Returns the price of the signal line for the specified bar.
• pShift - The shift of the bar to retrieve the indicator value from.
12