What Is an Algorithm?
An Algorithm is an effective procedure, a way of getting something done in a finite number of discrete steps.
David Berlinski
Berlinski's definition is exactly right on the money. The word algorithm sounds mysterious as well as intellectual but it's really a fancy name for a recipe. It explains precisely the stuff and steps necessary to accomplish a task. Even though you can perceive an algorithm to be a simple recipe, it must, like all things dealing with computers, follow specific criteria:
- Input: There are zero or more quantities that are externally supplied.
- Output: At least one quantity is produced.
- Definiteness: Each instruction must be clear and unambiguous.
- Finiteness: If we trace out the instructions of an algorithm, then for all cases the algorithm will terminate after a finite number of steps.
- Effectiveness: Every instruction must be sufficiently basic that it can in principle be carried out by a person using only pencil and paper. It is not enough that each operation be definite as in (3), but it must also be feasible. [Fundamentals of Data Structures: Ellis Horowitz and Sartaj Sahni 1976; Computer Science Press]
These criteria are very precise because they can be universally applied to any type of problem. Don't be turned off thinking this is going to be another computer science text, because even though the criteria of an algorithm seem to be very formal, an algorithm really is straightforward and quite eloquent. It is basically a guide that one must follow to convert a problem into something a computer can solve. Anybody can design an algorithm following these criteria with pencil and paper. The only prerequisite is that you must think like a Vulcan from Star Trek. In other words, think in logical terms by breaking ideas down into rudimentary building blocks. This is the first step—translation of idea into an algorithm. It takes practice to do this, and this is in part why programming can be difficult.
Another thing that makes programming difficult is understanding a computer language's syntax. Most people who have been exposed to a programming or scripting language at one time or another in their lives have probably exclaimed something like, “I forgot one stupid semicolon and the entire program crashed! Isn't the computer smart enough to know that? Arrgh! I will never be a computer programmer!” The question that is proffered in this temper tantrum is the question of the computer's intelligence. Computers are not smart—they only do what we tell them. It doesn't matter if you spend $500 or $5,000 on the hardware. They do things very quickly and accurately, but their intelligence is a reflection of their programmer's ability to translate idea into algorithmic form and then into proper syntax.
Algorithmic (algo) traders don't necessarily need to be programmers, but they must understand what a computer needs to know to carry out a trading signal, position sizing, and money management. If you can create an algorithm, then you are more than 50 percent there. I say more than 50 percent because most traders will utilize trading software and its associated programming or scripting language. Learning the syntax of a scripting language or a small subset of a programming dialect is much easier than learning an entire programming language like C# or C++. An algo trader only needs to be concerned with the necessary tools to carry out a trading system. The developers of EasyLanguage, AmiBroker, or TradersStudio's main objective was to provide only the necessary tools to put a trading idea into action. They accomplished this by creating a vast library of trading functions, easy access to these functions, and a simplified programming syntax. Now if you want to develop your own testing platform and want to use a full-blown programming language to do so, then you will need to know the language inside-out. If you are interested in doing this, chapters 5 and 6 will give you a head start. In these chapters, I show how I developed testing platforms in Python and Microsoft VBA from scratch.
However, at this introductory stage, let's take a look at a very simple trading algorithm and the possible exchange between a computer and trader. Pretend a trader wants to develop and test a simple moving-average crossover system and wants to use software specifically designed for system testing. Let's call this first trader AlgoTrader1, and since he has used this particular testing platform he knows it understands a trading vernacular and provides access to the common indicator functions and data. Box 1.1 shows a possible exchange between trader and computer.
Box 1.1 Algo Testing Software
AlgoTrader1 – AlgoTester ON Computer – AlgoTester ready AlgoTrader1 – load crude oil futures data Computer – data loaded AlgoTrader1 – buy whenever close is above moving average Computer – "moving average" function requires three inputs AlgoTrader1 - help with moving average function Computer - function calculates simple, weighted, exponential average Computer - function syntax moving average (type, price, length) AlgoTrader1 - buy whenever close is above moving average (simple,close,21) Computer – command completed AlgoTrader1 –short whenever close is below moving average (simple,close,21) Computer – command completed AlgoTrader1 – save algorithm as MovAvgCross Computer – command completed AlgoTrader1 – run MovAvgCross algorithm Computer – run completed and results are: $12,040 profit, $8,500 draw down, $1,200 avg. win AlgoTrader1 – load euro currency data Computer – command completed AlgoTrader2 – run MovAvgCross algorithm Computer – run completed and results are: -$32,090 profit, $40,000 draw down, $400 avg. win AlgoTrader1 – edit MovAvgCross algorithm Computer – command completed AlgoTrader2 – edit moving average function Computer - command completed AlgoTrader2 – change length input to 30 Computer – command completed AlgoTrader2 – run MovAvgCross algorithm Computer – run completed and blah blah blah
As you can see, the computer had to be somewhat spoon-fed the instructions. The software recognized many keywords such as: load, buy, short, run, edit, change, and save. It also recognized the moving average function and was able to provide information on how to properly use it. The trading algorithm is now stored in the computer's library and will be accessible in the future.
This simple exchange between computer and AlgoTrader1 doesn't reveal all the computations or processes going on behind the scene. Loading and understanding the data, applying the algorithm properly, keeping track of the trades, and, finally, calculating all of the performance metrics did not involve any interaction with the trader. All this programming was done ahead of time and was hidden from the trader and this allows an algo trader to be creative without being bogged down in all the minutiae of a testing platform.
Even though the computer can do all these things seamlessly it still needed to be told exactly what to do. This scenario is similar to a parent instructing a child on how to do his first long-division problem. A child attempting long division probably knows how to add, subtract, multiply, and divide. However, even with these “built-in” tools, a child needs a list of exact instructions to complete a problem. An extended vocabulary or a large library of built-in functions saves a lot of time, but it doesn't necessarily make the computer any smarter. This is an example of knowledge versus intelligence—all the knowledge in the world will not necessarily help solve a complex problem. To make a long story short, think like a computer or child when developing and describing a trading algorithm. Box 1.2 shows an algorithmic representa...