MQL Guide - page 2

 

The following function performs dynamic calculation of working lot.

extern bool uplot = true;
extern int lastprofit = 1;
extern double lotmin = 0.1;
extern double lotmax = 0.5;
extern double lotstep = 0.1;
 
double GetLots() 
{  
  double lot = lotmin;
  if (!uplot) return (lot);
  
  int ticket = GetLastOrderHist();
  if (ticket == -1) return (lot);
  
  if (!OrderSelect(ticket, SELECT_BY_TICKET, MODE_HISTORY)) return (lot);
  if (OrderProfit()*lastprofit < 0) return (lot);
  
  lot = MathMin(OrderLots() + lotstep, lotmax);
  return (lot);
}
 
int GetLastOrderHist(int type = -1)
{
  int ticket = -1;
  datetime dt = 0;
  int cnt = HistoryTotal();
    
  for (int i=0; i < cnt; i++) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
 
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
    
    if (type != -1 && OrderType() != type) continue;
    
    if (OrderCloseTime() > dt) {
      dt = OrderCloseTime();
      ticket = OrderTicket();
    }
  }
  
  return (ticket);
}
 
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[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;
  }
}
 
Heino:
Hello, how I can let draw simply a vertical line.
I has tested however nothing fit. I wont that it works like this
if(a==b){
  draw a vertical line!
}

bool CreateVertLine(string name, datetime tm)
{
  bool bRet = ObjectCreate(name, OBJ_VLINE, 0, tm, 0);
  if (!bRet)
  {
     Print("Can't create vertical line! code #", GetLastError());
     return (false);
  }
 
   return (true);
}
 
Hello there,
Can you show example of how to create code performing logic of T -type trigger
that can be characterized by equation:

Qnext = T(inverted Q) + (inverted T)Q

or if on input T signal does 1-0-1-0-1-0-1-0-.......
on Qoutput signal goes 1-1-0-0-1-1-0-0......
on inverted Qoutput 0-0-1-1-0-0-1-1-. .....

and it is like parity check

the link with description is herehttp://en.wikipedia.org/wiki/Flip-flop_(electronics)
 
stuCraig66:
How does one tell if a limit order has been activated via the MQL API?

1. At first you should build the table of limit orders (an array with tickets for example).
2. Add each new limit order into the table of limit orders.
3. Generate OnLimitOrderOpened event if the type of any order from the table is OP_BUY/OP_SELL or the order was cancelled.
 
highway3000:
Can someone please show me how I can set the level1 30 to Green and leve2 70 to Red ?

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
#property indicator_level1 30
#property indicator_level2 70
#property indicator_minimum 0
#property indicator_maximum 100

Thanks in advance

It seems there is no way to change the settings of an individual level.

void SetLevelStyle( int draw_style, int line_width, color clr=CLR_NONE)
The function sets a new style, width and color of horizontal levels of indicator to be output in a separate window.

 
jasongan:

I am new to forex and MQL4, I am looking for an EA not to auto trade but just give me an audible alert with message box. The condition is simple just give an alert when the D1 chart H and L is different by X value of pips. Does anyone have it? Or some pointers on how to code it.


int X = 100;
 
double H = iHigh(NULL, PERIOD_D1, 0);
double L = iLow(NULL, PERIOD_D1, 0);
 
if (H-L > X*Point)
{
  PlaySound("alert.wav");
  Alert("!!!");
}
 
s4325:
Is it possible to set a specific time for EA to become active.

Someone please help.... thanks in advance


inh hh = 9;
int mm = 10;
 
if (Hour() == hh && Minute() >= mm)
{
  //do something
}
 

Hello

I found the attached expert here but in backtesting it doesn't work after 12/2006 can you repair it and put it here again.

I don't know programming. thanks for your help

Files:
 
yocy1:
how do i choose the last order price of current symbol?


Here is the sample of history orders selection:

//-----
 
if (LastHistOrderSelect(OP_BUY, OP_SELL)) Print(OrderOpenPrice()); 
 
//-----
 
bool LastHistOrderSelect(int type1, int type2)
{
  datetime tm = -1;
  int ticket = -1;
 
  int cnt = OrdersHistoryTotal();
  for (int i=0; i < cnt; i++) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
    if (OrderSymbol() != Symbol()) continue;
    
    int type = OrderType();
    if (type == type1 || type == type2)
    {
      if (OrderCloseTime() > tm)
      {
        tm = OrderCloseTime();
        ticket = OrderTicket();
      }
    }
  }
  
  return (OrderSelect(ticket, SELECT_BY_TICKET, MODE_HISTORY));
}
Reason: