//+------------------------------------------------------------------+
//| Pending Order.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#define major 1
#define minor 1
#include <stdlib.mqh>
#include <stderror.mqh>
//+------------------------------------------------------------------+
//|Input |
//+------------------------------------------------------------------+
input int maxpendingorder = 5;
input int magicbuy = 121;
input int magicsell = 212;
input int firstpipstep = 5;
input int pipstep = 10;
input double lot = 1;
//+------------------------------------------------------------------+
//Global variable |
//+------------------------------------------------------------------+
int tradebuy=0, tradesell=0;
int signal1,signal2;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
int start()
{
//---
int TOB=OrderCountBuy(OP_BUYSTOP);
int TOS=OrderCountSell(OP_SELLSTOP);
//Pending Order Buy
if(TOB<maxpendingorder)
{
tradebuy=maxpendingorder-TOB;
}
else
{
return(0);
}
if(OrderCountBuy(OP_BUY)==0)
{
for(int i=0;i<tradebuy;i++)
{
int ticketbuy=OrderSend(Symbol(),OP_BUYSTOP,lot,(MarketInfo(Symbol(),MODE_ASK)+firstpipstep*10*Point)+(pipstep*10*Point*i),0,0,0,"some comment",magicbuy,0,Green);
if(ticketbuy<=0)
Print("Error = ",GetLastError());
else
{
Print("ticketbuy = ",ticketbuy);
while(IsTradeContextBusy())
Sleep(2000);
}
}}
//Pending Order sell
if(TOS<maxpendingorder)
{
tradesell=maxpendingorder-TOB;
}
else
{
return(0);
}
if(OrderCountSell(OP_SELL)==0)
{
for(int i=0;i<tradesell;i++)
{
int ticketsell=OrderSend(Symbol(),OP_SELLSTOP,lot,(MarketInfo(Symbol(),MODE_BID)-firstpipstep*10*Point)-(pipstep*10*Point*i),0,0,0,"some comment",magicbuy,0,Green);
if(ticketsell<=0)
Print("Error = ",GetLastError());
else
{
Print("ticketsell = ",ticketsell);
while(IsTradeContextBusy())
Sleep(2000);
}
}
}
return(0);
}
//+------------------------------------------------------------------+
//count buy order
int OrderCountBuy(int order_type) {
int countb = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSelect(i, SELECT_BY_POS))
if (OrderType() == order_type && OrderMagicNumber() == magicbuy) countb++;
}
return (countb);
}
//count sell order
int OrderCountSell(int order_type) {
int countb = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSelect(i, SELECT_BY_POS))
if (OrderType() == order_type && OrderMagicNumber() == magicsell) countb++;
}
return (countb);
}