Algorithmic Trading Tips 17.

BUY AND SELL ORDERS, POSITION SIZE AND STOPS.

PART 1: THE TOOLBOX FOR SYSTEMATIC TRADERS.

This issue contains five modules for the development or testing of a trading strategy in Tradesignal. An entry, a position size control and different stop types – modules allow quant traders a quick and easy start in the development of algorithmic trading strategies.

DOWNLOAD WORKSPACE. (For users with Refinitiv Eikon.)
DOWNLOAD WORKSPACE. (For users with Bloomberg.)

By downloading you agree to the terms of use below.

One of the great advantages of Tradesignal is the ability to develop and backtest trading systems quickly – even without programming knowledge or statistical studies. The algorithmic trading software provides hundreds of predefined examples and almost every indicator is included in its textbook format. If you look for the moving average for example, you will find all trading strategies associated with this indicator quickly and you can display all entry and exit signal on the chart. Therefore, many ideas can already be translated into action by using the prebuilt strategies.

In this Algorithmic Trading Tips edition, however, I’d like to present to you my private tools which will make your strategy development even easier.

01. THE ENTRY-MODUL.

Let us start with a test trading strategy which is responsible for the entry signals. It is based on a simple moving average and the Slow Stochastic Indicator.

Inputs:​​ PeriodSMA(21),​​ PeriodStoch(5);

Variables:​​ ma,​​ stoch;

 

ma=average(close,PeriodSMA);

stoch=slowk(PeriodStoch);

 

if​​ close>ma​​ // market above average

and​​ ma>ma[2]​​ // and average rises (bigger than 2 bars ago)

and​​ stoch​​ crosses above​​ 50​​ // and slowK crosses above 50

then buy this bar​​ on​​ close;​​ // --> buy!

if​​ close<​​ ma

and​​ ma<ma[2]

and​​ stoch​​ crosses below​​ 50

then short this bar​​ on​​ close;

drawline(ma);

 

The strategy is exemplified in two parts: First, the trend direction is determined. To place a long trade, the market must be above its rising moving average (SMA). If that is the case and the Slow Stochastic crosses above 50, the actual entry is triggered on the close of the next bar. This two-part approach ensures that many signals are generated in the trend direction. This will be advantageous if our position is eventually closed early although the trend is still intact. Thanks to the oscillator the next entry signal in trend direction is just a few bars away.

Figure 1 shows the pure entry signals on the chart. One necessary improvement can be seen too – the size of the position.

 

THE REASON BEHIND THIS: Backtest results will be heavily distorted, if you trade a market that fluctuates by more than 50% with a fixed position size all the time, then simply by higher absolute price levels a contract at 10,000 points would have a higher impact than a contract which is bought at 5,000.

 

BY THE WAY: If you enter the command drawline(slowk(5)) into the command line of Tradesignal, the Stochastic indicator is displayed below the chart. You can then add a line at 50 to visually get a clear understanding of how entry signals are generated.

FIGURE 1: ENTRY SIGNALS.

The entry module generates long and short signals based on a rising SMA and the Slow Stochastics Indicator.

02. THE POSITION SIZE MODULE.

The position size can easily be defined by the next script. It invests the same amount of capital in each trade signal. Thus, the influence of the absolute price levels is compensated.

Meta:​​ Synopsis("Invests the same amount of money in every trade");

Inputs:​​ Invest(100000);

Variables:​​ con;

 

con=round(Invest/close,0);

 

SetDefaultQuantity(maxlist(1,con));

On the following chart you can see the result: With every trade now EUR 100,000 are invested in the DAX (as you can see the size varies for each buy or sell signal). This prevents distortions in the backtest due to the high volatility of the underlying market.

FIGURE 2: ENTRY SIGNALS WITH POSITION SIZING MODULE.

In order to eliminate distortions due to different levels of price levels, the position sizing module is used, which employs a fixed capital amount for each trade (100.000 Euro in this example).

03. THE INITIAL STOP MODULE.

What stands at first glance while looking at the chart, is the lack of a stop loss. Although the entry strategy opens a position in trend direction at some time, as you can see in Figure 2, the strategy has long been caught in a short trade for a while. Use the following code to insert an Equilla initial stop into the strategy. The width of the stops can be controlled in the properties section – either on an absolute or a percentage level. In addition, visuals will (and this is the case in all scripts) also appear on the same chart, where the strategy places the stop.

INITIAL STOP (ABSOLUTE).

Meta:​​ Synopsis("Sets cash stop for total position");

Inputs:​​ StopCash(10000);

 

setstopposition;

setstoploss(StopCash);

 

drawsymbol(getactiveorderprice(1));

drawsymbol(getactiveorderprice(2));

INITIAL STOP (PERCENTAGE).

Meta:​​ Synopsis("Sets % Stop Loss");

Inputs:​​ StopPercent(10.0);

Variables:​​ price;

 

if isvalid(entryprice)​​ then​​ price=entryprice else​​ price=close;

 

setstopcontract;

setstoploss(price*StopPercent/100);

 

drawsymbol(getactiveorderprice(1));

drawsymbol(getactiveorderprice(2));

FIGURE 3: IMPLEMENTATION OF AN INITIAL STOP.

In the present example, a stop module has been implemented. The cash stop risks an absolute amount of 10.000 Euros.

04. THE PROFIT TARGET MODULE.

If the position is hedged with an initial stop, you can take the next step the following scripts to realize the accrued gains. These are, in each case, a simple profit target – either in absolute or in percentage terms. Please note that the absolute profit target is for the overall position, and not for individual contract to which it applies.

PROFIT TARGET (ABSOLUTE).

Meta:​​ Synopsis("Sets cash target for total position");

Inputs:​​ CashTargetTotalPosition(1000);

 

setstopposition;

setprofittarget(CashTargetTotalPosition);

 

drawsymbol(getactiveorderprice(1));

drawsymbol(getactiveorderprice(2));

PROFIT TARGET (PERCENTAGE).

Meta:​​ Synopsis("Sets Percent Target");

Inputs:​​ PercentTarget(10.0);

Variables:​​ price;

 

if marketposition=0​​ then​​ price=close​​ else​​ price=entryprice;

 

setstopcontract;

setprofittarget(price*PercentTarget/100);

 

drawsymbol(getactiveorderprice(1));

drawsymbol(getactiveorderprice(2));

As you can see, the use of the oscillator initially described brings a clear benefit here: In 2013 the profit target has been reached twice, but the entry-level logic has brought the system back into position quickly.

FIGURE 4: USING PROFIT TARGETS.

In this example, a profit target has been added to the modules already included. It performs a profit-taking on reaching a freely definable absolute profit level.

05. THE TRAILING STOP MODULE.

In addition to the use of a pure profit target, protecting accrued gains without realizing them with a fixed barrier can also be a rewarding exit strategy. The following Equilla code allows you to use such a trailing stop.

Meta:​​ Synopsis("Protects Profits. Stop is set % below last trade equity high");

Inputs:​​ PercentTrailingStop(10.0);

Variables:​​ hh,​​ ll;

 

If barssinceentry>=0​​ then begin

hh=highest(high,maxlist(1,barssinceentry+1));

ll=lowest(low,maxlist(1,barssinceentry+1));

if marketposition=1​​ then sell next bar​​ at​​ hh-PercentTrailingStop*hh/100​​ stop;

if marketposition=-1​​ then cover next bar​​ at​​ ll+PercentTrailingStop*ll/100​​ stop;

end;

 

setstopcontract;

setstoploss(close/100*PercentTrailingStop);​​ // close used, as entryprice is not available at first bar

 

drawsymbol(getactiveorderprice(1));

drawsymbol(getactiveorderprice(2));

NOTE:
Please note that when applying the exits Tradesignal always automatically takes the exit, which is closer to the current market level. If we add another five percent stop loss into the chart above, the stop remains five percent below the entry price, until a gain of more than five per cent has been reached. In this case, the 10 percent trailing stop would be closer to the market than the initial stop and would be gradually trailed into the absolute gain range.

FIGURE 5: USING A TRAILING STOP.

In order to achieve a continuous hedging of accrued gains, a trailing stop is used in this example. With this tool, trends can be optimally utilized.

 

DEVELOP AND TEST YOUR ENTRY STRATEGY USING THE MODULAR PRINCIPLE.

With the presented modules you can test the robustness of any entry idea in a simple manner. Without “crunching numbers” you can adjust the correct parameters visually. Don`t pay too much attention on the results of the strategy at this stage of system development, but rather on whether the signals are plausible or not.

As you can see on Figure 4, a trailing stop of 10% is a good choice for this market and entry style. A much smaller trailing stop would not exploit the trends, while a much further stop would not protect profits enough. I prefer to find out the first parameters for a potential further optimization in a visual way rather than by stubborn optimization. Therefore, all presented scripts show where you place the orders.

A workspace including this indicator can be downloaded.