Using Custom Indicator in EA (MT4) - page 3

 

Do you have ate least a "frame" for such an EA or that is just an idea?

nduru22:
Hi people, im trying to limit the number of trades my e.a can get to with respect to pips, such that if the market moves a number of pips, another order is opened irrespective of the direction. can anyone help me???
 

well i just have a basic e.a that is buying and selling and is looking at the macd lines to determine the entries and exits. but it gets into so many trades. so id like to limit the number of trades it executes but with respect to movement in pips. that is what i have.

 

thanks mladen,

let me try it out and il give you feed back.

thanks

mladen:
This function will return you the price of the last order (opened or closed, does not matter). If it returns 0, it means there were no orders at all before. If you will to test only currently opened orders, remove the second loop. Once when you have the last open price it is nor difficult to calculate the difference between current price and that price and decide if you have the criteria to enter on not
double lastOrderPrice(int magicNumber=0)

{

datetime lastTime = 0;

double lastPrice = 0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_TRADES)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;

lastTime = OrderOpenTime();

lastPrice = OrderOpenPrice();

}

// if you do not want to test already closed orders

// delete the next block of code

if (lastPrice==0)

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastPrice = OrderOpenPrice();

}

return(lastPrice);

}

 

This function will return you the price of the last order (opened or closed, does not matter). If it returns 0, it means there were no orders at all before. If you will to test only currently opened orders, remove the second loop. Once when you have the last open price it is nor difficult to calculate the difference between current price and that price and decide if you have the criteria to enter on not

double lastOrderPrice(int magicNumber=0)

{

datetime lastTime = 0;

double lastPrice = 0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_TRADES)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;

lastTime = OrderOpenTime();

lastPrice = OrderOpenPrice();

}

// if you do not want to test already closed orders

// delete the next block of code

if (lastPrice==0)

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastPrice = OrderOpenPrice();

}

return(lastPrice);

}

 

Hi Mladen,

im now placing the code that you gave me into my e.a Im fairly new to mql4 programming so i hope you dont mind me asking afew questions about the code, just so that I can really understand it.

why do you say,

if (OrderMagicNumber() !=magicNumber) continue;

if (OrderSymbol() !=Symbol()) continue;shouldnt the magicNumber be equal to the OrderMagicNumber()? or the symbol() shouldnt it be similar to the symbol of the currency pair?

I just want to understand why they shouldnt be equal

thank you

mladen:
This function will return you the price of the last order (opened or closed, does not matter). If it returns 0, it means there were no orders at all before. If you will to test only currently opened orders, remove the second loop. Once when you have the last open price it is nor difficult to calculate the difference between current price and that price and decide if you have the criteria to enter on not
double lastOrderPrice(int magicNumber=0)

{

datetime lastTime = 0;

double lastPrice = 0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_TRADES)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;

lastTime = OrderOpenTime();

lastPrice = OrderOpenPrice();

}

// if you do not want to test already closed orders

// delete the next block of code

if (lastPrice==0)

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastPrice = OrderOpenPrice();

}

return(lastPrice);

}

 

nduru22

Yes, they should be the same with an addition : if you omit magic number in a call to a function then magic number is not checked

nduru22:
Hi Mladen,

im now placing the code that you gave me into my e.a Im fairly new to mql4 programming so i hope you dont mind me asking afew questions about the code, just so that I can really understand it.

why do you say,

if (OrderMagicNumber() !=magicNumber) continue;

if (OrderSymbol() !=Symbol()) continue;shouldnt the magicNumber be equal to the OrderMagicNumber()? or the symbol() shouldnt it be similar to the symbol of the currency pair?

I just want to understand why they shouldnt be equal

thank you
 

still not getting a break with the code: here is my full program, where do you think im going wrong?

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

//| myea2.mq4 |

//| peter

//| peter_nduru@yahoo.co.uk |

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

#property copyright "Copyright 2013, MetaQuotes Software Corp."

#property link "http://www.metaquotes.net"

//--- input parameters

extern double takeprofit=500.0;

extern double lots=1.0;

extern double stoploss=1000.0;

extern double thtpnt=800.0;

extern double close1=0.8;

extern double close2=0.8;

extern double pips = 400.0;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

int crossed (double line1, double line2)

{

static int currdir = 0;

static int lastdir = 0;

if (line1>line2)

currdir = 1;

if (line1<line2)

currdir = 2;

if (currdir != lastdir)

{

lastdir = currdir;

return (lastdir);

}

else

return (0);

}

double lastOrderPrice(int magicNumber=0)

{

datetime lastTime = 0;

double lastPrice = 0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_TRADES)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;

lastTime = OrderOpenTime();

lastPrice = OrderOpenPrice();

}

return(lastPrice);

}

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

//| expert start function |

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

int start()

{

int mn; //for the magic number in the lastOrderPrice() function

double reslt; //the result of the function LastOrderPrice()

int total,cnt, ticket;

double main = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0);

double sig = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL,0);

if (Bars < 100)

{

Print("Bars are less than 100");

return (0);

}

if (takeprofit < 10)

{

Print ("Take profit is less than 10");

return (0);

}

int crossd = crossed (main, sig);

reslt = lastOrderPrice (mn);

total = OrdersTotal();

if (crossd==1)

{

if (reslt == 0)

{

ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask-stoploss*Point, Ask+takeprofit*Point, "This is a buy", 12345, 0, Green);

if(ticket>0)

{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

Print("Your order", OrderType() ,"has been executed at", OrderOpenPrice());

}

else

Print("Error Opening buy Order", OrderType());

}

if (reslt != 0)

{

if (Ask >= OrderOpenPrice() + pips*Point)

{

ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask-stoploss*Point, Ask+takeprofit*Point, "This is a buy", 12345, 0, Green);

if(ticket>0)

{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

Print("Your order has been executed at after 40 pips at ",OrderOpenPrice());

}

else

Print("Error Opening buy Order ", OrderType()," after 40 pips");

}

}

}

if (crossd==2)

{

if (reslt == 0)

{

ticket=OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Bid+stoploss*Point, Bid-takeprofit*Point, "This is a sell", 12345,0, Red);

if(ticket>0)

{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

Print("Your order has been executed at ", OrderOpenPrice());

}

else

Print("Error opening Sell order ", GetLastError());

}

if (reslt != 0)

{

if (Bid <= OrderOpenPrice() - pips *Point)

{

ticket=OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Bid+stoploss*Point, Bid-takeprofit*Point, "This is a sell", 12345,0, Red);

if(ticket>0)

{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

Print("Your order has been executed after 40 pips at ", OrderOpenPrice());

}

else

Print("Error opening Sell order after 40 pips ",GetLastError());

}

}

}

for (cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if (OrderType()==OP_BUY )

{

if (crossd==2)

{

OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet);

}

}

}

}

for (cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if (OrderType()==OP_SELL )

{

if (crossd==1)

{

OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet);

}

}

}

}

for (cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if (OrderType()==OP_BUY )

{

if (Bid < OrderOpenPrice() - thtpnt * Point)

{

OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet);

}

}

}

}

for (cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if (OrderType()==OP_SELL )

{

if (Ask > OrderOpenPrice() + thtpnt * Point)

{

OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet);

}

}

}

}

return(0);

}

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

mladen:
nduru22 Yes, they should be the same with an addition : if you omit magic number in a call to a function then magic number is not checked
 

mladen,

thanks alot for the help. Iv struggled with it until it has paid off. im now able to control the entries and exits of the system.

nduru22:
still not getting a break with the code: here is my full program, where do you think im going wrong?

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

//| myea2.mq4 |

//| peter

//| peter_nduru@yahoo.co.uk |

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

#property copyright "Copyright 2013, MetaQuotes Software Corp."

#property link "http://www.metaquotes.net"

//--- input parameters

extern double takeprofit=500.0;

extern double lots=1.0;

extern double stoploss=1000.0;

extern double thtpnt=800.0;

extern double close1=0.8;

extern double close2=0.8;

extern double pips = 400.0;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

int crossed (double line1, double line2)

{

static int currdir = 0;

static int lastdir = 0;

if (line1>line2)

currdir = 1;

if (line1<line2)

currdir = 2;

if (currdir != lastdir)

{

lastdir = currdir;

return (lastdir);

}

else

return (0);

}

double lastOrderPrice(int magicNumber=0)

{

datetime lastTime = 0;

double lastPrice = 0;

for(int i=OrdersTotal()-1; i>=0; i--)

{

if (OrderSelect(i,SELECT_BY_POS, MODE_TRADES)==false) break;

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;

lastTime = OrderOpenTime();

lastPrice = OrderOpenPrice();

}

return(lastPrice);

}

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

//| expert start function |

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

int start()

{

int mn; //for the magic number in the lastOrderPrice() function

double reslt; //the result of the function LastOrderPrice()

int total,cnt, ticket;

double main = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0);

double sig = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL,0);

if (Bars < 100)

{

Print("Bars are less than 100");

return (0);

}

if (takeprofit < 10)

{

Print ("Take profit is less than 10");

return (0);

}

int crossd = crossed (main, sig);

reslt = lastOrderPrice (mn);

total = OrdersTotal();

if (crossd==1)

{

if (reslt == 0)

{

ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask-stoploss*Point, Ask+takeprofit*Point, "This is a buy", 12345, 0, Green);

if(ticket>0)

{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

Print("Your order", OrderType() ,"has been executed at", OrderOpenPrice());

}

else

Print("Error Opening buy Order", OrderType());

}

if (reslt != 0)

{

if (Ask >= OrderOpenPrice() + pips*Point)

{

ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask-stoploss*Point, Ask+takeprofit*Point, "This is a buy", 12345, 0, Green);

if(ticket>0)

{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

Print("Your order has been executed at after 40 pips at ",OrderOpenPrice());

}

else

Print("Error Opening buy Order ", OrderType()," after 40 pips");

}

}

}

if (crossd==2)

{

if (reslt == 0)

{

ticket=OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Bid+stoploss*Point, Bid-takeprofit*Point, "This is a sell", 12345,0, Red);

if(ticket>0)

{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

Print("Your order has been executed at ", OrderOpenPrice());

}

else

Print("Error opening Sell order ", GetLastError());

}

if (reslt != 0)

{

if (Bid <= OrderOpenPrice() - pips *Point)

{

ticket=OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Bid+stoploss*Point, Bid-takeprofit*Point, "This is a sell", 12345,0, Red);

if(ticket>0)

{

if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

Print("Your order has been executed after 40 pips at ", OrderOpenPrice());

}

else

Print("Error opening Sell order after 40 pips ",GetLastError());

}

}

}

for (cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if (OrderType()==OP_BUY )

{

if (crossd==2)

{

OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet);

}

}

}

}

for (cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if (OrderType()==OP_SELL )

{

if (crossd==1)

{

OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet);

}

}

}

}

for (cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if (OrderType()==OP_BUY )

{

if (Bid < OrderOpenPrice() - thtpnt * Point)

{

OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet);

}

}

}

}

for (cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if (OrderType()==OP_SELL )

{

if (Ask > OrderOpenPrice() + thtpnt * Point)

{

OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet);

}

}

}

}

return(0);

}

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

hello mladen,

id like some help with the logic part of my e.a. Im trying to modify the stoploss and take profit of any order whose position is lower than the current order to but the same as the ones on the current order.

would you please help me? I think that the place i am going wrong is the for statement.

the function you gave me last had

for (int i = OrdersTotal-1; i>=0; i --): this chooses the last order

now im thinking of choosing all orders from 0 to OrdersTotal-1

mladen:
nduru22 Yes, they should be the same with an addition : if you omit magic number in a call to a function then magic number is not checked
 

nduru22

Actual line was for (int i = OrdersTotal()-1; i>=0; i --) and it is apart of a loop that goes through all the currently opened orders and decides what to do with each selected order. You can invert the looping direction but the way it is it is covering the case even when you close orders (otherwise it would skip some orders) and I recommend that you use that form.

metaquotes states that orders are not sorted by any criteria (they do not guarantee any sort order) and you have to go through the whole list of orders regardless of what you are doing, so you might do it the way thet will prevent errors in some cases

nduru22:
hello mladen,

id like some help with the logic part of my e.a. Im trying to modify the stoploss and take profit of any order whose position is lower than the current order to but the same as the ones on the current order.

would you please help me? I think that the place i am going wrong is the for statement.

the function you gave me last had

for (int i = OrdersTotal-1; i>=0; i --): this chooses the last order

now im thinking of choosing all orders from 0 to OrdersTotal-1