Myth or Reality? Can We Create Effective Trading Strategies with Just 3 Lines of Code?

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.

BOOK YOUR FREE STRATEGY SESSION NOW >>

How many lines of code does it take to create an effective trading system? Many traders are convinced that between entries, exits, conditions and filters, numerous lines of code must be programmed to create well-performing systems.

In this video, we tried to understand if this is really the case or if it’s just another false myth....

By watching it, you’ll discover:
-how the length of a trading system can affect its performance
-why more conditions don’t lead to more robustness
-what overfitting is and why it can be related to the length of a system
-what performance we’ve achieved when trading Feeder Cattle using a system consisting of only three lines of code

Enjoy! 😎

Transcription

Introduction

Hey, everyone, and welcome to this brand new video! You know, a few lines of code certainly don't make a tradable strategy.

You often hear that you need many, many more, several dozens if not hundreds of lines. To set the entries, to set other conditions, not to mention the exits from the positions.

But are we really sure? One of the coaches at Unger Academy here, and today we're going to be dispelling this pretty common myth!

So, what we're going to be doing is we're going to discuss the length of code that is needed to trade our strategies live.

We'll try to find out if our codes work well, even if they do consist of only a few lines.

And we're going to do that by analyzing a real strategy that's been live for five years.

What is the length of the code? Three lines, which we’ll reveal together in a moment.

Before we start, though, please go and subscribe to our channel, click on the notification bell and leave us a Like as well. We thank you so much!

Overfitting in our strategies

Okay, so the fundamental question is: Do more conditions lead to better results?

First let's take a small step back and introduce the concept of overfitting, that invisible monster that, unfortunately, plagues all of our strategies, and from which we must try to stay as far away from as possible.

But what is overfitting, and how can our codes bring it up?

Let’s make a simple chart that will help us understand it better.

Let's imagine that we want to collect as many green notes as possible with a single line.

Here, we have got a straight line in this first example, and as you can see, the result could be better.

So let's go and add a few more conditions, which means changing the shape of this line, in order to maximize our result.

So let's continue in this direction and let's introduce as many conditions as necessary to bring home all the green notes on our table.

In the latter case, we’ve clearly produced an “overfitting” situation.

Leaving this example to go back to our trading systems, we could say that when we enter an excessive number of conditions, basically we are overfitting our strategy.

And why do we do that? Because we want the strategy to fish all the profitable trades from the historical data, leaving out the others.

However, this practice usually leads to losses in real trading precisely because the system doesn’t yet know the future data.

Suppose we want to find the right tradeoff between the number of conditions and system performance. In that case, we could say that the optimum is achieved with a limited number of conditions beyond which performance inevitably worsens.

When I talk about performance, I’m referring to the real performance of a live system, not to backtesting results.

Okay, but how do you know the optimal number of conditions?

Well, there’s no magic formula. The number really depends on many factors.

But it's still about finding the right balance between the number of conditions and the results you get, and this is something you'll refine over time as you develop your own strategies.

Now I told that we would see a simple strategy today that consisted of very few lines and yet still made good profits since when it entered the market.

Starting trigger on Feeder Cattle

It's time to take a closer look at this strategy.

So let's switch to our MultiCharts platform and upload the continuous 5-minute chart of Feeder Cattle with data from 2010.

First things first, the Feeder Cattle market is a bit of a peculiar market.

I say peculiar because, first of all, the trading session is relatively small: it goes from 08:30 am to 1:05 pm Exchange time, so, it lasts just a little over 4 hours, and this can cause some overnight gaps.

Also, the session hasn't always been as it is now.

Another aspect to consider is that the stock market sets overbought or oversold limit prices, beyond which it’s no longer possible to trade during the session.

So sometimes we aren’t able to open a position – which may not be much of a problem - or even to close a position.

Nevertheless, the Feeder Cattle has some interesting advantages, including the possibility to trade it with low capital and to set stop losses that can be as low as $400-$500 in intraday.

Let's start with a very simple trigger consisting of two lines.

So let's go and see our script. With the first line, we’ll buy with stop orders at the current week's high if we haven’t yet entered a position on the present day.

Conversely, we’ll sell, with stop orders, again, at the current week's low.

We‘ll apply our signal, or better said, our trigger, to the chart and then check the metrics.

First, we see that the strategy, if we want to call it that, has made a substantial profit of $168,000, which is well distributed between long and short.

It has a maximum drawdown of $21,000.

The shape of the curve is quite appealing, and both long and short curves have a good slope.

The average trade is $160, again well distributed between long and short.

Then, if we look at the annual results, we see that, apart from this year, the strategy has been performing quite well.

When we look at our code, though, we might have some doubts: we’ll enter with stop orders at the high or low of the current week, but we haven’t applied any time window.

So, at the beginning of the week, the high and low may not yet be far enough apart.

Let's take a look at this in the chart.

This is a Monday morning, and as we can see, the entry occurs only a few bars from the beginning of the session.

We could now run a test and change our code by adding a new condition.

So let's exclude the first day of the week, Monday, from our trading.

Now let's go and upload this new signal into our chart and let's go and see what happens.

So, the net profit has gone down, but after all, we did take a day out from our trading, so that is quite natural.

The equity isn’t as good as before, but it is still growing on both the long and short sides.

The average trade is almost unchanged.

So, are we going to add this new condition?

I’d say no because our goal is to create a profitable strategy with a minimum number of conditions.

It was enough for us to know that the exclusion of Monday from our trades didn’t significantly change our metrics.

Applying a Stoploss

However, we do have a trigger without a Stop loss.

We intentionally didn’t include it because we wanted to understand how our instrument responds to this simple signal with as few conditions as possible.

However, the Maximum Adverse Excursion shows many losing trades with a drawdown of over $1,000.

This suggests that we put a stop near a level of $1,000.

Let’s go to our code and enter a simple line that closes all of our trades after losing $1,000.

Let's upload this new signal to our platform and go and see how the metrics have changed.

Well, the net profit has gone up to $180,000.

The maximum drawdown has dropped to $16,000.

We are seeing a smoothly increasing equity on both the long and short sides.

The average trade, that remained at the level of about $165.

So, we can say that we have regulated our strategy a little bit, and as we can see in the annual results, we’ve also benefited from that.

Is it just a trigger or is it a strategy?

So guys, is that a trigger or is that already a strategy?

Right now we surely have some doubts.

Let's go back to the metrics. The maximum drawdown is less than 10% of the Net Profit.

We can also see that the equity line is very regular.

However, the average trade is still not big enough: look, 165$ for this instrument doesn’t protect us from the slippage costs that may derive from trading this market.

So, we inevitably have to introduce another condition to try to improve the system.

For example, we could introduce a pattern. I’ve started an optimization that I want to show you, and I’ve sorted the results here by decreasing Net Profit.

Remember that we’re dealing with a commodity: so we need to look for conditions where long and short mirror each other.

We started with this condition that I'm showing you, so the strategy with no pattern, and here we see the metrics that we already know.

We notice that the pattern that we’ve called "-13" leads to a slight increase in the maximum drawdown, but it also causes the Average trade to make a significant upswing.

So, yes, we like this pattern.

In addition, its "close relative", let's call it that, so the "-14" pattern, is associated with a very similar Average Trade.

So let’s introduce this pattern, but… what is it?

Let's go to our code and have a look at that right now.

This is how the code would change.

Namely, we’ve inserted a downtrend condition on the long side and an uptrend condition on the short side.

We’ll then buy on the current week’s high if yesterday’s Close was lower than the previous day’s close, and then we'll sell at the current week's low if yesterday's Close was higher than the day before.

Strategy analysis

That’s the code we got. Very simple.

But it doesn’t mean we have to stop there. On the contrary, we must look closer to see if there's something else that gives us some doubts.

Back to the metrics: the Net Profit is $194,000, well distributed between long and short.

The maximum drawdown is below 10%, and we like that.

We can also see it in the equity curve, it looks balanced between the long and short sides.

By the way, I want to show you how the strategy continued to work well after going live, more or less at the point that I'm showing you now.

The Buy & Hold doesn’t show any underlying bias, so we can’t assume that the instrument itself supported the strategy.

The average trade is excellent: at $310, and yes, we’re safe from slippage and commission costs.

In the annual overview, we also see that the strategy has performed well across the historical range, and even 2022 has been positively impacted by introducing the latest pattern.

The strategy works about once a week, and the average trade per year shows us that since it went live, namely, since 2018, it's maintained a good pace.

However, let’s look at some metrics we typically pay less attention to.

Using the time analysis, we can see that the strategy stays in the market about 80% of the time.

Here we can also see the average trade duration as well as the average number of bars per trade, I’d say about 20 hours, so one week, in line with the trigger that we used at the beginning.

I personally also look at the Total trades, and as we can see, they’re evenly distributed over the historical range, they aren’t concentrated in any particular area, and I like that.

With regards the MAE, there are losing trades to the right of the $1,000 maximum drawdown.

We might ask ourselves, how is that possible? We had set a stop.

The reason is the overnight gaps.

As we can see, this first trade was closed correctly with a stop, while this second trade was closed at the beginning of the next session, but with a loss higher than the stop loss that we had set.

Should we add more conditions?

So can we introduce some new conditions to improve the system?

For example, we could add a time window, but the session is already very tight.

At most, we could exclude the first and last two bars of the day to avoid trading at the session changes.

We could also enter a take profit or other patterns, but in doing so, we’re taking two risks.

First, we’ll be increasing the risk of overfitting the system, which we don't want, which we don't like.

And second, we’d lose five years of out-of-sample performance, which has told us a lot about how the strategy behaves when it does go live.

Final thoughts

So, we’ll decide to leave it at that.

Our work is done, and we can officially say that we’ve developed a strategy that consists of only three lines.

This strategy has proven to work well over the last five years and has led to excellent results.

The minimal number of conditions also makes us reasonably confident that such a strategy knows almost no overfitting.

So, to answer the question that we started with at the beginning of the video, can more conditions lead to better results?

What we’ve seen seems to tell us the opposite.

That doesn’t mean that a strategy with many conditions can’t work once it goes live, and it also doesn’t mean that all of our strategies have to be reduced to very few lines, but a system with so many constraints will be less robust and may show outcomes that are less consistent with our developments.

Guys, we’ve seen that it’s possible to develop profitable strategies even with very few lines of code, and that has dispelled the myth of complexity at any cost.

If anyone among you is interested in the world of systematic trading, I suggest that you go and click on the link in the description of this video.

From there, you can go and watch a free presentation by Andrea Unger, or you can go and get our best-selling book, "The Unger Method," by just covering the shipping costs, or hey, please, go and book a free consultation call with a member of our team.

If you have enjoyed the video, please leave us a Like, subscribe to our channel, and click on the notification bell to stay updated on the release of all our new videos.

We thank you so much for your attention watching this video and we will see you soon in our next video, bye bye for now!

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.

BOOK YOUR FREE STRATEGY SESSION NOW >>
Andrea Unger

Andrea Unger

Andrea Unger here and I help retail traders to improve their trading, scientifically. I went from being a cog in the machine in a multinational company to the only 4-Time World Trading Champion in a little more than 10 years.

I've been a professional trader since 2001 and in 2008 I became World Champion using just 4 automated trading systems. 

In 2015 I founded Unger Academy, where I teach my method of developing effecting trading strategies: a scientific, replicable and universal method, based on numbers and statistics, not hunches, which led me and my students to become Champions again and again.

Now I'm here to help you learn how to develop your own strategies, autonomously. This channel will help you improve your trading, know the markets better, and apply the scientific method to financial markets.

Becoming a trader is harder than you think, but if you have passion, will, and sufficient capital, you'll learn how to code and develop effective strategies, manage risk, and diversify a portfolio of trading systems to greatly improve your chances of becoming successful.