You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
...
Terrance
You wanted to test for a cross on a closed bar. Shift 1 means first closed bar and shift 2 means first bar before that. And if you wish to test for a cross on a closed bar those are the bars that you have to test
As of (diff1*diff2)<0) : it is quite simple actually
Any can be positive or negative. What we are interested in is when one is positive and the other negative because that means that the averages have crossed. Multiplying them gives us a fast answer if there is a cross (if signs are different, then the result is less than 0, if the signs are equal the result of that multiplication is greater than 0)
Hope this clarified what does that code do
Hi Mladen, i tried out what you told me and it work wonders. However, I don't quite understand the logic behind the codes. why do you set the shift of iMA to 1 and 2 and why do you code this: (diff1*diff2)<0) as well? Sorry but I am currently still on the learning phrase.
Thanks and regards
TerranceHow To Add Arrows
Attached is an abbreviated version of the MA crossover EA discussed in the last few posts.
When a crossover occurs on a closed candle, this version Prints a message to the log file.
Would it be possible to replace the Print logic with code to display up/down arrows on the chart?
Thank you for your help.
...
Try out this way ...
It will draw arrow and it will show a comment in the right lower corner of the chart - like this (added the averages on chart just to make it obvious what does it do) :
Attached is an abbreviated version of the MA crossover EA discussed in the last few posts.
When a crossover occurs on a closed candle, this version Prints a message to the log file.
Would it be possible to replace the Print logic with code to display up/down arrows on the chart?
Thank you for your help.MA Arrows
The "2MAcrossover with comments" works really well.
Just one question- once an arrow appears, it will disappear when a new candle opens, was this intentional?
I like the comment in the lower-right corner of the chart.
Thanks again.
Terrance
You wanted to test for a cross on a closed bar. Shift 1 means first closed bar and shift 2 means first bar before that. And if you wish to test for a cross on a closed bar those are the bars that you have to test
As of (diff1*diff2)<0) : it is quite simple actually
Any can be positive or negative. What we are interested in is when one is positive and the other negative because that means that the averages have crossed. Multiplying them gives us a fast answer if there is a cross (if signs are different, then the result is less than 0, if the signs are equal the result of that multiplication is greater than 0)
Hope this clarified what does that code doThanks Mladen for your help! But when i added that code in, my EA seems to go a bit haywire. Occasionally it will auto SL or TP on its own even before the actual SL or TP is reached like the one shown in my screenshot. why is this so?
Thanks and regards
Terrance
//--- input parameters
extern double TakeProfit=1000.0;
extern double Lots=0.1;
extern double StopLoss=1500.0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int mainCrossed (double mainline1 , double mainline2)
{
static int mainlast_direction = 0;
static int maincurrent_dirction = 0;
if(mainline1>mainline2)maincurrent_dirction = 1; //main up
if(mainline1<mainline2)maincurrent_dirction = 2; //main down
if(maincurrent_dirction != mainlast_direction) //main changed
{
mainlast_direction = maincurrent_dirction;
return (mainlast_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma, mainshortEma, mainlongEma;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
int isCrossed = 0;
double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);
double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);
double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);
double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);
double diff1 = shortEma1-longEma1;
double diff2 = shortEma2-longEma2;
mainshortEma = iMA(NULL,0,30,0,MODE_SMA,PRICE_CLOSE,0);
mainlongEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
int mainisCrossed = mainCrossed (mainshortEma,mainlongEma);
if ((diff1*diff2)<0)
{
if (shortEma1>longEma1)
isCrossed = 1;
else isCrossed = 2;
}
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1 && mainshortEma > mainlongEma)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
"My EA",12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2 && mainshortEma < mainlongEma)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,
Bid-TakeProfit*Point,"My EA",12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
return(0);
}
//+------------------------------------------------------------------+
Terrance
That code has nothing to do with SL or TP. It simply tests 2 averages for crosses (see how michaelB used that same code in his 2MACrosses, since that is nan EA too). The rest of your code needs to be revised for those SL and TP errors
Thanks Mladen for your help! But when i added that code in, my EA seems to go a bit haywire. Occasionally it will auto SL or TP on its own even before the actual SL or TP is reached like the one shown in my screenshot. why is this so?
Thanks and regards
Terrance
//--- input parameters
extern double TakeProfit=1000.0;
extern double Lots=0.1;
extern double StopLoss=1500.0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int mainCrossed (double mainline1 , double mainline2)
{
static int mainlast_direction = 0;
static int maincurrent_dirction = 0;
if(mainline1>mainline2)maincurrent_dirction = 1; //main up
if(mainline1<mainline2)maincurrent_dirction = 2; //main down
if(maincurrent_dirction != mainlast_direction) //main changed
{
mainlast_direction = maincurrent_dirction;
return (mainlast_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma, mainshortEma, mainlongEma;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
int isCrossed = 0;
double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);
double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);
double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);
double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);
double diff1 = shortEma1-longEma1;
double diff2 = shortEma2-longEma2;
mainshortEma = iMA(NULL,0,30,0,MODE_SMA,PRICE_CLOSE,0);
mainlongEma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
int mainisCrossed = mainCrossed (mainshortEma,mainlongEma);
if ((diff1*diff2)<0)
{
if (shortEma1>longEma1)
isCrossed = 1;
else isCrossed = 2;
}
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1 && mainshortEma > mainlongEma)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,
"My EA",12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2 && mainshortEma < mainlongEma)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,
Bid-TakeProfit*Point,"My EA",12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
return(0);
}
//+------------------------------------------------------------------+...
Yes, it is intentional
Since you made an EA and not an indicator, the addition was to only show last active cross and when there is no active cross, not to show an arrow. To make it able to show multiple arrows it would need to be converted into indicator first and then the whole thing would need to be rewritten, but I think that there are a lot of indicators that are showing averages crosses on chart already
The "2MAcrossover with comments" works really well.
Just one question- once an arrow appears, it will disappear when a new candle opens, was this intentional?
I like the comment in the lower-right corner of the chart.
Thanks again.Terrance That code has nothing to do with SL or TP. It simply tests 2 averages for crosses (see how michaelB used that same code in his 2MACrosses, since that is nan EA too). The rest of your code needs to be revised for those SL and TP errors
Hi Mladen,
do you mean that the way i set my TP and SL are wrong or?
Regards
Terrance
...
Terrance
Did you see this section of TSD : Lessons ?
I think that a lot of questions are already answered there and that you can find very good examples for general EA writing there. As of your question : I did not test your EA so I do not know. The problem with testing someone else s EA is that one has to know the idea of it and the logic of it and a lot of times it is simply impossible to "read" those from the code itself. The code snippet that we were talking about was concerning how to detect 2 averages crosses on a closed bar and that code is doing just that and nothing but that. That is why I said that "it has nothing to do with SL or TP"
regards
Mladen
Hi Mladen,
do you mean that the way i set my TP and SL are wrong or?
Regards
TerranceMQL Lessons Thread
mladen-- thank you for the link!!