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.
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.
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
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..
// 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