Collection of useful MT4 functions for coding

 

Hello all,

I thought it would be cool if there was a thread that shared useful functions people have coded.

I find it can be much easier coding EA's if you have a useful toolbox of coded functions to work from. I'm wanting to share some functions I have coded and found useful, and am hoping others will share some of their created functions. The hope is that we can all share a thread containing some of the functions we find most useful. I'm sure we could all learn from, or help each other.

I like to keep track or orders through the use of magic numbers, as I'm sure a lot of you do. The following functions are what I use in some of my EA's.

First, I like to know how many orders I have open with a particular magic number. The function I use for this is OTBM():

int OTBM(int intMagic)//OrdersTotalByMagic

{

int intCount=0;

int intPOS=0;

bool boolTerm=false;

while(boolTerm==false)

{

if(OrderSelect(intPOS,SELECT_BY_POS))

{

if(OrderMagicNumber()==intMagic) intCount++;

intPOS++;

}

else

boolTerm=true;

}

return(intCount);

}[/PHP]

If you have more than one order open with different magic numbers, and you want to close only orders with a specific magic number, I like to use this function:

int CBM(int intMagic)//CloseByMagic

{

int intOffset=0;

int Count = OTBM(intMagic);

while(OTBM(intMagic)>0 && Count > 0)

{

OrderSelect(intOffset,SELECT_BY_POS);

if(OrderMagicNumber()==intMagic)

{

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);

else if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);

Count--;

}

else {

intOffset++;

}

}

return(0);

}[/PHP]

If you want to know the order profit by magic:

double OPBM(int intMagic)//OrderProfitByMagic

{

double dblProfit=0;

int intPOS=0;

bool boolTerm=false;

while(boolTerm==false)

{

if(OrderSelect(intPOS,SELECT_BY_POS))

{

if(OrderMagicNumber()==intMagic) dblProfit=dblProfit+OrderProfit();

intPOS++;

}

else

boolTerm=true;

}

return(dblProfit);

}

[/PHP]

The total long orders open by magic number:

int OTBML(int intMagic)//OrdersTotalByMagicLong

{

int TotalLong=0;

int POS=0;

bool Term=false;

while(Term==false)

{

if(OrderSelect(POS,SELECT_BY_POS))

{

if((OrderMagicNumber()==intMagic) && (OrderType()==OP_BUY))TotalLong=TotalLong+1;

POS++;

}

else

Term=true;

}

return(TotalLong);

}

The total short orders open by magic number:

[PHP]int OTBMS(int intMagic)//OrdersTotalByMagicShort

{

int TotalShort=0;

int POS=0;

bool Term=false;

while(Term==false)

{

if(OrderSelect(POS,SELECT_BY_POS))

{

if((OrderMagicNumber()==intMagic) && (OrderType()==OP_SELL))TotalShort=TotalShort+1;

POS++;

}

else

Term=true;

}

return(TotalShort);

}

Total number of open losing orders by magic:

[PHP]int OTLoser(int intMagic)//OrdersTotalLoser

{

int TotalLoser=0;

int POS=0;

bool Term=false;

while(Term==false)

{

if(OrderSelect(POS,SELECT_BY_POS))

{

if((OrderMagicNumber()==intMagic) && (OrderProfit() <= 0))TotalLoser=TotalLoser+1;

POS++;

}

else

Term=true;

}

return(TotalLoser);

}

Total number of open winning orders by magic:

[PHP]int OTWinner(int intMagic)//OrdersTotalWinner

{

int TotalWinner=0;

int POS=0;

bool Term=false;

while(Term==false)

{

if(OrderSelect(POS,SELECT_BY_POS))

{

if((OrderMagicNumber()==intMagic) && (OrderProfit() > 0))TotalWinner=TotalWinner+1;

POS++;

}

else

Term=true;

}

return(TotalWinner);

}

These are a few of the functions I have used in the aid of coding EA's. Please feel free to share some of your useful functions, or improve upon posted functions.

-wolfe

 

Such good idea! and thanks for share.

 
Linuxser:
Such good idea! and thanks for share.

Thanks Linuxser,

I hope others can share some of their favorite coded functions.

 

Very very nice thread...compliments wolfe !

i would post something but i'm only a beginner coder so

 

Here are a few more that may be useful:

To close all losing trades with specific magic number:

int CBMLoser(int intMagic)//CloseByMagicLosers

{

int intOffset=0;

int Count = OTLoser(intMagic);

while(OTLoser(intMagic)>0 && Count > 0)

{

OrderSelect(intOffset,SELECT_BY_POS);

if(OrderMagicNumber()==intMagic)

{

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);

else if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);

Count--;

}

else {

intOffset++;

}

}

return(0);

}[/PHP]

To close all winning trades with specific magic number:

int CBMWinner(int intMagic)//CloseByMagicWinner

{

int intOffset=0;

int Count = OTWinner(intMagic);

while(OTWinner(intMagic) > 0 && Count > 0)

{

OrderSelect(intOffset,SELECT_BY_POS);

if(OrderMagicNumber()==intMagic)

{

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);

else if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);

Count--;

}

else {

intOffset++;

}

}

return(0);

}

To close EVERYTHING:

[PHP]int CA()//Close All

{

while(OrdersTotal()>0)

{

OrderSelect(0,SELECT_BY_POS);

if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);

if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);

if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());

}

return(0);

}

 

Thanks and here's a challenge....Without any Magic number....I want to close the OLDEST order..... to free up margin....

The reason I want this is if a currency is moving against us and

if (AccountFreeMargin() < AccountMargin())

{

Close the Oldest Buy or Oldest sell

}

Simple code is better....so that just one buy and or sell closes....but a loop

checking to see if AccountFreeMargin() is more than used (AccountMargin()

would be a nice learning tool..... I'm STUMPED....!!

I can easily close the newest orders..... but that isn't what I want!!

Thanks !!

 
N8937G:
Thanks and here's a challenge....Without any Magic number....I want to close the OLDEST order..... to free up margin....

The reason I want this is if a currency is moving against us and

if (AccountFreeMargin() < AccountMargin())

{

Close the Oldest Buy or Oldest sell

}

Simple code is better....so that just one buy and or sell closes....but a loop

checking to see if AccountFreeMargin() is more than used (AccountMargin()

would be a nice learning tool..... I'm STUMPED....!!

I can easily close the newest orders..... but that isn't what I want!!

Thanks !!

This stumped me for a little bit, but it turns out to be quite simple.

When you call OrderSelect() and SELECT_BY_POS, position 0 is the oldest open ticket. All you have to do is select position 0, determine if you have a Buy or a Sell open, and then close the OrderTicket().

Here is a function to do this:

void CloseOldestOrder()

{

if(OrderSelect(0,SELECT_BY_POS))

{

if(OrderType()==OP_BUY)

{

OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);

}

if(OrderType()==OP_SELL)

{

OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);

}

}

}

I tested by manually opening several orders in a demo account, then attached an EA with the function call in the Init() section of the EA. It closed the oldest open order every time.

To use, make sure you have the function "locked out" in your code until you want it executed, because EVERY time it's called it will close the oldest open order.

 

Thanks Wolfe

Many thanks to you Wolfe for taking the time and effort to provide such useful and helpful information. Very much appreciated.

 

Thanks!! Yes it is sooo simple...Sure beats looping by Order time... Ticket number etc etc etc....THANKS!!

 
N8937G:
Thanks!! Yes it is sooo simple...Sure beats looping by Order time... Ticket number etc etc etc....THANKS!!

Glad I could help.

 

my donation LabelMaker and LineMaker

void LineMaker(string name,color LineColor,double price, string Description){

ObjectCreate(name, OBJ_HLINE, 0,TimeCurrent(),price);

ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);

ObjectSet(name, OBJPROP_COLOR, LineColor);

ObjectSet(name, OBJPROP_BACK, true);

ObjectSetText(name,Description,8,"Arial",LineColor);

return(0);

}

void LabelMaker(string lblname,int x,int y,string window,string txt,int size,string font,color txtcolor){

ObjectCreate(lblname, OBJ_LABEL, WindowFind(window), 0, 0);

ObjectSet(lblname, OBJPROP_CORNER, 0);

ObjectSet(lblname, OBJPROP_XDISTANCE, x);

ObjectSet(lblname, OBJPROP_YDISTANCE, y);

ObjectSetText(lblname,txt,size,font, txtcolor);

return(0);

}