|
|||||||
| 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 |
|
![]() |
|
|
LinkBack (1) | Thread Tools | Display Modes |
|
||||
|
MQL4 Guide
Hi,
I'm going to place here the various helpful MQL4 code.
__________________
MQL4: idea * experience + creative solution |
|
||||
|
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);
}
Code:
int start()
{
int BuyCnt = OrdersCount(OP_BUY);
if (BuyCnt > 0) return (0);
...
__________________
MQL4: idea * experience + creative solution |
|
||||
|
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);
}
}
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);
}
}
__________________
MQL4: idea * experience + creative solution |
|
||||
|
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);
}
__________________
MQL4: idea * experience + creative solution |
|
||||
|
Quote:
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, ...
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());
}
}
}
__________________
MQL4: idea * experience + creative solution |
|
||||
|
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);
}
__________________
MQL4: idea * experience + creative solution |
|
||||
|
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;
}
}
__________________
MQL4: idea * experience + creative solution |
|
|||
|
hi
is there any mql script that can show daily , weekly and monthly range for one currency ?
Thx =================== Forex Indicators Collection |
|
||||
|
Quote:
__________________
MQL4: idea * experience + creative solution |
|
||||
|
Quote:
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);
}
__________________
MQL4: idea * experience + creative solution |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |