My shell EA code

Post Reply
K-Lander
Trader
Posts: 35
Joined: Thu Nov 17, 2011 9:03 pm
Location: Buenos Aires, Argentina

My shell EA code

Post by K-Lander »

Hi Steve,
Attached I am sending Bare Bones with a working ForceSleepUntilNextBar feature.

A friendly reminder to anyone using the shells: Remember to DELETE the failsafe on the code (you´ll find it within the LookForTradingOpportunities function). Nothing will ever trigger a trade unless you do it (I forgot about it and took me 2 hours to figure out why the bugger doesn´t fire).

Thanks for the help Steve, and for the coding lesson.

All the best, and stay safe in these strange times.

Ariel

The subconscious is an amazing thingy and mine was at work as I typed the above. It just told me that I had given you duff info about the call to the function as well. Think about this call:

Code: Select all

if (ForceSleepUntilNextBar && DidTradeCloseBeforeCurrentCandle()) return (true);
   else
   return (false);  
This will also return 'false' if you have ForceSleepUntilNextBar set to 'false' to disable this filter, so the bot will never Trade. My fault. Here is the corrected code:

Code: Select all

   if (ForceSleepUntilNextBar)
   {
      if (DidTradeCloseBeforeCurrentCandle() ) return (true);
      else
      return (false);
   }//if (ForceSleepUntilNextBar)
:xm: :rocket:
You do not have the required permissions to view the files attached to this post.
User avatar
simplex
Trader
Posts: 127
Joined: Thu Feb 07, 2013 5:21 pm
Location: An insignificant small town close to an insignificant former capital at the Rhine River.

Re: My shell EA code

Post by simplex »

Hi all,
I'm just working my way through this formidable piece of software. Stumbled over the object handling functionality:

Code: Select all

if (ObjectFind(LineName) == -1) {
	ObjectCreate(LineName, OBJ_HLINE, 0, TimeCurrent(), 0);
	...
}
https://docs.mql4.com/objects/objectfind states:
If the object is not found, the function returns a negative number.
So that return value is not -1 necessarily.
Maybe it's worth considering to replace

Code: Select all

ObjectFind(LineName) == -1
by

Code: Select all

ObjectFind(LineName) < 0
just to be sure ... as sure as one can be using mql ...
There are several similar ObjectFind calls in the source code.
If you can't explain it simply, you don't understand it well enough. (Albert Einstein)
It appears that the Weighted Moving Average was invented by a trader who did not have a firm grasp of filter theory in hopes of reducing lag. (John F. Ehlers)
User avatar
SteveHopwood
Owner
Posts: 9798
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

Post by SteveHopwood »

simplex wrote: Sat May 13, 2023 4:49 pm Hi all,
I'm just working my way through this formidable piece of software. Stumbled over the object handling functionality:

Code: Select all

if (ObjectFind(LineName) == -1) {
	ObjectCreate(LineName, OBJ_HLINE, 0, TimeCurrent(), 0);
	...
}
https://docs.mql4.com/objects/objectfind states:
If the object is not found, the function returns a negative number.
So that return value is not -1 necessarily.
Maybe it's worth considering to replace

Code: Select all

ObjectFind(LineName) == -1
by

Code: Select all

ObjectFind(LineName) < 0
just to be sure ... as sure as one can be using mql ...
There are several similar ObjectFind calls in the source code.
Thanks for this. Brilliant point. :clap: :clap: :clap: :clap: :clap:

I am not going to do anything with this immediately as I can make the changes as I use the code. Plus, anyone such as yourself, can read your post and make the changes themselves. Anyone who cannot, cannot use the shells.

Thanks again. Hugely appreciated.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

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 Thomas (tomele) for all the incredible work he does here.
User avatar
simplex
Trader
Posts: 127
Joined: Thu Feb 07, 2013 5:21 pm
Location: An insignificant small town close to an insignificant former capital at the Rhine River.

Re: My shell EA code

Post by simplex »

SteveHopwood wrote: Mon May 15, 2023 7:34 pm Plus, anyone such as yourself, can read your post and make the changes themselves. Anyone who cannot, cannot use the shells.
Agree & thanks! Now for my next find, which dives a little bit deeper in the trading logic. See function LookForTradingOpportunities():

Code: Select all

	if (BuySignal) {
		SendLong = true;
	}
	if (SendLong) {       // User choice of trade direction
		if (!TradeLong) {
			return;
		}
This is how buy trades are started. For sell trades, it looks like this:

Code: Select all

	if (!SendLong) {
		if (SellSignal) {
			SendShort = true;
		}
		if (SendShort) {
			if (!TradeShort) {      // User choice of trade direction
				return;
			}
So sell signals are only being processed if there's no buy signal present. In my opinion, long and short trades are generally equitable. By this logic, long trades are prioritized over short trades.

This may work in a perfect world. But let's assume we as coders implement some sort of faulty logic when it comes to analyse the market for entry opportunities. Allright: this is highly unlikely, but let's just assume the impossible is happening: our trading logic issues a buy and a sell signal at the same time for a given pair.

In above logic, the long trade will be placed, while the short trade is silently ignored. Do I want that? Don't know about you, but I do not. What I find alarming most of all is the fact that ignoring the short trades obviously happens silently. Maybe there are security checks later on dealing with such a situation, which I possibly am not aware of yet. Anyway, I believe it's better to deal with this case before actually placing trades.

So this is my proposal to (hopefully) improve that bit of the shell code:

Code: Select all

	// Security check: is our trading system going haywire? (simplex mod)
	if (BuySignal && SellSignal) {
		/* 
			do whatever seems appropriate to ring the alarm bells ...
			... suspend trading?
			... close / delete all trades?
			... display visible and / or audible alarm?
			... send an email to Steve for immediate help? ;-)
			... 
		 */
		return;
	}
	
	if (BuySignal) {
		SendLong = true;
	}
	if (SendLong) {
...
	if (SellSignal) {
		SendShort = true;
	}
	if (SendShort) {
...
Now long and short trades are treated in the exact same manner, and on the same indentation level in our code. Furthermore we have the opportunity to implement some code to ring the alarm bells if the unthinkable is actually happening.

I'm just working on this mod, it's not yet ready, but I'd like to discuss my proposal here, before possibly running (coding) into a completely wrong direction. Maybe this idea is worth to find its way into the shell itself, not only in my upcoming EA's. In the end, it makes the shell a bit more foolproof (which suits me perfectly!), because it deals with a faulty signal logic in a more appropriate manner as compared to just ignoring it.
If you can't explain it simply, you don't understand it well enough. (Albert Einstein)
It appears that the Weighted Moving Average was invented by a trader who did not have a firm grasp of filter theory in hopes of reducing lag. (John F. Ehlers)
User avatar
simplex
Trader
Posts: 127
Joined: Thu Feb 07, 2013 5:21 pm
Location: An insignificant small town close to an insignificant former capital at the Rhine River.

Re: My shell EA code

Post by simplex »

Still using the incredible Barebones 17-6-20.mq4 once in a while as a reference for certain tasks in MT5. So just today I stumbled over the following code, which I believe contains a bug. Please refer to function

Code: Select all

void TrailingStopLoss(int ticket)
line 3206:

Code: Select all

       if (OrderType()==OP_SELL)
       {
          if (sl > OrderOpenPrice() ) return;//Not at breakeven yet
          ...
          if (CloseEnough(sl, 0) ) sl = MathMin(OrderStopLoss(), OrderOpenPrice());
          if (CloseEnough(sl, 0) ) sl = OrderOpenPrice();
          if (Bid <= sl  - (TrailingStopPips / factor))//George
          {
             ...
          }
Since we're working on SELL orders here, shouldn't this be Ask instead of Bid?

Cheers, sx
If you can't explain it simply, you don't understand it well enough. (Albert Einstein)
It appears that the Weighted Moving Average was invented by a trader who did not have a firm grasp of filter theory in hopes of reducing lag. (John F. Ehlers)
User avatar
SteveHopwood
Owner
Posts: 9798
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

Post by SteveHopwood »

simplex wrote: Sun Nov 10, 2024 6:06 pm Still using the incredible Barebones 17-6-20.mq4 once in a while as a reference for certain tasks in MT5. So just today I stumbled over the following code, which I believe contains a bug. Please refer to function

Code: Select all

void TrailingStopLoss(int ticket)
line 3206:

Code: Select all

       if (OrderType()==OP_SELL)
       {
          if (sl > OrderOpenPrice() ) return;//Not at breakeven yet
          ...
          if (CloseEnough(sl, 0) ) sl = MathMin(OrderStopLoss(), OrderOpenPrice());
          if (CloseEnough(sl, 0) ) sl = OrderOpenPrice();
          if (Bid <= sl  - (TrailingStopPips / factor))//George
          {
             ...
          }
Since we're working on SELL orders here, shouldn't this be Ask instead of Bid?

Cheers, sx
I have been coding this stuff for about 16 years now and still am not sure about the answer to your question - one I have asked myself umpteen times over the years.

Notice the comment following this line of code:
if (Bid <= sl - (TrailingStopPips / factor))//George

George was SHF member gaheitman. At the time, he knew more about coding than most of us here. He was most helpful and I adopted anything he suggested. Sadly, he has not visited here for more than 5 years, so I guess that age took its usual toll. It is coming to us all but I still think about him every time I use one of the shells.

These days, I code closing buys at the Bid and sells at the Ask with one difference. I use the minimal spreads of Afterprime to minimise any damage that my possible misconceptions might cause. Sorry not to be more definitive in my answer.

Of course, none of this really matters unless: an EA responds to lines drawn on a chart to crossing said tpsl lines to close a trade - those are a real headache; your broker is a criminal and the spreads are monumental - bugger all I can do about that.

I hope this helps whilst not being sure it will.

:xm: :rocket:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

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 Thomas (tomele) for all the incredible work he does here.
markft
Trader
Posts: 82
Joined: Thu Dec 08, 2011 12:06 pm
Location: Sydney, Australia

Re: My shell EA code

Post by markft »

If I understand correctly from the code section, this is where we are managing the trailing stop for a Sell order. Yes I think you are right and that should be the Ask, as we would be executing a market order to buy, to close the sell, at the Ask price. That being said, as Stave mentioned, this is usually not significant almost all of the time, unless somehow the spreads have widened a lot.
Post Reply

Return to “Coders Hangout”