Forex



Go Back   Forex Trading > Programming > MetaTrader
Forex Forum Register More recent Blogs Calendar Advertising Others Help






Register
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.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 10-09-2009, 01:29 AM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 818
wolfe is on a distinguished road
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():

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()==intMagicintCount++;
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);

while(
OTBM(intMagic)>&& Count 0)
{
OrderSelect(intOffset,SELECT_BY_POS);
if(
OrderMagicNumber()==intMagic)
{
if(
OrderType()==OP_BUYOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
else if(
OrderType()==OP_SELLOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
Count--;
}
else {
intOffset++;
}
}
return(
0);

If you want to know the order profit by magic:

PHP Code:
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()==intMagicdblProfit=dblProfit+OrderProfit();
intPOS++;
}
else
boolTerm=true;
}
return(
dblProfit);

The total long orders open by magic number:

PHP Code:
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.

-wolfe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 10-09-2009, 04:05 AM
Linuxser's Avatar
User Root
 
Join Date: May 2006
Location: Helliconia (Winter)
Posts: 4,410
Blog Entries: 56
Linuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond reputeLinuxser has a reputation beyond repute
Such good idea! and thanks for share.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 10-09-2009, 05:03 AM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 818
wolfe is on a distinguished road
Quote:
Originally Posted by Linuxser View Post
Such good idea! and thanks for share.
Thanks Linuxser,

I hope others can share some of their favorite coded functions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 10-19-2009, 11:11 AM
dr.house7's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Italy
Posts: 186
dr.house7 is on a distinguished road
Thumbs up

Very very nice thread...compliments wolfe !

i would post something but i'm only a beginner coder so
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 10-20-2009, 02:50 AM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 818
wolfe is on a distinguished road
Here are a few more that may be useful:

To close all losing trades with specific magic number:

PHP Code:
int CBMLoser(int intMagic)//CloseByMagicLosers
{
int intOffset=0;
int Count OTLoser(intMagic);

while(
OTLoser(intMagic)>&& Count 0)
{
OrderSelect(intOffset,SELECT_BY_POS);
if(
OrderMagicNumber()==intMagic)
{
if(
OrderType()==OP_BUYOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
else if(
OrderType()==OP_SELLOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
Count--;
}
else {
intOffset++;
}
}
return(
0);

To close all winning trades with specific magic number:

PHP Code:
int CBMWinner(int intMagic)//CloseByMagicWinner
{
int intOffset=0;
int Count OTWinner(intMagic);

while(
OTWinner(intMagic) > && Count 0)
{
OrderSelect(intOffset,SELECT_BY_POS);
if(
OrderMagicNumber()==intMagic)
{
if(
OrderType()==OP_BUYOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
else if(
OrderType()==OP_SELLOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
Count--;
}
else {
intOffset++;
}
}
return(
0);

To close EVERYTHING:

PHP Code:
int CA()//Close All
{
 while(
OrdersTotal()>0)
 {
  
OrderSelect(0,SELECT_BY_POS);
  if(
OrderType()==OP_BUYOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
  if(
OrderType()==OP_SELLOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
  if(
OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMITOrderDelete(OrderTicket());
  }
 return(
0);

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #6 (permalink)  
Old 10-29-2009, 12:23 AM
Junior Member
 
Join Date: Oct 2008
Posts: 9
N8937G is on a distinguished road
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 !!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #7 (permalink)  
Old 10-29-2009, 03:34 AM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 818
wolfe is on a distinguished road
Quote:
Originally Posted by N8937G View Post
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:

PHP Code:
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #8 (permalink)  
Old 10-29-2009, 04:22 AM
Junior Member
 
Join Date: Jan 2009
Posts: 14
solfx is on a distinguished road
Thanks Wolfe

Many thanks to you Wolfe for taking the time and effort to provide such useful and helpful information. Very much appreciated.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #9 (permalink)  
Old 10-30-2009, 12:44 AM
Junior Member
 
Join Date: Oct 2008
Posts: 9
N8937G is on a distinguished road
Thanks!! Yes it is sooo simple...Sure beats looping by Order time... Ticket number etc etc etc....THANKS!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #10 (permalink)  
Old 10-30-2009, 12:51 AM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 818
wolfe is on a distinguished road
Quote:
Originally Posted by N8937G View Post
Thanks!! Yes it is sooo simple...Sure beats looping by Order time... Ticket number etc etc etc....THANKS!!
Glad I could help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Idea about new order functions fxtrader625 Metatrader 4 7 06-28-2007 12:00 AM
a collection of indicators tokyowalker Indicators - Metatrader 4 3 08-30-2006 04:03 PM
File Functions intelligent_14 Questions 6 07-11-2006 09:37 PM
Unusual Functions intelligent_14 Questions 3 06-24-2006 12:20 AM
Lesson 7 - Functions codersguru Lessons 2 11-02-2005 04:32 PM


All times are GMT. The time now is 02:46 PM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.