Note that I only ever use: Bare Bones; Bob Stuff; Bugger All Included versions with and without all the gridding/hedging/offsetting paraphernalia. All the developments I have added apply to these only. Bare Bones is not much of a bare bones any more, but does contain the stuff that I automatically add to all the EA's I code so the user has the choice of using them or not. There are a lot of useful functions in 'Shell auto trading robot code by Steve Hopwood.mq4' that I dip into and copy as and when needed, but never use it to create an auto-trader.
Attached are the latest EA shells I use to create the trading robots I code. You will find them in the "Shell codes updated 9th Dec 2017.zip". Previous versions of the shells are in the "Shell codes.zip" file.
Instructions for use are included early on in the code. You strip out what you do not want and add any extras that you do want.
I have posted a tutorial describing how to add indicator calls to my shells - http://www.stevehopwoodforex.com/phpBB3 ... =50&t=4519 Have a read if you are unsure.
I am not claiming to be some sort of coding genius. Far from it; there are professional coders here whose work makes me look silly. I am a pianist who has learned to code in pursuit of some wealth to make up for the wealth I do not derive from the job I love so much. En route, I have picked up a few coding skills that I offer to anyone who can make use of them.
Advice I offer to beginner coders is:
- comment everything. A chunk of code whose purpose seems so screamingly obvious that it cannot possibly justify the time commenting, will take you an hour to decipher in two months time. Guess how I know.
- learn the basics then start a project. Start with something simple, such as the trigger being the cross of a slower ma by a quicker one. Perhaps make this the setup, and the trigger is x candles heading in the right direction. Involve some management features and perhaps some sutomatic lot-sizing for money management. My code is open source so anyone can pinch it freely, but better for you if you work out your own salvation because you will understand better what is going on.
- Develop your own methods of presenting on-screen information that tell you what is going on inside the code as well as providing valuable feedback.
- As you master each individual step, try to make it into a universal function that you can add to your own shell ea, from which you can code a new bot in minutes - no point in re-inventing the wheel every time you have an idea and want an ea to try it out, This is how my shell ea evolved and I am still adding to it.
- Once you have the basics, delve into drawing trend lines etc on the charts via your code. To get started, type ObjectCreate into the mql4 editor, highlight it and press F1 for help; this will present the list of commands used to create and manipulate shapes on your charts, confusingly known as 'objects'.
- Remember Google. When stuck, google the problem. If it is solvable, then someone out there has both solved it and posted the solution.
Feel free to use the code here in any way that suits you, so long as you distribute your code free of charge to the users. Charge for using it, and I will sue the socks off you. The code contains substantial contributions from a number of other coders; they will not be happy to discover you selling this stuff commercially.
Coding the trading decision
[*]void ReadIndicatorValues() contains calls to indicator reading functions, then sets BuySignal/SellSignal to 'true' when trading conditions are met.
[*]void LookForTradingOpportunities() sends the trade if there is a trade signal. Here is where I put extra conditions that are not easily accommodated by ReadIndicatorValues().
- There is a call to bool IsTradingAllowed() to check those filters such as max spread, swap etc that are not a part of the trade direction finding code.
- Inside the buy/sell conditional blocks, the individual trend variables such as RsiTrend are consulted. It is easy to alter this code to reflect whatever way you want to use the values returned by the indi reading functions.
I have gradually refined void LookForTradingOpportunities() so it is really easy to code the trade trigger. SendLong and SendShort are initially set to 'false', then turned to 'true' if all the trade conditionals succeed.
To avoid the new EA sending a buy immediately you load it onto a chart and before adding the individual system's conditionals, I have added this just after cycling through the conditionals:
Code: Select all
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//REMEMBER TO DELETE THIS OR THE EA WILL NEVER TRADE
SendLong = false; SendShort = false;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Closing/modifying/deleting trades
There are two multi-purpose functions to deal with the usual OrderModify(), OrderDelete() and OrderClose() functions:
- bool ModifyOrder(int ticket, double price, double stop, double take, datetime expiration, color col, string function, string reason)
- bool CloseOrder(int ticket, string function, double CloseLots, string reason)
For example, CloseBasketTrades() has this call:
Code: Select all
bool result = CloseOrder(OrderTicket(), __FUNCTION__, OrderLots(), ocm );
Code: Select all
bool result = CloseOrder(OrderTicket(), __FUNCTION__, OrderLots() / 2, "Closing half the trade" );
Example of ModifyOrder() for moving the stop loss:
Code: Select all
result = ModifyOrder(OrderTicket(), OrderOpenPrice(), stop, OrderTakeProfit(), OrderExpiration(), clrNONE, __FUNCTION__, slm);
Matt's Order Reliable code
It seems that the latest garbage from those cretins at Crapperquotes occasionally breaks Matts's code. I have added an extra layer of security.
PostTradeAttemptWaitMinutes and its partner TimeToStartTrading are in the hidden variables of the General Inputs section. The shells will set
TimeToStartTrading = TimeCurrent() + (PostTradeAttemptWaitMinutes * 60);
when there is a OrderSend() attempt, regardless of whether OR has picked up a receipt return from the broker's server. The snippet of code that calls LookForTradingOpportunities() includes a check that TimeCurrent() is >= TimeToStartTrading.
Bare bones version
This has all the additional stuff removed; use this when you know you will not need it.
Bare bones with Recovery
This has Nanningbob's Recovery added.
Bugger all included
This has nothing not actually needed for trading apart from: stealth; trading hours; average spread calculation.
Bob stuff version
Includes: SuperSlope, HGI and the H4 240 MA trend detection.
Retro-fitting Slope
This contains the code and information you need to add Slope to an existing EA.
'Rogue' trades and the safety features
Sometimes a concatenation of unexpected combination of inputs plus a bit of coding logic failure creates a sequence of trades that open and close immediately - often only a tick apart. What is happening is this:
- the EA is detecting trade opening conditions.
- at the same time, there are trade exit conditions in place, so the bot:
- obediently opens a new trade because the conditions are in place for opening.
- at the next tick, obediently closes the trade because the conditions are in place for closing.
- at the next tick, obediently opens a new trade because the conditions are in place for opening.............
For convenience, I have elected to call these trades 'rogue' trades. Whenever I refer to rogue trades, I am discussing those I just described.
We need to hunt down and correct the code/logic that is causing the behaviour, but this might not manifest itself immediately. A number of us had been using Flying Slope for a few weeks before one of our members hit on just the combination of input choice and my faulty algorithm to produce a sequence of rogues, through no fault of his own. We need to prevent this from draining an unsupervised account.
I have introduced some safety features:
- 1) I have introduced a minimum time between trades:
- MinMinutesBetweenTrades (minimum time allowed to elapse between the closing of one trade and the opening of the next).
- this will need adjusting to suit each ea you are coding.
- IsTradingAllowed() calls TooClose(), the function that aborts the trade if insufficient time has passed. Also sets SafetyViolation to 'true' so the EA knows to put a message at the head of the feedback display.
- when a trade closure is detected, the ea calls IsClosedTradeRogue(), which examines the time elapsed in between opening and closing the trade. You can see the actions I have coded if this is < MinMinutesBetweenTradeOpenClose.
- MinMinutesBetweenTrades (minimum time allowed to elapse between the closing of one trade and the opening of the next).
This is a user guide template that you can use to quickly create a user guide if you want to release your EA here at SHF. It contains a brief description of most of the EA functions, so you remove those you do not want and add in anything new. Inside the zip is the Open Office document that you can edit to produce your own pdf.
OpenOffice is freeware and every bit as good as any of its paid-for rivals. Download it from http://www.openoffice.org/
Enhanced chart feedback
lifesys (Paul) sent me the code to display text in user-adjustable labels instead of the near-useless CrapTx Comment(). Fantastic, Paul; thanks from all of us. I have included it with the Bare Bones, Bugger All Included and Bob Stuff shells along with the grid versions. Thomas shared code with us that enhances the display still further.
Another approach
Mahmut is taking a slightly different approach to all this and has posted what he is getting up to at http://www.stevehopwoodforex.com/phpBB3 ... 7762#p7762 Try his approach and see if it suits you better; Mahmut is an exceptionally clever programmer..
And another approach
fxdytrader has put this at http://www.stevehopwoodforex.com/phpBB3 ... 966#p32966
Private EA coding
I receive may requests to code EA's. I will do so free of charge so long as I am able to share the EA and trading system here. Most of what I have learned to do as a coder, I have learned from contributors here. Sharing my work with them is my way of giving back in return for what I have gained. Having said that, I need to have some interest in the project to take it on; a period of successful live trading with the system guarantees my interest. If you are unable to persuade me to code your EA for free and for sharing here, then you will have to pay me for the work at the rate I quote in the next paragraph.
I will also code EA's for traders who want their EA kept private. For this, I charge a fee of $100 payable into my Paypal account. I charge so little because I am not a professional programmer. EA's I code for you will have bugs that we will have to hunt down and eradicate. I may easily misunderstand the brief that you send me. I shall need your patience as I work towards the difference between what I think you have asked for, and what you have actually asked for.
Should you need a more professional programmer, then contact renexxxx. He will charge a lot more but will send you code that is an accurate and bug-free representation of your brief.