EA Based on Price Movement

 

Hi Guys,

Can someone please help me by making a simple EA...which places a BUY order few pips away from the current price.....e.g GBPUSD is 1.8000 what I want EA to do is place BUY order if price goes to 1.8100 and place SELL order if the price drops to 1.7900 with trailing stop.......i dont know what do you call this type of trading.....

Thanks in Advance

Babar

 
babarmughal:
Hi Guys,

Can someone please help me by making a simple EA...which places a BUY order few pips away from the current price.....e.g GBPUSD is 1.8000 what I want EA to do is place BUY order if price goes to 1.8100 and place SELL order if the price drops to 1.7900 with trailing stop.......i dont know what do you call this type of trading.....

Thanks in Advance

Babar

Babar,

It's call a "Pending Order"

Please use this function to open a pending order:

int OpenPendingOrder(int pType=OP_BUYLIMIT,double pLots=1,double pLevel=5,int sp=0, double sl=0,double tp=0,string pComment="",int pMagic=123,datetime pExpiration=0,color pColor=Yellow)

{

int ticket=0;

int err=0;

int c = 0;

switch (pType)

{

case OP_BUYLIMIT:

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_BUYLIMIT,pLots,Ask-pLevel*Point,sp,(Ask-pLevel*Point)-sl*Point,(Ask-pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

break;

}

}

}

break;

case OP_BUYSTOP:

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_BUYSTOP,pLots,Ask+pLevel*Point,sp,(Ask+pLevel*Point)-sl*Point,(Ask+pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

break;

}

}

}

break;

case OP_SELLLIMIT:

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_SELLLIMIT,pLots,Bid+pLevel*Point,sp,(Bid+pLevel*Point)+sl*Point,(Bid+pLevel*Point)-tp*Point,pComment,pMagic,pExpiration,pColor);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

break;

}

}

}

break;

case OP_SELLSTOP:

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_SELLSTOP,pLots,Bid-pLevel*Point,sp,(Bid-pLevel*Point)+sl*Point,(Bid-pLevel*Point)-tp*Point,pComment,pMagic,pExpiration,pColor);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

break;

}

}

}

break;

}

return(ticket);

}

 

Thanks CodersGuru,

now how can I get the coding for trailing stop.....can you please help me making this EA....

Thanks

Babar

 
babarmughal:
Hi Guys,

Can someone please help me by making a simple EA...which places a BUY order few pips away from the current price.....e.g GBPUSD is 1.8000 what I want EA to do is place BUY order if price goes to 1.8100 and place SELL order if the price drops to 1.7900 with trailing stop.......i dont know what do you call this type of trading.....

Thanks in Advance

Babar

Hi Babar, then what you do with the trade ? If price goes to 1.8100 and fill your BUY order, then move back to 1.8000, what to do ? If price retrace back all the way to fill the SELL order at 1.7900, what to do ? Hope you could give more entry rule / trading plan for this strategy. Just my two cents

 

Hi Dave,

Thanks for your reply.....well if the price hits 1.8100 then ofcourse there will be stop loss and trailing stop. and ake Profit will be say 50 pip......price reverses...and will be closed b stop loss...and vice versa for Sell...

Thanks

Babar

 

Hi Dave ,

Can you please help me in this EA.......Thanks

Babar

 

Come on guy ....can you please help me making this EA..........

Thanks in Advance

Babar

 

You might find this to be interesting:

http://www.metaquotes.ru/forum/6950/

It might be exactly what you're looking for...

I hope it helps!

 

CG - You Rule! Thanks for the Error Handling gem!

switch (pType)

{

case OP_BUYLIMIT:

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_BUYLIMIT,pLots,Ask-pLevel*Point,sp,(Ask-pLevel*Point)-sl*Point,(Ask-pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

break;

}

}

}

break;

case OP_BUYSTOP:

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_BUYSTOP,pLots,Ask+pLevel*Point,sp,(Ask+pLevel*Point)-sl*Point,(Ask+pLevel*Point)+tp*Point,pComment,pMagic,pExpiration,pColor);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

break;

}

}

}

break;

SUPER! THANK YOU!

 
Pecunia non olet:
You might find this to be interesting:

http://www.metaquotes.ru/forum/6950/

It might be exactly what you're looking for...

I hope it helps!

Cool Stuff

Thanks

Babar

 

help with LIMIT ORDERS embedded

,,dear friends i have this ea but as i tested it i observed

that the price retraces too much so i decided to put embedded

a LIMIT ORDER in "Order == SIGNAL_BUY" according what is in

this thread but so far it is not buying could someone correct

the code as i am just a beggineer,, i got the code for this ea from

EABuilder,,

i will appreciate someone can correct the code for the BUYLIMIT

i will procced accordingly with the SELLLIMIT,, i guess ther

problem is within the iteration,,

//+------------------------------------------------------------------+

//| This MQL is generated by Expert Advisor Builder |

//| http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/ |

//| |

//| In no event will author be liable for any damages whatsoever. |

//| Use at your own risk. |

//| |

//| Please do not remove this header. |

//+------------------------------------------------------------------+

#property copyright "Expert Advisor Builder"

#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = True;

extern double Lots = 0.1;

extern double pLevel = 5;

extern int Slippage = 1;

extern bool StopLossMode = True;

extern int StopLoss = 8;

extern bool TakeProfitMode = True;

extern int TakeProfit = 18;

extern bool TrailingStopMode = False;

extern int TrailingStop = 30;

extern string pComment="";

extern int pMagic=123;

extern datetime pExpiration=0;

extern color pColor=Yellow;

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

int BarCount;

int Current;

int err=0;

int c = 0;

extern int NumberofTries = 100;

bool TickCheck = False;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit() {

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

//+------------------------------------------------------------------+

//| Variable Begin |

//+------------------------------------------------------------------+

double Buy1_1 = iClose(NULL, 0, Current + 0);

double Buy1_2 = iCustom(NULL, 0, "StopReversalv2", 0, Current + 0);

double Buy2_1 = iClose(NULL, 0, Current + 1);

double Buy2_2 = iCustom(NULL, 0, "StopReversalv2", 0, Current + 1);

double Buy3_1 = iCustom(NULL, 0, "StopReversalv2", 0, Current + 1);

double Buy3_2 = 0;

double Buy4_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, Current + 0);

double Buy4_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, Current + 0);

double Buy5_1 = iRSI(NULL, PERIOD_M15, 14, PRICE_CLOSE, Current + 0);

double Buy5_2 = iRSI(NULL, 0, 14, PRICE_CLOSE, Current + 1);

double Buy6_1 = iStochastic(NULL, PERIOD_H4, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, Current + 0);

double Buy6_2 = iStochastic(NULL, PERIOD_H4, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, Current + 0);

double Sell1_1 = iClose(NULL, 0, Current + 0);

double Sell1_2 = iCustom(NULL, 0, "StopReversalv2", 0, Current + 0);

double Sell2_1 = iClose(NULL, 0, Current + 1);

double Sell2_2 = iCustom(NULL, 0, "StopReversalv2", 0, Current + 1);

double Sell3_1 = iCustom(NULL, 0, "StopReversalv2", 0, Current + 1);

double Sell3_2 = 0;

double Sell4_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, Current + 0);

double Sell4_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, Current + 0);

double Sell5_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, Current + 0);

double Sell5_2 = 0.25;

double Sell6_1 = iRSI(NULL, PERIOD_M15, 14, PRICE_CLOSE, Current + 0);

double Sell6_2 = iRSI(NULL, 0, 14, PRICE_CLOSE, Current + 1);

double Sell7_1 = iStochastic(NULL, PERIOD_H4, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, Current + 0);

double Sell7_2 = iStochastic(NULL, PERIOD_H4, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, Current + 0);

//+------------------------------------------------------------------+

//| Variable End |

//+------------------------------------------------------------------+

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Signal Begin(Entry) |

//+------------------------------------------------------------------+

if (Buy1_1 > Buy1_2 && Buy2_1 Buy4_2 && Buy5_1 > Buy5_2 && Buy6_1 > Buy6_2) Order = SIGNAL_BUY;

if (Sell1_1 Sell2_2 && Sell3_1 != Sell3_2 && Sell4_1 Sell5_2 && Sell6_1 < Sell6_2 && Sell7_1 < Sell7_2) Order = SIGNAL_SELL;

//+------------------------------------------------------------------+

//| Signal End |

//+------------------------------------------------------------------+

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

for(c = 0 ; c < NumberofTries ; c++)

{

if (StopLossMode) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUYLIMIT, Lots, Ask-pLevel*Point, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

Print("Error opening BUY order : ", GetLastError());

}

}

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

break;

}

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (StopLossMode) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (TakeProfitMode) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

//+------------------------------------------------------------------+

Thank You very much in advance,,

Mille

Reason: