Trading Algorithms – The Elements of a Computer Language – Part III: Objects
The most striking feature of modern programming is object-oriented programming. This video will explain the underlying philosophy and why OOP is such a big deal in modern app development.
Procedural programming versus OOP
Traditional programming is based on procedures or functions applied to a pre-defined collection of data structures. The main procedure starts moving and modifying variables and structures to obtain an output to print or display on a screen.
The main drawback is that most of the primary data is globally allocated and potentially modified by other application sections. Thus a change to improve or correct one section of the code may interact with other sections, potentially creating hard to detect new bugs. The maintenance of large projects based on procedural programming is a nightmare, especially when a different programmer has to do it.
Object-oriented programming, on the other hand, uses objects with their own inner data structures. So, code mods happen within a single self-contained object, and any new bug is limited to that object.
The basic unit on Object-Oriented Programming is the Class. A Class is the description of an Object. Then, several objects are to be created using that Class description, called “instances” of the Class.
Simply put, a Class is a collection of data structures and the procedures or functions allowed for these data structures. Classes provide data and function together.
In our real-life, we are surrounded by objects with shape and functionality, such as cars, TVs, houses, and pants. All have their intrinsic properties. A vehicle has an engine, four wheels, battery, throttle, brakes, steering wheel, doors, seats, and so forth, and all these parts are also objects. But not all cars are equal; brand, color, engine power, seat materials, etc., change. That also happens with computer objects.
A new class can be created from a parent class, with new functionality, or with changing functionality from the parent class in a process called “inheritance.”
An example of a class
The Bag class is just a container for other objects. We can add or take out items to and from the Bag. The main data storage is in the self.data variable. But, bear in mind that self.data is different for every new Bag object created!. We can see that the data structure of the Bag object cannot be accessed but with the supplied methods, add, sub, and show.
A Python financial class
A financial class can be made of around a historical OHLC data structure. Using it, we can create new information such as indicators and various stats, such as swing high/low length and duration statistics, and other information related to price analysis and forecasting.
You can see an example of what a pro-built class can do by looking at the stock-pandas class package documentation. We can see that the stock-pandas project is solely focused on the creation of a class to handle statistics and indicators for a financial data series, presenting a complete package.
As we can see, the advantages of OOP are huge. Packages can be built, which, later, can easily be versioned, updated, and expanded. The creation of apps using classes and OOP is much more straightforward, so the time needed to complete a project is shortened drastically.
Now that we have reviewed the basics of modern programming, let’s move back to trading algorithms.