Categories
Forex Videos

Forex Trading Algorithms Part 6 Elements Of Computer Languages For EA Design!


Trading Algorithms VI –  The Stages of a trading algorithm

In this video, we will discover the different parts needed for a complete trading system. 

One of the most common systems involves the crossover of two moving averages, a short- and a long-term SMA. Let’s do a system based on this idea.

Creating a trading algorithm involves at least two stages: the entry logic and the trade management logic, and the position sizing logic. 

The Entry Logic

The entry logic sets the rules for entries. The logic can be subdivided into two sections: the entry signal and the Filter or Trade setup

The entry signal  

An entry signal is a moment in time when something happens in the asset. Entries can be MA crossovers, level breakouts, bullish or bearish candlestick formations, and so forth.

The Filter

A filter is a condition imposed for the entry signal to be valid. For instance, you can allow a MA crossover to the long side only if the main trend is up. Then, you can programmatically define the primary trend, and this is the filter. For instance, we could describe an upward direction when the price is higher than the +1 SD line of a Bollinger Band ( in which we set the bands to 1 SD instead of the standard 2SD). We could also say that the upward trend is the price above its 200-period MA, or when there are higher highs and higher lows. A down-trend can be defined using the opposite logic.

On mean-reverting assets, an interesting filter might be overbought/oversold indicators such as RSI, Percent R, or Stochastics curving against the current move, allowing then use of candlestick reversal signals.

We can also add a filter that excludes trades whose projected reward/risk ratio is below one.

As a caveat, the higher the number of conditions, the higher the probability of over-optimizing it. The best designs are those with a few parameters. Also, the higher the number of filters, the less the system will trade. Thus, not always a filter improves a raw signal.

When building your trading system, a sound methodology starts with raw entry signals and a time stop at a determined number of bars. If the entry has an edge, it will be proved by higher than 50% profitable signals after 5-10 bars.

The trade management Logic

Trade management logic takes care of open trades. It is constituted of at least a stop-loss logic and a take-profit logic.  It may include other decision steps, such as break-even logic and trail stops.

The Stop-loss 

We have already published several articles on stop-losses. There are several ways to set a stop-loss level. We can do it using a multiple of the Average True Range of the asset, using the last swing low (or high in the case of a short-trade), or by statistically optimizing the distance using John Sweeney’s Maximum Adverse Excursion (MAE) concept.

Trail stops are also a recurrent idea in trading, although the developer should test them and assess if they really improve the results.  The same is valid for the break-even logic. Both concepts seem logical and mind relieving, but I have yet to find their utility to improve a trading system.

In some trading systems, a time stop can be handy. A time stop closes if the trade is not profitable after a certain period or a specific number of bars.

The Take-profit.

Take profit logic can also be varied, from dollar-based stops to stops based on key levels, supports, and resistances or pivots. A take-profit condition may be set, too, when a signal opposite to the current trade happens, such as an MA crossover against the trade, the price below the -1SD Bollinger line on longs or over +1SD line on short positions.

Take profit code can be added for scaling out the trade, letting a percent of the original position open to ride the wave and improve profits when your trade is right.

Position Sizing logic

The position size logic is a final step that involves setting up the right trade size for the trader’s objectives. This step should be used only in real-time trading, not during the definition, optimization, and evaluation of a new trading system.

During the definition step, a trading system must be used with one trade unit, and its results normalized to its risk, so instead of dollar profits, it should produce a stream of multiples of R, a standard one-dollar risk.

Position sizing logic is key to maximizing the returns of a system and limiting the max drawdown to the levels desired by the trader.

We will further develop these concepts in the coming videos, with specific algorithms demonstrating how to create them properly.

Categories
Forex Videos

Forex Trading Algorithms Part 4 Elements Of Computer Languages For EA Design!

Trading Algorithms – The Elements of a Computer Language – Part II

 

A computer program is a combination of data structures and a set of instructions or commands in the form of functions that process the data structures to construct a solution or solutions.

 

Control flow tools

To efficiently process information, a high-level programming language owns specific instructions to do mathematical operations, check for conditions, and control data flow.

 

The if Statement:

The if statement is the most basic flow-control instruction. It allows us to check for conditions or the validity of a statement.

for example, 

if x > 0 checks for the variable x being higher than zero. If it is zero or negative, it will deliver a False value. If over zero, it will provide a True condition.

The if statement, combined with the else statement, handles the flow of the information. If/else is mostly similar in all languages. ( Example taken from docs.python.org 

Iterators

Iterators are used to move through the components or a data structure, such as lists or arrays. There are several ways to iterate, some language-specific, but most are present in all languages.

 

 The for statement

The for statement is used to do an orderly iteration on an array or list. In C++, it has the following structure:

for (initialization; condition; increase) . Initialization is the starting point; condition defines the endpoint, and increase sets the step.

CPP example, source cplusplus.com

Python’s for is more versatile and simple. To loop through a list is straightforward (taken from docs.python.org):

But we can use the range() function to do a similar C++ for (taken from docs.python.org):

The While statement

The while statement creates a computer loop that is exited only after a certain condition is met:

For example, the above while loop appends the Fibonacci numbers up to n, storing them in the fibo list. The loop breaks only when a is bigger than n.

 

Function definition

In a computer app, the code repeats itself most of the time, sometimes the values may be different, but the basic computational structure is the same. To organize these computational structures, all computer languages have functions. 

In C++ a funtion is defined with the following structure:

<out type> function name (<type> parameter1, …. <type> parameter n){

body

}

The out type is the output type of the function. It can be an integer, a floating-point, or any other data structure, pointer, or no output at all.

The parameters are inputs to the function but can be used to modify an external structure as well.

In Python, the definition is simpler. 

def function_name ( parameter1…parameter n):

body

If the function returns a value or data structure, it is delivered through a return statement.

The following example shows the fib function, which computes the Fibonacci numbers up to the input parameter. The results of the Fibonacci computations are stored in the fibo list, which, after exiting the while loop, is returned. The variable res is assigned the output of the fib function and printed. Please note that the last two statements are not part of the fib function.

The last introductory article on high-level languages will talk about classes, objects, and object-oriented programming.

Once we have completed this basic wrap-up on programming language features, we’ll start studying trading-focused algorithms in the coming videos.