Trading Options with AFL

Hi - I'm new to Amibroker and Algo-trading. I've had a good scan for information on using AFL for Options Trading. I have found fragments but nothing really helpful so far.
I'm looking for basic information - something I can read to help me understand the possibilities and what others are doing. Also some examples of code.
I've been trading Options on IBKR for ~5 years and I have an IT background.

1 Like

Not sure why I have had no replies. Maybe a dumb question? However I have continued my research and I have found a few books and training courses that are helpful. Nothing that relates to AFL unfortunately.
The most useful at explaining how to code for Options Trading on IBKR is a course on UDEMY the link is here:
The decision I have is whether to use AFL for Option Trading or simply code something in Python.

i think you get no replies because people just have no answer. I have little experience with options but recently started calculating "Option Max Pain". I have however done this outside of Amibroker. Because you need option chains and there are a large number of option symbols. So for the 'futures options' on ES for the month of June for instance there are currently 246Calls and 245Puts. So I can make a list and import them into Amibroker. This is all possible. Especially because you import them as "Daily" data since (at least with IQFeed) the Open Interest is only available in the Daily timeframe. But then you have to import say 490 symbols only for the month of June. Then another 490 symbols for September etc.

IQFeed has an Option chain App. Somehow they are able to display all the data for the entire chain almost instantly in their App. However, when I want to access this data using Python it is very slow. There is probably a secret I do not know of. You want to be able to access the chain data all at once not having to load each symbol separately. This is probably possible in IQFeed but they did not reply and I do not know how it can be done.

I have some Python code that uses Yahoo data to calculate max pain. I will look if there is similar code for IBKR. I would think good code would allow you to access the option chain all at once. So not having to access each symbol.

But all is possible in Amibroker. You just have to make a list of symbols and download the data. With Amibroker you can backtest ideas. For real time trading using options chains (if there is such a thing) it seems problematic since you would have to scan through all the symbols. To have fast access to chain data these data should be stored and updated in a list. I guess this would also be possible in Amibroker. I have not tried because I only wanted to play a bit with options max pain. In theory I could test Options Max Pain theory inside Amibroker. But this means for ES-mini futures options I need to download 490 symbols per expiration month and therefor 1960 symbols per year.

Here is the result for ES :smiley:

there is actually IB Python API. It is also possible to access options chains. I will play with this a bit when I have time. But this would mean that you would be able to use Python to make a list of symbols for a certain chain and then import them inside Amibroker.

The chain data is interesting since as I mentioned you do not want to have to load each symbol. You want to access the entire chain instead.

here is some code I found I will play with when I have time:

So this is actually nice since you can download the data inside Amibroker to do a backtest but at the same time you can get instant chain data from IB

Hi - Thanks for your feedback and comments. The course I mentioned above is great. Myank Rasu has very deep knowledge of algo trading and Options on IBKR.
I am still going through his course. His step-by-step approach makes it fairly easy to understand exactly what's involved..
I think I'll get it working in Python first before I move across to AmiBroker.
Thanks again for your help - I'll check out the code.

i will try to make it running in Amibroker using Python. I have hardly any knowledge of options. I just wanted to use the distribution of the open interest of option contracts to forecast the futures price using the 'max pain theory'. So I will try to make it running when I have time.

Just my 2 cents worth, totally personal opinion, take it with a big grain of salt: I traded some options in the past but I found myself "incompatible" with options markets. Those markets are dominated by institutions using robots written specifically to exploit all inefficiencies making it very hard to make any money if you are retail trader. Theoretical advantages of options seem to vanish for me in practice and the vanishing time premium, unpredictable exposure to "market sentiment" due to sudden jumps in fear index (VIX) makes them a difficult, short-lived gamble. For this reason I never really cared about any option-specific functionality in AB. Futures, ETF, stocks are so much easier to profit from.

1 Like

I am playing a bit with Max Pain not to trade the options itself but to forecast the futures price. But I think to do a backtest inside Amibroker with options that would work just fine. It is however not possible to get some of the functions like the list of options in a chain. But that can be done from within Amibroker using Python.

I downloaded an option chain into Amibroker but have not really worked with it yet. I have some code to calculate Max Pain for a series of expiration dates using Yahoo, see chart below. This is very fast. In Amibroker it would also be fast as long as all the data is backfilled. So a chart like I showed below would only be fast in Amibroker if all the data would be backfilled. Since all these dates you see at the bottom are all separate option chains each having a different date op expiration. That would be just an enormous amount of symbols you have to download into Amibroker.

But I am not even sure if the Max Pain theory works. It makes sense but so far I am just observing it.

here is some background on max pain:

I also have fast code to calculate it for Futures Options using IQFeed data. Then I have to first download the data from the IQFeed Option Chain App to a CSV (which I do not think can be automated unfortunately). Slow IQFeed code I posted in another thread. That code gets the data for each and every symbol. There should be a faster way to get the entire chain at once. But I am so far only able to get this by downloading the CSV file using the IQFeed Option Chain App.

HI Tomasz - Thanks for your advice. I started off trading Options and have had very little experience trading anything else - apart from buy-and-hold. I live in Australia. Depending on daylight saving time in both countries, I only get an hour or two access to the US market. This works quite well for Options. However there are many times when the market conditions are bad for Option Trading producing little or no income.
This volatility and the desire to increase my access to the US market led me to AmiBroker. I'm a little stuck in my Options mindset and still want to automate some 0DTE strategies but my plan is to start trading other strategies in the future.

Yes it could be hard, but not impossible. A common retailer can consistently achieve about 2% per month on deployed capital with "hard" work.

Trading naked options is dangerous!

If hedged properly, one can easily attain an edge > 70% probability-of-profit (POP) to begin with. The ability to adjust existing position (either to increase profit or to mitigate risk) with respect to market dynamics makes all the difference. By expiry day that 70% POP can be increased to > 90% POP and ultimately 100% upon expiry!

VIX works like a barometer to gauge broader market's likelihood (σ). And ATM IV for respective Stock or ETF. That critical information can thus be used to define adjustable and flexible breakeven.

Like a game of Chess, one must be prepared for market's next move and act accordingly. Only Options facilitate implementation of consistent processes (for "all" market conditions) which is not possible trading other instruments.

In case one prefers to request options chains from Amibroker (without using Python): I've developed a fork of IBController which can do that. Actually, it exposes all of TWS API 1019. I'm testing it with the last stable TWS and so far so good, it should work with the recommended TWS 983 too. I barely trade options (in rare cases for hedging) but I wrote a simple test for option chains request, and it seems to work. If someone is interested in...


I know. Trouble is that even if you do sophisticated fully hedged strategies that look beautifully on paper in your trading book, they fail in practice because option pricing is set by large institutions' robots and there is little if any room for profits. Also there is liquidity problem for SSO and makes it difficult to trade when things get ugly. You end up with a lot of effort and very little gain (if any).

Started my serious trading journey 6 years back in 2017!

Feb'17 till Aug'17 (first 6 months):
Market taught that I know nothing!

Sep'17 till May'22:
Self-studied as much as could, learned several TA based strategies, systems and what not! Intermittently took sporadic breaks, applied some of the systems on live market to fail in vain only.

Since June'22:
Everyday market still shows that I know nothing, however, profitable "only" because of flexibility provided by Options.

Between August and September 2018, purchased from various sources to collect 10 years worth of historical weekly and monthly Options intraday data. For about 4 years, with an open mind studied on my own how Option prices behave with respect to its underlying. I've even developed a custom simulator and backtested (several times with various strategies) almost every week and every month of available data. What revealed remains mind-boggling!

To my astonishment, one does not need to know mumbo-jumbo mathematics to be successful in Options trading! Although difficult to grasp, but not complex at the end, to be profitable using Options one just need:

  1. High school Algebra;
  2. Very basic statistics - Standard Deviation & Probability, that's all!

In my understanding, knowledge of Black-Scholes & Greeks enhances academic knowledge only, it has nothing to do with "making money".

There exist three main problems because of which Options are not revered at par its "real" worth:

  1. "Deer In The Headlights" phenomenon

People freeze with their applied strategies and tend to not think out-of-box at the time of market adversities. Beauty about multi-legged option strategies, is that, one or several legs of these strategies can be shifted/added so that the end-result of the trade can be adjusted towards profitability. How? Because market cannot move in both direction at a given point in time.

  1. Lack of well chalked-out plan considering all market possibilities

Quoting author of "The Art of War", Sun Tzu, "Victorious warriors win first and then go to war, while defeated warriors go to war first and then seek to win".

  1. Over expectancy

Imagine two scenarios during WWII:

  • Handed with a Walther-P38 pistol, you are asked to confront enemy at the foremost trench;

  • Holding a FG-42 rifle, you are asked to confront enemy from the very last trench.

Which position would you choose? Obviously latter one.

Proper application of Option strategies are margin intensive - minimum $50K, ideal would be anything greater than $100K. Margin is your weapon, leverage is the weapon of self-destruction. Margin and leverage are not same thing, at all cost leverage must be avoided.

This is a zero-sum "margin" game, in fact because of commissions (in reality) negative-sum game. People come to market with $1K and think to make 50% every day, pretty unrealistic isn't it!

Now let us consider another situation:
Imagine I run a local delivery service.
Scenario 1: You give me a box of M&M's to deliver from place A to place B. You give me $5. No risk, little reward deal.
Scenario 2: You give me a box of MJ joints to deliver from place A to place B. Would I take same $5? No way! :slight_smile:

To draw same analogy in markets, replace Place A to B with Time T1 to T2. With same logic, how much you want by when, would determine the risk you take!

Basis my study, after mitigating risks (a.k.a. cost of business) I aim for +2% every month with well-defined processes in place.

Let's say we start with $100K, this chart shows CAGR of 24% at end of 36 months:

At end of 60 months:

At end of 120 months:

At end of 240 months:

Little is more... And there is room for every body in the market!

With utmost respect I disagree on this one, Tomasz!

In Futures, Stocks/ETFs, I need to be correct at least 6/10 times to come out positive. What if I lose the 7th one as well? Any x% drawdown really hurts no matter how much mechanically one applies a system.

Using Options, with adjustments I can be wrong 10/10 times still won't lose nothing or meagre 0.5% max that too owing to commissions. I did not make money, but my capital preserved! I am still alive with confidence to take on next set of 10 trades, so on and so forth.


That explains why you are optimistic. I have been thru 2000 and 2008, and it wasn't nice for options, believe me. What you are talking about are book examples. When times are nice and easy everything works. But when you get hit by the storm book knowledge fails because it is "six sigma" event, VIX goes crazy and suddenly your options become non-tradable (especially SSO). As man gets older you start to appreciate peace of mind more than money.


This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.