MQL4 guide

MQL4 guide

Postby RickD » Fri Feb 27, 2009 12:59 am

All the questions about MQL4 you can find here.
Last edited by RickD on Sun Mar 15, 2009 11:31 pm, edited 1 time in total.
MQL4: idea * experience + creative solution
MQL4 Coding Service
CopyTool Software
User avatar
RickD
 
Posts: 27
Joined: Tue Feb 10, 2009 11:35 pm

Re: MQL4 guide

Postby RickD » Fri Feb 27, 2009 1:05 am

RecountOrders function gets the amount of BUY and SELL orders in the market.

Code: Select all
int BuyCnt, SellCnt;

void RecountOrders()
{
  BuyCnt = 0;
  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;
   
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
  }
}


Usage:
Code: Select all
  RecountOrders();
  if (BuyCnt+SellCnt > 0) return; 
MQL4: idea * experience + creative solution
MQL4 Coding Service
CopyTool Software
User avatar
RickD
 
Posts: 27
Joined: Tue Feb 10, 2009 11:35 pm

Re: MQL4 guide

Postby RickD » Thu Mar 05, 2009 9:35 pm

The way to close open market orders:

Code: Select all
int Slippage = 3;

CloseOrders();

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;

    int type = OrderType();
    if (type == OP_BUY)
    {
      OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage);
    }

    if (type == OP_SELL)
    {
      OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage);
    }
  }
}
MQL4: idea * experience + creative solution
MQL4 Coding Service
CopyTool Software
User avatar
RickD
 
Posts: 27
Joined: Tue Feb 10, 2009 11:35 pm

Re: MQL4 guide

Postby RickD » Tue Mar 10, 2009 12:04 am

This function makes it possible to get the date of next day.

Code: Select all
void NextDay(int& day, int& month, int& year)
{
  datetime Time0 = TimeCurrent();
  datetime Tomorrow = Time0 + 24*60*60;
 
  day = TimeDayOfYear(Tomorrow);
  month = TimeMonth(Tomorrow);
  year = TimeYear(Tomorrow);
}


Usage:

Code: Select all
int day, month, year;
NextDay(day, month, year);
MQL4: idea * experience + creative solution
MQL4 Coding Service
CopyTool Software
User avatar
RickD
 
Posts: 27
Joined: Tue Feb 10, 2009 11:35 pm

Re: MQL4 guide

Postby RickD » Sun Mar 15, 2009 11:31 pm

The following function can select the ticket of the last closed order in account history:

Code: Select all
bool LastOrderSelect(int pool, int type1, int type2 = -1)
{
  datetime tm = -1;
  int ticket = -1;

  if (pool == MODE_TRADES)
  {
    int cnt = OrdersTotal();
    for (int i=0; i < cnt; i++)
    {
      if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != Magic) continue;
       
      int type = OrderType();
      if (type == type1 || type == type2)
      {
        if (OrderOpenTime() > tm)
        {
          tm = OrderOpenTime();
          ticket = OrderTicket();
        }
      }
    }
 
    return (OrderSelect(ticket, SELECT_BY_TICKET));
  }

  if (pool == MODE_HISTORY)
  {
    cnt = OrdersHistoryTotal();
    for (i=0; i < cnt; i++)
    {
      if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != Magic) continue;
       
      type = OrderType();
      if (type == type1 || type == type2)
      {
        if (OrderCloseTime() > tm)
        {
          tm = OrderCloseTime();
          ticket = OrderTicket();
        }
      }
    }
 
    return (OrderSelect(ticket, SELECT_BY_TICKET));
  }
   
  return (false);
}


Usage:
Code: Select all
  if (LastOrderSelect(MODE_HISTORY, OP_BUY, OP_SELL))
  {
    Print("OrderType: ", OrderType());
  }
MQL4: idea * experience + creative solution
MQL4 Coding Service
CopyTool Software
User avatar
RickD
 
Posts: 27
Joined: Tue Feb 10, 2009 11:35 pm

Re: MQL4 guide

Postby RickD » Mon Mar 23, 2009 12:41 am

Standard trailing:

Code: Select all
extern bool TrailingOn = true;
extern int TrailingStart = 30;
extern int TrailingSize = 30;

void TrailPositions()
{
  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)
    {
      if (Bid-OrderOpenPrice() > TrailingStart*Point)
      {
        if (OrderStopLoss() < Bid - (TrailingSize+1)*Point)
        {
          OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingSize*Point, OrderTakeProfit(), 0);
        }
      }
    }

    if (type == OP_SELL)
    {
      if (OrderOpenPrice()-Ask > TrailingStart*Point)
      {
        if (OrderStopLoss() > Ask + (TrailingSize+1)*Point || OrderStopLoss() == 0)
        {
          OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingSize*Point, OrderTakeProfit(), 0);
        }
      }
    }
  }
}


Usage:
Code: Select all
  if (TrailingOn) TrailPositions(); 
MQL4: idea * experience + creative solution
MQL4 Coding Service
CopyTool Software
User avatar
RickD
 
Posts: 27
Joined: Tue Feb 10, 2009 11:35 pm

Re: MQL4 guide

Postby FinGeR » Sat May 30, 2009 12:13 am

...
Last edited by FinGeR on Sat May 30, 2009 12:37 am, edited 1 time in total.
FinGeR
 
Posts: 3
Joined: Sat May 30, 2009 12:10 am

Re: MQL4 guide

Postby FinGeR » Sat May 30, 2009 12:35 am

@RickD
What is your opinion about the upcoming new MQL5?
FinGeR
 
Posts: 3
Joined: Sat May 30, 2009 12:10 am

Re: MQL4 guide

Postby FinGeR » Sat Jun 06, 2009 12:19 pm

How to program in MQL4
Pin formation ?
Attachments
Capture_133.gif
(4.51 KiB) Downloaded 24 times
FinGeR
 
Posts: 3
Joined: Sat May 30, 2009 12:10 am

Re: MQL4 guide

Postby RickD » Fri Jun 19, 2009 9:21 pm

FinGeR wrote:@RickD
What is your opinion about the upcoming new MQL5?


Hi,

We hope that MQL5 will have alot of new and useful features.
MQL4: idea * experience + creative solution
MQL4 Coding Service
CopyTool Software
User avatar
RickD
 
Posts: 27
Joined: Tue Feb 10, 2009 11:35 pm

Re: MQL4 guide

Postby RickD » Fri Jun 19, 2009 9:23 pm

FinGeR wrote:How to program in MQL4 Pin formation ?


MQL4 is the program language for trading only.
MQL4 has no functions to implement GUI items.
MQL4: idea * experience + creative solution
MQL4 Coding Service
CopyTool Software
User avatar
RickD
 
Posts: 27
Joined: Tue Feb 10, 2009 11:35 pm

Trailing stop?

Postby wealthsecrets » Mon Oct 05, 2009 4:24 am

Hi Rick,

Since I don't know how to install this code to find out what it does.

I was curious if this trailing method was better than what most MT4 brokers supply.

For example, from my experience unless I am doing something wrong, when you initiate
a trail stop of 15 pips the trail should immediately engage when the price action has moved
15 pips into profit from entry.

However when I have tried to initiate a trail, the trail stop jumps right close to present price
action which is 30 pips instead of entry and then I get stopped out because the stop is right
there next to present price action and retraces a few pips. Shouldn't the trail jump to entry
since it should start to trail 1 pip for every 1 pip movement past 15 pips into profit always
keeping the stop 15 pips away from present price action?

Is this something you have noticed as well or am I doing something wrong?

Mike










RickD wrote:Standard trailing:

Code: Select all
extern bool TrailingOn = true;
extern int TrailingStart = 30;
extern int TrailingSize = 30;

void TrailPositions()
{
  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)
    {
      if (Bid-OrderOpenPrice() > TrailingStart*Point)
      {
        if (OrderStopLoss() < Bid - (TrailingSize+1)*Point)
        {
          OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingSize*Point, OrderTakeProfit(), 0);
        }
      }
    }

    if (type == OP_SELL)
    {
      if (OrderOpenPrice()-Ask > TrailingStart*Point)
      {
        if (OrderStopLoss() > Ask + (TrailingSize+1)*Point || OrderStopLoss() == 0)
        {
          OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingSize*Point, OrderTakeProfit(), 0);
        }
      }
    }
  }
}


Usage:
Code: Select all
  if (TrailingOn) TrailPositions(); 
wealthsecrets
 
Posts: 1
Joined: Mon Oct 05, 2009 3:43 am
Location: Miami, FL


Return to MQL4 language

Who is online

Users browsing this forum: No registered users and 1 guest

cron