Hi, I wrote an EA, and want it to long positions and close out the shorts when SMA is up, and vice versa when SMA is down. Unfortunately it doesn't open any positions when I use it in the strategy tester. I am very grateful if anyone could point out the mistakes.
#property copyright "Dogberry"
#property link "http://www.forex-tsd.com"
extern double Lots=1;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int UpOrDown (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;
if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down
if(current_dirction != last_direction) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double shortEma, shortEmap;
shortEma = iMA(NULL,PERIOD_H4,8,0,MODE_SMA,PRICE_CLOSE,0);
shortEmap = iMA(NULL,PERIOD_H4,8,0,MODE_SMA,PRICE_CLOSE,1);
int isUpOrDown = UpOrDown(shortEma,shortEmap);
// static variable to track bar of last entry
static datetime tLastEntry = 0;
// if there wasn't an entry this bar, perform entry check
if(tLastEntry < Time[0])
if(doOrderEntry())
tLastEntry = Time[0];
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY && isUpOrDown==2) // long positions are opened
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
else if(OrderType()==OP_SELL && isUpOrDown == 1)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
return(0);
}
return(0);
}
bool doOrderEntry()
{
int cnt, ticket;
double shortEma, shortEmap;
shortEma = iMA(NULL,PERIOD_H4,8,0,MODE_SMA,PRICE_CLOSE,0);
shortEmap = iMA(NULL,PERIOD_H4,8,0,MODE_SMA,PRICE_CLOSE,1);
int isUpOrDown = UpOrDown(shortEma,shortEmap);
if(isUpOrDown == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,
"My EA",12345,0,Green);
return(true);
}
else if(isUpOrDown == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,
"My EA",12345,0,Green);
return(true);
}
else
return(false);
}
#property copyright "Dogberry"
#property link "http://www.forex-tsd.com"
extern double Lots=1;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int UpOrDown (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;
if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down
if(current_dirction != last_direction) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double shortEma, shortEmap;
shortEma = iMA(NULL,PERIOD_H4,8,0,MODE_SMA,PRICE_CLOSE,0);
shortEmap = iMA(NULL,PERIOD_H4,8,0,MODE_SMA,PRICE_CLOSE,1);
int isUpOrDown = UpOrDown(shortEma,shortEmap);
// static variable to track bar of last entry
static datetime tLastEntry = 0;
// if there wasn't an entry this bar, perform entry check
if(tLastEntry < Time[0])
if(doOrderEntry())
tLastEntry = Time[0];
for(int i=OrdersTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY && isUpOrDown==2) // long positions are opened
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
else if(OrderType()==OP_SELL && isUpOrDown == 1)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
return(0);
}
return(0);
}
bool doOrderEntry()
{
int cnt, ticket;
double shortEma, shortEmap;
shortEma = iMA(NULL,PERIOD_H4,8,0,MODE_SMA,PRICE_CLOSE,0);
shortEmap = iMA(NULL,PERIOD_H4,8,0,MODE_SMA,PRICE_CLOSE,1);
int isUpOrDown = UpOrDown(shortEma,shortEmap);
if(isUpOrDown == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,
"My EA",12345,0,Green);
return(true);
}
else if(isUpOrDown == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,
"My EA",12345,0,Green);
return(true);
}
else
return(false);
}