You’ve probably never heard of it, but it’s D or Dlang. A programming language with the simplicity of Python and speed of C (or extremely close to it).
I used it to build all my algo-trading bots as well as to create algo-bot starter code. Plus, to demonstrate how fast, simple, and powerful Dlang really is, I published a (FREE) AI For Finance Course.
And the syntax?
Very familiar and very simple.
If you ever coded in JavaScript, C, C++ … you’ll feel right at home.
import std.stdio : print = writeln; void main() { auto numbers = [1, 2, 3]; foreach(number; numbers) { print(number); } }
Why not Python?
Because when you’re using Python, you cannot code in Python.
(and this applies to other “fancy-cool” languages)
You have to use some library that uses C language underneath.
And C just uses a regular for loop to iterate through the data.
You would also rather use for loops if Python was not so excruciatingly slow… like 80x or some wild number.
What no one tells you, is that when you chose to learn/use Python you’re required to learn many complex library syntaxes, like Pandas, NumPy, etc.
Here is just a small taste of problems when dealing with Python DataFrames:
# if you want to find an element -> .iloc df.iloc[some_index] # but if you want to change an element -> .loc df.loc[some_index] = new_value # but if you want to change last element df.loc[-1] = new_value # error - invalid index # but -1 on .iloc is perfectly fine df.iloc[-1] # returns the last row
Don’t get me wrong… I like Python… I really do… but I wouldn’t entrust any serious cash to that slow, unreliable, runtime-error piece of 💩
Don’t agree?
Oh good… I wish all my competitors used Python 😈
Rust?
Undoubtedly a great language. Maybe even “better” than D. But it has a steep learning curve.
So how can it be the best when it’s hard to learn?
When it takes away my time and focus from algo development?
It can’t be the best.
Why not just C or C++?
For similar reason as Rust – it takes away algo development time.
Maybe we’re all more-less familiar with the syntax (especially if you ever touched JavaScript).
But the complexity and verbosity required to do even the simplest of tasks is insane.
(it’s so bad I’d rather use Python) 😅
What’s so special about D or Dlang?
D/Dlang allows you to use the same language for prototyping and production.
It allows you to prototype things quickly without thinking about the low-level stuff.
But it sure gives you every opportunity to go as low as you want.
(including Assembly-level low with just one line of code)
D language basically took all the best parts of C and C++ and removed all the worst parts.
And now you have this engineering marvel no one even talks about (dating back to 1999).
Therefore, you can be rest assured it is tested, stable, and it stood the test of time.
Examples of D
Let’s say you need to calculate simple average. You can always use for loops and this is perfectly good and optimized code:
void main() { auto numbers = [1.2, 3.4, 5.6, 7.8]; auto total = 0.0; for (auto i = 0; i < numbers.length; i++) { total += numbers[i]; } auto average = total / numbers.length; // 4.5 }
But you can also do it like this:
import std.algorithm.iteration : sum; void main() { auto numbers = [1.2, 3.4, 5.6, 7.8]; auto average = numbers.sum / numbers.length; // 4.5 }
Or even simpler:
import std.algorithm.iteration : mean; void main() { auto numbers = [1.2, 3.4, 5.6, 7.8]; auto average = numbers.mean; // 4.5 }
How about calculating moving average?
import std.algorithm.iteration : mean; import std.range : iota; void main() { auto numbers = 1000.iota; // [0, 1, 2 ... 999] auto ma_50 = numbers[$-50 .. $].mean; // 974.5 }
Want to do operations on an entire array? No problem:
void main() { auto arr = [1, 2, 3]; arr[] *= 2; // [2, 4, 6] }
How about joining arrays?
void main() { auto arr = [1, 2, 3]; auto addedArr = [4, 5, 6]; arr ~= addedArr; // [1, 2, 3, 4, 5, 6] }
How about doing operations with each individual element of the array:
import std.stdio : print = writeln; void main() { auto a1 = [1, 2, 3]; auto a2 = [4, 5, 6]; a1[] = a1[] + a2[]; // a1[] += a2[]; print(a1); // [5, 7, 9] }
How about all those high-level programming language functions like map, reduce, or filter?
import std.algorithm.iteration : filter; void main() { auto arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; auto evenArr = arr.filter!(x => x % 2 == 0); // [2, 4, 6, 8] }
How about making an API call:
import std.stdio : print = writeln; import std.net.curl : get; import std.json : parseJSON; void main() { auto response = get("https://dummyjson.com/products/1"); auto json = parseJSON(response); print(json["title"]); // "iPhone 9" print(json["brand"]); // "Apple" }
And you get all of this at near-C speed. 🤯😲
If you are not impressed by now, I have no idea what’s wrong with you.
Who would want to go back to Python after seeing this.
Bonus
If you’re still thinking about Python, here is something Python cannot do – multi-core execution.
Instead, it just fakes it by running multiple threads on a single core.
Totally weak.
On the other hand, D can do it effortlessly either for API calls or heavy calculations. Just use parallel:
import std.net.curl : get; import std.json : JSONValue, parseJSON; import std.parallelism : parallel; void main() { auto productUrls = [ "https://dummyjson.com/products/1", "https://dummyjson.com/products/2", "https://dummyjson.com/products/3" ]; JSONValue[] jsonResponses; foreach(url; parallel(productUrls)) { auto response = get(url); jsonResponses ~= parseJSON(response); } // jsonResponses array contains all 3 products }
Even more impressive
You get all of this (and more) without any external libraries.
Everything I just showed you is a part of the standard library that comes with D language compiler.
So all you need is download DMD compiler and simply run…
rdmd your_program.d
…just like you would run Python or NodeJS program.
In fact, an entire algo-bot starter code was developed from scratch without using a single external library!
This is how powerful D language really is.
It is enough for everything you will want to do in finance.
P.S.
To be clear… since D language is so awesome, it comes with package manager (DUB) that is also bundled with the compiler. And adding a package is simple as:
dub add <packageName>
…and it overall works similar to npm from NodeJS
P.P.S.
You can also try out D language without installation through this interactive online tour.
Wanna see D in action?
(no signups & no newsletters)