Need help with my exit code.

 

The idea of my EA is very basic. I want to enter and exit on an EMA crossover.

Entry:

Long

55 EMA Crosses below the 13 EMA on the hourly timeframe.

Vice versa with a short postion.

Exit:

55 EMA crosses above the 13 EMA on the 30 min timeframe.

I have put together some code already however it sometimes exits on the hourly cross and sometimes on the misses entries. I cant figure out how to correct this. Please help if you can.

Thanks, and here is the code:

//+------------------------------------------------------------------+
//| MA8-13 cross.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"

extern int ExtPeriod1=13;
extern int ExtPeriod2=55;
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
static datetime PrevTime=0;
double Ma8Current, Ma8Previous, Ma8Previous2;
double Ma13Current, Ma13Previous, Ma13Previous2;
bool IsCrossDown, IsCrossUp;
double Ma8Current30, Ma8Previous30, Ma8Previous230;
double Ma13Current30, Ma13Previous30, Ma13Previous230;
bool IsCrossDown30, IsCrossUp30;
double StopLoss;
//---- ???? ?? ??????? ???? ?????????? ??? ??? ?????????, ???????.
if (PrevTime==Time[0]) return(0);
PrevTime=Time[0];
//---- ??????? ???????? ????????
Ma8Current=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,0);
Ma8Previous=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,-1);
Ma8Previous2=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,1);
Ma13Current=iMA(NULL,60,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,0);
Ma13Previous=iMA(NULL,60,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,-1);
Ma13Previous2=iMA(NULL,60,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,1);
//-----30 minute EMAs
Ma8Current30=iMA(NULL,30,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,0);
Ma8Previous30=iMA(NULL,30,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,-1);
Ma8Previous230=iMA(NULL,30,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,1);
Ma13Current30=iMA(NULL,30,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,0);
Ma13Previous30=iMA(NULL,30,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,-1);
Ma13Previous230=iMA(NULL,30,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,1);
//---- ? ???????? ??????? ???????????
IsCrossDown=(Ma8Current<Ma13Current && Ma8Previous>=Ma13Previous && Ma8Previous2>Ma13Previous2);
IsCrossUp =(Ma8Current>Ma13Current && Ma8Previous<=Ma13Previous && Ma8Previous2<Ma13Previous2);
//----#0 minutre cross
IsCrossDown30=(Ma8Current30<Ma13Current30 && Ma8Previous30>=Ma13Previous30 && Ma8Previous230>Ma13Previous230);
IsCrossUp30 =(Ma8Current30>Ma13Current30 && Ma8Previous30<=Ma13Previous30 && Ma8Previous230<Ma13Previous230);



//----
if(IsCrossDown)
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
//---- exit longs
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,3);
}
//---- entry short
OrderSend(Symbol(),OP_SELL,1.0,Bid,3,0.0,0.0,"Short by sample");
}
if(IsCrossUp30)
{
for(i=OrdersTotal()-1; i>=0; i--)
{
//---- exit shorts
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,3);
}
}
//----
//----
if(IsCrossUp)
{
for(i=OrdersTotal()-1; i>=0; i--)
{
//---- exit shorts
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,3);
}
//---- entry long
OrderSend(Symbol(),OP_BUY,1.0,Ask,3,0.0,0.0, "Long by Sample");
}
if(IsCrossDown30)
{
for(i=OrdersTotal()-1; i>=0; i--)
{
//---- exit longs
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,3);
}
}
//----

return(0);
}
//+------------------------------------------------------------------+

 
Use the Print() statement liberally.
 
derrickc wrote >>

The idea of my EA is very basic. I want to enter and exit on an EMA crossover.

Entry:

Long

55 EMA Crosses below the 13 EMA on the hourly timeframe.

Vice versa with a short postion.

Exit:

55 EMA crosses above the 13 EMA on the 30 min timeframe.

I have put together some code already however it sometimes exits on the hourly cross and sometimes on the misses entries. I cant figure out how to correct this. Please help if you can.

Thanks, and here is the code:


Ma8Current=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,0);
Ma8Previous=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,-1);
Ma8Previous2=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,1);
Ma13Current=iMA(NULL,60,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,0);
Ma13Previous=iMA(NULL,60,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,-1);
Ma13Previous2=iMA(NULL,60,ExtPeriod2,0,MODE_EMA,PRICE_CLOSE,1);

Ma8Previous=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,-1);
Ma8Previous2=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,1);
should not be

Ma8Previous=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,1);
Ma8Previous2=iMA(NULL,60,ExtPeriod1,0,MODE_EMA,PRICE_CLOSE,2);

?

The last Parameter is the BarShift. 0 for current Bar, 1 for the Bar before, 2 for 2 Bars before ...

(Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).)

Reason: