______________________
OVERVIEW
This is a general purpose EA that will close open orders, and/or delete pending orders, that match your specified criteria.
A lot of the code has come from VO-Pro, hence it should work correctly. I will test it further when the market re-opens on Monday.
It is strongly recommended that you run the EA in demo, until you are 100% sure that you understand how the parameter settings work, and that the EA behaves as you would expect it to.
I'm not sure that the EA is all that helpful, as I don't believe that P/L-based trade management can provide an 'edge'. But, for better or worse, here it is.......
TERMS AND CONDITIONS
Please note that all code in this thread is supplied FREE of charge. Therefore the following conditions apply:
1. You agree that, if you download and use the code, it is ENTIRELY AT YOUR OWN RISK. I offer NO guarantee as to the accuracy or completeness of the code, or that is free from error. I accept NO LIABILITY for any financial losses or computer related damage, caused by either the correct or incorrect use of the code.
2. Feel welcome to share the EA freely. However, you may NOT sell, or otherwise distribute, any part of the code commercially, without my prior written consent.
3. The code may not run correctly on Windows 7, Windows 8 or Vista, probably for the reasons given here.
4. I'm sorry, but due to my current work commitments, I am no longer modifying code to suit people’s personal requirements, nor posting replies to every individual question in this thread. (If you can’t get the code to do what you want, you’ll need to find another EA).
5. Source code (.mq4 file) is NOT available.
INSTALLATION
Copy the .ex4 file to your …/(MT4)/experts folder, and then restart MT4.
WARNING: attaching this EA to more than one price chart may cause unpredictable results.
PARAMETER SETTINGS
All settings (except Windows font name) are case insensitive, you may use capitals and lowercase interchangeably.
string SelectIfOrderType = "BS";
- B = select buy orders only
- S = select sell orders only
- BS = select both buy and sell order types
- (blank) = select no orders (useful for temporarily suspending the EA)
string SelectIfSymbolID = "ALL";
- ALL = select orders for all available symbols
- (blank) = select only orders for the symbol on whose chart the EA is attached
- comma separated list = select only orders for symbols in list (e.g. GU, EU, UJ). Allowed abbreviations: A=AUD, C=CAD, E=EUR, F=CHF, G=GBP, J=JPY, N=NZD, U=USD
string SymbolSuffix = "";
Suffix will be added to all symbols in above list (e.g. m selects GBPUSDm etc)
string SelectIfOrderNumber = "";
- (blank) = all orders (that meet all other criteria) will be selected
- comma separated list = only orders whose last digits in their ticket numbers match the digits entered here will be selected (e.g. 032,1237 means that only orders with ticket numbers ending in 032 or 1237 will be selected)
string SelectIfMagicNumber = "";
- (blank) = all orders (that meet all other criteria) will be selected
- comma separated list = only orders whose last digits in their magic numbers match the digits entered here will be selected (e.g. 032,1237 means that only orders with magic numbers ending in 032 or 1237 will be selected)
string SelectIfCommentContains = "";
- (blank) = all orders (that meet all other criteria) will be selected
- comma separated list = only orders whose comment contains the specified text will be selected (e.g. aaa,bb means that only orders with ‘aaa’ or ‘bb’ embedded somewhere in their comment will be selected)
string SelectIfProfitGEthan = "$0";
Only orders with a profit that equals or exceeds this value will be selected. You may enter a negative number; or a very large negative value, to select all orders (that meet all other criteria), and thus effectively disregard this setting. For example, $–999999 will select all orders whose loss doesn’t currently exceed $999,999
Typing a dollar sign ($) anywhere in the field means that the value represents dollars (or whatever currency your account is denominated in). Dollar profit includes commissions and swaps. If there is no dollar sign, the value is assumed to represent a number of pips
bool CloseOpenOrders = false;
- true = all open orders that meet ALL of the above criteria (in the SelectIf… settings) will be fully or partially closed (see FractionToClose). Only one attempt will be made to close these orders, and any errors will be posted to the experts log and debug file (see LogErrorsToDebugFile). This is to stop repeated partial closing of orders on each new price tick, and it may mean that some selected orders are closed, but not others (if the Close instruction fails due to an error being returned by the br0ker).
- false = selected orders will be displayed (see WindowCornerHposVposVspacing), but will not be closed.
string CloseIfTotalGEthan = "$0";
Orders will be closed only if the total profit of all selected orders equals or exceeds this value. You may enter a negative number; or a very large negative value, to ensure that all selected orders (that meet all other criteria) are closed
Typing a dollar sign ($) anywhere in the field means that the value represents dollars (or whatever currency your account is denominated in). Dollar profit includes commissions and swaps. If there is no dollar sign, the value is assumed to represent a number of pips
string FractionToClose = "1/1";
1/1 means that selected orders will be fully closed.
Any other value means that selected orders will be partially closed (ADVANCED USERS ONLY!):
- fraction (forward slash included in field), e.g. 1/2, 1/3, 3/5, 20/100 — each selected order will be partially closed according to the fraction you specify, rounded down to your br0ker’s smallest allowable lot size. For example, if the open order has a size of 0.32 lots, and you specify 20/100 (close 20%), then 0.32 x 20/100 = 0.064, i.e. 0.06 lots will be closed. Note that, in reality, MT4 closes the position and creates a new order for the remaining position (0.26 lots in this example)
- any numeric value without a slash — each selected order will be partially closed by the number of (full) lots that you specify. Selected orders with smaller volume than this value will be fully closed. For example, if the open order has a size of 0.32 lots, and you specify 0.2, then 0.2 lots will be closed. Note that, in reality, MT4 closes the position and creates a new order for the remaining position (0.12 lots in this example)
string CloseArrowColor = "Red";
- valid MT4 color token (e.g. Red, LimeGreen, Goldenrod, MediumSeaGreen) — arrow will be plotted on chart in this color
- (blank) — no arrow will be plotted
bool DeletePendingOrders = false;
- true = any pending orders matching all of the SelectIf… criteria will be deleted. This operates additionally and independently of the CloseOpenOrders option
- false = no pending orders will be deleted
string WindowCornerHposVposVspacing = "0,TR,20,20,20";
Unless blank, it must be 5 entries separated by commas, which control where the P/L statistics of selected orders will be displayed:
#1 = chart window: 0=main chart, 1,2,3, etc = lower sub-windows
#2 = corner of the chart: TR=top right; BL=bottom left , etc
#3 = starting horizontal co-ordinate (pixels)
#4 = starting vertical co-ordinate (pixels)
#5 = vertical spacing (pixels) between the four items
To suppress the statistics from being displayed, blank out the whole parameter
string FontIdSizeColor = "Arial,12,White";
Must be 3 entries separated by commas:
#1 = Font ID (must be the name of an installed Windows font (case sensitive))
#2 = Font Size
#3 = Font Color (e.g. Red, LimeGreen, Goldenrod, MediumSeaGreen)
string OrdersFormat = "'Orders = '6";
string LotsFormat = "'Lots = '6.2";
string ProfitDollarsFormat = "'Profit $ = 'R,-6.2";
string ProfitPipsFormat = "'Profit pips = 'R,-6.1";
These are the format masks for the four P/L statistics that are displayed. For more info, see the NumberToStr section in this post.
To suppress an item from being displayed, blank out its format
bool LogErrorsToDebugFile = true;
- true = the EA will output any errors to the file …/(MT4)/experts/files/debug.txt (recommended), in addition to the experts log
- false = errors will be output to the experts log only
ERRORS & TROUBLESHOOTING
If you want me to attempt to troubleshoot a scenario for you, I will need the following info:
1. The content of the DEBUG.TXT file (you will need to have had LogErrorsToDebugFile set to true, at the time the EA attempted to close the open orders).
2. The name of your br0ker, MT4 build number, and Windows version.