Practical use of martingale code in automated trading

Category: Programming

 

When it comes to  automated trading, we often read that no automated strategy can be as good as a manual trading system managed by a smart trader with many years of trading knowledge behind him. That is true, and not true. Because every path lead to Rome, you know, there is not a single path to follow in trading ; while some people make a living with averaging down, some others loose everything with their bad stoplosses price levels.

Of course, i will not say that martingale money management is the way to get better and consistent profit, but it can give us an hedge on medium performance’s trading strategies that underperform some time.

As i constantly tell to my customers, automated trading need often more work than manual trading, as it relies on maths, theory, hard research and myriad of others technicals things we have to manage : slippage, good data, spread, server lags, etc.

Fortunately, ProRealTime comes with ProOrder and server side execution, we got here a very nice improvement on what represent automated trading for retails traders. No technical issues to handle, no laggy VPS or expensive server to pay, everything is managed by ProRealTime servers and that’s a very good point for us. But that’s not the subject of this article, so let’s do programming with martingale codes we can find in the ProBacktest documentation.

 

A simple trading strategy to automate

Let’s make a trading strategy with breakout entries on the last X days high. Then, positions exit will be managed by Y days low breakout.

 

The indicator code for better visualization of entries and exits : (you’ll find the ITF file attached at the end of this article)

// hp = 50 (periods of the highest high lookback for entries purposes)
// lp = 20 (periods of the lowest low lookback for trades exit)

x = highest[hp](high)
y = lowest[lp](low)

RETURN x as "long term highest", y as "short term lowest"

 

Now the ProBacktest code for the automated entries and exit :

// our tresholds for our long only strategy
x = highest[50](high)
y = lowest[20](low)

// we are buying when close of the actual candle is above the previous 50 bars highest
TradeBuy = close>x[1] 
// we exit our buy trade when close of the actual candle fall below the last 20 bars low
TradeBuyExit = close<y[1]

IF NOT LongOnMarket AND TradeBuy THEN
   BUY 1 CONTRACTS AT MARKET
ENDIF

If LongOnMarket AND TradeBuyExit THEN
   SELL AT MARKET
ENDIF

 

Good performance of a classic breakout strategy traded on Spanish IBEX35 index, with no optimised parameters, as we are using 50 and 20 periods that are quiet common.

IBEX breakout strategy performance

Martingale position sizing

Now let’s figure how can result this strategy with a D’Alembert martingale pyramid :

Here is the code :

ONCE OrderSize = 1
ONCE ExitIndex = -2

// our tresholds for our long only strategy
x = highest[50](high)
y = lowest[20](low)

// we are buying when close of the actual candle is above the previous 50 bars highest
TradeBuy = close>x[1] 
// we exit our buy trade when close of the actual candle fall below the last 20 bars low
TradeBuyExit = close<y[1]

IF NOT LongOnMarket AND TradeBuy THEN
   BUY OrderSize CONTRACTS AT MARKET
ENDIF

If LongOnMarket AND TradeBuyExit THEN
   SELL AT MARKET
ExitIndex=BarIndex
ENDIF

// positions sizing
IF Barindex= ExitIndex+1 THEN
   ExitIndex =0
IF PositionPerf(1)<0 THEN
   OrderSize = OrderSize+1
ELSIF PositionPerf(1)>=0 THEN 
   OrderSize =MAX(OrderSize-1,1)
ENDIF
ENDIF
This martingale was made famous by d’Alembert, a French 18th century mathematician. In case of loss, the position size is increased by 1 unit, in case of gain it is decreased by 1 unit. This technique of position size management is relevant only if we suppose that successive gains reduce the probability of winning again and successive losses reduce the probability of losing again.

IBEX breakout strategy performance with martingale

Of course the gain are higher, it’s a martingale sir 🙂 but loss too.. Though, 540% performance for a 50% drawdown, more than the double drawdown for 10x gain on the initial version can make us decide to study it a bit more..

For this trading strategy, on the original version without martingale, there were only 30% of winning positions. So let’s try the other version of this D’Alembert martingale theory that make an inverse position sizing while we are winning : it increased the position by 1 lot and reduced it while losing.
In the code, we just have to change the positing sizing formula :
// positions sizing
IF Barindex= ExitIndex+1 THEN
   ExitIndex =0
IF PositionPerf(1)<0 THEN
   OrderSize =MAX(OrderSize-1,1)
ELSIF PositionPerf(1)>=0 THEN
   OrderSize = OrderSize+1
ENDIF
ENDIF
Let’s give it a try :
IBEX breakout strategy performance with anti martingale
OK, now what we see here is clear. A 30% winning strategy that give you good result at the end of your backtest is a strategy where you can add a martingale as a money management system. Why ? Because winners are always bigger than losers so if you can already have good performance with as little as 30% winning trades, then you already have a solid strategy on your side, and increase your positing sizing will give you better results, at the end, than the original one.

What to conclude ?

Thus, is it better to increase my risk to increase my performance ? The choice is yours, no one will give you a clear answer while it is only a question of risk aversion on your side. If you can stand big drawdown you surely can have it in your trading arsenal. But remember that martingale position sizing is only a mathematical approach that will work in an universe where your time and your account balance is infinite. So since it is impossible in real life, it is largely possible that big losers will erase past good results, you must be aware of that fact, really.
So what we can conclude, at the end ? Well, good strategies merit martingale, not the bad ones.

 

 

 

Logo Logo
Loading...