DAS Trader Advanced Hotkeys part 53
How to write and use functions in DAS Trader Pro
This is what ChatGPT will tell you asking about functions.
In programming, a function is a reusable block of code that performs a specific task.
Basic Concept
A function:
Has a name
Can take inputs (called parameters or arguments)
Can return an output (a result)
Helps you organize, reuse, and simplify your code
Why Use Functions?
Avoid repeating code (DRY principle: Don't Repeat Yourself)
Improve readability and structure
Make testing and debugging easier
Enable code reusability across use cases
The implementation of this in DAS Trader Pro has some challenges.
the parameters need to be variables
the output need to be variables too
My personal when to use function is
when I do not know what the input parameters will be
An example
Let’s say I need to find out a specific candle properties like yesterday’s high and low.
Normally, I would address just the candle index -1 with the getbar(-1) function and that would give me the 1 day ago candle output.
$MYYHOD=getwindowobj("MYDAILY").getbar(-1).Hi;
$MYLOD=getwindowobj("MYDAILY").getbar(-1).Lo;
Will give me just that.
But what if I do not know which candle’s properties I would need to look at? Like if I am searching for a specific candle shape for example a reverse hammer?
I would need to find the candle shape first and do not know the index of that candle yet to use it in a code. That is why I need to use an index variable instead. getbar($MYINDEX).
Now the function would need to look like
$MYYHOD=getwindowobj("MYDAILY").getbar($MYINDEX).Hi; $MYLOD=getwindowobj("MYDAILY").getbar($MYINDEX).Lo;
So the input of the function would be $myindex and the output would be $MYHOD and $MYLOD. Save the function as a hotkey named f_hod_lod_daily.
Now whenever I need to get a candle HOD or LOD property I need to call the function with
exechotkey("f_hod_lod_daily");
After that I can just use the $MYHOD and $MYLOD variables in the code as needed. The only variable would be the global variable $MYINDEX which I can set before calling the function.
$MYDAY=input("Enter Date to test",$DATE,1);
$mydatetime=$MYINDEX+" 16:00:00";
exechotkey("f_hod_lod_daily");
msgbox("HOD: "+$MYHOD+" LOD: "+$MYLOD);
Where do we go from here?
This use is crucial to be able to scroll back in time and do some series of tests of a situation on a chart in the past. This will allow us to calculate relative time to the candles of interest. You guessed it probably - it allows us to do candle related back-tests.
Real life example
One of my readers asked me if there is a way to draw the volume with thousands separators.
Like 15247511 display as 15,247,511 which is technically not a number anymore. It is a string/text.
This is a good candidate for a function because there will be one input variable, and we want to make it into another output variable every time we change the symbol.
The function will need to
read the number (volume from the current symbol in this case)
determine the length of the number (how many digits)
split the string into groups of 3 (separate every 3 digits)
construct it back to a string (from the split parts)
As you probably guessed by now, each of these steps can be written as a separate function if needed. I will do it all in one. The reading of the variable step is basically the existence of the variable itself. If you set it, it exists and can be used for reading.
Keep reading with a 7-day free trial
Subscribe to Peter’s Substack to keep reading this post and get 7 days of free access to the full post archives.