Forex
Google

Go Back   Forex Trading > Programming > Metatrader Programming
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 05-27-2007, 05:59 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
  #2 (permalink)  
Old 05-27-2007, 06:00 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
  #3 (permalink)  
Old 05-27-2007, 06:01 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
  #4 (permalink)  
Old 05-27-2007, 06:02 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
  #5 (permalink)  
Old 05-27-2007, 06:20 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
  #6 (permalink)  
Old 05-29-2007, 12:58 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
  #7 (permalink)  
Old 06-05-2007, 12:05 AM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
  #8 (permalink)  
Old 06-05-2007, 12:30 AM
prasxz prasxz is offline
Senior Member
 
Join Date: Jun 2006
Posts: 1,044
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!
Reply With Quote
  #9 (permalink)  
Old 06-05-2007, 12:42 AM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
  #10 (permalink)  
Old 06-06-2007, 03:22 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
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!
Reply With Quote
Reply


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

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

LinkBacks (?)
LinkBack to this Thread: http://www.forex-tsd.com/metatrader-programming/7813-mql4-guide.html
Posted By For Type Date
E2E-Fx This thread Refback 08-17-2007 11:18 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Welcome to the MQL4 course codersguru Metatrader 4 mql 4 - Development course 84 05-18-2008 05:59 PM
Help for convert from VT to MQL4 M-E-C Expert Advisors - Metatrader 4 11 07-27-2007 06:53 PM
Arrays in MQL4 clippertm Questions 4 04-12-2007 09:35 PM
www.mql4.com DeSt Metatrader 4 18 02-01-2006 11:59 PM


All times are GMT. The time now is 08:56 AM.