BUY/SELL AT NEXT BAR IF PREVIOUS HAS CLOSED ABOVE/BELOW SMA

 

Hi,


As many of the people here I am new to MQL4. I am trying to make sense of it all and I have some questions. I am trying to develop a very simple EA for programm practice purposes but I can not figure out how to express the following thought


I want when the Fast SMA crosses the Slow SMA (AND CLOSES ABOVE/BELOW the SLOW SMA) to open buy/sell in the beginning of next bar ONLY (i.e bar responsible for bringing the FAST SMA to cross SLowSMA). Here is what i have patched together.


//---- input parameters
extern double TakeProfit=30.0;
extern double Lots=0.1;
extern double TrailingStop=0.0;
extern double StopLoss = 15;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}

bool NewBar()
{
static datetime lastbar = 0;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----\

//----
return(0);
}
int Crossed (double line1, double line2)
{
static int last_direction = 0;
static int current_direction = 0;
if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortSma, longLwma;
if (NewBar() == true)
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<1)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
shortSma = iMA(NULL,0,11,0,MODE_SMA,PRICE_CLOSE,0);
longLwma = iMA(NULL,0,55,0,MODE_LWMA,PRICE_CLOSE,0);
int isCrossed = Crossed (shortSma,longLwma);
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*MarketInfo(Symbol(),MODE_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)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid-StopLoss*MarketInfo(Symbol(),MODE_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);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position is opened
{
if(StopLoss>0)
if (StopLoss==Ask+StopLoss*Point)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||
(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,
OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+


I will greatly appreciate your help and advice.


Sonia

 

Try to put this simple code just after the start() function using prevtime like a global variable:


static int prevtime=0;


start()

{

.....

if(Time[0] == prevtime) return(0);
prevtime = Time[0];

......

}

 
giaras:

Try to put this simple code just after the start() function using prevtime like a global variable:


static int prevtime=0;


start()

{

.....

if(Time[0] == prevtime) return(0);
prevtime = Time[0];

......

}

Thank you for your help.

 
soniabvc wrote >>

Hi,

As many of the people here I am new to MQL4. I am trying to make sense of it all and I have some questions. I am trying to develop a very simple EA for programm practice purposes but I can not figure out how to express the following thought

I want when the Fast SMA crosses the Slow SMA (AND CLOSES ABOVE/BELOW the SLOW SMA) to open buy/sell in the beginning of next bar ONLY (i.e bar responsible for bringing the FAST SMA to cross SLowSMA). Here is what i have patched together.

//---- input parameters
extern double TakeProfit=30.0;
extern double Lots=0.1;
extern double TrailingStop=0.0;
extern double StopLoss = 15;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}

bool NewBar()
{
static datetime lastbar = 0;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----\

//----
return(0);
}
int Crossed (double line1, double line2)
{
static int last_direction = 0;
static int current_direction = 0;
if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortSma, longLwma;
if (NewBar() == true)
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<1)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
shortSma = iMA(NULL,0,11,0,MODE_SMA,PRICE_CLOSE,0);
longLwma = iMA(NULL,0,55,0,MODE_LWMA,PRICE_CLOSE,0);
int isCrossed = Crossed (shortSma,longLwma);
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*MarketInfo(Symbol(),MODE_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)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid-StopLoss*MarketInfo(Symbol(),MODE_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);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) // long position is opened
{
if(StopLoss>0)
if (StopLoss==Ask+StopLoss*Point)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||
(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,
OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+

I will greatly appreciate your help and advice.

Sonia

Hi Sonia, you are a good ea writer i am newbie and need your help with my ea codes if you have a chance drop me a email:franktradem@hotmail.com thank you