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 05-27-2007, 06:59 PM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
MQL4 Guide

Hi,
I'm going to place here the various helpful MQL4 code.
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 05-27-2007, 07:00 PM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
OrdersCount function allows to get the orders count of predefined type.
Code:
int OrdersCount(int type)
{
  int orders = 0;
 
  int cnt = OrdersTotal();
  for (int i=0; i<cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
 
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
 
    if (OrderType() == type) orders++;
  }
 
  return (orders);
}
Example:
Code:
int start() 
{
  int BuyCnt = OrdersCount(OP_BUY);
  if (BuyCnt > 0) 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
  #3 (permalink)  
Old 05-27-2007, 07:01 PM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
How to close the all market orders:
Code:
int Slippage = 3;
 
void CloseOrders() 
{
  int cnt = OrdersTotal();
  for (int i=cnt-1; i>=0; i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
 
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
 
    if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage);
    if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage);
  }
}
How to close all the orders of predefined type:
Code:
void CloseOrders(int type) 
{
  int cnt = OrdersTotal();
  for (int i=cnt-1; i>=0; i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
 
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
    
    if (OrderType() != type) continue;
    
    if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage);
    if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage);
  }
}
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 05-27-2007, 07:02 PM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
GetLastOpenTime function gets OpenTime of the last order with predefined type.
The function makes a search of open trades and the history.
-1 means there are no orders found.

Code:
datetime GetLastOpenTime(int type) 
{
 
  datetime tm = -1;
 
  int cnt = OrdersTotal();
  for (int i=0; i<cnt; i++) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    
    //Optional
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
    
    if (OrderType() != type) continue;
    
    tm = MathMax(tm, OrderOpenTime());
  }
 
  cnt = OrdersHistoryTotal();
  for (i=0; i<cnt; i++) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
 
    //Optional
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
    
    if (OrderType() != type) continue;
    
    tm = MathMax(tm, OrderOpenTime());
  }
 
  return (tm);
}
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 05-27-2007, 07:20 PM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
Quote:
Originally Posted by trader101
This is a very informative thread..Please do not stop..Continue teaching us who are new to this programming...

How do i code this procedure???
1. I want to open 3 trades in 3 different chart only after checking that there is not trade open at the moment then ...
2. I want to check the PL and if it is greater then 0, it will close all open and pending orders.
3. Then I want to open the same 3 trades in the opposite directions.
Thanks
ok.

1. I want to open 3 trades in 3 different chart only after checking that there is not trade open at the moment then ...

3. Then I want to open the same 3 trades in the opposite directions.

Code:
  int Magic = ...

  int BuyCnt = 0;
  int SellCnt = 0;
  
  int cnt = OrdersTotal();
  for (int i=0; i < cnt; i++) 
{
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
  }
  
  if (BuyCnt > 0 || SellCnt > 0) return;

  //OrderSend(OP_BUY, ...
  //OrderSend(OP_SELL, ...
Run this code on 3 different charts you need.


2. I want to check the PL and if it is greater then 0, it will close all open and pending orders.

Code:
  if (AccountProfit() > 0) 
  {
    DeleteOrders();
    CloseOrders();
  }

void CloseOrders() 
{  
  int cnt = OrdersTotal();
  for (int i=cnt-1; i >= 0; i--) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    
    int type = OrderType();
    
    if (type == OP_BUY) 
    {
      RefreshRates();
      OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3);
    }
          
    if (type == OP_SELL) 
    {
      RefreshRates();
      OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3);
    }
  }
  
}

void DeleteOrders() 
{
  int cnt = OrdersTotal();
  for (int i=cnt-1; i >= 0; i--) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
        
    int type = OrderType();
    
    if (type == OP_BUYSTOP || type == OP_SELLSTOP || type == OP_BUYLIMIT || type == OP_SELLLIMIT) 
    {
      OrderDelete(OrderTicket());
    }
  }

}
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 05-29-2007, 01:58 PM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
NextDay function allows to get the date of next day.

Code:
void NextDay(int& day, int& month, int& year) 
{
  datetime Time0 = CurTime();
  datetime Tomorrow = Time0 + 24*60*60;
  
  day = TimeDayOfYear(Tomorrow);
  month = TimeMonth(Tomorrow);
  year = TimeYear(Tomorrow);
}
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 06-05-2007, 01:05 AM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
The following code allows to open a position at a preset time.

Code:
extern string OpenTime = "10:00-10:05; 12:20-12:31; 13:40-13:55";

void OpenPosition() 
{
  string OTA[];
  string OTI[];
  split(OTA, OpenTime, ";");
  
  datetime tm0 = CurTime();
  datetime tm1, tm2;
  
  bool cond = false;
  
  int cnt = ArraySize(OTA);
  for (int i=0; i < cnt; i++) {
    split(OTI, OTA[i], "-");
    if (ArraySize(OTI) != 2) continue;
    
    tm1 = StrToTime(TimeToStr(CurTime(), TIME_DATE) + " " + OTI[0]);
    tm2 = StrToTime(TimeToStr(CurTime(), TIME_DATE) + " " + OTI[1]);
   
    cond = cond || (tm1 <= tm0 && tm0 < tm2);
  }
  
        
  if (cond)
  {
    // Opening a position...
  }
}

void split(string& arr[], string str, string sym) 
{
  ArrayResize(arr, 0);
  string item;
  int pos, size;
  
  int len = StringLen(str);
  for (int i=0; i < len;) {
    pos = StringFind(str, sym, i);
    if (pos == -1) pos = len;
    
    item = StringSubstr(str, i, pos-i);
    item = StringTrimLeft(item);
    item = StringTrimRight(item);
    
    size = ArraySize(arr);
    ArrayResize(arr, size+1);
    arr[size] = item;
    
    i = pos+1;
  }
}
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 06-05-2007, 01:30 AM
Senior Member
 
Join Date: Jun 2006
Posts: 1,512
prasxz is on a distinguished road
hi

is there any mql script that can show daily , weekly and monthly range for one currency ?

Thx

===================
Forex Indicators Collection
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 06-05-2007, 01:42 AM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
Quote:
Originally Posted by prasxz
is there any mql script that can show daily , weekly and monthly range for one currency ?

Thx

===================
Forex Indicators Collection
Good idea. I will add the required code ASAP.
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 06-06-2007, 04:22 PM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 54
RickD is on a distinguished road
Quote:
Originally Posted by prasxz
is there any mql script that can show daily , weekly and monthly range for one currency ?

Thx

===================
Forex Indicators Collection
Code:
double dRange = GetRange(D'01.01.2004 00:00', PERIOD_D1);
double wRange = GetRange(D'01.01.2004 00:00', PERIOD_W1);
double mRange = GetRange(D'01.01.2004 00:00', PERIOD_MN);

double GetRange(datetime tm, int period)
{
  int bar = iBarShift(NULL, period, tm);
  double range = iHigh(NULL, period, bar) - iLow(NULL, period, bar);
  return (range);
}
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

Tags
mql4 guide, mql4 orderclose


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
Welcome to the MQL4 course codersguru Metatrader 4 mql 4 - Development course 124 10-12-2009 01:45 AM
Arrays in MQL4 clippertm Questions 5 08-11-2008 02:16 AM
Help for convert from VT to MQL4 M-E-C Expert Advisors - Metatrader 4 11 07-27-2007 07:53 PM
www.mql4.com DeSt Metatrader 4 18 02-02-2006 12:59 AM


All times are GMT. The time now is 07:06 AM.



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