MQL5 Book Include Reference
MQL5 Book Include Reference
The include files are located in the \MQL5\Include\Mql5Book\ folder of your MetaTrader 5 installation. To include a file in
your program, use the #include directive:
#include <Mql5Book\Trade.mqh>
Trade.mqh
CTrade class
Opens a buy or sell position. Returns true if the position is placed, false otherwise.
1
Opens a pending order of the specified type. Returns true if the position is placed, false otherwise.
Opens a buy or sell stop limit order. Returns true if the position is placed, false otherwise.
Modifies the stop loss and take profit price of the current position. If either the stop loss or take profit price will not be
changed, the current price must be passed to the function. You can retrieve the current price using the
PositionStopLoss() and PositionTakeProfit() functions. Returns true if the position was modified, false otherwise.
2
bool CTrade::ModifyPending(ulong pTicket, double pPrice, double pStop, double pProfit,
datetime pExpiration=0)
Modifies the opening price, stop loss or take profit price of a specified order. If either the opening price, stop loss or take
profit price will not be changed, the current price must be passed to the function. You can retrieve the current prices of the
order using the OrderOpenPrice(), OrderStopLoss() or OrderTakeProfit() functions from the Pending.mqh file.
Returns true if the order was modified, false otherwise.
Closes the current position on the specified symbol. Returns true if the position was closed successfully, false otherwise.
• pVolume - The volume to partially close. If pVolume is greater than the current position volume, or pVolume is 0,
the entire position will be closed.
Deletes the specified pending order. Returns true if the order was closed successfully.
Sets the magic number, deviation or fill type for all orders. Call the function once in the OnInit() event handler to set the
relevant property.
3
double BuyStopLoss(string pSymbol,int pStopPoints, double pOpenPrice = 0)
double SellStopLoss(string pSymbol,int pStopPoints, double pOpenPrice = 0)
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.
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.
4
string PositionComment(string pSymbol = NULL)
long PositionType(string pSymbol = NULL)
long PositionIdentifier(string pSymbol = NULL)
double PositionOpenPrice(string pSymbol = NULL)
long PositionOpenTime(string pSymbol = NULL)
double PositionVolume(string pSymbol = NULL)
double PositionStopLoss(string pSymbol = NULL)
double PositionTakeProfit(string pSymbol = NULL)
double PositionProfit(string pSymbol = NULL)
• pSymbol - The position to return information for. If not specified, the current chart symbol will be used.
Returns a string describing the order type, for example "buy" or "sell stop."
Pending.mqh
CPending class
Returns the number of currently open orders of the specified type for the specified symbol.
Returns an array filled with the ticket numbers of open orders on the specified symbol.
• pTickets - An array passed by reference that will be filled with the order tickets.
5
long OrderType(ulong pTicket)
long OrderExpirationTime(ulong pTicket)
long OrderExpirationType(ulong pTicket)
long OrderMagicNumber(ulong pTicket)
double OrderVolume(ulong pTicket)
double OrderOpenPrice(ulong pTicket)
double OrderStopLimit(ulong pTicket)
double OrderStopLoss(ulong pTicket)
double OrderTakeProfit(ulong pTicket)
string OrderComment(ulong pTicket)
Price.mqh
CBars class
Updates the price data for the last 100 bars on the specified symbol and period. The MAX_BARS constant in the Price.mqh
file adjusts the number of bars to retrieve data for.
• pPeriod - The period of the chart to retrieve the price data for.
Returns the specified price for the specified bar. The current bar has a shift value of 0, the previous bar is 1 and so on. The
Update() function above must be called first to ensure the most current prices are used.
6
double Ask(string pSymbol=NULL)
double Bid(string pSymbol=NULL)
Retrieves the current Bid or Ask price for the specified symbol.
• pSymbol - The symbol to retrieve the price for. If no value is specified, the current chart symbol will be used.
• pSymbol - The symbol to retrieve the spread for. If no value is specified, the current chart symbol will be used.
• pSymbol - The symbol to retrieve the stop level for. If no value is specified, the current chart symbol will be used.
Retrieves the highest high or lowest low price for the specified bar range.
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.
7
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.
Trailing.mqh
CTrailing class
Implements a trailing stop for open positions. The first variant of the function takes a trailing stop value in points. The
second variant takes a trailing stop price. Returns a value of true if the trailing stop was adjusted, false otherwise.
• pSymbol - The symbol of the position 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).
8
bool CTrailing::BreakEven(string pSymbol, int pBreakEven, int pLockProfit = 0)
Implements a break even stop for open positions. The stop loss is moves 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.
• pSymbol - The symbol of the position to adjust the break even stop for.
• pBreakEven - 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.
• pLocalTime - If true, will use your computer's local time. Otherwise, the server time will be used (optional).
9
bool CTimer::BlockTimer(TimerBlock &pBlock[], bool pLocalTime = false)
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.
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.
10
Indicators.mqh
CIndicator class
Returns the value of the first indicator buffer for the specified bar. All indicator classes based on CIndicator implement
this function.
• pShift - The shift of the bar to retrieve the indicator value from.
void Release()
Releases the indicator from memory. All indicator classes based on CIndicator implement this function.
CiMA class
CiRSI class
11
• pRSIPrice - The price series to use.
CiStochastic class
Returns the value for the %D line for the specified bar.
• pShift - The shift of the bar to retrieve the indicator value from.
CiBollinger class
12
double CiBollinger::Lower(int pShift=0)
double CiBollinger::Upper(int pShift=0)
Returns the lower or upper band price for the specified bar.
• pShift - The shift of the bar to retrieve the indicator value from.
CiMACD class
Returns the price of the signal line for the specified bar.
• pShift - The shift of the bar to retrieve the indicator value from.
CiSAR class
13
CiADX class
Returns the price for the plus and minus DI lines for the specified bar.
• pShift - The shift of the bar to retrieve the indicator value from.
14