Trade Not Opening

 

Hello Everybody

I have a mt4 EA that has been working fine. In the past when the conditions were right, trades are opened on multiple currency pairs. The other day, however, I had a problem arise which I can't figure out. I had the EA attached to about 10 charts. (The EA includes magic number processing so the system can keep track of each open order.) At any given time there usually is only 3 or 4 open, active orders, so it doesn't exceed the default maximum number of 10 orders opened at a given time. Again, I have had multipled orders opened in the past with no problems.

But the other day there were only 2 open orders on 2 seperate currency pairs. When the conditions were right for a 3rd order to open, it did not open. What would be a possible reason for this? The 2 open orders that were opened were losing money, but I don't think this prevented the 3rd order from opening because there was plenty of margin to cover it. Could it be that the signal for the trade happened so fast, such as a spike, that the EA did not have time to process the OrderSend command?

Any insights would be appreciated.

 
No mind readers here. No code, no information, no help.
 
WHRoeder:
No mind readers here. No code, no information, no help.


Here is the code:

#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net//"

extern int magic_number = 0;
extern double lot_size = 0.2;
extern int filter_points = 1;
extern int take_profit_points = 30;
extern int stop_loss_points = 100;

int ticket = 0;
int check_for_trade = 1;

double market_bid = 0.0;
double currency_point = 0.0;
double order_open_price = 0.0;
double sl = 0.0;
double tp = 0.0;

string currency_pair = " ";


int init()
{

return(0);
}

int deinit()
{

return(0);
}

int start()
{

currency_pair = Symbol();
market_bid = MarketInfo(currency_pair,MODE_BID);
currency_point = MarketInfo(currency_pair,MODE_POINT);


if(check_for_trade == 1)
{
if (market_bid > iMA(currency_pair,0,50,0,MODE_EMA,PRICE_CLOSE,0) + currency_point * filter_points * 10)
{
check_for_trade = 0;
ticket = OrderSend(currency_pair,OP_SELL,lot_size,Bid,0,0,0,NULL,magic_number,0,Red);
if (OrderSelect(ticket, SELECT_BY_TICKET)== true)
{
order_open_price = OrderOpenPrice();
sl = order_open_price + ( stop_loss_points * Point * 10);
tp = order_open_price - (take_profit_points * Point * 10);
OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,Green);

}
}

}



return(0);
}

 

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


Your code is very dangerous.

What happens if two of the EAs try to place orders at the same time ? order context busy ? you don't check for errors at all so what happens if the OrderModify fails due to OrderContext Busy ?

After you have placed a trade you set check_for_trade = 0; but then you don't check is the trade has hit SL or TP so you never set it back to 1 to place the next trade . . so each instance of the EA will only ever place one order and then stop . . .

Did you bother to check the logs to see what had happened ?

 
RaptorUK:

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


Your code is very dangerous.

What happens if two of the EAs try to place orders at the same time ? order context busy ?

Well, I give each instance of the EA for each currency-pair a unique magic number as an extern input. Doesn't this prevent any conflict issues?
 
RaptorUK:

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


Your code is very dangerous.

What happens if two of the EAs try to place orders at the same time ? order context busy ? you don't check for errors at all so what happens if the OrderModify fails due to OrderContext Busy ?

After you have placed a trade you set check_for_trade = 0; but then you don't check is the trade has hit SL or TP so you never set it back to 1 to place the next trade . . so each instance of the EA will only ever place one order and then stop . . .

Did you bother to check the logs to see what had happened ?


Ok, I know what I have to do. Your questions have sent me in the right direction. In the past I've only used single instances of my ea, one ea for one chart and nothing else. This is the first time I have used multiple ea's on different currency pairs. I need to study up on the IsTradeAllowed() function. Thanks for the insight.
 
forestmyopia:
Well, I give each instance of the EA for each currency-pair a unique magic number as an extern input. Doesn't this prevent any conflict issues?
  1. Use SRC
  2. Can't see that from the code. I use the same MN for the EA on all charts and let them filter by MN and pair.
  3. Always test return codes
    int ticket = OrderSend(...);
    if (ticket < 0) Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET)) Alert("OrderSelect(",ticket,") failed: ", GetLastError());
    else{ ...
  4. if (bool == true) is redundant. if (bool) You wouldn't code if ( (v > 0) == true) you would code if (v > 0)
  5. Once you decide to open, on the next tick you open, on the net tick you open... "so it doesn't exceed the default maximum number of 10 orders opened at a given time" there is NO code to prevent that.
  6. Why are there multiplies by ten "currency_point * filter_points * 10" and " stop_loss_points * Point * 10". Either ALL your values are in points and the user must adjust every parameter for 4/5 digit brokers, or the values should all be in pips and the EA must adjust TP, SL, AND slippage
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

 
WHRoeder:
  1. Why are there multiplies by ten "currency_point * filter_points * 10" and " stop_loss_points * Point * 10". Either ALL your values are in points and the user must adjust every parameter for 4/5 digit brokers, or the values should all be in pips and the EA must adjust TP, SL, AND slippage

I used currency_point in the context of iMA because I didn't know if Point would work in iMA.

IBFX is the only broker I use which uses the 5 digit system. So, I have to multiply by 10 to get the right adjustment.

Filter points are additional points added to the moving average level so that there is a "buffer". The price has to go above the current moving average by a number of filter points to "insure" the price really has exceeded the moving average. Its more of a compulsive psychological thing then anything else. Like, checking if you locked your door 3 times when once is enough.

Your suggestion about checking return codes is what I need to do. Especially within the context of IsTradeAllowed, which I am new to, and I need to learn.

Thanks for your input.

Reason: