I need a simple EA

[Deleted]

Hi everyone.

I want to create a simple EA that generates live trading signals based on the crossing of Parabolic SAR and the actual price. So basically any time the SAR goes under the price there will be a buy order and every time it goes above will be a sell order. there will be no set take profit or stop loss. When there is a buy position opened, for example, it will be closed when the SAR goes above the price and then of course the next sell order will be placed and so on.

Can anyone please help me with this? I know it is a simple EA but I am pretty new. I know how to technically create an EA, I just don't know the code for this one. ..

Thanks.

1759

This is a very basic example but gives you the idea - some people prefer to use the Pivot price instead of Highs & Lows of the Bars

NB The open/close should be by Magic Number, as written here, this will close all orders for the current symbol!

//+------------------------------------------------------------------+
//| Reversal 1.mq4 |
//| BD |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

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

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

int iPeriod=PERIOD_D1;
int iStopLoss=200, iTakeProfit=4000;
string strSymbol="GBPUSD";
int iMagicSell=5566;
string strAction;
int i;
static int iDay=0;
static int iYesterday=0;
int iTotal;

if(iBars(strSymbol,iPeriod)<4)
{
Print("Bars less than 4");
return(0);
}

iDay=TimeDayOfYear(TimeCurrent());

if (iDay == iYesterday) {return(0);} // Quit
else {iYesterday=iDay;} // Change value & proceed

iAction=SARResult(strSymbol, iPeriod);

if(iAction==1)
{

//Close any Sell orders this Expert opened
CloseAllSellOrders();

//Sleep(300000); //Execute once per 5 mins
}

if(iAction==2)
{
//Close any Buy orders this Expert opened

ticketSell=OrderSend(strSymbol,OP_SELL,1,Bid,5,Bid+iStopLoss*Point, Bid-iTakeProfit*Point, "BD sell order #2",iMagicSell,0,Red);
//Sleep(300000); //Execute once per 5 mins
}

//----
return(0);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| find trend change direction using PSAR |
//+------------------------------------------------------------------+
int SARResult(string strSymbol, int iPeriod)
{
/*
R4 = R3 + RANGE (same as: PP + RANGE * 3)
R3 = R2 + RANGE (same as: PP + RANGE * 2)
R2 = PP + RANGE
R1 = (2 * PP) - LOW
PP = (HIGH + LOW + CLOSE) / 3 or PP6=(H+L)/2
S1 = (2 * PP) - HIGH
S2 = PP - RANGE
S3 = S2 - RANGE (same as: PP - RANGE * 2)
S4 = S3 - RANGE (same as: PP - RANGE * 3)
*/
double dAccell=0.02, dMax=0.2;
int iDirection1, iDirection2; // 1=Up, 2=Down

if((iSAR(strSymbol,iPeriod,dAccell,dMax,2)<Low[2]) && (iSAR(strSymbol, iPeriod,dAccell,dMax,1)>Low[1])) //Downtrend
{return(2);}

if((iSAR(strSymbol,iPeriod,dAccell,dMax,2)>High[2]) && (iSAR(strSymbol, iPeriod,dAccell,dMax,1)<High[1])) //Uptrend
{return(1);}

return(0);
}

{
int i, iTotalOrders;

iTotalOrders=OrdersTotal();

for (i=0; i<iTotalOrders; i++)
{
{
if (OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(), OrderLots(), Bid, 5,Violet);

OrderDelete(OrderTicket());
}
}
}
}

void CloseAllSellOrders()
{
int i, iTotalOrders;

iTotalOrders=OrdersTotal();

for (i=0; i<iTotalOrders; i++)
{
{
if (OrderSymbol()==Symbol())
{
if (OrderType()==OP_SELL)
if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT )
OrderDelete(OrderTicket());
}
}
}
}

16

Look at my thread 'My first EA simple Parabolic SAR'

I am just starting out as well so good luck to you.
[Deleted]

I noticed this is for pivot points rather than SAR. The one I want to make is very simple - whenever there is a cross between SAR and the price it will open a buy or sell order and close the previous one. Shouldn't be a very simple code?

Thanks

BarrowBoy:

This is a very basic example but gives you the idea - some people prefer to use the Pivot price instead of Highs & Lows of the Bars

NB The open/close should be by Magic Number, as written here, this will close all orders for the current symbol!

//+------------------------------------------------------------------+
//| Reversal 1.mq4 |
//| BD |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

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

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

int iPeriod=PERIOD_D1;
int iStopLoss=200, iTakeProfit=4000;
string strSymbol="GBPUSD";
int iMagicSell=5566;
string strAction;
int i;
static int iDay=0;
static int iYesterday=0;
int iTotal;

if(iBars(strSymbol,iPeriod)<4)
{
Print("Bars less than 4");
return(0);
}

iDay=TimeDayOfYear(TimeCurrent());

if (iDay == iYesterday) {return(0);} // Quit
else {iYesterday=iDay;} // Change value & proceed

iAction=SARResult(strSymbol, iPeriod);

if(iAction==1)
{

//Close any Sell orders this Expert opened
CloseAllSellOrders();

//Sleep(300000); //Execute once per 5 mins
}

if(iAction==2)
{
//Close any Buy orders this Expert opened

ticketSell=OrderSend(strSymbol,OP_SELL,1,Bid,5,Bid+iStopLoss*Point, Bid-iTakeProfit*Point, "BD sell order #2",iMagicSell,0,Red);
//Sleep(300000); //Execute once per 5 mins
}

//----
return(0);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| find trend change direction using PSAR |
//+------------------------------------------------------------------+
int SARResult(string strSymbol, int iPeriod)
{
/*
R4 = R3 + RANGE (same as: PP + RANGE * 3)
R3 = R2 + RANGE (same as: PP + RANGE * 2)
R2 = PP + RANGE
R1 = (2 * PP) - LOW
PP = (HIGH + LOW + CLOSE) / 3 or PP6=(H+L)/2
S1 = (2 * PP) - HIGH
S2 = PP - RANGE
S3 = S2 - RANGE (same as: PP - RANGE * 2)
S4 = S3 - RANGE (same as: PP - RANGE * 3)
*/
double dAccell=0.02, dMax=0.2;
int iDirection1, iDirection2; // 1=Up, 2=Down

if((iSAR(strSymbol,iPeriod,dAccell,dMax,2)<Low[2]) && (iSAR(strSymbol, iPeriod,dAccell,dMax,1)>Low[1])) //Downtrend
{return(2);}

if((iSAR(strSymbol,iPeriod,dAccell,dMax,2)>High[2]) && (iSAR(strSymbol, iPeriod,dAccell,dMax,1)<High[1])) //Uptrend
{return(1);}

return(0);
}

{
int i, iTotalOrders;

iTotalOrders=OrdersTotal();

for (i=0; i<iTotalOrders; i++)
{
{
if (OrderSymbol()==Symbol())
{
OrderClose(OrderTicket(), OrderLots(), Bid, 5,Violet);

OrderDelete(OrderTicket());
}
}
}
}

void CloseAllSellOrders()
{
int i, iTotalOrders;

iTotalOrders=OrdersTotal();

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