How to detemine cross-over point value? - page 2

 

Hi everyone,

Thanks for the tips about writing an EA based upon the crossing of two moving averages. I have just written an that also does the same thing so if you would like a look at my code Dohung or anyone else you are more than welcome even it is only at the basic opening and closing stage and lacks multiple currency trading, account safety and error processing at this stage. However, getting the right MA settings to prevent too many loss trades is proving troublesome at the moment!

I use a similar method to the efficient version WHRoeder posted for determining if the MAs have crossed but use integers to store the direction of up or down, 0 and 1 respectively and store them in an array called Direction. Now and Last are defined constants of 0 and 1 for referencing the Direction array which just aid readability really and are used to store the now and last market directions I'm checking against. A bitwise XOR is used to check if the direction of the current MAs and the last MAs and if both values are different then they have crossed and the Direction [Now] array element stores the current direction and the Direction [Last] array element stores the last direction. Bitwise operations can only be performed with integers and not bool data types.

I don't think there is anything slower or faster about my code compared to WHRoeder and they both have the same benefits, just implemented differently. I'm just offering my code as an alternative for anyone who is interested. The benefit of either version is that the value of the Crossed variable can be used in the OrderSend command to open an order in the direction of the market (OP_BUY = 0 and OP_SELL = 1) in one line without using more if statements to determine the market direction and needing two OrderSend commands, one for each direction. Another benefit is the third block of code tests (using another bitwise operation) if the direction of the market is the same as the type of order and if it is not the same, setting CloseTest to 1, the order is closed.

A code extract from my EA is below.

    if (MA 5 > MA 20) Direction [Now] = 0;                // Determines the direction of the moving averages for the current tick, 0 for an uptrend and 1 for a downtrend

    else Direction [Now] = 1;
    

    int Crossed = (Direction [Now] ^ Direction [Last]);   // Checks if the two moving averages have crossed since the last tick


    int CloseTest = Direction [Now] ^ OrderType ();                                     // Closes an order if it is not going in the same direction as the trend
    if (CloseTest == 1) CloseOrder ();

Sorry for the essay but I hope it helps anyone who reads it.

Regards,

Noddy

 
WHRoeder:

Exactly.

v20 = MA20_LastValue + (MA20_Current - MA20_LastValue) * x

v5  =  MA5_LastValue + ( MA5_Current -  MA5_LastValue) * x

Use this and you would have found this among others.


That's great!

It's exactly what I am looking for!

Thank you very much!