Simple Coding Help - No Promise.

Please help, I am getting too much email alert by this code. I want one email alert while buy signal generate and sell vice-versa. Please modify this code according above mentioned condition.

AlertIf( Buy-1, "EMAIL", "BUY @ " + C, 1 );
AlertIf( Sell-1, "EMAIL", "SELL @ " + C, 2 );
 

TracerBullet

Well-Known Member
Simple question - how do you round price to 0.05 in AB ? Round function outputs int.
Below generally works, but is wrong as we are multiplying by a float at the end without another round/floor/ceil.
0.05 * floor( 0.5 + price * 20 );
Ex - breaks for 9744.351. Output is 9744.351

I use AHK/python which supports round to decimal places and works well but so far unable to find answer for AB.
 

yusi

Well-Known Member
Simple question - how do you round price to 0.05 in AB ? Round function outputs int.
Below generally works, but is wrong as we are multiplying by a float at the end without another round/floor/ceil.
0.05 * floor( 0.5 + price * 20 );
Ex - breaks for 9744.351. Output is 9744.351

I use AHK/python which supports round to decimal places and works well but so far unable to find answer for AB.
Have generally used this form without noticing any issues:

Round(price * 20) / 20
 

TracerBullet

Well-Known Member
Have generally used this form without noticing any issues:

Round(price * 20) / 20
Same here

I use Round(Ratio / 5) * 5 to round off any ratios to always give a number with 0 or 5 in units place :)


.
Yeah, i had something similar in ahk/python but then ran across issues for some number and had to add a round again. ahk example below. Last line i had to modify later after getting the bug.

Unfortunately dont have example number as i did not add it to comment. AB code example i gave too worked most of the time until this particular value broke it.

I suspect your examples may also break for some value as we have a division producing float without a round afterwards. Its working great for values i throw at it for now ..

For 5/10, if you ever get issue, fix is simple and call round again. Problem is only if need to round to some decimal places.

Code:
    roundToTickSize( price ){
        global TickSize  
       
        out := Round(  price / TickSize ) * TickSize
       
        return Round( out, 2 )
}
 
Last edited:

Romeo1998

Well-Known Member
@TracerBullet Sir, i am not sure if this is the correct approach, but we can just check the last digit n change it accordingly...
like if last digit >=0 and <=4 , then make last digit 0
if last digit >=5 and <= 9 , then make last digit 5....

Code:
price  = 1.3499;

a =  StrRight(  NumToStr(price,1.8), StrLen(NumToStr(price,1.8))-9) ;
a = StrToNum(a);
b=0;
if ( a >=0 AND a<=4 ) b = 0;
if ( a >=5 AND a<=9 ) b = 5;

a =  NumToStr(price,1.7) + b ;

Title = " " + a;
:)
 

Similar threads