I programmed this EA to (close short trade and) buy when price closes above 5 ema and (close long trade and) sell when price closes below 5 ema.
The logic: (From MetaStock)
Close short: CLOSE > Ref(Mov(CLOSE,5,E),-1)
Enter long: CLOSE > Ref(Mov(CLOSE,5,E),-1)
Close long: CLOSE < Ref(Mov(CLOSE,5,E),-1)
Enter short: CLOSE < Ref(Mov(CLOSE,5,E),-1)
And If trend remains the same, do nothing.
There must be some error in the code (Mql 4 for metatrader) because this is how the backtesting went (See Attached Picture).
Anyone familiar with this error ? Can anyone correct the code ? Thanks.
The logic: (From MetaStock)
Close short: CLOSE > Ref(Mov(CLOSE,5,E),-1)
Enter long: CLOSE > Ref(Mov(CLOSE,5,E),-1)
Close long: CLOSE < Ref(Mov(CLOSE,5,E),-1)
Enter short: CLOSE < Ref(Mov(CLOSE,5,E),-1)
And If trend remains the same, do nothing.
There must be some error in the code (Mql 4 for metatrader) because this is how the backtesting went (See Attached Picture).
Anyone familiar with this error ? Can anyone correct the code ? Thanks.
Inserted Code
//+------------------------------------------------------------------+
//| MA-EA.mq4 |
//| Copyright © 2009, Simran |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Simran"
#property link "http://www.metaquotes.net"
//---- input parameters
extern int MA_Method=2;
extern int MA_Period=5;
extern int Lots=1;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int BarsCount = 0;
//----
int start()
{
if (Bars > BarsCount)
{
double MA1 = iMA(NULL,0,MA_Period,0,MA_Method,0,1);
double MA2 = iMA(NULL,0,MA_Period,0,MA_Method,0,2);
double C1 = iClose(NULL,0,1);
double C2 = iClose(NULL,0,2);
if ((C2<MA2 && C1<MA1)||(C2>MA2 && C1>MA1)) //No trend change
{
return; //Do nothing. Just wait for opposing signal.
}
//---------------------------------------------------------------------
if (C2<MA2 && C1>MA1) //Trend change from down to up.
{
int total = OrdersTotal(); //Script to close all open orders.
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
} //End of Script to close all orders.
int ticket; //Place Buy Order
RefreshRates();
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-1000*Point,Ask+1000*Point,"Buy Order",9999,0,Green);
if(ticket<0)
{
Print("Buy Order failed with error #",GetLastError());
}
return;
}
//---------------------------------------------------------
if(C2>MA2 && C1<MA1) //Trend change from up to down.
{
total = OrdersTotal(); //Script to close all open orders.
for(i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
type = OrderType();
result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
} //End of Script to close all orders.
//Place Sell Order
RefreshRates();
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+1000*Point,Bid-1000*Point,"Sell Order",9999,0,Green);
if(ticket<0)
{
Print("Sell Order failed with error #",GetLastError());
}
return;
}
BarsCount = Bars;
}
return(0);
}
//+------------------------------------------------------------------+[ATTACH]318015[/ATTACH]