Hello guys,
I am not a noob in programming, however, I am new Empty4.
I want to create a simple ea. I want to trade when the EMA 10 touches the EMA 20.
Please give some code on how to program this touch in the Empty4 language.
Thx in advance!!!
Empty4 4 - Trade when the EMA 10 touches the EMA 20?
- gaheitman
- Trader
- Posts: 655
- Joined: Tue Nov 15, 2011 10:55 pm
- Location: Richmond, VA, US
Re: Empty4 4 - Trade when the EMA 10 touches the EMA 20?
First of all, you need to think through how to translate what you see into what is happening mathematically. It's easy to look at a chart and see that two MAs are touching. Sometimes it happens during the current bar, but often it happens in the space between bars. The MAs can also touch and then move apart again so that there is no visual record of them touching when viewing history.mrmagic wrote:Hello guys,
I am not a noob in programming, however, I am new Empty4.
I want to create a simple ea. I want to trade when the EMA 10 touches the EMA 20.
Please give some code on how to program this touch in the Empty4 language.
Thx in advance!!!
Since the mathematical value of two MAs will seldom be equal, you are really looking for a cross of the MAs. In order to capture the cross on the current bar as well as the cross that happened between the current bar and the last bar, you need to look and see if the order of the two MAs has changed between the close of the previous bar and the "close" of the current bar. (The MA of the "close" of the current bar is actually the current value of the MA.)
So, to get the MAs of the previous bar, you would use code similar to:
Code: Select all
Prev10 = iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1);
Prev20 = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,1);Code: Select all
Cur10 = iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0);
Cur20 = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);Code: Select all
cross=false;
//test for the rare case
if (Cur10==Cur20) cross=true;
//test for the crosses
if (Prev10 > Prev20 && Cur10 < Cur20) cross = true;
if (Prev10 < Prev20 && Cur10 > Cur20) cross = true;
if (cross == true) GoMakeMoney();
If you want to ignore crosses that happen between the bars, you will need to capture the Cur10 and Cur20 values immediately at the open of the new bar, save them to a different variable and do the comparisons to them.
George
-
mrmagic
Re: Empty4 4 - Trade when the EMA 10 touches the EMA 20?
thx you very much for your answer!!!gaheitman wrote:First of all, you need to think through how to translate what you see into what is happening mathematically. It's easy to look at a chart and see that two MAs are touching. Sometimes it happens during the current bar, but often it happens in the space between bars. The MAs can also touch and then move apart again so that there is no visual record of them touching when viewing history.mrmagic wrote:Hello guys,
I am not a noob in programming, however, I am new Empty4.
I want to create a simple ea. I want to trade when the EMA 10 touches the EMA 20.
Please give some code on how to program this touch in the Empty4 language.
Thx in advance!!!
Since the mathematical value of two MAs will seldom be equal, you are really looking for a cross of the MAs. In order to capture the cross on the current bar as well as the cross that happened between the current bar and the last bar, you need to look and see if the order of the two MAs has changed between the close of the previous bar and the "close" of the current bar. (The MA of the "close" of the current bar is actually the current value of the MA.)
So, to get the MAs of the previous bar, you would use code similar to:
and to get the current values:Code: Select all
Prev10 = iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1); Prev20 = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,1);
The test for crossing is simply:Code: Select all
Cur10 = iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0); Cur20 = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
It's important to remember that the current values can change with every incoming tick, so the cross could happen over and over again as price moves up and down. You will need to add some sort of check to only count the first cross (depending on your strategy, of course).Code: Select all
cross=false; //test for the rare case if (Cur10==Cur20) cross=true; //test for the crosses if (Prev10 > Prev20 && Cur10 < Cur20) cross = true; if (Prev10 < Prev20 && Cur10 > Cur20) cross = true; if (cross == true) GoMakeMoney();
If you want to ignore crosses that happen between the bars, you will need to capture the Cur10 and Cur20 values immediately at the open of the new bar, save them to a different variable and do the comparisons to them.
George
-
jimmyjjohn
Re: Empty4 4 - Trade when the EMA 10 touches the EMA 20?
Legend Brokers the world’s leading Forex news and views portal, has become a one-stop solution provider for Forex traders and brokers as well as Bitcoins news. It provides all the required tools to individuals to become a professional Forex trader. Also, it helps Forex brokers provide high-end user-friendly trading experience to traders with an array of resources e.g. financial news by the minute, fundamental analysis, technical analysis, Forex tools and others.
Thanks in advanced!
Thanks in advanced!