How to Create a Trading System on RBOB Gasoline from A to Z (with open-source 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.


Did you know that the RBOB Gasoline Future has a recurring weekly price pattern that can be effectively exploited for trading?

If you are curious to know how to do this, then don't miss this video!!!

We'll show you how to build, from A to Z, a simple automated strategy that can exploit this recurring price pattern of the Nymex-listed gasoline future. 

By watching the video you'll discover: 

- How the weekly price pattern of the RBOB Gasoline works

- How to create a simple trading system to exploit it effectively (step-by-step explanations, open-source code, analysis of backtest results)

- Some tips on how to use the strategy correctly

Enjoy! 😎


Hey hey, good to be back! Welcome everyone, I’m one of the coaches at Unger Academy, and today we're going to be looking at a bias system, namely a system that exploits the recurring behaviors of a certain market, but we're going to talk about a bias that is a little bit more particular than the traditional intraday bias, because today we're going to analyze an intraweek bias.

So I started out using proprietary software from Unger Academy that lets you examine different types of biases. This includes intraday biases, which means buying from hour to hour during the day, weekly biases, which means buying during the week, monthly biases and of course yearly biases. In our case, we’re going to focus on the weekly bias.

So, I just looked at the weekly performance of this future, which is the Nymex-listed RBOB Gasoline. This underlying asset looks pretty explosive and volatile. Indeed, it has normally very high volatility however it also has some very interesting features, especially in terms of this kind of approach.

In this chart we can see what happens on average during the week, so from the Sunday opening until the Friday closing, buying the underlying asset at different times of the week.

After that, this equity is created, which is a composite of what would happen at any given time during the week, so we can track the average weekly trend of a certain underlying asset.

What immediately catches my eye is that this underlying on average, in the different periods of historical data considered… Because, as you can see each of the four lines refers to a different historical period… On average this underlying tends to go down from the Sunday opening or shortly after the Sunday opening throughout the day on Monday.

After that, the trend on Tuesday seems a bit confusing, let's say it doesn't seem so straightforward, looking at the different periods, especially the blue line, which takes into account all available data. Then, you can see that prices tend to rise on Wednesday, Thursday, and Friday.

So, let's say that the idea of the underlying system that we're trying to develop is to try to ride the downtrend with a short trade on Monday, so after the stock market opens, stay flat on Tuesday and look for a long entry on Wednesday, Thursday, or Friday.

This is the equity line we get from this system. It's certainly interesting and will allow for other studies to be done as well.

Now here's the system code. The code is, as you can see, pretty simple. We can say that the basic conditions are simply these two, namely, we'll have to trade short on Monday, while to trade long we'll use condition2, so the day of the week will have to be equal to 3, 4, or 5. So we can trade long on Wednesday, Thursday, or Friday.

At this point here, I've written basic conditions, so I'll operate on neither the first nor the last session bar to avoid possible backtesting errors.

Plus, I added the condition of making only one entry per day to avoid entries if stop losses, breakevens, or profit targets are hit during the same session.

At this point, I'll use condition1 for a short entry, so on Monday, and we'll enter short at the breakout of a Donchian channel with a stop order.

Specifically, here I optimized the number of bars used for the Donchian channel for the short and I got the value of 4. And this is something that does not surprise me. Because let’s we go back to the previous chart: as you can see, the last bars of the week are generally bullish and at the beginning of the week there seems to be a small gap up, so if we tried a short entry over a larger number of bars we might enter very late.

Also because, as we said, this system can make short entries only on Monday, so the number of four bars was a stable value and I decided to take it as the final value.

On the other hand, when I optimized the number of bars of the long side I got a value of 21 bars, which is almost a whole session. In fact the session lasts 23 bars: we’re using a 60-minute chart and the RBOB session goes from 6 p.m. to 5 p.m. the following day, so that's 23 hours.

So 21 bars was a stable value very close to a full session, so since it gave very good results, I decided to use it.

So, the conditions are simply that to enter short we have to be on Monday plus the basic conditions that I described to you earlier. And at this point, we try to enter on the lowest low of the last 4 bars. Whereas for the long side, condition2 applies, so it has to be either Wednesday or Thursday or Friday, and we'll put a breakout order on the highest high of the last 21 bars.

At this point when the conditions will no longer be present, we'll exit at the next bar, namely the first useful bar. And after that, we'll also have the conditions to manage the position through stop loss, breakeven, and take profit.

I optimized the values a bit and tried to choose the most stable ones and the ones that made the most sense to me, also based on my experience. In the end, I chose $1,700 as the stop loss, $1,500 as the breakeven, and $5,000 as the profit target.

So, let's go and see the results of this strategy. I had already spoilered the equity line to you a little bit at the beginning. In any case it's very good, it's a rising equity line. Especially in the last part on the long side.

Also, because if we take a look at the Buy and Hold, we can see that there has been a surge in gasoline and so this certainly has favored the long side more than the short side, especially in more recent times.

If we look at the short side, however, especially if we consider the growth of the Buy and Hold, it hasn't suffered too much. It even had a few recovery attempts given what it obviously could do in this very powerful bullish trend.

Let's also look at the equity curve with drawdown. Considering that this is the RBOB and that the drawdown remains contained around $20,000, we can say that this drawdown is more than acceptable. Also because the net profit of the strategy, which is $534,000 using only one contract, is certainly pretty good.

At this point, let’s take a look at the average trade. The overall average trade is $321. The percent win is 48.8%, but I remind you that although the number of winning trades is higher than the number of losing trades, in this case, we have many trades that end up at breakeven because of the rather short breakeven stop. And these are not counted in the profitable trades because they are break-even trades and not profitable trades.

Consequently, this 48.8% is also affected by the fact that there are a fair number of trades closed in breakeven, but still, the number of winning trades is higher than the number of losing trades.

The average trade of the long trades only is very good, however, we also have to consider the fact that it can stay in position for up to three days, so the trade has time to develop from Wednesday to Friday.

Clearly since the long positions can be much longer than the short ones, which can stay open only on Monday, the resulting average trade will be a little bit higher, as well as the number of long trades will be higher than the number of short trades because there will be more days when it can enter, although the trigger is a little bit more restrictive.

All in all, the short still works well and can cope with commissions and slippage. And the overall average trade is pretty good, so you know, I would say keep the system this way.

Let’s also take a look at the annual period analysis, which is pretty interesting. Here we can see that all the years are positive. However, you have to consider that in a year like 2020, for example, if you had traded this system, you would have lost money because the average trade for that year would have been $24.87, which is very low, and so it wouldn't have protected you from the impact of slippage and commissions.

The same is true for 2018, where you most likely wouldn't have broken even with a $57 average trade anyway.

Gasoline is a very volatile underlying. When it touches some extreme levels it's likely to be very explosive in breaks and thus cause very high slippage. So, I'd still recommend that if you work with this underlying asset, you shouldn't take particularly low average trades because they could put you at risk.

And with that, this video is over! However, before we say goodbye I'd like to leave you with a piece of advice. If you are interested in learning more about how systematic trading works, check out the link you find in the description of this video. That link will take you to a page from where you can access a free presentation by Andrea Unger, our founder and the only 4-time world trading champion. You'll also be able to order your free copy of our best-selling book, "The Unger Method", or even book a free call with a member of our team.

If you liked this video please don't forget to leave us a like, subscribe to our channel and go and click on the bell to stay updated on the release of all our new and upcoming videos.

Thank you again so much for watching this video and I will see you again in our next video, bye-bye!

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.

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.