2 SMA Crossover Help Needed Please

To add comments, please log in or register
Brendan Vogt
47
Brendan Vogt  
Hi,

I am new at writing an expert adviser, and I need help with my double crossover simple moving average expert adviser. My code is very basic. It sells/buys when the smaller moving average moves below/above the greater moving average.

What I am wanting to do is this.. An order is placed when the smaller moving average crosses above the greater moving average, so the EA buys. When the smaller moving average then crosses below the greater moving average I want to sell that order. This is all fine! And at the same time I want to place another/new order to buy/sell depending on the last order. I don't want to wait for the next crossover. So when one order is closed another one needs to be placed. Get it?

Please can someone help modify my existing code? Seeing that I am a newbie, can someone please comment on the code that was written?

I have attached my current file.

Regards,
Brendan
(South Africa)
Files:
Figlan
16
Figlan  
bioluminescence wrote >>
Hi,

I am new at writing an expert adviser, and I need help with my double crossover simple moving average expert adviser. My code is very basic. It sells/buys when the smaller moving average moves below/above the greater moving average.

What I am wanting to do is this.. An order is placed when the smaller moving average crosses above the greater moving average, so the EA buys. When the smaller moving average then crosses below the greater moving average I want to sell that order. This is all fine! And at the same time I want to place another/new order to buy/sell depending on the last order. I don't want to wait for the next crossover. So when one order is closed another one needs to be placed. Get it?

Please can someone help modify my existing code? Seeing that I am a newbie, can someone please comment on the code that was written?

I have attached my current file.

Regards,
Brendan
(South Africa)

Hi Brendan,

Your files are not attached. Can you try attaching the mq4 file?

blogzr3
505
blogzr3  

Just a side comment that the crosses of MA's are not the best time to buy/sell. By the time the crosses happen, the change of direction is already well in progress, so the buy/sell may be a bit late.


The MACD which tracks the gap differences between 2 MA's is a better indicator of change of direction. The histogram tracks the direction, the signal line indicates whether the direction is up or down.


That's the theory anyway...

Brendan Vogt
47
Brendan Vogt  

Hey blogzr3,

So you think I must scratch the 2 MA crossover and write a new expert system with a MACD? Have you maybe got a working example of this with you please?

Here is my 2 SMA crossover:

//+------------------------------------------------------------------+
//| 2SMA_EA_H1.mq4 |
//| Brendan Vogt |
//+------------------------------------------------------------------+
#property copyright "Brendan Vogt"

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

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}

int Crossed(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()
{
//----

int cnt, ticket, total;
double shortSma, longSma;

if(Bars < 100)
{
Print("bars less than 100");
return(0);
}

if(TakeProfit < 10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}

shortSma = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 0);
longSma = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);

int isCrossed = Crossed(shortSma, longSma);

total = OrdersTotal();

if(total < 1)
{
if(isCrossed == 1)
{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, (Ask + TakeProfit * Point), "Double SMA Crossover H1", 12345, 0, Blue);
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, 0, (Bid - TakeProfit * Point), "Double SMA Crossover H1", 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
{
// should it be closed?
if(isCrossed == 2)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet); // close position
return(0); // exit
}
// 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
{
// should it be closed?
if(isCrossed == 1)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet); // close position
return(0); // exit
}
// 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);
}
//+------------------------------------------------------------------+

Regards

Brendan

blogzr3
505
blogzr3  

See the MACD sample EA included with the MT4 installation, and the article available here https://www.mql5.com/en/articles/1510.


Probably not going to make you rich overnight, but they should be good starting points. :)

Figlan
16
Figlan  
bioluminescence wrote >>

Hey blogzr3,

So you think I must scratch the 2 MA crossover and write a new expert system with a MACD? Have you maybe got a working example of this with you please?

Here is my 2 SMA crossover:

//+------------------------------------------------------------------+
//| 2SMA_EA_H1.mq4 |
//| Brendan Vogt |
//+------------------------------------------------------------------+
#property copyright "Brendan Vogt"

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

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}

int Crossed(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()
{
//----

int cnt, ticket, total;
double shortSma, longSma;

if(Bars < 100)
{
Print("bars less than 100");
return(0);
}

if(TakeProfit < 10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}

shortSma = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 0);
longSma = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);

int isCrossed = Crossed(shortSma, longSma);

total = OrdersTotal();

if(total < 1)
{
if(isCrossed == 1)
{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, (Ask + TakeProfit * Point), "Double SMA Crossover H1", 12345, 0, Blue);
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, 0, (Bid - TakeProfit * Point), "Double SMA Crossover H1", 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
{
// should it be closed?
if(isCrossed == 2)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet); // close position
return(0); // exit
}
// 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
{
// should it be closed?
if(isCrossed == 1)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet); // close position
return(0); // exit
}
// 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);
}
//+------------------------------------------------------------------+

Regards

Brendan

Figlan
16
Figlan  
figlan1 wrote >>

I agree with blogzr that an MA crossover is not the best way to go. I Have tested your algorithm and I think I see what you are trying to achieve. Since you are new to EA's; I see the potential of your 50MA. I would suggest that rather than a 2MA crossover, try a Price-50MA crossover. The Moving Average sample EA included with the MT4 installation is an example of this.

Good Luck

Brendan Vogt
47
Brendan Vogt  
figlan1:

I agree with blogzr that an MA crossover is not the best way to go. I Have tested your algorithm and I think I see what you are trying to achieve. Since you are new to EA's; I see the potential of your 50MA. I would suggest that rather than a 2MA crossover, try a Price-50MA crossover. The Moving Average sample EA included with the MT4 installation is an example of this.

Good Luck

The The Moving Average sample EA is this correct? I've read that it has bugs?

To add comments, please log in or register