MQL4 Guide

 

Hi,

I'm going to place here the various helpful MQL4 code.

 

OrdersCount function allows to get the orders count of predefined type.

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]

Example:

[CODE]

int start()

{

int BuyCnt = OrdersCount(OP_BUY);

if (BuyCnt > 0) return (0);

...

 

How to close the all market orders:

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]

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);

}

}

 

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.

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);

}

 
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.

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, ...

[/CODE]

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());

}

}

}

 

NextDay function allows to get the date of next day.

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);

}

 

The following code allows to open a position at a preset time.

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, "-");

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 = item;

i = pos+1;

}

}

 

hi

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

Thx

===================

Forex Indicators Collection

 
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.

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

Thx

===================

Forex Indicators Collection

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);

}

 
yocy1:
i would like to write a code for a hedge strategy in one currency and stop open when, for example, buy OP are more then sell OP, so i need to write something like, "if totalorder of OP_BUY > totalorders of OP_SELL" any idea?

int BuyCnt = 0;

int SellCnt = 0;

int BuyStopCnt = 0;

int SellStopCnt = 0;

int BuyLimitCnt = 0;

int SellLimitCnt = 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 (type == OP_BUYSTOP) BuyStopCnt++;

if (type == OP_SELLSTOP) SellStopCnt++;

if (type == OP_BUYLIMIT) BuyLimitCnt++;

if (type == OP_SELLLIMIT) SellLimitCnt++;

}