TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A high-frequency trading model using Interactive Brokers API in Python

125 pointsby holoiiialmost 10 years ago

13 comments

chollida1almost 10 years ago
Great stuff.<p>I think the author would have been better suited without mentioning HFT, maybe algo trading model?, as its a lighting rod for controversy.<p>If anything I thin this is useful to illustrate just how hard it is to write a full blown trading system.<p>So maybe we could look at what you could add to this to make it something you could use in production(Note, please don&#x27;t use this in production).<p>1) Risk system, before you write any algos, before you learn how to ingest market data, before you write anything else, you write the risk system.<p>in a true trading system because you always have orders flying between you and the exchange you never really know what your positions is. The Risk system allows you to deal with this uncertainty by giving each algo rules as to how many shares its allowed to be offside.<p>If you only ever take away one thing let it be this:<p><i>Trading Rule #1</i> Sooner or later your algo&#x2F;code will make a mistake, it&#x27;s your risk system that determines if you have a job tomorrow or not.<p>2) The system has no rate limiter, what do you do when the quotes come in too fast for you to deal with?<p>3) Locking the world, the system retrieves a quote and locks up the entire system while the quote is acted on. Essentially the very design of the system means you can only ever run one strategy for one set of tickers at a time.<p>If you wanted to tackle this start by looking at this data structure&#x2F;library:<p><a href="https:&#x2F;&#x2F;lmax-exchange.github.io&#x2F;disruptor&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lmax-exchange.github.io&#x2F;disruptor&#x2F;</a><p>4) Back testing, no HFT trade idea&#x27;s go into production without backtracking, Every HFT firm is different but I think they&#x27;d all adhere to this rule.<p>5) Closing positions, every algo gets offside at some point. How do you notify a trader to close out a position? How does the trader close the position and notify the algo?<p>6) Multiple algos over multiple symbols.<p>7) Real time PnL. Your PnL is everything, it means you get paid, it means you can do this again tomorrow. it is the single most important piece of information(Risk metrics are a close second) that you can track. This is probably my only quibble with the demo.
评论 #9688596 未加载
评论 #9689181 未加载
评论 #9688611 未加载
评论 #9689722 未加载
评论 #9688898 未加载
jamesmawmalmost 10 years ago
Wow, didn&#x27;t expect my repo to make it this far. It was my first time using Reddit, with the aim of introducing this for research more than actual trading.<p>Just some thoughts after reading comments:<p>- I&#x27;ve got a number of Python examples for trading futures, but a third-party app is required as &#x27;the gateway&#x27;. Browse on GitHub if you wish: <a href="https:&#x2F;&#x2F;github.com&#x2F;hftstrat&#x2F;The-Gateway-code-samples" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hftstrat&#x2F;The-Gateway-code-samples</a><p>- Backtesting is a topic not to be taken lightly. There are far too many issues to address than just historical simulation. I&#x27;ve written a book covering this topic.<p>- In my book I&#x27;ve also covered the use of Oanda API with a simple trend-following strategy. Available on Amazon: <a href="http:&#x2F;&#x2F;www.amazon.com&#x2F;Mastering-Python-Finance-James-Weiming&#x2F;dp&#x2F;1784394513" rel="nofollow">http:&#x2F;&#x2F;www.amazon.com&#x2F;Mastering-Python-Finance-James-Weiming...</a><p>- Table of contents and source codes used in my book are also available on GitHub: <a href="https:&#x2F;&#x2F;github.com&#x2F;jamesmawm&#x2F;Mastering-Python-for-Finance-source-codes" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jamesmawm&#x2F;Mastering-Python-for-Finance-so...</a><p>Alright, I&#x27;m done with shameless promotion of my links ;p
noname123almost 10 years ago
Pretty cool stuff. Thanks for posting holoiii. Last year, I took some time off to build an IB API client in Java&#x2F;Scala for options trading.<p>The biggest challenge I faced personally was dealing with concurrency of maintaining my positions, orders and quote ticks which led to orders as IB API is based on a asynchronous tick model. I ended up re-writing my code with lots of threads&#x2F;locks to more code with immutability. Curious how you guys architected your IB client?<p>Also I hosted my client on the cloud on AWS. I subscribed to maybe a few symbols but always wondered in a more production environment whether bandwidth will kill you (subscribed to 100+ symbols and to a real tick-by-tick quote stream not quote snapshots).<p>Finally I&#x27;d love to hear more about folks out there who might be doing more options or futures trading off of IB API. The equity world is definitely very interesting but I found out early on that I enjoy trading delta-neutral and am not good at predicting directions.<p>Did you guys find that you have to scale up and write your own execution algo&#x27;s when your size got too big for the market you are trading? Nowadays I use my IB client mostly as a GUI to view my positions and execute complex option spreads; so it&#x27;s more of a &quot;gray-box&quot; for me. Do you guys trade pure algorithmically or half-and-half?
评论 #9689879 未加载
browneggalmost 10 years ago
It is so, so infrequently that I get to say this about something HFT-related on the internet: fucking awesome, dude. There is actual information here that is of use and doesn&#x27;t mislead. It&#x27;s amazing.<p>edit: s&#x2F;dude&#x2F;guy-who-made-this
phaefelealmost 10 years ago
The one key part this seems to be missing is a backtesting framework. Did you look at <a href="https:&#x2F;&#x2F;www.quantopian.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.quantopian.com&#x2F;</a>?
martijn_himselfalmost 10 years ago
Tangentially related question. As a relative newbie to (automated&#x2F; algo) trading systems, it seems most of posts here focus on systems that deal with large volumes of trades in very short timeframes.<p>I&#x27;d be interested in writing an automated system that would focus on trading (investing) over longer timespans (shares, options), with (average) returns. Are there any resources you would recommend to get into this? Or are automated systems really only employed in HFT?
评论 #9690770 未加载
ChaoticGoodalmost 10 years ago
Also, I recommend checking out the QuantStart project started by Michael Halls-moore. Really handy if you are using the Oanda Api. Plus, there exists some great articles on the quantstart site about the creation of the code. <a href="https:&#x2F;&#x2F;github.com&#x2F;mhallsmoore&#x2F;qsforex" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mhallsmoore&#x2F;qsforex</a>
lifeisstillgoodalmost 10 years ago
I have read the anti-FlashBoys book to try and grasp it (HFT is not bad and has cut spreads in equities by 5&#x2F;6ths in ten years) but I still do not understand one HFT algorithm (other than the debunked front running idea)<p>Could someone explain what algo traders &#x2F; HFT actually <i>do</i>? Is it all looking for a variation off of a relationship between say OrangeGrowersInc and OrangeJuiceBottlers Inc? And why is speed so important then?
评论 #9690575 未加载
paperworkalmost 10 years ago
It&#x27;s strange to call this HFT. The author makes a note of it, and I suppose has the right to call it whatever he wants.<p>However, newbies who use this project to learn automated trading should be aware that this is not considered HFT in the industry.
评论 #9689543 未加载
frugalmailalmost 10 years ago
It&#x27;s great that you&#x27;re sharing, so thanks for that. I get it that it&#x27;s better than pseudo code but I&#x27;m having a hard time seeing the words &quot;high-frequency trading and Python&quot; next to each other
fbarrigaalmost 10 years ago
Python can be a good tool to prototype hft algos but not for trading (probably you want to trade under &lt; 1ms).
princebalmost 10 years ago
see a few comments on the speed of python - given that the strategy is on a retail platform i&#x27;m not sure choice of language will be the bottleneck.
sterlalmost 10 years ago
This is really cool, thanks for sharing it out!