Need help making an indicator into an EA.

 

Ok so I have found an indicator that I really like and have done well with. Im trying to make it into an EA but am having trouble. Very basic. It is an double EMA cross, so you would think that it would be easy to code. The code that I have taken from other EAs and put together isnt making every trade that the indicator is taking.

I want it to enter when the 55 and the 13 EMA cross on the 1H chart, then close when the it crosses back but on the 30 Min chart.

Im going to place the indicator code and the EA code. The indicator has been way more accurate then my EA so that is what Im trying to mimic. If you can help I very much appreciate it.

Thanks!

EMA Code:

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,2);
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,2);
//-----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,2);
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,2);
//----
IsCrossDown=(Ma8Current<Ma13Current && Ma8Previous>=Ma13Previous && Ma8Previous2>Ma13Previous2);
IsCrossUp =(Ma8Current>Ma13Current && Ma8Previous<=Ma13Previous && Ma8Previous2<Ma13Previous2);
//----
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(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);
}
}

//----
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");
}
//----
return(0);
}
//+------------------------------------------------------------------+

Indicator Code:

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 SeaGreen
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
extern int FasterEMA = 13;
extern int SlowerEMA = 55;
extern bool SoundON=true;
double alertTag;
double control=2147483647;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY,3);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY,3);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {
int limit, i, counter;
double fasterEMAnow, slowerEMAnow, fasterEMAprevious, slowerEMAprevious, fasterEMAafter, slowerEMAafter;
double Range, AvgRange;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(i = 0; i <= limit; i++) {

counter=i;
Range=0;
AvgRange=0;
for (counter=i ;counter<=i+9;counter++)
{
AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range=AvgRange/10;

fasterEMAnow = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i);
fasterEMAprevious = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
fasterEMAafter = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);

slowerEMAnow = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i);
slowerEMAprevious = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i+1);
slowerEMAafter = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);

if ((fasterEMAnow > slowerEMAnow) && (fasterEMAprevious < slowerEMAprevious) && (fasterEMAafter > slowerEMAafter)) {
CrossUp[i] = Low[i] - Range*0.5;

}
else if ((fasterEMAnow < slowerEMAnow) && (fasterEMAprevious > slowerEMAprevious) && (fasterEMAafter < slowerEMAafter)) {
CrossDown[i] = High[i] + Range*0.5;
}
if (SoundON==true && i==1 && CrossUp[i] > CrossDown[i] && alertTag!=Time[0]){
Alert("EMA Cross Trend going Down on ",Symbol()," ",Period());
alertTag = Time[0];
}
if (SoundON==true && i==1 && CrossUp[i] < CrossDown[i] && alertTag!=Time[0]){
Alert("EMA Cross Trend going Up on ",Symbol()," ",Period());
alertTag = Time[0];
}
}
return(0);
}

Reason: