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

My shell EA code
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=79
Page 3 of 23
Author:  gaheitman [ Thu Dec 08, 2011 4:25 pm ]
Post subject:  Re: My shell EA code

magft wrote:BTW the time checking script by George had to be modified to work correctly. I think it now works without issues.
I needed to have a third trading session, one for each global banking session, so I updated the code to speak in terms of Asian, European and US sessions. Not sure if anyone else needs this, but I thought I'd post it should the need arise.

Externs:

Code: Select all

extern string  tt="----Trading hours----";
extern string  Trade_Hours= "Set Session Trading Hours";
extern string  Trade_Hoursi= "Use 24 hour, Server time";

extern string  Trade_Hours_E= "Europe Session 7:00-12:00";
extern string  Start_TimeE = "7:00";
extern string  End_TimeE = "12:00";

extern string  Trade_Hours_U= "US Session 13:00-16:00";
extern string  Start_TimeU = "13:00";
extern string  End_TimeU = "17:00";

extern string  Trade_Hours_A= "Asia Session 23:00-4:00";
extern string  Start_TimeA = "0:00";
extern string  End_TimeA = "4:00";
Procedure that checks the trading times:

Code: Select all

bool CheckTradingTimes()
{
   datetime AsiaBeg, AsiaEnd, EuropeBeg, EuropeEnd, USBeg, USEnd;

   //datetime Now = TimeLocal();    //use local time
   datetime Now = TimeCurrent();  //use broker time

//we need to cast our conversions into the current date
   AsiaBeg = StrToTime(TimeToStr(Now,TIME_DATE)+" "+ Start_TimeA);
   AsiaEnd = StrToTime(TimeToStr(Now,TIME_DATE)+" "+ End_TimeA);
   EuropeBeg = StrToTime(TimeToStr(Now,TIME_DATE)+" "+ Start_TimeE);
   EuropeEnd = StrToTime(TimeToStr(Now,TIME_DATE)+" "+ End_TimeE);
   USBeg = StrToTime(TimeToStr(Now,TIME_DATE)+" "+ Start_TimeU);
   USEnd = StrToTime(TimeToStr(Now,TIME_DATE)+" "+ End_TimeU);


   bool Asia = false;

   //Check normal case for morning , 7:00-13:00
   if (AsiaBeg < AsiaEnd && (Now >= AsiaBeg && Now < AsiaEnd))
      Asia = true;

   //Check to see if AsiaBeg to AsiaEnd spans 00:00, e.g. 22:00 - 04:00
   if (AsiaEnd < AsiaBeg && (Now >= AsiaBeg || Now < AsiaEnd))
      Asia = true;


   bool Europe = false;
   //Check normal case for Europe , 17:00 - 22:00
   if (EuropeBeg < EuropeEnd && (Now >= EuropeBeg && Now < EuropeEnd))
      Europe = true;

   //Check to see if EuropeBeg-EuropeEnd spans 00:00, 22:00 - 04:00
   if (EuropeEnd < EuropeBeg && (Now >= EuropeBeg || Now < EuropeEnd))
      Europe = true;
   
   bool US = false;
   //Check normal case for US , 17:00 - 22:00
   if (USBeg < USEnd && (Now >= USBeg && Now < USEnd))
      US = true;

   //Check to see if USBeg-USEnd spans 00:00, 22:00 - 04:00
   if (USEnd < USBeg && (Now >= USBeg || Now < USEnd))
      US = true;
   

   return(Asia || Europe || US);
   
}//bool CheckTradingTimes()
And an update to UserFeedBack:

Code: Select all

   ScreenMessage = StringConcatenate(ScreenMessage,Gap, "Trading hours", NL);
   if (Start_TimeA == "00:00" && End_TimeA == "24:00") ScreenMessage = StringConcatenate(ScreenMessage,Gap, "            24H trading", NL);
   else
   {
      ScreenMessage = StringConcatenate(ScreenMessage,Gap, "            Asian Session:         ", Start_TimeA," - ", End_TimeA, NL);
      ScreenMessage = StringConcatenate(ScreenMessage,Gap, "            European Session:    ", Start_TimeE," - ", End_TimeE, NL);
      ScreenMessage = StringConcatenate(ScreenMessage,Gap, "            US Session:           ", Start_TimeU," - ", End_TimeU, NL);
                      
   }//else
I standardized on using the Asian session to specify 24 hour trading since that's the session that 0:00 occurs for my broker <== OK, I just wanted to see it change. I hid one in the code too to see if it edits code. :D

EDIT: OK, it does change "b-r-o-k-e-r" to "criminal" in code. :(

George
Author:  SteveHopwood [ Thu Dec 08, 2011 10:17 pm ]
Post subject:  Re: My shell EA code

Latest update in post 1. This acts upon advice George gave me in our NB 10.1 about avoiding order modify errors.

Question for you George, and anyone else here with ideas.

A lot of the Stealth stop loss manipulation depends on HiddenPips being > 0. The usual construct is:
if (HiddenPips > 0) do something;

What if, right at the outset, I made the bot draw the tp/al lines, but set the colour to CLR_NONE if HiddenPips = 0? Then, we could forget about whether the user has chosen to use Stealth or not.

The only thing that niggles me is this: will there be problems if the bot is trying to close a trade by Stealth that has already closed through hitting the 'hard' sl/tp? I think the code takes care of this with the very first line in bool LookForTradeClosure(int ticket)

Code: Select all

if (!OrderSelect(ticket, SELECT_BY_TICKET) ) return(true);
but we all know what chaos ensues when I start thinking.

Any comment, guys and guyessess, or children of tender years but unimaginably high iq's?

:D
Author:  gaheitman [ Thu Dec 08, 2011 11:05 pm ]
Post subject:  Re: My shell EA code

SteveHopwood wrote:Latest update in post 1. This acts upon advice George gave me in our NB 10.1 about avoiding order modify errors.

Question for you George, and anyone else here with ideas.

A lot of the Stealth stop loss manipulation depends on HiddenPips being > 0. The usual construct is:
if (HiddenPips > 0) do something;

What if, right at the outset, I made the bot draw the tp/al lines, but set the colour to CLR_NONE if HiddenPips = 0? Then, we could forget about whether the user has chosen to use Stealth or not.

The only thing that niggles me is this: will there be problems if the bot is trying to close a trade by Stealth that has already closed through hitting the 'hard' sl/tp? I think the code takes care of this with the very first line in bool LookForTradeClosure(int ticket)

Code: Select all

if (!OrderSelect(ticket, SELECT_BY_TICKET) ) return(true);
but we all know what chaos ensues when I start thinking.

Any comment, guys and guyessess, or children of tender years but unimaginably high iq's?

:D
I like the invisible lines. What I did for my own code was I changed HiddenPips to a double and set it to 0.1 and called it a day. :D If I were to delete the hidden lines they'd get redrawn, right?

As for the existing filter, the OrderSelect() will still select it whether it is open or not, since it is selecting by ticket number (unless it is passed a bad ticket number). The code to catch the closed ticket is the next line. I don't think you need the OrderSelect() in the second line, since it will be selected if we get past the first test.

The current test:

Code: Select all

   if (!OrderSelect(ticket, SELECT_BY_TICKET) ) return(true);
   if (OrderSelect(ticket, SELECT_BY_TICKET) && OrderCloseTime() > 0) return(true);
Could be:

Code: Select all

   if (!OrderSelect(ticket, SELECT_BY_TICKET) ) return(true);
   if (OrderCloseTime() > 0) return(true);
I'm not sure how Empty4 parses and performs compound boolean tests. If we knew that it would do them in the order we typed them, the test could be:

Code: Select all

   if (!OrderSelect(ticket, SELECT_BY_TICKET) || OrderCloseTime() > 0) return(true);
But's that's only safe if we know the OrderSelect will get called before the OrderCloseTime() is evaluated. No need to get fancy, of course. Your current code should stop you from trying to close already closed tickets. Of course it might be in the process of closing (which sometimes takes longer if the broker needs to recalculate their standard slippage to take in a bit more since they are at month end and the numbers need to look better), and then your code would throw an error.

Reading a bit further, you probably want to call GetLastError() to see why the error happened. You don't necessarily have to do anything with it, but if there is an error and you don't check it, the "last error" is still there and other code may call GetLastError() when there wasn't an error and think there was one.

Wow, that last sentence doesn't read very well. :lol:

George
Author:  SteveHopwood [ Thu Dec 08, 2011 11:18 pm ]
Post subject:  Re: My shell EA code

Hehe. I have learned this over the last 4 years: beat Empty4 over the head until it finally submits and allows you to do what you want.

So, if a code snippet can be lumped together in one line or can be separated into multiple lines with a gazillion checks to make sure that each line has executed, then go for the second option.

I know my own code does not always reflect this understanding, but that is because it has developed over 4 years. Were I to code mptm from scratch now, for example, the code would be radically different. This does nothing to change this fact of life: Empty4 is total crap and as coders, we have to spend most of our lives battling with it.

George, for sure you are making my battles easier with each post you make.

:D
Author:  dietcoke [ Thu Dec 08, 2011 11:43 pm ]
Post subject:  Re: My shell EA code

SteveHopwood wrote: A lot of the Stealth stop loss manipulation depends on HiddenPips being > 0. The usual construct is:
if (HiddenPips > 0) do something;

What if, right at the outset, I made the bot draw the tp/al lines, but set the colour to CLR_NONE if HiddenPips = 0? Then, we could forget about whether the user has chosen to use Stealth or not.

:D
Steve,

FWIW, my view is that the implementation of stealth in the shell code is not complete and not really generic enough. I think the biggest problem is that there is only one value for HiddenPips which is used for both the SL and TP.

I know this is probably a ridiculous amount of work but how about bringing in the stealth code in MPTM, to manage TP/SL.

The two functions:
void HiddenStopLoss()
void HiddenTakeProfit()

could form the basis of stealthy TP/SL. Instead of hidden pips

This code does not use lines to manage the TP/SL at all, the calculations are done on the fly based on the trade management strategy.


Doing this would give a number of advantages such as:

Different values for SL and TP.
Display of lines could then become an extern option when in stealth mode.
You can have a stealthy stop and a visible tp.


Just a thought.

DC.
Author:  SteveHopwood [ Thu Dec 08, 2011 11:58 pm ]
Post subject:  Re: My shell EA code

dietcoke wrote:Just a thought.

DC.
And a worthy one too.

Thing is, mptm is a vast piece of software. Coders alter it at their peril. The moment anyone considers making changes, LUC starts cranking out the invitations to the party by the lorry load.

The cut-down mptm functions that ship with all my bots these days bear only a passing resemblance to those in the real mptm. They work very well and target specific needs. mptm is still the grand-daddy of trade management and can be used instead of the cut-down version when more sophisticated thingies are needed.

:D
Author:  gaheitman [ Fri Dec 09, 2011 8:52 am ]
Post subject:  Re: My shell EA code

Steve (and everyone else),

I need to write a procedure for my own strategy that stops trading for the day, so I thought I'd try to make a generic one for use in other bots.

I was planning on calling it DoneForTheDay() and hooking it into IsTradingAllowed(). That way all the trade management stuff will still happen, just no new trades.

Some options I've come up with to trigger a stop for the day:

The obvious ones:
  • MaxTrades, MaxLosingTrades, MaxWinningTrades
    MaxPipGain, MaxPipLoss
    MaxBalanceGain, MaxBalanceLoss
    MaxPercentGain, MaxPercentLoss
For the options above, there will be a switch for UseEquity or UseBalance as well as combinable options for UseMagicNumber and UseCurrencyPair. That way the code works for multi-currency bots as well as allows people running the same EA across several currencies. All the options make calculating percents difficult, but there it is...

The less obvious (need more idea here):
  • MaxADRPercent //generally if it's a big move day signals are out of whack
    HardStopDateTime //For example, hard coding to stop on next Thursday at 2:15 GMT
    StrategyDoneForTheDay() //a strategy specific procedure that the user modifies that by default just returns false
Once we've determined we are done for the day, there are certain options for actions to take:
  • close all trades
    close all pending
    add tight trailing stop
    sendmail/write to a file/run program/User Procedure
Finally, we'd need a reset time each day, likely right before the first open hour of the trading day. I was originally planning on just looking at the start times of the different sessions, but that makes assumptions on what the user / strategy considers a day.

So that should get people started. I certainly have enough to start coding. :)

George
Author:  SteveHopwood [ Fri Dec 09, 2011 9:20 am ]
Post subject:  Re: My shell EA code

That is fantastically generous again George. Hehe; in a pm, I have just described you as TIG - The Incomparable George. :lol:

Sometime over the weekend I am going to adopt your trading-times code into the shell.

Cheers

:D
Author:  ironrick [ Fri Dec 09, 2011 9:15 pm ]
Post subject:  Re: My shell EA code

I AGREE! Three Cheers for George! Hip! Hip! Hip!

What a cool thing to have such a brain trust here! But not just a passive one -- a trust that is so generous with their time to actively help others!

You have accomplished an incredible feat, Steve, over the years. And the rapid rise of your forum is the proof of others' thankfulness.

Thanks!
Rick

SteveHopwood wrote:That is fantastically generous again George. Hehe; in a pm, I have just described you as TIG - The Incomparable George. :lol:

Sometime over the weekend I am going to adopt your trading-times code into the shell.

Cheers

:D
Author:  ironrick [ Fri Dec 09, 2011 9:24 pm ]
Post subject:  Re: My shell EA code

gaheitman wrote: Some options I've come up with to trigger a stop for the day:

The obvious ones:
  • MaxTrades, MaxLosingTrades, MaxWinningTrades
    MaxPipGain, MaxPipLoss
    MaxBalanceGain, MaxBalanceLoss
    MaxPercentGain, MaxPercentLoss
For the options above, there will be a switch for UseEquity or UseBalance as well as combinable options for UseMagicNumber and UseCurrencyPair. That way the code works for multi-currency bots as well as allows people running the same EA across several currencies. All the options make calculating percents difficult, but there it is...

The less obvious (need more idea here):
  • MaxADRPercent //generally if it's a big move day signals are out of whack
    HardStopDateTime //For example, hard coding to stop on next Thursday at 2:15 GMT
    StrategyDoneForTheDay() //a strategy specific procedure that the user modifies that by default just returns false
Now back to the subject at hand... :)

Because it has been on mind, I was thinking of other trade-length time-based options.

Rather then just a clock time -- End trade at 2:30pm, end the trade/trading after a certain AMOUNT of time -- Start at 7:00am/x signal/MA cross/whatever and end after 5.5 hours.

Just a thought.

Rick
All times are UTC Page 3 of 23