Jane Streetのトレーディングシステムの構築に欠かせないOCamlというプログラミング言語について簡単に掘り下げます。OCamlを採用する理由は、大規模で複雑なトレーディングシステムを、安全に、速く開発し、長期間保守できるからです。その特徴は次の4点です。
- 静的型付けによる安全性
OCamlはコンパイル時に多くの誤りを見つけます。Jane Streetは、型システムによってリファクタリング時に入り込むバグを大幅に減らせると説明しています。 - 表現力が高い
OCamlは関数型言語ですが、実務的な大規模システムも書けます。Jane StreetのYaron Minskyは、OCamlがプログラミング言語設計上の「sweet spot」にあると表現しています。つまり、安全性・表現力・性能のバランスがよいという評価です。 - プログラマーの生産性が高い
Jane Streetは技術部門を意図的に小さく保っているため、一人あたりの生産性を最大化する必要があり、そのためにfunctional programmingが有効だと説明しています。
*研究から本番まで同じ言語を使える
Jane StreetはOCamlを広範囲に使っています。低レイテンシ取引システムにもOCamlを使い、さらにFPGA設計にもHardCamlを使っています。異なる工程で同じ言語・ライブラリ・開発者の知識を共有できること自体が大きな価値だと説明しています。
まず、大雑把にJane Streetのトレーディングの考え方を知るために一つのビデオを見ます。この中にOCamlというプログラミング言語が出てきます。
日本語訳
Interviewer:
あなたたちが行っているトレーディングでは、取引判断をしている最中に、大規模なモデルを動かすだけの時間的余裕はあるのでしょうか。
Jane Street側:
ここで理解しておくべきなのは、時間軸が一つだけではないということです。多くの異なる時間軸があります。
私たちが構築しているトレーディングシステムや実際に行っている取引の中には、競争力を保つために、パケットを受け取ってから応答を返すまでを100ナノ秒未満で行わなければならないものがあります。
これはまったく別の世界ですよね。ときどき、「そういうこともOCamlでできるんですか」と聞かれることがあります。もちろんOCamlでもできます。しかし、このレベルの速度になると、OCamlかRustかC++かという違いは問題ではありません。そもそもCPUを使うことができないからです。
ネットワークに直接つながるような構成にして、非常に高速にパケットへの応答を返します。そのため、入力側と出力側のケーブルにオシロスコープを取り付けて観測したとすると、入力パケットをまだすべて受信し終わっていないうちに、出力パケットが送り出され始めているのが見えるほどです。
これは非常に特殊な、まったく別の領域です。
しかし、そのような時間領域では、多くの計算を行うことはできません。したがって、そこで行う判断は非常に単純なものになります。
実際には、どれだけ高度な判断を行うかと、どれだけ速くその判断を返すかとの間には、一連のトレードオフがあります。その判断がモデルによるものであれ、あるいは別の方法、場合によっては人が手作業で書いた判断ルールのようなものであれ、同じです。
そして、トレーディング戦略を構築する正しい方法は、実際には一種のアンサンブル・アプローチを取ることだと思います。
ある種の判断では、非常に単純な判断を非常に速く行います。一方、別の種類の判断では、100ナノ秒という時間軸ではなく、数マイクロ秒、数十マイクロ秒、数百マイクロ秒、あるいはミリ秒という時間軸で処理します。
さらに場合によっては、その判断を1時間以内、あるいはその日のうちに出せれば、それでまったく問題ないというプロセスもあります。
そして、それぞれの時間軸において、時間という観点で競争力を持つ必要があります。ただし、それぞれの時間軸で行っている判断の種類は大きく異なります。
Interviewer:
もしかすると答えられないかもしれませんが、これらのモデルはいったい何を予測しているのでしょうか。単にオーダーブックで次に起こることを予測しているのでしょうか。あるいは、実際そうなのでしょうか。
Jane Street側:
確かに、だんだんと具体的には話しにくい領域に近づいてきていますね。ただ、最も単純で、しかも最も重要なものの一つを挙げるなら、あるもののfair value(適正価値)を予測することです。
これは現在も考えていることですが、私が25年前にJane Streetに入った頃、線形回帰などを使ってモデルを作っていた時代からそうでした。ある対象についてfair valueを予測するというのは、非常に有用なことです。
「私たちは、このものにはいくらの価値があると考えるのか」ということです。
そして、そのfair valueの予測は、さまざまなトレーディング・プロセスの中に、他の要素と組み合わせやすい形で組み込むことができます。
もちろん、それだけが私たちの予測対象ではありません。しかし、fair valueは重要な予測対象の一つです。
特に最後の部分は、かなり重要です。
公正価格を予測すること
ここでは
「fair valueを予測すること=取引戦略そのもの」
とは言っていません。
むしろ、
$$
\text{Fair value prediction}
\rightarrow
\text{さまざまなTrading Processへ組み込む}
$$
と言っています。
したがって composable は単なる「構成可能」より、
「共通部品として、さまざまな取引判断に組み込みやすい」
と訳すのが、この文脈では最も正確です。
つまり、公正価格の予測はそれだけで取引戦略になるのではなく、さまざまな取引判断の共通部品として利用されます。実際のトレーディングシステムでは、この予測値に市場価格、ポジション、リスク、取引コスト、処理速度などの条件を組み合わせて、最終的な取引判断を行います。
その結果、トレーディングシステムは多数の判断ロジックを組み合わせた大規模で複雑なプログラムになります。Jane StreetがOCamlを重視する理由は、まさにこのようなシステムを高速かつ安全に構築し、継続的に変更・保守する必要があるからです。
OCamlをPoweShellで動かしてみよう
opam --version
もし、versionがないなら
winget install OCaml.opam
opam init
1. Automatically create an internal Cygwin installation that will be managed by opam (recommended)
2. Use an existing Cygwin/MSYS2 installation
3. Abort initialisation
1を選びます。
Please select a shell to configure
1. sh
2. bash
3. zsh
4. csh
5. fish
6. pwsh
> 7. powershell
8. cmd
[1/2/3/4/5/6/7/8]
7を選びます。
opam doesn't have any configuration options for powershell; you will have to run (& opam env) -split '\r?\n' |
ForEach-Object { Invoke-Expression $_ } whenever you change you current 'opam switch' or start a new terminal session.
Alternatively, would you like to select a different shell? [y/N]
Nを選びます。この辺は今くいかなければ、ChatGPTに聞いてください。
ここが通ったら、つぎはOCamlの環境をPowerShellに反映して、default switch ができているか確認します。Windowsでは opam init 後に通常 default switch が作られます。
(& opam env) -split '\r?\n' |
ForEach-Object { Invoke-Expression $_ }
通ったら
opam switch
→ default ocaml....
となっていたら正常です。確認します。
ocaml --version
ここまで通れば、次はいよいよOCamlを動かします。
PowerShellのプロンプトにocamlと入力しenterを押します。

つぎに
let fair_value = 101.0;;
let market_price = 100.0;;
if fair_value > market_price then
print_endline "BUY"
else
print_endline "SELL";;
を貼り付けます。

英語書き起こし
Interviewer:
The trading you guys do, can you afford to be running big models in the middle of making trading decisions?
Jane Street側:
I think the thing to understand here is there isn't one time horizon. There are many time horizons.
There are trading systems we build and trades that we do where, in order to be competitive, you actually have to turn around a packet in under 100 nanoseconds.
And, like, that's a very different regime, right? You know, people sometimes talk about, like, “Oh, can you guys [do] stuff in OCaml?” It's like, we can, but, like, for this kind of speed, it doesn't matter — OCaml or Rust or C++, you can't use a CPU, right?
You're going to be directly wired to the network, and you're going to be turning around the packet so fast that if you, like, attach an oscilloscope to the wire on the way in and the wire on the way out, you would see the packets start to leave before it's done being consumed.
That's, like, a very different, very specialized regime.
But, like, when you're in that time regime, you really can't do very much computation. The decisions you're making are going to be very simple.
And, in fact, there's this kind of whole curve of trade-offs between how smart is the decision that you're making — be it a model or some other kind of, maybe even, like, handwritten decision-making process — and how fast the turnaround is.
And, like, the right way to build a trading strategy is really to have a kind of ensemble approach.
Some kinds of decisions, you're making very simple decisions very quickly. For some kind of decisions, you're operating, you know, instead of thinking of 100 nanos, maybe, like, a handful of micros, or tens of microseconds, or hundreds of microseconds, or milliseconds.
And in some cases, there are processes where if you can get that decision turned around, you know, in an hour or that day, that's totally fine.
And you're kind of competitive on a time basis at each of these horizons — very different kinds of decisions at all of them.
Interviewer:
Maybe you can't say, but what exactly are these models predicting? Just the next thing in the order book? Or maybe it is, right?
Jane Street側:
So we definitely like dancing towards stuff that's hard to talk about, but I think the simplest and most important one — like, we think about it now, but, like, also 25 years ago when I started at Jane Street, when I was building, like, models out of linear regression, you know, stuff like that — like, a very useful kind of thing is to predict a fair value for a thing.
What do we think this thing is worth?
And that fits in, in a very kind of composable way, into lots of different trading processes.
That's not the only kind of thing that we use as a prediction target, but it's an important one.
参考
Python3ではじめるシステムトレード【第2版】環境構築と売買戦略
「画像をクリックしていただくとpanrollingのホームページから書籍を購入していただけます。