How to get last two digits of Bid or Ask price (without string conversion)?

 

I am trying to set up an EA that looks for the last two digits of price to equal certain amounts. For example, when last two digits equals "00", perform a Print function. I was able to do it by converting the Bid/Ask to a string and using the StringFind function, but this is using too much resources because it involves a for loop within a for loop.

 

string institutionalLevels[4] = { "00" , "20" , "50" , "80" },
       priceString[2];
 
int start()
{
   priceString[0] = DoubleToStr(Bid, 4);
   priceString[1] = DoubleToStr(Ask, 4);

   
   
     for(int i = 0; i < 2; i++)
     {
       for(int j = 0; j < 4; j++)
       {
           if(StringFind(priceString[i], institutionalLevels[j], 4) != -1)
           {
             // do something.
           }
        }
      }
    
   return;
}
     

 

Does anyone have a way to do this without converting a double to a string? 

 
theloneranger:

I am trying to set up an EA that looks for the last two digits of price to equal certain amounts. For example, when last two digits equals "00", perform a Print function. I was able to do it by converting the Bid/Ask to a string and using the StringFind function, but this is using too much resources and is kind of wonky.

Does anyone have a way to do this without converting a double to a string? 

Yes,  Mathematics . . . .

int BidAsInt = MathPow(10,Digits) * Bid;

if (BidAsInt%100  == 0)
   Alert("Last 2 digits of Bid are 00 ! ! !);
 
RaptorUK:

Yes,  Mathematics . . . .

 

 


Thank you. Sometimes I overcomplicate things.
 
theloneranger: For example, when last two digits equals "00",

You really don't want the last two digits, you want the last two pips.
//++++ 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());
     */
:
/////////////
double price = Bid;
int    priceAsPips = price / pips2dbl,
       lastTwo     = priceAsPips % 100;
 
WHRoeder:

You really don't want the last two digits, you want the last two pips.

Raptor's code is correct, yours as so many times here failes. Just try it for yourself with 5 digits and a price of 1.3000'9. The result "00" is neither correct nor as expected "009/09". The intention was exactly NOT to cut subpips but wait for round numbers.

Don't you want to change your slogan "not compiled, not tested" to "compiled, tested"?

 
paulepanke: Raptor's code is correct, yours as so many times here failes.
Both codes are correct, depending on the interpretation of the question.
string institutionalLevels[4] = { "00" , "20" , "50" , "80" },
Those are pip levels. He doesn't want 9 from 1.3000'9 he wants 00.
 
WHRoeder:
Both codes are correct, depending on the interpretation of the question.Those are pip levels. He doesn't want 9 from 1.3000'9 he wants 00.


Yes, sorry I wasn't more clear with my question. Here is the code I ended up using (sort of a combination of both?)

             int bidAsInt; 
             
             if(Digits%2 == 1)
                bidAsInt = MathFloor(Bid/(Point*10));
             else
                bidAsInt = MathFloor(Bid/Point);
                
             if(bidAsInt%100 == 0 || bidAsInt%100 == 20 || bidAsInt%100 == 50 || bidAsInt%100 == 80)
               {
                   ....code....
               }

 

Reason: