I need a simple EA

To add comments, please log in or register
[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.

Brian Dee - Random Trader
1759
Brian Dee - Random Trader  

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/ |
//+------------------------------------------------------------------+
#property copyright "BD"
#property link "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";
double iAction=0; // 1=Buy, 2=Sell
int iMagicBuy=2233;
int iMagicSell=5566;
string strAction;
int i;
static int iDay=0;
static int iYesterday=0;
int iTotal;
double ticketBuy, ticketSell;


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();

ticketBuy=OrderSend(strSymbol,OP_BUY,1,Ask,5,Ask-iStopLoss*Point, Ask+iTakeProfit*Point, "BD buy order #2",iMagicBuy,0,Green);
//Sleep(300000); //Execute once per 5 mins
}


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

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);
}


void CloseAllBuyOrders()
{
int i, iTotalOrders;

iTotalOrders=OrdersTotal();

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

if (OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
OrderDelete(OrderTicket());
}
}
}
}

void CloseAllSellOrders()
{
int i, iTotalOrders;

iTotalOrders=OrdersTotal();

for (i=0; i<iTotalOrders; i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol())
{
if (OrderType()==OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 5,Violet);
if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT )
OrderDelete(OrderTicket());
}
}
}
}

Chris Hinton
16
Chris Hinton  
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/ |
//+------------------------------------------------------------------+
#property copyright "BD"
#property link "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";
double iAction=0; // 1=Buy, 2=Sell
int iMagicBuy=2233;
int iMagicSell=5566;
string strAction;
int i;
static int iDay=0;
static int iYesterday=0;
int iTotal;
double ticketBuy, ticketSell;


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();

ticketBuy=OrderSend(strSymbol,OP_BUY,1,Ask,5,Ask-iStopLoss*Point, Ask+iTakeProfit*Point, "BD buy order #2",iMagicBuy,0,Green);
//Sleep(300000); //Execute once per 5 mins
}


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

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);
}


void CloseAllBuyOrders()
{
int i, iTotalOrders;

iTotalOrders=OrdersTotal();

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

if (OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
OrderDelete(OrderTicket());
}
}
}
}

void CloseAllSellOrders()
{
int i, iTotalOrders;

iTotalOrders=OrdersTotal();

for (i=0; i<iTotalOrders; i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol())
{
if (OrderType()==OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 5,Violet);
if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT )
OrderDelete(OrderTicket());
}
}
}
}

To add comments, please log in or register