Skip to main content

Common MQL4 Functions Library

Description

Top-quality MetaTrader 4 library with different functions to support you with improving your returns. All functions were tested thoroughly and optimized for minimal speed and latency.

Calculate orders profit based on type:
double LongOrdersProfit(string argSymbol) export
double ShortOrdersProfit(string argSymbol) export

Close orders based on a specific condition:
bool CloseOrder(int argOrder) export
bool CloseOrder(int argOrder,double argLotSize) export
bool CloseLongOrders(string argSymbol) export
bool CloseShortOrders(string argSymbol) export
bool ClosePendingOrder(int argOrder) export
bool CloseAllPendingOrders() export
bool CloseAllProfitingOrders(string argSymbol) export
bool CloseAllLosingOrders() export
bool CloseAllOrders(int argMagicNumber) export
bool CloseAllOrders(string argSymbol) export
bool CloseAllOrders() export

Verify the lot size is valid for this symbol:
double VerifyLotSize(string argSymbol,double argLotSize) export

Calculate lot size based on risk parameters such as the equity percentage you want to use for this order and the volatility for this period:
double CalcRiskBasedLotSize(string argSymbol,double argPercentageRiskPerOrder,int argTimeframe,int argSTDFactor,int argSTDPeriod) export

Calculate the P&L for today based on the closed historical orders:
double DailyProfitAndLoss() export

Confirm there is enough margin in the account prior to opening an order:
bool ConfirmEnoughMargin(string argSymbol,int argOrderType,double argLotSize) export

Open order function, return code can be -1 for a failure or the ticket ID:
int OpenOrder(string argSymbol,int argOrderType,double argLotSize,double argSL=0,double argTP=0,string argComment=NULL,int argMagicNumber=0,int argSpread=0) export
int OpenStopOrder(string argSymbol,int argStopOrderType,double argLotSize,double argPendingPrice,double argSLPoints=0,double argTPPoints=0,string argComment=NULL,int argMagicNumber=0) export

Calculate profit / loss for the open positions:
double AccountProfitAdj(string argSymbol=””) export
double AccountLossAdj(string argSymbol=””) export

Update stop loss / take profit for a specific order:
bool AddStopProfit(int argOrder,double argStopLoss,double argTakeProfit) export

Count open orders based on type:
void CountOpenOrders(int &argResult[],string argSymbol) export

Print the un-initialization message on adviser exit:
string GetUninitReasonText(int reasonCode) export

Close all open charts:
void CloseAllCharts(string argProductsExclude="") export

Initialize a specific product with an existing template (i.e. expert adviser):
long InitializeProduct(string argTemplate,string argProduct,int argargTimeFrame) export

Formatted logging functions:
void LogError(string argSymbol,double argOrderLots,string argMessage,int argErrorCode=0,bool argSilent=false) export
void Log(string argMessage,bool argSilent=false) export

Generate magic number based on the product name:
int GenerateMagicNumber(string argInput) export

Quick sort an array:
void QuickSort(string &arr[],int beg,int end) export

Retry closing tickets which failed to close:
void RetryClosingTickets(string &TicketsString) export


Usage

To use this library, place it in the libraries folder, compile and import the function you want to use in the expert adviser / indicator. For example, to use the close all charts function, use the following statement at the beginning of the file:

#import "Common.ex4"
void CloseAllCharts(string argProductsExclude="");
#import
And then calling the function -
CloseAllCharts();

Download

Comments

Popular posts from this blog

MetaTrader 4 Indicator to show Currency trend in Different Timeframes

Usage When trading, you probably have several windows open next to each other, different products, different time frames. Or, you have the same product but in different timeframes to see how it performs.
I wrote this indicator so I can see for a given product, how it behaves in the different timeframes available by MetaTradre 4. It has three different lines to express the following information: The trend for a given timeframe, blue arrow means bullish, red arrow means bearish.The pips change for a given timeframe, blue text means bullish, red text means bearish.The standard deviation for a given timeframe, red text means that the timeframe movement is higher thatn the volatility, otherwise black. Once attached, a panel will be drawn in the lower left corner as in the following screenshot for EURUSD:

You may see, that the daily trend for example is bullish. And that the hourly movement in pips is higher than the volatility, thus the STD value for hourly timeframe is painted in red.

To …

Mutual Exclusion (Mutex) Implementation in MQL4

Introduction In computer science, mutual exclusion is a property of concurrency control, which is instituted for the purpose of preventing race conditions; it is the requirement that one thread of execution never enters its critical section at the same time that another concurrent thread of execution enters its own critical section.

In Algotrading, you may encounter this situation while:
Running several expert advisers using a shared object which may worked on only once and not in parallel.Trading only in certain conditions, for example allowing only one expert adviser to open a position while the other ones are blocked. In my case, I am running several expert advisers on the same account trading different currencies. But, I wanted to have only one product open at a given moment while the others are blocked. So, if for example both EURUSD & EURJPY have the right conditions to open an order, only one order will be open. I wanted to avoid to be double exposed to products which are c…

How I make Money trading Forex

Did you know that on average 80% of retail traders lose money when trading Forex?

But, if you've been following my blog, you realized by now that I make my income from trading Forex, and to be specific from trading EURUSD only. This is the only product I trade and live from, which is quite counter intuitive, how is it possible to trade only one product with all it's ups and downs and live from it? In this post, I will explain exactly how I make a living from trading Forex and which traps to avoid.

Still, even that it worked for me and the setup is quite simple, there's no guarantee it will work for you the reader. We are all in a different situations in life, with more or less to risk, knowledge and so on. But still, I suggest to read this post to understand which traps are out there and which path you can follow to avoid losing your money.

The Traps



But first, let's start with the typical pitfalls out there, unfortunately, in some of them I fell myself and lost money:

Buyi…