Rounding function is a crucial part of the scripting in DAS Trader Pro because various technological rules apply
- for symbols below $1 price, rounding to 3(4) decimals is required
- for symbols above $1 price rounding to 2 decimals is required
- for options below $3 rounding to nearest 0.05 is required
- for options above $3 rounding to nearest 0.1 is required
- rounding to lots of 100 might be needed for locates for some locate routes
Allthough the commands
ROUND1 ROUND2 ROUND3 ROUND4
exist for a long time, for advanced scripting a dedicated round() function is to be used.
It is used like this
Round(NUMBER, precision); where precision is a number from 0-6
or
Round(NUMBER, precision, direction); where direction is 0-down or 1-up
Round(123.456,2);
will return 123.46
Round(123.456,2,0);
will return 123.45
Round(123.456,2,1);
will return 123.46
Rounding Examples
Round to 2 or 4 decimals based on price
This is used in many of my entry hotkeys with conditional rounding. If the price is below $1, we need to use different rounding. This is very useful for low floats, which can go above or drop below $1 quickly, so we do not need to use different sets of hotkeys for entries, exits, or stops.
// round to 2 decimals by default
$MONTAGE.Price=round($MONTAGE.LAST*0.997,2);
//round to 4 for sub dollar stocks
if($MONTAGE.LAST<1)
{
$MONTAGE.Price=ROUND($MONTAGE.LAST*0.997,4);
}
Round to the nearest increment of X
For options, we need to round by increments of 0.05 or 0.10
This is more tricky, but the developers at DAS gave us a function that works like this
// round to 0.1 increments by default
$PRICE=round($PRICE,102,0.1);
//round to 0.05 for sub 3 dollar options
if($PRICE<3)
{
$PRICE=ROUND($PRICE,102,0.05);
}
Notice the number 102, which is the definition of the rounding type
Round down to the nearest lot
Mind the word “down”. This is useful if you calculate the number of shares to short considering your buying power. That way, if we end up with a number of shares of 157, the mathematical rounding (type 100) would round mathematically to 200, but the order would fail because of not enough buying power.
Therefore, we need to use rounding down to the nearest lot with round 101 type like this
which gives us the nearest lowest lot number – 100
msgbox(round(124,100));

Rounding a number less than 100 gives us a 0
msgbox(round(99,100));

This allows us to use automated locates of the proper size without guessing.
Round to the nearest lot
This is achieved with the type 101 parameter to the round function
187 gets rounded to 200
while 149 gets rounded to 100
msgbox(round(187,101));

msgbox(round(149,101));

Other rounding types and other round function uses.
There is rounding type 103, which converts the number to a string
Round(123.456,103)
will return a string “123.45” rather than a decimal number. It is supposed to be used for currency conversions where 2 decimals are needed, as the function rounds to 2 automatically and is not configurable.
Similarly, we can use the round function to convert strings to numbers with just
Round("123.456",3)
This is very helpful when retrieving data from the charts, as the data of moving averages from the past are retrieved as a string, which needs to be followed by a substring function to cut off the unnecessary characters, leaving us with the number in a string format.

Below is an example of the retrieval function of the ATR value from the study named “MYATR” from a daily chart named “MYATRCHART”
The retrieval of the historical value comes into the $MYATRX variable as the string “AverageTrueRange %R: 5.292” which is later cut off to “5.292” which is still a text. That is being converted into a number with the ROUND($MATMP,3), which finally converts it to a number we can then work with by comparing it, doing calculations, etc.
// 0 means current day, -1 means yesterday
$WHICHBAR=0;
$BARTIME=getwindowobj("MYDAILY").getbar($WHICHBAR).time;
$BARDATE=substring($BARTIME,0,9);
$801="08:01:00";
$MYATRX=getwindowobj("MYATRCHART").getstudyval("MYATR","ByDateTime", $BARDATE+$801);
$MYATRXLENGTH=strlen($MYATRX);
if($MYATRXLENGTH==0)
{return;}
// 4 symbols ATR
if($MYATRXLENGTH==27)
{
$MATMP=SubString($MYATRX,21,$MYATRXLENGTH-1);
$MYATR=ROUND($MATMP,3);
}
// 5 symbols ATR
if($MYATRXLENGTH==28)
{
$MATMP=SubString($MYATRX,21,$MYATRXLENGTH-1);
$MYATR=ROUND($MATMP,3);
}
msgbox($MYATR);
