Please someone tell me what kind of mistake I made!!! HELP

 

I have Spent two days and fail to find out what mistake I made, so please help me out!!

Case: I want to open two sell orders, the second one will be open when the market go up 5 pip, but when I run this code (below), it only open the first one and never open the second one.

Your helps gonna be really helpful to me and I appreciate that. Thanks for your time!!

int magic.number=0;



int CheckIfPlaceOrderAlready(int magic.number)
{
 int i;
 int count=0;
 for (i=0;i<OrdersTotal();i++)
 {
  if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
  && OrderSymbol()==Symbol()
  && OrderMagicNumber()==magic.number)
  {
  ////there is order opening
   count=count+1;
  }
 }
 
 ///return the number of order open
 return (count);
}


double ReturnPreviousOpenPrice(int magic.number)
{
 int j;
 for(j=0;j<OrdersTotal();j++)
 {
  if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES)
  && OrderSymbol()==Symbol()
  && OrderMagicNumber()==magic.number)
  {
   return (OrderOpenPrice());
  }
 }
}


void start()
{
 if(CheckIfPlaceOrderAlready(magic.number)==0)
 {
  OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",magic.number);///////first order
 }
 
 if(CheckIfPlaceOrderAlready(magic.number)==1
 && Bid-ReturnPreviousOpenPrice(magic.number)>=5*Point)
 {
  OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",magic.number);///////second order
 }
}
 
Pain:

I have Spent two days and fail to find out what mistake I made, so please help me out!!

Case: I want to open two sell orders, the second one will be open when the market go up 5 pip, but when I run this code (below), it only open the first one and never open the second one.

Your helps gonna be really helpful to me and I appreciate that. Thanks for your time!!

Hi Pain,

Rather spend two days trying, every time you encounter a problem, try to get GetLastError, that way you know what the err was.

Maybe your second order is right inside brokers's MarketInfo's MODE_STOPLEVEL and MODE_FREEZELEVEL. You're not open them at 5 pips but at 5 Points. In 5 digits broker, 5 pips is equal with 50 Points.

:D

  #include <stdlib.mqh>

  int ticket = OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0,"",magic.number);
  if(ticket < 0)
    {
    Print ("Fail send order with err "+ ErrorDescription(GetLastError()));
    }
    else
    {
    Print ("Success sending order "+ticket);
    }
 
onewithzachy:

Hi Pain,

Rather spend two days trying, every time you encounter a problem, try to get GetLastError, that way you know what the err was.

Maybe your second order is right inside brokers's MarketInfo's MODE_STOPLEVEL and MODE_FREEZELEVEL. You're not open them at 5 pips but at 5 Points. In 5 digits broker, 5 pips is equal with 50 Points.

:D


Hi onewithzachy, first of all, thanks so much for replying me.

My broker is 4 digits broker. I have tried put PRINT statements in every step to find out the mistake. And it seems the code (below) is never be True

So my code does not try to make a second order, therefore it will not get GetLastError().

My question is how can I fix the code (below) so that it will not never be True. Thanks for your time!!

Bid-ReturnPreviousOpenPrice(magic.number)>=5*Point
 
Pain:

My broker is 4 digits broker.

My question is how can I fix the code (below) so that it will not never be True.

  1. Don't assume. Adjust for 4/5 digit brokers, TP, SL, AND slippage. Adjust for ECN brokers, open first and THEN set stops.
    //++++ 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(){                                             OptInitialization();
         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());
         */
    
  2. Always test your return codes What are Function return values ? How do I use them ? - MQL4 forum
  3. Print out your values so you find out WHY
    //Bid-ReturnPreviousOpenPrice(magic.number)>=5*Point
    double oop = ReturnPreviousOpenPrice(magic.number);
    Print("Bid=", PriceToStr(Bid), " oop=", PriceToStr(oop), 
          " dif=",DeltaToPips(Bid-oop),">=",DeltaToPips(5*Point));
    /////////////////////////////
    string  PriceToStr(double p){ return( DoubleToStr(p, Digits) ); }
    string  DeltaToPips(double d){
        double pips = d / pips2dbl;
        return( DoubleToStr(pips, Digits.pips) ); 
    }
    




 

Hi Pain,

LOL - this is funny - I'm not gonna spend 2 days finding why ?, coz I don't see any error in your codes. I have a feeling that you are already done all I'm gonna say here, whatever then ...

1. I'm afraid you just have to print everything, print those ReturnPreviousOpenPrice() function and the value of subtracted Bid. There's gonna be lots of print though :(.

Several ideas comes to mind ...

2. Add if OrderType() == OP_SELL

double ReturnPreviousOpenPrice(int magic.number)
{
 int j;
 for(j=0;j<OrdersTotal();j++)
 {
  if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES)
  && OrderSymbol()==Symbol()
  && OrderMagicNumber()==magic.number)
  {
  if (OrderType() == OP_SELL) return (OrderOpenPrice());
  }
 }
}

3. Save the ticket number of first order send and change ReturnPreviousOpenPrice() into

double ReturnPreviousOpenPrice(int ticket)
  {
  if (OrderSelect (ticket, SELECT_BY_TICKET)) return (OrderOpenPrice());
  return (0);
  }

You still have to go though that print :(

:D

 

I've found out a bug in the 432 build ! OrderSelect function retrieve opened and closed order without filter!! If i use MODE_TRADES or MODE_HISTORY the result is the same !!! To work around this bud I have to check the OrderCloseTime() function value: if it returns zero the order is opened, if return a value grater is closed.

To find out this I've spended so lot time to reviews all my code searching for a my bug, but the bug is of MetaQuotes!!

 
Dainesi:

I've found out a bug in the 432 build ! OrderSelect function retrieve opened and closed order without filter!! If i use MODE_TRADES or MODE_HISTORY the result is the same !!! To work around this bud I have to check the OrderCloseTime() function value: if it returns zero the order is opened, if return a value grater is closed.

To find out this I've spended so lot time to reviews all my code searching for a my bug, but the bug is of MetaQuotes!!


Why are you posting this in this thread ?

Don't hijack someone else's thread.

Are you using OrderSelect() with SELECT_BY_POS or SELECT_BY_TICKET ?

 
Dainesi: I've found out a bug in the 432 build ! OrderSelect function retrieve opened and closed order without filter!! If i use MODE_TRADES or MODE_HISTORY the result is the same !!
  1. Don't hijack other peoples threads.
  2. RTFM. It plainly says The pool parameter is ignored if the order is selected by the ticket number
 

I wrote this post because I believe that Pain's problem is caused by this bug.

Of course the used mode of OrderSelect method is SEL_BY_POS.

 
Dainesi:

I wrote this post because I believe that Pain's problem is caused by this bug.

Of course the used mode of OrderSelect method is SEL_BY_POS.


Well you had better show your code . . . I don't see the bug you are describing.