My shell EA code

User avatar
LittleCaro
Trader
Posts: 63
Joined: Tue May 19, 2015 7:09 am

My shell EA code

Post by LittleCaro »

Hello all,

I have a little problem,

So i'm coding an expert with the amazing steve's shell ea code, and i'm using the read indicator values module, and the close order on opposite direction signal fonction.


Here is the problem, sometimes (maybe 3 times out of 10), the expert closes the order (so he reads correctly the indicators and get the good signall), BUT the opposite trade is not sent.

And i didn't see any tracks of "context is busy" in the logs.

I wonder if some doctors could help here ?


Thanks !
User avatar
LittleCaro
Trader
Posts: 63
Joined: Tue May 19, 2015 7:09 am

My shell EA code

Post by LittleCaro »

Hey guys,

I think i got it :

At the end of the code (in the lines 4400), i replace the line :

Code: Select all

TimeToStartTrading = 0;//Set to TimeCurrent() + (PostTradeAttemptWaitMinutes * 60) when there is an OrderSend() attempt)
With the line :

Code: Select all

TimeCurrent() + PostTradeAttemptWaitSeconds;

And since the expert didn't miss any trades.

Greatly helped with the comment in the code.

:!!: :!!: :!!: :!!: :!!:
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

My shell EA code

Post by SteveHopwood »

:clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap:

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

Hello Everyone,
I´ve been using the Barebones shell to code different strategies based on price action. One problem I have stumbled upon several times, and was unable to solve due to my limited coding skills, is related to the fact that many of my strategies consider the price action since the beginning of a given bar and how the bar develops in time.

The problem is that if the EA works on H1 and a trade closes at, let´s say, 15:33, I have no way to make the EA get back to work at 16:00, i.e. at the beginning of the next H1 bar (the same applies to any other timeframe). The "Sleep After Trade Closure" input only allows me to make the EA sleep for a fixed number of minutes, while I need it to sleep until the beginning of the next bar.

I think it would be a nice addition to the shell. I would be truly happy and grateful if anyone could propose a solution.

Thanks y´all, and stay safe.

Ariel
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

My shell EA code

Post by SteveHopwood »

K-Lander » Tue Jun 16, 2020 4:50 pm wrote:Hello Everyone,
I´ve been using the Barebones shell to code different strategies based on price action. One problem I have stumbled upon several times, and was unable to solve due to my limited coding skills, is related to the fact that many of my strategies consider the price action since the beginning of a given bar and how the bar develops in time.

The problem is that if the EA works on H1 and a trade closes at, let´s say, 15:33, I have no way to make the EA get back to work at 16:00, i.e. at the beginning of the next H1 bar (the same applies to any other timeframe). The "Sleep After Trade Closure" input only allows me to make the EA sleep for a fixed number of minutes, while I need it to sleep until the beginning of the next bar.

I think it would be a nice addition to the shell. I would be truly happy and grateful if anyone could propose a solution.

Thanks y´all, and stay safe.

Ariel
Great to see you getting your hands dirty. :clap: :clap: :clap: :clap: :clap:

I will tell you what to do because this is usually the hardest part to work out. You can work out the actual code because it is more satifsying for you, but post again if you get hopelessly stuck.

Declare a boolean in the general inputs section, that tells your bot whether to wait for the open of the next candle or not, so you can experiment if need be:

Code: Select all

bool ForceWaitUntilNextCandle = true;
Next create a function that examines the orders in your History tab to find out whether the most recently closed trade closed before the open of the current candle. One of the thingies I have learned since creating these shells is to add functions in such a way that they can be pasted into a multi-pair EA without needing adjustment. I would do something like this:

Code: Select all

bool didTradeCloseBeforeCurrentCandle(string symbol)
{

}//End didTradeCloseBeforeCurrentCandle(string symbol)
Inside the function goes:

Code: Select all

bool didTradeCloseBeforeCurrentCandle(string symbol)
{
     iterate through the orders in your history tab
     {
           check that the bot 'owns' the trade. The line that checks the symbol will be:
           if (OrderSymbol() != symbol) continue;
           examine the order close time and return false if it is >= the open time of the current candle
     }//end of iteration

     getting to here means there was no closed order found that closed during the current
      candle, so return true
}//End didTradeCloseBeforeCurrentCandle(string symbol)
Then you go to the bool IsTradingAllowed() function. This is the final step in LookForTradingOpportunities() that could cancel an order send. This is the code to add:

Code: Select all

if (ForceWaitUntilNextCandle && didTradeCloseBeforeCurrentCandle(Symbo() ) )
{
   return (true);
   else
   return (false);
}

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

Great Steve, will give it a try this week and post again for help if need be. I was thinking in a much more complicated way to do it, actually. Your approach makes a lot more sense.

Thanks a bunch,
Ariel
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,
I understand the logic; not so sure about the syntax and where to insert it within Bare Bones. I came up with this time-checking routine though. Would you mind take a look? Thanks,

bool DidTradeCloseBeforeCurrentCandle()
{
total = OrdersHistoryTotal();
for(i = total - 1; i >= 0; i--){
OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol() == Symbol())
{
if(OrderCloseTime()>=iTime(NULL,0,0)) return (false);
}}
return (true);
}



Great to see you getting your hands dirty. :clap: :clap: :clap: :clap: :clap:

I will tell you what to do because this is usually the hardest part to work out. You can work out the actual code because it is more satifsying for you, but post again if you get hopelessly stuck.

Declare a boolean in the general inputs section, that tells your bot whether to wait for the open of the next candle or not, so you can experiment if need be:

Code: Select all

bool ForceWaitUntilNextCandle = true;
Next create a function that examines the orders in your History tab to find out whether the most recently closed trade closed before the open of the current candle. One of the thingies I have learned since creating these shells is to add functions in such a way that they can be pasted into a multi-pair EA without needing adjustment. I would do something like this:

Code: Select all

bool didTradeCloseBeforeCurrentCandle(string symbol)
{

}//End didTradeCloseBeforeCurrentCandle(string symbol)
Inside the function goes:

Code: Select all

bool didTradeCloseBeforeCurrentCandle(string symbol)
{
     iterate through the orders in your history tab
     {
           check that the bot 'owns' the trade. The line that checks the symbol will be:
           if (OrderSymbol() != symbol) continue;
           examine the order close time and return false if it is >= the open time of the current candle
     }//end of iteration

     getting to here means there was no closed order found that closed during the current
      candle, so return true
}//End didTradeCloseBeforeCurrentCandle(string symbol)
Then you go to the bool IsTradingAllowed() function. This is the final step in LookForTradingOpportunities() that could cancel an order send. This is the code to add:

Code: Select all

if (ForceWaitUntilNextCandle && didTradeCloseBeforeCurrentCandle(Symbo() ) )
{
   return (true);
   else
   return (false);
}

:xm: :rocket:[/quote]
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

My shell EA code

Post by SteveHopwood »

K-Lander » Wed Jun 17, 2020 12:49 am wrote:Hi Steve,
I understand the logic; not so sure about the syntax and where to insert it within Bare Bones. I came up with this time-checking routine though. Would you mind take a look? Thanks,

bool DidTradeCloseBeforeCurrentCandle()
{
total = OrdersHistoryTotal();
for(i = total - 1; i >= 0; i--){
OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol() == Symbol())
{
if(OrderCloseTime()>=iTime(NULL,0,0)) return (false);
}}
return (true);
}
You are on the right track.

A tip. Next time you post code, encase it within the code button for readability - 5th button along. Compare this:
bool DidTradeCloseBeforeCurrentCandle()
{
total = OrdersHistoryTotal();
for(i = total - 1; i >= 0; i--){
OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol() == Symbol())
{
if(OrderCloseTime()>=iTime(NULL,0,0)) return (false);
}}
return (true);
}

with this:

Code: Select all

bool DidTradeCloseBeforeCurrentCandle()
{
   int total = OrdersHistoryTotal();
   for(int i = total - 1; i >= 0; i--){
      bool result = OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol() == Symbol())
      {
         if(OrderCloseTime()>=iTime(NULL,0,0)) return (false);
      }
   }
return (true);
}
and you will see what I mean.

Errors would show up if you tried to compile your version i.e.
  • total was not declared. I made it an int.
  • ditto i.
  • a warning that the return value of OrderSelect should be checked, so I added bool result =
You need to use Thomas' BetterOrderSelect() function instead of the Mql4 OrderSelect(). Read the comment just above the function for explanation. As coded, your function will not give accurate results. Your OrderSelect() call needs to be:

Code: Select all

bool result = BetterOrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
Where the function goes in your code does not matter so long as it does not go inside another function.

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

Well, here it is. Attached I am posting Bare Bones with the new feature. I´ll be running it to make sure it indeed works as expected. In the meantime, if you could check the code, it would be great.

The extern user input is in line 229, right after TradingHours;
The routine that checks whether there is a closed trade in the current candle is in lines 2187 to 2200.
The conditional within IsTradingAllowed is in lines 1175 to 1182.

It compiled ok. Strange, no mismatching parentheses this time. Must be doing something right for a change :smile:
You do not have the required permissions to view the files attached to this post.
User avatar
SteveHopwood
Owner
Posts: 9754
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

My shell EA code

Post by SteveHopwood »

K-Lander » Wed Jun 17, 2020 4:12 pm wrote:Well, here it is. Attached I am posting Bare Bones with the new feature. I´ll be running it to make sure it indeed works as expected. In the meantime, if you could check the code, it would be great.

The extern user input is in line 229, right after TradingHours;
The routine that checks whether there is a closed trade in the current candle is in lines 2187 to 2200.
The conditional within IsTradingAllowed is in lines 1175 to 1182.

It compiled ok. Strange, no mismatching parentheses this time. Must be doing something right for a change :smile:
Looks good to me. :clap: :clap: :clap: :clap: :clap: :clap: :clap:

By the by, what I wrote about OrderSelect() was slightly incorrect and only applies to orders selected by ticket, not pos. Even so, stick with BetterOrderSelect() because: it makes sure you never fall foul of the OrderSelect() function; when one of the genius coders here replaces a Crapql4 function with his/her own code, then it is best to use it.

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:
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.
Post Reply

Return to “Coders Hangout”