My shell EA code

My shell EA code

Postby SteveHopwood » Wed Nov 23, 2011 11:56 pm

This thread does not offer a, "Create your own EA instantly" shell. You cannot use my shells if you are not already a proficient CrapQl4 coder. It offers the shells that I use to create trading robots quickly and easily. Studying them also means that you can read my EA code more easily; you can adapt my code more quickly to your own ends once you understand my coding style.

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/viewtopic.php?f=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]
 Please Login or Register to see this content
<


Watch that. I am always forgetting and wondering why the code-checking backtest in ST results in no trades.

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)

There is a single error reporting function - void ReportError(string function, string message) - that is called from within ModifyOrder() and CloseOrder(). So, instead of all those OrderClose(list of params) calls with all those error call functions when something goes wrong, we have a simple one line call.

For example, CloseBasketTrades() has this call:
Code: [Select all]
 Please Login or Register to see this content
<

__FUNCTION__ is a useful thingy that I found. It is two underscores on either side of the word. It holds the name of the function from which CloseOrder() is called. 'ocm' is the 'reason' parameter and stands for 'Order Close Message'. The CloseOrder() function sends 'reason' to ReportError() if the trade closure fails. There is a list of defined constants at the top of the code. You can add to them yourself, and you can pass 'reason' as a string. Suppose you wanted to close half the trade:
Code: [Select all]
 Please Login or Register to see this content
<

CloseOrder() also deletes pending trades, so this might need modifying, depending on what you are trying to achieve.

Example of ModifyOrder() for moving the stop loss:
Code: [Select all]
 Please Login or Register to see this content
<


Bear all this in mind when adding your own custom functions; it will make your life easier.

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.............

Well brunged up, my bots. Exceptionally obedient, but somewhat thick. :lol:

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.
    2) I have added code that will detect when a trade has closed (see the calls to CountOpenTrades() in init() and in start() ):
    • 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.

Shell user guide
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. :clap: 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.
You do not have the required permissions to view the files attached to this post.
Read the effing manual, ok?

Global Prime is the official SHF broker. Click here to sign up for a live account with Global Prime and join the several thousand SHF members who wisely choose GP as their broker of choice.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Tommaso (milanese) for all the incredible work he does here.
User avatar
SteveHopwood
 
Posts: 9768
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Oops

Postby SteveHopwood » Wed Nov 23, 2011 11:59 pm

Sorry about the disappeared posts from the original thread, I managed to delete the entire thread a few minutes ago.

If I had a firing brain cell, I would be unstoppably lethal.

:D
Read the effing manual, ok?

Global Prime is the official SHF broker. Click here to sign up for a live account with Global Prime and join the several thousand SHF members who wisely choose GP as their broker of choice.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Tommaso (milanese) for all the incredible work he does here.
User avatar
SteveHopwood
 
Posts: 9768
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: My shell EA code

Postby SteveHopwood » Thu Nov 24, 2011 11:12 am

Latest update in post 1, again courtesy of TIG (The Invaluable George).

George made the obvious suggestion of solving the mptm features only kicking in when HiddenPips > 0, is to remove the stipulation in CountOpenTrades() that HP must be >0 before drawing the tp/sl lines. Obvious to him, that is, not to me. :oops:

He also suggested a pretty cool way of working out when the stops need moving in the jumping/trailing stop functions; I have commented them so you know which ones belong to George.

In making these changes, I noticed that the stop line is moved to reflect the new stop, not the new stop +- HiddenPips/ I shall alter this later.

:D
Read the effing manual, ok?

Global Prime is the official SHF broker. Click here to sign up for a live account with Global Prime and join the several thousand SHF members who wisely choose GP as their broker of choice.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Tommaso (milanese) for all the incredible work he does here.
User avatar
SteveHopwood
 
Posts: 9768
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Restricted Currency Direction

Postby gaheitman » Thu Nov 24, 2011 2:23 pm

Steve,

I'd like to offer up the following as a possible addition to the shell code to allow for restricting trading by currency to a set direction. You already have this for single currency bots, but not for multi-currency ones.

In short, the code is modeled after your TradeDirectionBySwap() code and is intended to be called after TradeDirectionBySwap() in the IsTradingAllowed() function. It uses two extern variables to hold a list of currencies to only trade long or only trade short. The code compares the long side of the TradePair against the two lists and sets TradeLong and TradeShort accordingly. It then does the same with the short side, inverting the meaning since it is the short side of the pair.

Here are the proposed additions as they would appear in the code.

Additions to Extern variables:

Code: [Select all]
 Please Login or Register to see this content
<


Utility Procedure to raise strings to upper case:

Code: [Select all]
 Please Login or Register to see this content
<


Procedure to turn off disallowed directions

Code: [Select all]
 Please Login or Register to see this content
<


And finally, the call in IsTradingAllowed()

Code: [Select all]
 Please Login or Register to see this content
<


I think that does it. Perhaps an update to the User Feedback might be nice too. I've also attached the script I used to test the code.

Now, I'm off to find some Turkey! :D

George
You do not have the required permissions to view the files attached to this post.
User avatar
gaheitman
 
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: My shell EA code

Postby SteveHopwood » Sat Nov 26, 2011 4:22 pm

George, I am not ignoring your recent post and will act on this at some point soon.

Latest update in post 1 guys. In preparation for forexpippy EA, I have added risk-based lot sizing.

:D
Read the effing manual, ok?

Global Prime is the official SHF broker. Click here to sign up for a live account with Global Prime and join the several thousand SHF members who wisely choose GP as their broker of choice.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Tommaso (milanese) for all the incredible work he does here.
User avatar
SteveHopwood
 
Posts: 9768
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: My shell EA code

Postby SteveHopwood » Sat Nov 26, 2011 6:16 pm

One more update, in the spirit of tightening up the code.

I have changed 'trend' to 'OverallTrend' and added separate trend variables to all the other trend detection code - RsiTrend, AaTrend etc.

TrendDetectionModule() then sets up each of the individual trends. We then combine them in whatever combination the system requires, into OverallTrend; this is then used in the trading decision.

:D
Read the effing manual, ok?

Global Prime is the official SHF broker. Click here to sign up for a live account with Global Prime and join the several thousand SHF members who wisely choose GP as their broker of choice.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Tommaso (milanese) for all the incredible work he does here.
User avatar
SteveHopwood
 
Posts: 9768
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: My shell EA code

Postby magft » Sun Nov 27, 2011 10:51 pm

Steve

I have done an update to the time checking routine to allow the times to be specified as 00:00 format. The attached script should help show the method, though i haven't fully tested for our down under associates.

Mike
You do not have the required permissions to view the files attached to this post.
magft
 
Posts: 195
Joined: Tue Nov 15, 2011 9:59 pm
Location: East Midlands, UK

Re: My shell EA code

Postby youcrazykids » Mon Nov 28, 2011 6:29 pm

Hi all

Although nothing startling, I've found a way of displaying comments and screen messages, without them being obscured by bars on the chart.

To do this, in the init() function, I add two text labels to the chart, at coordinates 0,0. The Webdings character set contains a solid block, which you can scale up to 150pts, to create a "box". So, a couple of these stacked vertically, then coloured black, does the trick. The comment text sits atop these, and they both stay fixed relative to the window, not the scrolling chart.

Here's the code, and a screenshot. Feel free to adopt/adapt/ridicule it as you wish ;)

Code: [Select all]
 Please Login or Register to see this content
<


Example.JPG


Cheers

youcrazykids
You do not have the required permissions to view the files attached to this post.
User avatar
youcrazykids
 
Posts: 35
Joined: Sat Nov 19, 2011 8:38 am
Location: Northumberland, UK

Re: My shell EA code

Postby SteveHopwood » Mon Nov 28, 2011 6:42 pm

youcrazykids, no way am I going to ridicule this. Thanks. I shall look at it and perhaps adopt it.

Mike, sorry but I only saw your latest when looking at yck's post; I missed it when you posted it. Does the code work for our cousins in upside-down land?

Cheers guys

:D
Read the effing manual, ok?

Global Prime is the official SHF broker. Click here to sign up for a live account with Global Prime and join the several thousand SHF members who wisely choose GP as their broker of choice.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. [email protected] is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Tommaso (milanese) for all the incredible work he does here.
User avatar
SteveHopwood
 
Posts: 9768
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: My shell EA code

Postby magft » Tue Nov 29, 2011 11:32 am

SteveHopwood wrote:youcrazykids, no way am I going to ridicule this. Thanks. I shall look at it and perhaps adopt it.

Mike, sorry but I only saw your latest when looking at yck's post; I missed it when you posted it. Does the code work for our cousins in upside-down land?

Cheers guys

:D


George has posted a much cleaner version that seems to work on my Fratelli EA thread so i'm posting the script here for your thoughts.
You do not have the required permissions to view the files attached to this post.
magft
 
Posts: 195
Joined: Tue Nov 15, 2011 9:59 pm
Location: East Midlands, UK

Next

Return to Coders Hangout

Who is online

Users browsing this forum: No registered users and 1 guest

cron