EA not doing as I expect on strategy test runs, help!

 

Hi All,

I'm fairly new to metatrader and to programming EA's. I have a lot of experience with VB/VBA so the programming side isn't the problem, at least I don't think it is.

I've written a basic EA to test my skills while learning and it doesn't seem to take the trades when I expect while running a visual strategy test.

It's supposed to take a buy when the bar open is below the Moving Average and the bar close is above the Moving Average. It isn't though, it's taking a buy as soon as the bar pushes through the Moving Average, same with a sell.

I'm using a 15 min chart on the test and I'm thinking the strategy tester is using the open/close of the 1 min bars that it is using to generate the visual 'ticks'.

I hope [if you're reading this] you can understand what I'm going on about.

The 2 lines of code that trigger buys/sells are thus...

if (barClose > movingAv && barOpen < movingAv) Order = SIGNAL_BUY; 

if (barClose < movingAv && barOpen > movingAv) Order = SIGNAL_SELL;

The above should [I would expect] produce a buy when the bar opens below the MA and closes above, and vice versa for a sell. But what's happening on test is it's triggering a trade [buy or sell] as soon as the developing bar pushes through the MA.

Anyone shed any light on this?

Ron

 

A variety of thoughts come to mind - some helpful, some not

A) One passenger to another on bus, "Does this bus go by the library?"
"Yes"
"Which stop do I get off for it?"
"Well, that man over there gets off just after the library, so all you have to do is get off at the bus stop before he does"

That's the problem with knowing "which tick is the last one for the current bar?" - it's the one that was before the first tick of the NEXT bar!

So, consider entering trade on FIRST tick of NEXT bar - you're only 1 tick late, and it's VERY easy to pick (change of value for Time[0])

B) 'this bar Close price' varies during the bar, starting at 'bar Open price' & traversing from there to High & Low (or vice versa) and most points in between, before finally resting at 'the real Close' when the next bar starts. This causes many line-crossing EAs to fail badly in reality, when the chart looks so clear after the event.

C) "Oh dear, how sad, never mind"

 
brewmanz:

A variety of thoughts come to mind - some helpful, some not


That's the problem with knowing "which tick is the last one for the current bar?" - it's the one that was before the first tick of the NEXT bar!

So, consider entering trade on FIRST tick of NEXT bar - you're only 1 tick late, and it's VERY easy to pick (change of value for Time[0])




Hi brewmanz,

Firstly, thanks for taking the time to answer.

I am, or at least I thought I was, getting the trade to open on the first tick of the next open bar. Clearly it looks like I'm not.

In your comment " (change of value for Time[0] ", could you please elaborate on that a bit? I'm brand new to the properties of mql4 so need to be pointed in the right direction.


Thanks,

Ron

 

something like

static datetime prevTickBarTime = 0;
bool firstTickOfBar = false;
if(prevTickBarTime != Time[0])
{
  prevTickBarTime = Time[0];
  firstTickOfBar = true;
}
if(firstTickOfBar)
{
  CheckForCreatingOrder();
}

(just typed, not compiled or checked)

 

Hi brewmanz,

Thanks for explaining that. I managed to achieve what I wanted after a bit of tinkering last night.

I followed you line of suggestion, or at least I thought I was :) and used the index offset for the iClose and iOpen functions.

My EA does exactly what it should now, however it totally bombed. While it was "broken" before I got the code corrected it was making a profit.

Sods law I suppose.

Thanks again for the help.

Ron

Reason: