stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

My shell EA code
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=79
Page 8 of 23
Author:  SteveHopwood [ Sun Jul 08, 2012 10:42 pm ]
Post subject:  Re: My shell EA code

Latest bare bones version in post 1.

I have added the code that checks that there are enough bars on the chart for Slope to work properly. Also added Paul's Spread stuff.

:D
Author:  SteveHopwood [ Mon Jul 30, 2012 11:28 am ]
Post subject:  Re: My shell EA code

Latest version of Bare Bones in post 1:
  • - tidied up the feedback display code using a suggestion made by Lifesys
    - fixed a bloop in the Slope trend detection code
    - added the balance and Zeljko filters
:D
Author:  SteveHopwood [ Sat Aug 04, 2012 9:06 am ]
Post subject:  Re: My shell EA code

Latest bare bones version in post 1:
  • - fixed the errant (Point*BreakEvenPips) code that I left behind in the conversion to Lifesys' pip factor. This caused BreakEvenPips to be wrong by a magnitude of 10 when the quote is 5 digits.
    - added the swap and balance filters. There is a new input, OnlyTradePositiveSwap, that allows you to filter out trades that attract positive swap.
    - enhanced error reporting. I have expanded ReportError to accept two sting parameters: the function name where the error occurred; a constant that describes the error. This is to help us find those infuriating errors generated by some errant code trying to modify a stop when the stop does not need modifying.
    - added half-close to ally with BreakEvenStopLoss()
:D
Author:  SteveHopwood [ Sat Aug 11, 2012 4:54 pm ]
Post subject:  Re: My shell EA code

Latest update to Bare Bones in post 1. Details in 'Rogue' trades and the safety features.

:D
Author:  user666 [ Thu Aug 16, 2012 10:33 am ]
Post subject:  Re: My shell EA code

SteveHopwood wrote:Note that I only ever use the bare bones version and all the developments I have added apply to that version only.

Attached is the EA shell I use to create the trading robots I code.

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 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 their 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
As the shell code develops and my understanding of this weird thingy called 'programming' grows, I am refining the code so that LUC has fewer opportunities to throw a party. I have separated trend detection and indicator reading altogether, and refined the use of both in the trading opportunity detection code:
  • - void TrendDetectionModule() contains all the code needed to detect the current trend. Here you add your own routines to achieve trend detection. You can set a trend based on the indi used, such as RsiTrend.
    - void ReadIndicatorValues() contains calls to indicator reading functions, where the indi is not a part of overall trend detection.
    - void LookForTradingOpportunities() uses the information gained from these two functions.
    • - 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 'true', then turned to 'false' if any of the trade conditionals fail.

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 DELET THIS OR THE EA WILL NEVER TRADE
SendLong = false; SendShort = false;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Watch that. I am always forgetting and wondering why the code-checking backtest in ST results in no trades. :lol:

Bare bones version
This has all the additional stuff such as Hanover etc removed; use this when you know you will not need it.

'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 jusdt 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 has 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; find the inputs straight under the general inputs:
  • 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 FF. 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/

Editing your code
Don't get stuck with Metaeditor. Go to http://www.stevehopwoodforex.com/phpBB3 ... f=15&t=627 and see what w633 has produced - you will soon realise what rubbish the editor that ships with Empty4 is.

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

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

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 hanover. He will charge a lot more (I seem to recall him quoting a development fee $70 per hour somewhere) but will send you code that is an accurate and bug-free representation of your brief.

All links ARE BROKEN here
Author:  SteveHopwood [ Thu Aug 16, 2012 10:43 am ]
Post subject:  Re: My shell EA code

user666 wrote: All links ARE BROKEN here
Thanks, but they work when I click them.

:D
Author:  gaheitman [ Wed Aug 29, 2012 2:15 pm ]
Post subject:  Re: My shell EA code

Steve,

I've started to familiarize myself with the current code base. You know, at some point we aren't going to accept that you aren't a coder.... :>

I found what I think are a couple of issues with the HalfClose functionality.

From CountOpenTrades():

Code: Select all

      if (OrderType() == OP_BUY) 
      {
         BuyOpen = true;
         if (HalfCloseEnabled && OrderLots() == Lot && OrderStopLoss() >= OrderOpenPrice() ) success = HalfCloseTrade();
      }//if (OrderType() == OP_BUY) 
      
      if (OrderType() == OP_SELL) 
      {
         SellOpen = true;
         if (HalfCloseEnabled && OrderLots() == Lot && OrderStopLoss() <= OrderOpenPrice() ) success = HalfCloseTrade();
      }//if (OrderType() == OP_SELL) 
First of all, we probably shouldn't try to close the trade if OrderLots() == MinLot. The test could be in the function to close the trade. It doesn't break anything, but it will continue to try to close a trade it can't.

More importantly, the code looks to be closing half the trade at break even. I think the "or equal to" portion of "OrderStopLoss() >= OrderOpenPrice()" and "OrderStopLoss() <= OrderOpenPrice()" should be removed. Even though some days getting out at break even feels like a win, I don't think it is what you intended here. ;)

I'm also a little confused by this line in BreakEvenStopLoss:

Code: Select all

      if (HalfCloseEnabled && OrderLots() < Lot) bool success = HalfCloseTrade();
I think it is trying to close half the trade only when it has already closed half the trade since OrderLots() is now less than Lot.

Of course, I haven't spent any time looking at code for months, so I could be way off! :D

George
Author:  NeoTrader [ Wed Aug 29, 2012 2:40 pm ]
Post subject:  Re: My shell EA code

Hi Steve,

Since I want to do some more experimenting with EAs in the future I started also to look into your Barebone Shell EA.

I'm also writing a full documentation that can be used for End users and Developers that I will share with our community once it is finished and revised by you.
By the way do you have MS Word (and if yes which version) otherwise which format do you prefer.

When I was evaluating your code the following point come to my attention that I want to be clarified by you if it is a bloop or a features.. ;).

external Parameters TradeLong and TradeShort
Both Parameters can be set by the user to define in which direction they want to trade.
Nevertheless you change both settings in TradeDirectionBySwap() without a check of the Parameters and therefor override the user Settings(wishes) if one of the external swap Parameters is enabled (set to true).

thanks in advance,

happy trading,

NeoTrader

-
Author:  SteveHopwood [ Wed Aug 29, 2012 2:45 pm ]
Post subject:  Re: My shell EA code

gaheitman wrote:Steve,

I've started to familiarize myself with the current code base. You know, at some point we aren't going to accept that you aren't a coder.... :>
Well, my coding is being dragged kicking and screaming into the 1990's by the likes of you, Gary, Lifesys et al
I found what I think are a couple of issues with the HalfClose functionality.

From CountOpenTrades():

Code: Select all

      if (OrderType() == OP_BUY) 
      {
         BuyOpen = true;
         if (HalfCloseEnabled && OrderLots() == Lot && OrderStopLoss() >= OrderOpenPrice() ) success = HalfCloseTrade();
      }//if (OrderType() == OP_BUY) 
      
      if (OrderType() == OP_SELL) 
      {
         SellOpen = true;
         if (HalfCloseEnabled && OrderLots() == Lot && OrderStopLoss() <= OrderOpenPrice() ) success = HalfCloseTrade();
      }//if (OrderType() == OP_SELL) 
First of all, we probably shouldn't try to close the trade if OrderLots() == MinLot. The test could be in the function to close the trade. It doesn't break anything, but it will continue to try to close a trade it can't.
Frankly, George, if people are too thick to work out that their lot size needs to be divisible by 2 when they want half-close at some stage, then there is nothing I am prepared to do to help them.
More importantly, the code looks to be closing half the trade at break even. I think the "or equal to" portion of "OrderStopLoss() >= OrderOpenPrice()" and "OrderStopLoss() <= OrderOpenPrice()" should be removed. Even though some days getting out at break even feels like a win, I don't think it is what you intended here. ;)
'tis exactly what it is intended for. 95% of the ea's I code privately require this.
I'm also a little confused by this line in BreakEvenStopLoss:

Code: Select all

      if (HalfCloseEnabled && OrderLots() < Lot) bool success = HalfCloseTrade();
I think it is trying to close half the trade only when it has already closed half the trade since OrderLots() is now less than Lot.
That is one of those bloops that crept through when I first added the code. I subsequently fixed it, along with a fair amount of other stuff as I used the shell more and more. Also added a few more thingies without ever uploading the latest version - interest in my shell code hardly reaches fever pitch.

The purpose of this little snippet is to pick up those occasions when the stop was moved to breakeven successfully but the half-close failed for any of the gaziliion reasons deliberately programmed into Empty4 by the cowboys that wrote it. If half-close is required, the order stop is at breakeven or better, and half the trade has not closed, then have another go.
Of course, I haven't spent any time looking at code for months, so I could be way off! :D

George
Cheers George. You are a star. I have uploaded the very latest version of the shell to post 1.

:D
Author:  SteveHopwood [ Wed Aug 29, 2012 2:53 pm ]
Post subject:  Re: My shell EA code

NeoTrader wrote:Hi Steve,

Since I want to do some more experimenting with EAs in the future I started also to look into your Barebone Shell EA.
I have just uploaded the very latest, so you might want to get that.
I'm also writing a full documentation that can be used for End users and Developers that I will share with our community once it is finished and revised by you.
By the way do you have MS Word (and if yes which version) otherwise which format do you prefer.
Fantastic. This is the sort of thing that I never have the patience to do. You should see the steam coming from my ears when I have to compose a thread page 1.

I have Word 2003, but actually use OpenOffice these days. Why pay, when it is free?
When I was evaluating your code the following point come to my attention that I want to be clarified by you if it is a bloop or a features.. ;).

external Parameters TradeLong and TradeShort
Both Parameters can be set by the user to define in which direction they want to trade.
Nevertheless you change both settings in TradeDirectionBySwap() without a check of the Parameters and therefor override the user Settings(wishes) if one of the external swap Parameters is enabled (set to true).
This is deliberate. If traders do not want to sell the high-swap pairs, then this is more important than the tradeShort input, that they may easily forget about. I know I do. :lol:
thanks in advance,

happy trading,

NeoTrader

-
My pleasure. You have rapidly become one of the stars here and your contributions are invaluable. Thanks for everything you do.

:D
All times are UTC Page 8 of 23