Need help in understanding why the OrderSend does not seem to place orders

 

Hi

I am new to MQ4 and haven't really figured out how to do troubleshooting so would appreciate it if somebody with experience, pls

take a look and point me in the correct direction.

I've been trying to run the code below on IBFX but when I set ip up for manual confirmation, it works at the correct time to tell me to buy or sell.

However, when I just set it to trade live without manual confirmation, nothing seems to happen.

I inserted some SendMail function into the CheckForOpen function below and found started receiving many emails, showing me that it is entering that

function and seems to be sending orders but I cannot see any errors and don't know what's the matter.... can anybody pls help!

//+------------------------------------------------------------------+
//| Bands Breakout.mq4 |
//| Brian Rue |
//| brianrue@gmail.com |
//+------------------------------------------------------------------+

#property copyright "Brian Rue"
#property link "brianrue@gmail.com"

// Permission is granted for personal use. You may distribute this file as long as it is contains this notice,
// including the author's name, and distributed for free.
// If you do trade this system live, I'd appreciate if you would send me an email and tell me how it's going. And if it's making
// you tons of money, I wouldn't mind a thank-you check :)
// This software is provided AS IS, with no warranty of any kind. No guarantee is made as to the future profitability of this system.
// Trade at your own risk.

#define MAGIC 24097

//---- input parameters
extern int DeMarker_Period = 14;
extern double demarker_delta = 0.20; // trade when the DeMarker is outside of .5 +/- demarker_delta
extern int Bollinger_Period = 14;
extern int bollinger_delta = 5; // trade when the price is within bollinger_delta points of the band (negative means price must be outside)
extern int MinADX = 40; // only enter a trade when the ADX is at least this level

extern int TakeProfit = 20;
extern double StopLossATR = 10; // number of ATRs to use for the stoploss
extern int TimePeriod = 5; // in minutes
extern int Slippage = 3;
extern bool UseFixedLots = false;
extern int FixedLotSize = 1;
extern double VAR = 0.5; // .1 means the value-at-risk is 50% of the account (i.e., if the stoploss is hit, this amount will be lost)

double entry = 0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}

double StopLoss()
{
double atr = iATR(NULL,240,100,1);
if (atr * StopLossATR < 10 * Point) return (10 * Point);
return (atr * StopLossATR);
}

double LotSize()
{
if (UseFixedLots == true) return (FixedLotSize);

double size;

size = (AccountEquity() * VAR) / (StopLoss() / Point);

if (size < 0.01) size = 0.01;
if (size > 5) size = 5;
return (size);
}

bool EnterLong()
{
double demarker, highboll, adx;

demarker = iDeMarker(NULL,TimePeriod,DeMarker_Period,1);
highboll = iBands(NULL,TimePeriod,Bollinger_Period,2,0,PRICE_LOW,MODE_UPPER,1);
adx = iADX(NULL,TimePeriod,14,PRICE_TYPICAL,MODE_MAIN,1);

// Enter long if demarker is extreme, AND Ask is outside of upper band, AND ADX is extreme
if ((Ask - bollinger_delta * Point >= highboll) && (MathAbs(demarker - 0.5) >= demarker_delta) && (adx >= MinADX)) {
return (true);
}
else return (false);
}

bool CloseLong()
{
// Close intentionally if we're profitable
if (Bid > entry) {
double ma;
ma = iMA(NULL,TimePeriod,Bollinger_Period,0,MODE_EMA,PRICE_TYPICAL,1);

// Close when the price drops below the moving average
if ((Bid < ma)) {
return (true);
}
else return (false);
} else return (false);
}

bool EnterShort()
{
double demarker, lowboll, adx;

demarker = iDeMarker(NULL,TimePeriod,DeMarker_Period,1);
lowboll = iBands(NULL,TimePeriod,Bollinger_Period,2,0,PRICE_HIGH,MODE_LOWER,1);
adx = iADX(NULL,TimePeriod,14,PRICE_TYPICAL,MODE_MAIN,1);

// Enter short if demarker is extreme, AND Bid is outside of lower band, AND ADX is high
if ((Bid + bollinger_delta * Point <= lowboll) && (MathAbs(demarker - 0.5) >= demarker_delta) && (adx >= MinADX)) {
return (true);
}
else return (false);
}

bool CloseShort()
{
// Close intentionally if we're profitable
if (Ask < entry) {
double ma;

ma = iMA(NULL,TimePeriod,Bollinger_Period,0,MODE_EMA,PRICE_TYPICAL,1);

// Close when the price is above the moving average
if ((Ask > ma)) {
return (true);
}
else return (false);
} else return (false);
}

//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
int res;
int sent=0;
// long conditions
if (EnterLong()) {
res = OrderSend(Symbol(),OP_BUY,LotSize(),Ask,Slippage,Ask-StopLoss(),Bid+TakeProfit*Point,"",MAGIC);
entry = Ask;
return;
}

// short conditions
else if (EnterShort()) {
res = OrderSend(Symbol(),OP_SELL,LotSize(),Bid,Slippage,Bid+StopLoss(),Ask-TakeProfit*Point,"",MAGIC);
entry = Bid;
}

}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if (OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;

if (OrderType()==OP_BUY) {
// close long conditions
if (CloseLong()) {
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);
if (EnterShort()) {
OrderSend(Symbol(),OP_SELL,LotSize(),Bid,Slippage,Bid+StopLoss(),Ask-TakeProfit*Point,"",MAGIC);
entry = Bid;
}
break;
}
}
if (OrderType()==OP_SELL) {
// close short conditions
if (CloseShort()) {
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
if (EnterLong()) {
OrderSend(Symbol(),OP_BUY,LotSize(),Ask,Slippage,Ask-StopLoss(),Bid+TakeProfit*Point,"",MAGIC);
entry = Ask;
}
break;
}
}
}
}


//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
return(0);
}
//+------------------------------------------------------------------+

 

Before posting please read some of the other threads . . . then you would have seen numerous requests like this one:

Please use this to post code . . . it makes it easier to read.

 
Check res, the result from the OrderSend to see if it worked or failed, if it failded print the error then you will know what is wrong . . . . may well be an ECN issue, is IBfx an ECN Broker ?
 
On 5 digit broker you must modify TP, SL, AND slippage. On a ECN broker you must open first and THEN set stops. Always test your return codes so you find out WHY!
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){                                                 OptParameters();
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    if (ticket < 0)
       Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET))
       Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
       Alert("OrderModify failed: ", GetLastError());
     */
 
Tks for the suggestions, will try out and slowly work through this! Cheers! :)
Reason: