Page 1 of 1

complete Moving Average Cross Over function

Posted: Apr 06 2014
by Wally_AD
Hi all,
tpyicall the condition to detect a cross over event looks like this:

Code: Select all

MA9 = XAverage( Price5, 9) ;
MA13 = XAverage( Price5, 13) ;
MA9_Shows_Up = MA9 > MA13;
MA5_Penetrated_Up = CurrentBar > 1 and MA9_Shows_Up and (MA9_Shows_Up[1] = false);
or so:

Code: Select all

inputs:
Price( Close ),
variables:
var0( 0 ) ;
var0 = XAverage( Price, 9) ;
condition1 = Price > var0 and var0 > var0[1] and var0[1] <= var0[2] ;
I used the first version.

My goal is to create only 1 order directly after a cross over event occurrs.
And a certain time frame shall be used. For example five minute bars shall be used for the calculation of the moving averages.

Without further precautions this code snippet will not work in the following cases:
  • 1) intraday system shall only buy/sell during certain hours
  • 2) the system is started after the market opening time, e.g. 11.00 CET
  • 3) a gap of the market data occurs, eg. due to a problem of the internet provider
  • 4) options in Strategy properties: Recalculate on Broker Events Signal like Market Position Change or Order Filled
  • 5)IOG on and/or Bar Magifier enabled
to number 1:

Code: Select all

if time >= SessionStartTime( 0, 1 ) and time < SessionEndTime( 0, 1 ) then
RegularMarketHours = true;
I encountered the problem that in the cases above var[0] and var[1] must not be subsequent in time and could be separated by 1 hour or any other gap

So I wonder how a complete function could look like, which ensures that only close bars of the intended time frame are used and that always two subsequent bars are used without a gap or the last data from the day before.

my idea is:

Code: Select all

inputs:
Price( Close ),
variables:
MA9 = XAverage( Price, 9) ;
MA13 = XAverage( Price, 13) ;
StartTime (0830);

if BarStatus(1)[0] = 2 and BarStatus(1)[1] = 2 and (CurrentBar - CurrentBar[1] =1 ) and (Time <> StartTime) Then
condition1 = MA9 > MA13 and MA9[1] <= MA13[1] ;
Or should instead crosses over be used ?

Code: Select all

condition1 = var0 crosses over var1 ;
Are all conditions above covered by 'crosses over'?

Any help on this will be appreciated.

Re: complete Moving Average Cross Over function

Posted: Apr 06 2014
by JoshM
I'm not sure if I follow you completely, since you provide a lot of info and after reading your statements and pseudo-code twice I still doubt I fully understand you. :]

But when you say this:
So I wonder how a complete function could look like, which ensures that only close bars of the intended time frame are used and that always two subsequent bars are used without a gap or the last data from the day before.
..then I would code that like:

Code: Select all

Variables:
myDataGapDefinition(false),
firstMA(0), secondMA(0), myCrossOver(False);

myDataGapDefinition = ....

if (BarType_ex <> 2) or // Only for minutes charts
(Date <> Date[1]) or // but not on the previous day
(myDataGapDefinition = true) // and without data gaps
then
#return;

if (BarStatus(1) = 2) then begin // Only calculating on bar close

firstMA = ...
secondMA = ...

myCrossOver = (firstMA > secondMA) and (firstMA[1] <= secondMA[1]);

end;

Re: complete Moving Average Cross Over function

Posted: Apr 06 2014
by Wally_AD

Code: Select all

MA9 = XAverage( Price5, 9) ;
MA13 = XAverage( Price5, 13) ;
MA9_Shows_Up = MA9 > MA13;
MA5_Penetrated_Up = CurrentBar > 1 and MA9_Shows_Up and (MA9_Shows_Up[1] = false);

Code: Select all

inputs:
Price( Close ),
variables:
var0( 0 ) ;
var0 = XAverage( Price, 9) ;
condition1 = Price > var0 and var0 > var0[1] and var0[1] <= var0[2] ;
One of my points was that I am wondering wether both functions above behave in the same way.

Re: complete Moving Average Cross Over function

Posted: Apr 06 2014
by Wally_AD
myDataGapDefinition = ....
How would you calculate myDataGapDefinition ?

if 5 minute bars are used, then the time different between two bars must be 5 minutes.

It would mean:

Code: Select all

if time - time[1] = 5 then
myDataGapDefinition = true
else
myDataGapDefinition = false;
time - time[1] works for 1230 and 1235 but does not work for 1255 and 1300. This results into 45. This would would require an appropriate function.
Let us call the function Check_Timeframe(time1,time2, timeframe) returning true if the difference equals to timeframe, e.g. 5 minutes. If somebody is interested I can provide Check_Timeframe.

Then we get:

Code: Select all

Variables:
myDataGapDefinition(false),
firstMA(0), secondMA(0), myCrossOver(False);

myDataGapDefinition = Check_Timeframe(time, time[1], 5)

if (BarType_ex <> 2) or // Only for minutes charts
(Date <> Date[1]) or // but not on the previous day
(myDataGapDefinition = false) // and without data gaps
then
#return;

if (BarStatus(1) = 2) then begin // Only calculating on bar close

firstMA = ...
secondMA = ...

myCrossOver = (firstMA > secondMA) and (firstMA[1] <= secondMA[1]);

end;
This could work.

Re: complete Moving Average Cross Over function

Posted: Apr 11 2014
by JoshM
One of my points was that I am wondering wether both functions above behave in the same way.
I think that is something you can easily test yourself, if I understand your goal correctly.
myDataGapDefinition = ....
How would you calculate myDataGapDefinition ?
That's up to you; I don't know what your definition of a data gap is. In this thread, you've used two possible definitions: time difference between two bars or the loss of an internet connection. Both require something completely different when implementing.
time - time[1] works for 1230 and 1235 but does not work for 1255 and 1300. This results into 45. This would would require an appropriate function.
You could use DateTime for those calculations (see the wiki).
Let us call the function Check_Timeframe(time1,time2, timeframe) returning true if the difference equals to timeframe, e.g. 5 minutes. If somebody is interested I can provide Check_Timeframe.
I don't understand; why do you ask us if you already have a function to monitor data gaps?
This could work.
Just test it, then you'll know for sure. :]

Re: complete Moving Average Cross Over function

Posted: Apr 11 2014
by Wally_AD
Thank you for your time.
I don't understand; why do you ask us if you already have a function to monitor data gaps?
I am asking because it does not work as it should and I am struggling with this since a longer time.
Just test it, then you'll know for sure. :]
It is quite an effort to test it. As it should be a standard function I expected somebody has a solution.
Testing requires:
- start system normally before start time
- start system at start time
- start system during the day
- disconnect internet connection
- run it with IOG enabled
- run it with bar magnifer enabled

I do not want to avoid test effort, but so far it was a headache to code this.

Thanks for your time.