
Using Python and Pandas to Create Continuous Futures Contracts - shogunmike
http://www.quantstart.com/articles/Continuous-Futures-Contracts-for-Backtesting-Purposes
======
minimax
_The main difference between a futures contract and equity ownership is the
fact that a futures contract has a limited window of availability by virtue of
the expiration date._

There are many more differences between futures and equities, but one big
thing to keep in mind is the leverage you can get with futures. The CL
contract he's talking about is for physical delivery (or receipt) of 1000
barrels of WTI light sweet crude oil in Cushing, OK. The front month contract
is currently trading around $97/barrel so the total value value of one
contract is about $97,000. The CME will let you buy or sell a single contract
with a little less than $4,000 worth of margin. That's like 20x or 25x
leverage if you are trading close to the margin limits. I guess what I'm
saying is that it's a pretty easy way to blow yourself up if you don't know
what you're doing.

~~~
toomuchtodo
In my teens, my father traded commodities. Hogs, corn, soy, all on the CME
(for a brief period, he worked on the trading floor, but then I came along).
During this time, I provided IT support (this was in the early 90's),
aggregating historical market data (hello Knight Ridder!), plotting charts
(Supercharts, when it was still software and not a SaaS platform, on Windows
3.1), and so forth.

I saw first hand what happens when you're sweating the market going against
your trades. My father would smoke heavily and not sleep at night.

Now that I'm 31, we joke about those times. I still build algos with
Quantopian for fun (and share them with my dad over a beer, throwing the
backtest up on a 70" TV), and make a few bucks here and there, but you would
be fucking insane to try to be a day trader now (competing against people with
crazy financial and technological resources).

------
jstauth
Very cool. I have revisited the WTI/Brent spread trade a few times in the last
few years - actually wrote a simple algo using Quandl data for USO and BNO,
two ETFs linked to the WTI and Brent futures. I would love to update it to use
this continuous futures contract logic and see how it does.
[https://www.quantopian.com/posts/brent-slash-wti-spread-
fetc...](https://www.quantopian.com/posts/brent-slash-wti-spread-fetcher-
example)

~~~
shogunmike
A nice algo with a good Sharpe. Although I'm not sure I could stomach a 37%
drawdown!

~~~
jstauth
Thanks. The drawdown is not ready for prime time indeed, I haven't traded it
for that (among other) reasons. But I like the idea a lot and the futures
linked ETFs seemed like an easier place to start that the contracts
themselves.

~~~
shogunmike
Futures linked ETFs in general are a great approach that have really helped
"retail" traders get access to a previously difficult market.

Ernest Chan provides some good examples re GLD/GDX spreads, for instance.

------
Sukotto
Fascinating read and a quick skim of the article index made me add the blog to
my rss reader. Looks like quite a bit of interesting stuff in there.

It amazes me just how much more there is to learn, no matter how much I think
I know about a topic. For example, I have never heard about the importance of
smoothing the price between futures months. Even though I was in the front-
office of a large energy trading firm for over 6 years[1]. (I lead a small
tech team handling pre-trade research data so my experience was tangential to
the actual trading).

It seemed to me that the traders compartmentalized the trading months. They
trade prompt right up to expiry, then they start trading a new prompt (of
course they were also trading spot and other stuff, not only prompt month).
Nobody ever seemed to care about the price disjunction when the month rolled.

So I find this idea of different methods to smooth that price really
interesting. What kind of trading are you doing where that matters?

[1] and I really miss it. Hope I can go back to something like that again
someday.

~~~
ewood
It would not generally be of interest to discretionary traders (as you are
likely describing) but for automated trading using statistical models it is
important to be able to construct a continuous series. The sudden price jump
from backwardation or contango contract shifts would otherwise appear to be a
large price move. As the article points out the smoothing method chosen also
matters in that ultimately some information is being lost so the smoothing
method needs to match the model.

I'm not sure what is meant by the rollover smoothing increasing transaction
costs, this might have caused you some confusion if you're familiar with
futures trading. Generally the actual mechanics of trading with the continuous
series is that trade entries and exits and position sizing are calculated from
the artificial continuous series and a trade that spans a roll will be rolled
over (ie the previous contract position closed and a matching position in the
new contract opened) on the roll date just as a traditional position would be.
There would be no need to trade every day of the roll window, it would just be
a paper exercise to come up with a proportional price for the artificial
continuous contract.

------
mildtrepidation
My first thought was that this could be about automating panda reproduction
via Python. It would continuously ensure the future of the species.

Python _does_ have a library for everything, you know.

~~~
shogunmike
I do wonder whether zoologists are now continually frustrated with the Python
data analysis ecosystem when they perform their Google searches :-)

~~~
yen223
Our local Python user group receives inquiries about snakes on a regular
basis...

------
bjoerns
that's pretty cool, thanks very much for sharing this!

just had a quick scan through the code and it looks there's one thing missing
(apologies if not as I haven't had the time yet to really run the code). if
you actually want to create a tradable backtest you have to bear in mind that
you are selling/buying during the roll period meaning that if you invest
amount x before rolling over what you end up with after the roll is not equal
to the value of your perpetual series.

to give you a simple example: if you roll over 1 day only and CLG14 settles at
t1=100, t2=110 and CLH14 settles at t1=90, t2=95 your position after the roll
is worth 105.56 (not 95).

makes sense?

