Welcome to Kidspips! In Kidspips our mission is to make the forex trading easier for beginners. We are already a family of more than 300 forex traders . Lots of new services of Kidspips is coming soon. You are almost welcomed. Stay tuned!


 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mql4: Useful functions
07-21-2010, 07:07 PM
Post: #1
Mql4: Useful functions
Mql4: Useful Functions

Receive quantity of orders (market or postponed).
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);
}
In the expert
Code:
int start()
{
  int BuyCnt = OrdersCount(OP_BUY);
  if (BuyCnt > 0) return (0);
  ...

Close all orders:
Code:
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(), Bid, Slippage);
    if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage);
  }
}

Remove all pending orders of the given type:
Code:
void DeleteOrders(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 (type == OP_BUYSTOP || type == OP_SELLSTOP) OrderDelete(OrderTicket());
    if (type == OP_BUYLIMIT || type == OP_SELLLIMIT) OrderDelete(OrderTicket());    
  }
}

The date of the next day:
Code:
datetime Time0 = CurTime();
  datetime Tomorrow = Time0 + 24*60*60;
  int day = TimeDayOfYear(Tomorrow);
  int month = TimeMonth(Tomorrow);
  int year = TimeYear(Tomorrow);

Take ticket of last closed order from history:
Code:
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);
}

Function for calculation a working lot.
Code:
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);
}

TrailingStop

TrailingStep-step.
l - a prefix for Long's
s - префикс для short's

Code:
extern bool UseTrailing = true;
extern int lMinProfit = 30;
extern int sMinProfit = 30;
extern int lTrailingStop = 15;
extern int sTrailingStop = 15;
extern int lTrailingStep = 5;
extern int sTrailingStep = 5;

if (UseTrailing) TrailingPositions();  

void TrailingPositions()
{
  int cnt = OrdersTotal();

  for (int i=0; i<cnt; i++) {
    if (!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
    if (OrderSymbol() != Symbol()) continue;        

    if (OrderType() == OP_BUY) {
      if (Bid-OrderOpenPrice() > lMinProfit*Point) {
        if (OrderStopLoss() < Bid-(lTrailingStop+lTrailingStep-1)*Point) {
          OrderModify(OrderTicket(), OrderOpenPrice(), Bid-lTrailingStop*Point, OrderTakeProfit(), 0, Blue);
        }
      }
    }

    if (OrderType() == OP_SELL) {
      if (OrderOpenPrice()-Ask > sMinProfit*Point) {
        if (OrderStopLoss() > Ask+(sTrailingStop+sTrailingStep-1)*Point || OrderStopLoss() == 0) {
          OrderModify(OrderTicket(), OrderOpenPrice(), Ask+sTrailingStop*Point, OrderTakeProfit(), 0, Blue);
        }
      }
    }
  }
  
}

Open lot at set time.

Code:
extern string OpenTime = "00:00";
  extern int OpenPeriod = 10;

  datetime tm0 = StrToTime(TimeToStr(CurTime(), TIME_DATE) + " " + OpenTime);
  datetime tm1 = tm0 + OpenPeriod*60;

  if (CurTime() < tm0 || CurTime() > tm1) return;

Expert close orders with present takeprofit.
TakeProfit are 1, 2, 3, ...

Code:
extern int TakeProfit = 2;
extern int Slippage = 3;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void init () {
}

void deinit() {
}

void start() {

  if (TakeProfit == 0) return;

  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) {
      if (Bid > OrderOpenPrice() + TakeProfit*Point)
        OrderClose(OrderTicket(), OrderLots(), Bid, Slippage);  
    }
  
    if (type == OP_SELL) {
      if (Ask < OrderOpenPrice() - TakeProfit*Point)
        OrderClose(OrderTicket(), OrderLots(), Ask, Slippage);
    }
  }
}

Function returns length of orders with positive takeprofit (from the end of history).
Code:
int GetLastPositiveOrdersCount()
{
  int PosCnt = 0;
  int cnt = HistoryTotal();
  for (int i = cnt-1; i >=0; i--) {
    
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;

      
    int type = OrderType();
    if (type != OP_BUY && type != OP_SELL) continue;
      
    if (OrderProfit() < 0) break;

    PosCnt++;
  }

  return (PosCnt);
}

Function return type of the last order.
Code:
int GetLastPositiveOrdersCount()
{
  int PosCnt = 0;
  int cnt = HistoryTotal();
  for (int i = cnt-1; i >=0; i--) {
    
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;

      
    int type = OrderType();
    if (type != OP_BUY && type != OP_SELL) continue;
      
    if (OrderProfit() < 0) break;

    PosCnt++;
  }

  return (PosCnt);
}

Kidspips - Easy way to learn and earn from forex - Collection of all profitable forex signals, forex indicators and forex trading strategies - We respect your trading passion and always happy to help!!
reply



Forum Jump:


Contact UsKidspips Return to TopReturn to ContentLite (Archive) ModeRSS Syndication