Rounding function is a crucial part of the scripting in DAS Trader Pro because various technological rules apply
Rounding a number less than 100 gives us a 0
This allows us to use automated locates of the proper size without guessing.
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.
- 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
ROUND1 ROUND2 ROUND3 ROUND4exist 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 - 100msgbox(round(124,100));
Rounding a number less than 100 gives us a 0
msgbox(round(99,100));
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 100msgbox(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 stringRound(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);
For even more advanced scripting, you can visit my Substack - Peter's Substack where I elaborate on the newest features, ideas and do custom solutions for specific needs.
Author: PeterB
Disclosure Statement 1. Hotkey scripts should always be thoroughly tested in a paper trading environment prior to any live deployment. Guardian Trading assumes no responsibility for errors, malfunctions, or financial losses arising from the use, misuse, or modification of custom hotkey configurations. Traders are solely responsible for the creation, testing, and implementation of their own scripts.This guide is provided for informational and educational purposes only and does not constitute trading advice or an endorsement of any specific configuration. The examples herein are illustrative in nature and should not be copied, replicated, or relied upon without independent verification and testing. Use of this material constitutes acknowledgment and acceptance of these terms.
2. No information provided by Velocity Clearing, LLC (“Velocity” or the “Firm”), directly or indirectly, should be considered a recommendation or solicitation to adopt any particular trading or investment strategy or to invest in, or liquidate, a particular security or type of security. Information provided by Velocity on its Twitter, Facebook or Blog pages is for informational and educational purposes only and is not intended as a recommendation of any particular security, transaction or strategy. Commentary and opinions expressed are those of the author/speaker and not necessarily those of the Firm. Velocity does not guarantee the accuracy of, or endorse, the statements of any third party, including guest speakers or authors of commentary or news articles. All information regarding the likelihood of potential future investment outcomes are hypothetical. Future results are never guaranteed. Any examples that discuss potential trading profits or losses may not take into account trading commissions or fees, which means that potential profits could be lower and potential losses could be greater than illustrated in any example. Users are solely responsible for making their own, independent decisions about whether to use any of the research, tools or information provided, and for determining their own trading and investment strategies.


