Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
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():
PHP Code:
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);
}
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:
PHP Code:
int CBM(int intMagic)//CloseByMagic
{
int intOffset=0;
int Count = OTBM(intMagic);
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 Code:
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 Code:
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 Code:
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.
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 !!
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().
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.