Giter VIP home page Giter VIP logo

thetagang's Introduction

Docker publish Python Publish Docker Pulls PyPI download month

๐Ÿ’ฌ Join the Matrix chat, we can get money together.

ฮ˜ ThetaGang ฮ˜

Beat the capitalists at their own game with ThetaGang ๐Ÿ“ˆ

Decay my sweet babies

ThetaGang is an IBKR trading bot for collecting premium by selling options using "The Wheel" strategy. The Wheel is a strategy that surfaced on Reddit, but has been used by many in the past. This bot implements a slightly modified version of The Wheel, with my own personal tweaks.

How it works

Start by reading the Reddit post to get some background.

The strategy, as implemented here, does a few things differently from the one described in the post above. For one, it's intended to be used to augment a typical index-fund based portfolio with specific asset allocations. For example, you might want to use a 60/40 portfolio with SPY (S&P500 fund) and TLT (20 year treasury fund). This strategy reduces risk, but may also limit gains from big market swings. By reducing risk, one can increase leverage.

The main difference between ThetaGang and simply buying and holding index funds is that this script will attempt to harvest volatility by selling options, rather than buying shares directly. This works because implied volatility is typically higher than realized volatility on average. Instead of buying shares, you write puts. This has pros and cons, which are outside the scope of this README.

You could use this tool on individual stocks, but I personally don't recommend it because I am not smart enough to understand which stocks to buy. That's why I just buy index funds.

ThetaGang will try to acquire your desired allocation of each stock or ETF according to the weights you specify in the config. To acquire the positions, the script will write puts when conditions are met (config parameters, adequate buying power, acceptable contracts are available, enough shares needed, etc).

ThetaGang will continue to roll any open option positions indefinitely, with the only exception being ITM puts (although this is configurable). Once puts are in the money, they will be ignored until they expire and are exercised (after which you will own the underlying). When rolling puts, the strike of the new contracts are capped at the old strike plus the premium received (to prevent your account from blowing due to over-ratcheting up the buying power usage).

If puts are exercised due to being ITM at expiration, you will own the stock, and ThetaGang switches from writing puts to writing calls at a strike at least as high as the average cost of the stock held.

Please note: this strategy is based on the assumption that implied volatility is, on average, always higher than realized volatility. In cases where this is not true, this strategy will cause you to lose money.

In the case of deep ITM calls, the bot will prefer to roll the calls to next strike or expiration rather than allowing the underlying to get called away. If you don't have adequate buying power available in your account, it's possible that the options may get exercised instead of rolling forward and the process starts back at the beginning. Please keep in mind this may have tax implications, but that is outside the scope of this README.

In normal usage, you would run the script as a cronjob on a daily, weekly, or monthly basis according to your preferences. Running more frequently than daily is not recommended, but the choice is yours.

Paper account sample output

VIX call hedging

ThetaGang can optionally hedge your account by purchasing VIX calls for the next month based on specified parameters. The strategy is based on the Cboe VIX Tail Hedge Index, which you can read about on the internet. You can enable this feature in thetagang.toml with:

[vix_call_hedge]
enabled = true

Default values are provided, based on the VXTH index, but you may configure them to your taste (refer to thetagang.toml for details).

Buying VIX calls is not free, and it will create some drag on your portfolio, but in times of extreme volatilityโ€“such as the COVID-related 2020 market panicโ€“VIX calls can provide outsized returns.

Cash management

At the time of writing, interest rates have reached yields that make bonds look attractive. To squeeze a little more juice, thetagang can do some simple cash management by purchasing a fund when you have extra cash. Although you do earn a yield on your cash balance, it's not the juiciest yield you can get, so a little optimization might help you earn 1 or 2 extra pennies to take the edge off your rent payments.

There are quite a few ETFs that might be a decent place to stash your cash, and you should do some internet searches to find the most appropriate one for you and your feelings. Here are some internet web searches that you can test out to get some information on cash funds (ETFs):

You can enable cash management with:

[cash_management]
enabled = true

Refer to thetagang.toml for all the options.

Project status

This project is, in its current state, considered to be complete. I'm open to contributions, but I am unlikely to accept PRs or feature requests that involve significant changes to the underlying algorithm.

If you find something that you think is a bug, or some other issue, please create a new issue.

"Show me your gains bro" โ€“ i.e., what are the returns?

As discussed elsewhere in this README, you must conduct your own research, and I suggest starting with resources such as CBOE's BXM and BXDM indices and comparing those to SPX. I've had a lot of people complain because "that strategy isn't better than buy and hold BRUH"โ€“let me assure you, that is not my goal with this.

There are conflicting opinions about whether selling options is good or bad, more or less risky, yadda yadda, but generally, the risk profile for covered calls and naked puts is no worse than the worst case for simply holding an ETF or stock. I'd argue that selling a naked put is better than buying SPY with a limit order, because at least if SPY goes to zero you keep the premium from selling the option. The main downside is that returns are capped on the upside. Depending on your goals, this may not matter. If you're like me, then you'd rather have consistent returns and give up a little bit of potential upside.

Generally speaking, the point of selling options is not to exceed the returns of the underlying, but rather to reduce risk. Reducing risk is an important feature because it, in turn, allows one to increase risk in other ways (i.e., allocate a higher percentage to stocks or buy riskier assets).

Whether you use this or not is up to you. I have not one single fuck to give, whether you use it or not. I am not here to convince you to use it, I merely want to share knowledge and perhaps help create a little bit of wealth redistribution.

๐Ÿ’ซ

Requirements

The bot is based on the ib_insync library, and uses IBC for managing the API gateway.

To use the bot, you'll need an Interactive Brokers account with a working installation of IBC. If you want to modify the bot, you'll need an installation of Python 3.8 or newer with the poetry package manager.

One more thing: to run this on a live account, you'll require enough capital to purchase at least 100 shares of the stocks or ETFs you choose. For example, if SPY is trading at $300/share you'd need $30,000 available. You can search for lower priced alternatives, but these tend to have low volume on options which may not be appropriate for this strategy. You should generally avoid low volume ETFs/stocks. If you don't have that kind of capital, you'll need to keep renting out your time to the capitalists until you can become a capitalist yourself. That's the way the pyramid scheme we call capitalism works.

Installation

Before running ThetaGang, you should set up an IBKR paper account to test the code.

pip install thetagang

It's recommended you familiarize yourself with IBC so you know how it works. You'll need to know how to configure the various knows and settings, and make sure things like API ports are configured correctly. If you don't want to mess around too much, consider running ThetaGang with Docker.

Usage

thetagang -h

Up and running with Docker

My preferred way for running ThetaGang is to use a cronjob to execute Docker commands. I've built a Docker image as part of this project, which you can use with your installation. There's a prebuilt Docker image here.

To run ThetaGang within Docker, you'll need to pass config.ini for IBC configuration and thetagang.toml for ThetaGang. There's a sample ibc-config.ini included in this repo for your convenience.

The easiest way to get the config files into the container is by mounting a volume.

To get started, grab a copy of thetagang.toml and config.ini:

mkdir ~/thetagang
cd ~/thetagang
curl -Lq https://raw.githubusercontent.com/brndnmtthws/thetagang/main/thetagang.toml -o ./thetagang.toml
curl -Lq https://raw.githubusercontent.com/brndnmtthws/thetagang/main/ibc-config.ini -o ./config.ini

Edit ~/thetagang/thetagang.toml to suit your needs. Pay particular attention to the symbols and weights. At a minimum, you must change the username, password, and account number. You may also want to change the trading move from paper to live when needed.

Now, to run ThetaGang with Docker:

docker run --rm -i --net host \
    -v ~/thetagang:/etc/thetagang \
    brndnmtthws/thetagang:main \
    --config /etc/thetagang/thetagang.toml

Lastly, to run ThetaGang as a daily cronjob Monday to Friday at 9am, add something like this to your crontab (on systems with a cron installation, use crontab -e to edit your crontab):

0 9 * * 1-5 docker run --rm -i -v ~/thetagang:/etc/thetagang brndnmtthws/thetagang:main --config /etc/thetagang/thetagang.toml

Determining which ETFs or stocks to run ThetaGang with

I leave this as an exercise to the reader, however I will provide a few recommendations and resources:

Recommendations

  • Stick with high volume ETFs or stocks
  • Careful with margin usage, you'll want to calculate the worst case scenario and provide plenty of cushion for yourself based on your portfolio

Resources

  • For discussions about selling options, check out r/thetagang
  • For backtesting portfolios, you can use this tool and this tool to get an idea of drawdown and typical volatility

Development

Check out the code to your local machine and install the Python dependencies:

poetry install
poetry run autohooks activate
poetry run thetagang -h

You are now ready to make a splash! ๐Ÿณ

FAQ

Error Cause Resolution
Requested market data is not subscribed. Requisite market data subscriptions have not been set up on IBKR. Configure your market data subscriptions. The default config that ships with this script uses the Cboe One Add-On Bundle and the US Equity and Options Add-On Streaming Bundle. Note: You must fund your account before IBKR will send data for subscriptions. Without funding you can still subscribe but you will get an error from ibc.
No market data during competing live session Your account is logged in somewhere else, such as the IBKR web portal, the desktop app, or even another instance of this script. Log out of all sessions and then re-run the script.
ib_insync.wrapper ERROR Error 200, reqId 10: The contract description specified for SYMBOL is ambiguous. IBKR needs to know which exchange is the primary exchange for a given symbol. You need to specify the primary exchange for the stock. This is normal for companies, typically. For ETFs it usually isn't required. Specify the primary_exchange parameter for the symbol, i.e., primary_exchange = "NYSE".

Support and sponsorship

If you get some value out of this, please consider sponsoring me to continue maintaining this project well into the future. Like everyone else in the world, I'm just trying to survive.

If you like what you see but want something different, I am willing to work on bespoke or custom trading bots for a fee. Reach out to me directly through my GitHub profile.

Stargazers over time

Stargazers over time

thetagang's People

Contributors

a1exus avatar asemx avatar brndnmtthws avatar dependabot[bot] avatar junyuanz1 avatar shroom avatar sirugh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

thetagang's Issues

PROGRAM

ูƒู„ ุงู„ุดูƒุฑ ู„ูƒ ู…ู† A-B-N SYRIA |ุงุฏูˆุงุช ุงูˆู† ู„ุงูŠู† ,ูƒู„ุงู…ูƒ ุฑูˆุนุฉ
ย 

ValueError: cannot convert float NaN to integer - after Start of calculating target positions.

Thetagang gets portfolio positions but the it kind of crashes. Was working wonderful, haven't changed anything. Wonder what might have influenced this. Already switched back to 1.9.1 and 1.9.2 with the same issue.

Any ideas?

`โ”‚ T โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚ P โ”‚ -1 โ”‚ $0โ€ฆ โ”‚ $0โ€ฆ โ”‚ $-0 โ”‚ $-2 โ”‚ $2 โ”‚ 90.โ€ฆ โ”‚ $1โ€ฆ โ”‚ 202โ€ฆ โ”‚ 2 โ”‚ โ”‚
โ”‚ โ”‚ P โ”‚ -5 โ”‚ $0โ€ฆ โ”‚ $0โ€ฆ โ”‚ $-โ€ฆ โ”‚ $-98 โ”‚ $41 โ”‚ 41.โ€ฆ โ”‚ $1โ€ฆ โ”‚ 202โ€ฆ โ”‚ 65 โ”‚ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”˜
Calculating target positions... โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 0% -:--:--
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Traceback (most recent call last) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ /usr/local/lib/python3.10/dist-packages/thetagang/portfolio_manager.py:591 โ”‚
โ”‚ in manage โ”‚
โ”‚ โ”‚
โ”‚ 588 โ”‚ โ”‚ โ”‚ โ”‚ positions_table, โ”‚
โ”‚ 589 โ”‚ โ”‚ โ”‚ โ”‚ put_actions_table, โ”‚
โ”‚ 590 โ”‚ โ”‚ โ”‚ โ”‚ puts_to_write, โ”‚
โ”‚ โฑ 591 โ”‚ โ”‚ โ”‚ ) = self.check_if_can_write_puts(account_summary, portfol โ”‚
โ”‚ 592 โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ 593 โ”‚ โ”‚ โ”‚ # Look for lots of stock that don't have covered calls โ”‚
โ”‚ 594 โ”‚ โ”‚ โ”‚ (call_actions_table, calls_to_write) = self.check_for_unc โ”‚
โ”‚ โ”‚
โ”‚ /usr/local/lib/python3.10/dist-packages/thetagang/portfolio_manager.py:1005 โ”‚
โ”‚ in check_if_can_write_puts โ”‚
โ”‚ โ”‚
โ”‚ 1002 โ”‚ โ”‚ โ”‚ targets[symbol] = round( โ”‚
โ”‚ 1003 โ”‚ โ”‚ โ”‚ โ”‚ self.config["symbols"][symbol]["weight"] * total_buyi โ”‚
โ”‚ 1004 โ”‚ โ”‚ โ”‚ ) โ”‚
โ”‚ โฑ 1005 โ”‚ โ”‚ โ”‚ self.target_quantities[symbol] = math.floor( โ”‚
โ”‚ 1006 โ”‚ โ”‚ โ”‚ โ”‚ targets[symbol] / ticker.marketPrice() โ”‚
โ”‚ 1007 โ”‚ โ”‚ โ”‚ ) โ”‚
โ”‚ 1008 โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
ValueError: cannot convert float NaN to integer

** Press ANY KEY to close this window ** `

A key error raised

2021-10-28 02:21:41,205 ib_insync.ib INFO Synchronization complete
2021-10-28 02:21:41,493 eventkit.event ERROR Value () caused exception for event Event<connectedEvent, [[None, None, <function start..onConnected at 0x7f7b9dbc9f28>]]>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/eventkit/event.py", line 186, in emit
result = func(*args)
File "/usr/local/lib/python3.7/dist-packages/thetagang/thetagang.py", line 141, in onConnected
portfolio_manager.manage()
File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 380, in manage
(account_summary, portfolio_positions) = self.summarize_account()
File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 234, in summarize_account
"ExcessLiquidity": f"{float(account_summary['ExcessLiquidity'].value):,.0f}",
KeyError: 'ExcessLiquidity'
2021-10-28 02:21:41,494 ib_insync.Watchdog INFO Stoppingg/thetagang.toml

java.awt.AWTError: Can't connect to X11 window server using ':1' as the value of the DISPLAY variable.

Hello World!

I'm trying to follow Up and running with Docker, yet running into issue that can't pass(

the actual error:

No protocol specified
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2023-01-31 05:27:45:029 IBC: An exception has occurred:
java.awt.AWTError: Can't connect to X11 window server using ':1' as the value of the DISPLAY variable.
        at java.desktop/sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
        at java.desktop/sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:101)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
        at java.desktop/sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:60)
        at java.desktop/sun.awt.PlatformGraphicsInfo.createGE(PlatformGraphicsInfo.java:36)
        at java.desktop/java.awt.GraphicsEnvironment$LocalGE.createGE(GraphicsEnvironment.java:93)
        at java.desktop/java.awt.GraphicsEnvironment$LocalGE.<clinit>(GraphicsEnvironment.java:84)
        at java.desktop/java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:106)
        at java.desktop/sun.awt.X11.XToolkit.<clinit>(XToolkit.java:224)
        at java.desktop/sun.awt.PlatformGraphicsInfo.createToolkit(PlatformGraphicsInfo.java:40)
        at java.desktop/java.awt.Toolkit.getDefaultToolkit(Toolkit.java:599)
        at ibcalpha.ibc.IbcTws.createToolkitListener(Unknown Source)
        at ibcalpha.ibc.IbcTws.load(Unknown Source)
        at ibcalpha.ibc.IbcGateway.main(Unknown Source)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

step I took:

$ mkdir ~/thetagang
$ cd ~/thetagang
$ curl -Lq https://raw.githubusercontent.com/brndnmtthws/thetagang/main/thetagang.toml -o ~/thetagang/thetagang.toml
$ curl -Lq https://raw.githubusercontent.com/brndnmtthws/thetagang/main/ibc-config.ini -o ~/thetagang/config.ini
$ docker run --rm -i --net host \
    -v ~/thetagang:/etc/thetagang \
    brndnmtthws/thetagang:main \
    --config /etc/thetagang/thetagang.toml
+ export DISPLAY=:1
+ DISPLAY=:1
+ Xvfb :1 -ac -screen 0 1024x768x24
++ arch
+ export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/jni
+ LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/jni
+ exec /usr/local/bin/thetagang --config /etc/thetagang/thetagang.toml
_XSERVTransSocketUNIXCreateListener: ...SocketCreateListener() failed
_XSERVTransMakeAllCOTSServerListeners: server already running
(EE) 
Fatal server error:
(EE) Cannot establish any listening sockets - Make sure an X server isn't already running(EE) 
Config:
...
No protocol specified
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2023-01-31 05:27:45:029 IBC: An exception has occurred:
java.awt.AWTError: Can't connect to X11 window server using ':1' as the value of the DISPLAY variable.
        at java.desktop/sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
        at java.desktop/sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:101)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
        at java.desktop/sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:60)
        at java.desktop/sun.awt.PlatformGraphicsInfo.createGE(PlatformGraphicsInfo.java:36)
        at java.desktop/java.awt.GraphicsEnvironment$LocalGE.createGE(GraphicsEnvironment.java:93)
        at java.desktop/java.awt.GraphicsEnvironment$LocalGE.<clinit>(GraphicsEnvironment.java:84)
        at java.desktop/java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:106)
        at java.desktop/sun.awt.X11.XToolkit.<clinit>(XToolkit.java:224)
        at java.desktop/sun.awt.PlatformGraphicsInfo.createToolkit(PlatformGraphicsInfo.java:40)
        at java.desktop/java.awt.Toolkit.getDefaultToolkit(Toolkit.java:599)
        at ibcalpha.ibc.IbcTws.createToolkitListener(Unknown Source)
        at ibcalpha.ibc.IbcTws.load(Unknown Source)
        at ibcalpha.ibc.IbcGateway.main(Unknown Source)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Please advise.

Empty docker image?

Heya, has the image on docker been deleted? Trying to pull the prebuilt image but it seems to be empty.

How to keep ib's client long-running?

Hi,

Thanks for this project. One issue I have is that the ib's desk client would be closing on its own from time to time. In that case, the crontab's docker command won't be useful as I will have to start ib's client before running this binary.

Any workaround?

runtime error

Hi,

I'm getting runtime error when I iexcute the thetagang application, Please check and help me to fix this issue. Do I need to make any config changes? I got Snapshot requests limitation exceeded:100 per 1 second(s), contract: Option(conId=449748871, symbol='SPY'.. in one of my log. Not sure if i need to limit the number of request per sec to IBKR through some settings.

Config:

Account details:
Number = DU*******
Cancel existing orders = True
Margin usage = 0.5 (50.0%)
Market data type = 1

Roll options when either condition is true:
Days to expiry <= 15 and P&L >= 0.0 (0.0%)
P&L >= 0.9 (90.0%)

Write options with targets of:
Days to expiry >= 45
Default delta <= 0.3
Maximum new contracts = 15
Minimum open interest = 10

Symbols:
SPY, weight = 0.4 (40.0%), delta = 0.3p, 0.3c
QQQ, weight = 0.3 (30.0%), delta = 0.5p, 0.3c
TLT, weight = 0.3 (30.0%), delta = 0.4p, 0.4c

Account summary:

Excess liquidity = 1000000.00
Net liquidation = 1000000.00
Cushion = 1 (100.0%)
Full maint margin = 0.00
Buying power = 4000000.00
Total cash value = 1000000.00

Portfolio positions:

Checking positions...

0 puts will be rolled
0 calls will be rolled

Total buying power: $500000 at 50.0% margin usage
Total value: $500000

SPY
Current position quantity 0
Target value $200000.0
Target quantity 511
Target additional quantity (excl. existing options) 511
QQQ
Current position quantity 0
Target value $150000.0
Target quantity 451
Target additional quantity (excl. existing options) 451
TLT
Current position quantity 0
Target value $150000.0
Target quantity 1033
Target additional quantity (excl. existing options) 1033

Preparing to write additional 5 puts to purchase SPY, capped at 15

Searching option chain for symbol=SPY right=P, this can take a while...

An exception was raised, exiting
Check log for details

2021-02-20 09:48:11,465 ib_insync.Watchdog INFO Starting
2021-02-20 09:48:11,466 ib_insync.IBC INFO Starting
2021-02-20 09:48:41,479 ib_insync.client INFO Connecting to 127.0.0.1:4002 with clientId 1...
2021-02-20 09:48:41,481 ib_insync.client INFO Connected
2021-02-20 09:48:41,571 ib_insync.client INFO Logged on to server version 152
2021-02-20 09:48:41,587 ib_insync.client INFO API connection ready
2021-02-20 09:48:41,612 ib_insync.wrapper INFO Warning 2158, reqId -1: Sec-def data farm connection is OK:secdefil
2021-02-20 09:48:42,675 ib_insync.ib INFO Synchronization complete
2021-02-20 09:48:43,078 ib_insync.wrapper INFO Warning 2119, reqId -1: Market data farm is connecting:usfarm
2021-02-20 09:48:43,713 ib_insync.wrapper INFO Warning 2104, reqId -1: Market data farm connection is OK:usfarm
2021-02-20 09:49:33,012 ib_insync.wrapper ERROR Error 200, reqId 14: No security definition has been found for the request, contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20210416', strike=352.5, right='P', exchange='SMART', tradingClass='SPY')
2021-02-20 09:49:33,017 ib_insync.wrapper ERROR Error 200, reqId 20: No security definition has been found for the request, contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20210416', strike=357.5, right='P', exchange='SMART', tradingClass='SPY')

2021-02-20 09:49:37,943 ib_insync.wrapper ERROR Error 200, reqId 250: No security definition has been found for the request, contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20210716', strike=382.5, right='P', exchange='SMART', tradingClass='SPY')
2021-02-20 09:49:37,944 ib_insync.wrapper ERROR Error 200, reqId 256: No security definition has been found for the request, contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20210716', strike=387.5, right='P', exchange='SMART', tradingClass='SPY')
2021-02-20 09:49:38,225 ib_insync.ib ERROR Unknown contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20210416', strike=352.5, right='P', exchange='SMART', tradingClass='SPY')
2021-02-20 09:49:38,225 ib_insync.ib ERROR Unknown contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20210416', strike=357.5, right='P', exchange='SMART', tradingClass='SPY')
2021-02-20 09:49:38,226 ib_insync.ib ERROR Unknown contract: Option(symbol='SPY', lastTradeDateOrContractMonth='20210416', strike=362.5, right='P', exchange='SMART', tradingClass='SPY')
2021-02-20 09:49:39,424 ib_insync.wrapper INFO Warning 2119, reqId -1: Market data farm is connecting:usopt
2021-02-20 09:49:39,613 ib_insync.wrapper INFO Warning 2104, reqId -1: Market data farm connection is OK:usopt
2021-02-20 09:49:54,275 eventkit.event ERROR Value () caused exception for event Event<connectedEvent, [[None, None, <function start..onConnected at 0x7fb4338158b0>]]>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/eventkit/event.py", line 181, in emit
func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/thetagang/thetagang.py", line 113, in onConnected
portfolio_manager.manage()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/thetagang/portfolio_manager.py", line 271, in manage
self.check_if_can_write_puts(account_summary, portfolio_positions)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/thetagang/portfolio_manager.py", line 499, in check_if_can_write_puts
self.write_puts(symbol, puts_to_write)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/thetagang/portfolio_manager.py", line 390, in write_puts
sell_ticker = self.find_eligible_contracts(symbol, "P")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/thetagang/portfolio_manager.py", line 673, in find_eligible_contracts
raise RuntimeError(f"No valid contracts found for {symbol}. Aborting.")
RuntimeError: No valid contracts found for SPY. Aborting.
2021-02-20 09:49:54,287 ib_insync.Watchdog INFO Stopping
2021-02-20 09:49:54,288 ib_insync.ib INFO Disconnecting from 127.0.0.1:4002, 35.4 kB sent in 475 messages, 599 kB received in 2424 messages, session time 103 s.
2021-02-20 09:49:54,288 ib_insync.client INFO Disconnecting
2021-02-20 09:49:54,291 ib_insync.client INFO Disconnected
2021-02-20 09:49:54,297 ib_insync.Watchdog WARNING Disconnected
2021-02-20 09:49:54,298 ib_insync.IBC INFO Terminating

pip install not working

pip install thetagang is not working for me. Also, without a setup.py file, I'm not sure how to install from source

RuntimeError: No valid contracts found for ABNB.

Hi,

Thanks for making this project. I started using the Docker image, it will find several contracts, filter them out by open interest and then stop with all of the default contracts (TLT, SPY, ABNB etc...). I ran it multiple times during market hours and also looked manually through some of these options chains, but couldn't find anything that would fall outside of the ranges specified in the config.ini and thetagang.toml file. Everything is still at default values except for Market_data_type = 3, since this is a paper account (hoping to switch to live later :) ) this is the only option. I'm not sure if there may be an issue with market data still.

Thanks very much for any feedback

Searching option chain for symbol=ABNB right=P, strike_limit=None,
minimum_price=$0.000 preferred_minimum_price= this can take a while...
Scanning between strikes 104.0 and 118.0, from expirations 20231215 to 20240419
Filtering invalid prices for ABNB from 12 tickers... โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 100% 0:00:00
Timeout waiting on market data for contracts=[Option(conId=647961780,
symbol='ABNB', lastTradeDateOrContractMonth='20231215', strike=105.0, right='P',
multiplier='100', exchange='SMART', currency='USD', localSymbol='ABNB
231215P00105000', tradingClass='ABNB'), Option(conId=515426714, symbol='ABNB',
lastTradeDateOrContractMonth='20240119', strike=105.0, right='P',
multiplier='100', exchange='SMART', currency='USD', localSymbol='ABNB
240119P00105000', tradingClass='ABNB'), Option(conId=644112199, symbol='ABNB',
lastTradeDateOrContractMonth='20240315', strike=105.0, right='P',
multiplier='100', exchange='SMART', currency='USD', localSymbol='ABNB
240315P00105000', tradingClass='ABNB')], continuing...
Filtering by open interest for ABNB from 3 tickers... โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 100% 0:00:00
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Traceback (most recent call last) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ /usr/local/lib/python3.10/dist-packages/thetagang/portfolio_manager.py:897 โ”‚
โ”‚ in write_puts โ”‚
โ”‚ โ”‚
โ”‚ 894 โ”‚ def write_puts(self, puts): โ”‚
โ”‚ 895 โ”‚ โ”‚ for symbol, primary_exchange, quantity, strike_limit in puts: โ”‚
โ”‚ 896 โ”‚ โ”‚ โ”‚ try: โ”‚
โ”‚ โฑ 897 โ”‚ โ”‚ โ”‚ โ”‚ sell_ticker = self.find_eligible_contracts( โ”‚
โ”‚ 898 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ Stock( โ”‚
โ”‚ 899 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ symbol, โ”‚
โ”‚ 900 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ self.get_order_exchange(), โ”‚
โ”‚ โ”‚
โ”‚ /usr/local/lib/python3.10/dist-packages/thetagang/portfolio_manager.py:1518 โ”‚
โ”‚ in find_eligible_contracts โ”‚
โ”‚ โ”‚
โ”‚ 1515 โ”‚ โ”‚ โ”‚ โ”‚ if len(tickers) == 0: โ”‚
โ”‚ 1516 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ # if there are still no tickers remaining, ther โ”‚
โ”‚ 1517 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ # more we can do โ”‚
โ”‚ โฑ 1518 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ raise RuntimeError( โ”‚
โ”‚ 1519 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ f"No valid contracts found for {main_contract โ”‚
โ”‚ 1520 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ ) โ”‚
โ”‚ 1521 โ”‚ โ”‚ โ”‚ elif preferred_minimum_price is not None: โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
RuntimeError: No valid contracts found for ABNB. Continuing anyway...
Finding eligible contracts for ABNB failed. Continuing anyway...

ERROR:ib_insync.wrapper:Error 10197, reqId..... No market data during competing live session

I have this set up in a VM and I'm trying to run the Docker container with TWS 978 on my paper trading account.

The Bot hangs on "Searching option chain for symbol=SPY right=P, this can take a while..."

The ib_insync.loglogs show an error:

ERROR:ib_insync.wrapper:Error 10197, reqId..... No market data during competing live session.

I've verified that all my other IBKR sessions are closed, also it is getting market data, current prices for SPY show up in the console and in the error message. I can launch TWS by itself and it gets market data successfully on the same paper account.

I found these two threads regarding the same error message, but none has a resolution:

https://groups.io/g/insync/topic/19741887
https://groups.io/g/twsapi/topic/24929715

Docker image broken

Digest: sha256:6b3cd04158c888e7a584793cfaf701d17dc8498d4c9c818ce03f815e6e693711
Status: Downloaded newer image for brndnmtthws/thetagang:latest

  • export DISPLAY=:1
  • DISPLAY=:1
  • exec /usr/local/bin/thetagang --config /etc/thetagang/thetagang.toml
  • Xvfb :1 -ac -screen 0 1024x768x24
    Traceback (most recent call last):
    File "/usr/local/lib/python3.7/dist-packages/schema.py", line 396, in validate
    nvalue = Schema(svalue, error=e, ignore_extra_keys=i).validate(value)
    File "/usr/local/lib/python3.7/dist-packages/schema.py", line 417, in validate
    raise SchemaWrongKeyError(message, e.format(data) if e else None)
    schema.SchemaWrongKeyError: Wrong key 'RaiseRequestErrors' in {'gateway': True, 'ibcPath': '/opt/ibc', 'tradingMode': 'paper', 'RaiseRequestErrors': False, 'password': 'demo', 'userid': 'demo', 'ibcIni': '/etc/thetagang/config.ini', 'javaPath': '/opt/java/openjdk/bin'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/bin/thetagang", line 8, in
sys.exit(cli())
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 829, in call
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/thetagang/main.py", line 35, in cli
start(config)
File "/usr/local/lib/python3.7/dist-packages/thetagang/thetagang.py", line 27, in start
validate_config(config)
File "/usr/local/lib/python3.7/dist-packages/thetagang/config.py", line 120, in validate_config
schema.validate(config)
File "/usr/local/lib/python3.7/dist-packages/schema.py", line 400, in validate
raise SchemaError([message] + x.autos, [e.format(data) if e else None] + x.errors)
schema.SchemaError: Key 'ibc' error:
Wrong key 'RaiseRequestErrors' in {'gateway': True, 'ibcPath': '/opt/ibc', 'tradingMode': 'paper', 'RaiseRequestErrors': False, 'password': 'demo', 'userid': 'demo', 'ibcIni': '/etc/thetagang/config.ini', 'javaPath': '/opt/java/openjdk/bin'}

Error when running Docker image

org.w3c.dom.DOMException: jar:file:/root/Jts/978/jars/jts4launch-978.jar!/trader/common/images/gfis_blue_w47_h16.svg:
The attribute "fill" represents an invalid CSS value ("url(#a)").
Original message:
String index out of range: 4
at org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:776)
at org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:868)
at org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:81)
at org.apache.batik.bridge.CSSUtilities.convertDisplay(CSSUtilities.java:563)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:206)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171)
at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:82)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)
at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:92)
at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
at feature.svg.b.a(b.java:90)
at java.util.HashMap.computeIfAbsent(HashMap.java:1127)
at feature.svg.b.a(b.java:79)
at feature.svg.b.a(b.java:60)
at feature.svg.c.a(c.java:66)
at jtscomponents.plaf.dT.a(dT.java:2405)
at feature.gfis.component.b.onFontChanged(b.java:109)
at feature.gfis.component.b.b(b.java:76)
at feature.gfis.component.b.(b.java:61)
at feature.tabs.bf.f(bf.java:183)
at feature.tabs.bf.(bf.java:156)
at feature.tabs.bg.(bg.java:437)
at jclient.qm.b(qm.java:847)
at jclient.qm.eF(qm.java:831)
at jclient.qm.cn(qm.java:978)
at trader.tab.bc.cm(bc.java:1532)
at trader.tab.bc.(bc.java:1522)
at jclient.qk.(qk.java:216)
at jclient.ql.(ql.java:492)
at jclient.qm.(qm.java:513)
at jclient.qm.(qm.java:509)
at feature.mainwin.B.a(B.java:257)
at feature.mainwin.B.a(B.java:139)
at feature.mainwin.v.a(v.java:175)
at trader.common.factory.U.a(U.java:249)
at trader.common.factory.U.a(U.java:216)
at feature.mainwin.B.a(B.java:173)
at trader.tab.g.a(g.java:1227)
at trader.common.factory.b.a(b.java:1509)
at trader.common.factory.b.c(b.java:1524)
at trader.common.factory.b.b(b.java:1583)
at trader.common.factory.b.b(b.java:1467)
at jclient.f7.b(f7.java:3778)
at twslaunch.jutils.as.a(as.java:713)
at jclient.f7.a(f7.java:3776)
at jclient.f7.a(f7.java:2953)
at feature.welcome.f.run(f.java:501)
at feature.welcome.e.run(e.java:509)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
ERROR: null
Enclosed Exception:
jar:file:/root/Jts/978/jars/jts4launch-978.jar!/trader/common/images/gfis_blue_w47_h16.svg:
The attribute "fill" represents an invalid CSS value ("url(#a)").
Original message:
String index out of range: 4

Hey Brenden. New issues.

2021-02-16 06:42:11,675 ib_insync.client INFO Connecting to 127.0.0.1:4002 with clientId 1...
2021-02-16 06:42:11,678 ib_insync.client INFO Disconnecting
2021-02-16 06:42:11,692 ib_insync.client ERROR API connection failed: ConnectionRefusedError(111, "Connect call failed ('127.0.0.1', 4002)")
2021-02-16 06:42:11,693 ib_insync.client ERROR Make sure API port on TWS/IBG is open
2021-02-16 06:42:11,694 ib_insync.IBC INFO Terminating
2021-02-16 06:42:14,971 ib_insync.IBC INFO Starting
2021-02-16 06:42:44,996 ib_insync.client INFO Connecting to 127.0.0.1:4002 with clientId 1...
2021-02-16 06:42:44,999 ib_insync.client INFO Disconnecting
2021-02-16 06:42:45,000 ib_insync.client ERROR API connection failed: ConnectionRefusedError(111, "Connect call failed ('127.0.0.1', 4002)")
2021-02-16 06:42:45,000 ib_insync.client ERROR Make sure API port on TWS/IBG is open
2021-02-16 06:42:45,001 ib_insync.IBC INFO Terminating
2021-02-16 06:42:47,966 ib_insync.IBC INFO Starting

This really is the only error im getting, boss.

Issue running thetagang with IBC in Poetry

Good afternoon,

I'm trying to run Thetagang in Poetry to test some modifications later, but I can't get it to work with IBC.
My computer runs Zorin OS and TWS 1025, I've also attempted in an Ubuntu virtual machine with TWS 1019, issues are a bit similar. IBC by itself works and Thetagang without IBC works as well so I'm pretty sure TWS and config files are set correctly

With Ubuntu/TWS1019 I got the following message: (Offline TWS/Gateway version 1019 is installed)
Error: Offline TWS/Gateway version 1019 is not installed: can't find jars folder
Make sure you install the offline version of TWS/Gateway
IBC does not work with the auto-updating TWS/Gateway

With Zorin/TWS1025 I got the same message, then modified thetagang.py line 297 to point to TWS 1025. Then I got the following error msg
WARNING: Illegal reflective access by feature.search.recorder.JtsMultiLookAndFeel (file:/home/zorinhp/Jts/1025/jars/twslaunch-1025.jar) to method javax.swing.UIManager.getLAFState()
WARNING: Please consider reporting this to the maintainers of feature.search.recorder.JtsMultiLookAndFeel
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

I also get different error messages depending on if I installed openjdk or default-jre. I'm guessing I'm not building the poetry environment right or my machine runs the wrong java version, but not sure how to address this. I haven't used autohooks as I'm not sure what this is meant to do. The poetry toml file looks as follows right now:
[tool.poetry]
name = "zorinhp-thetagang"
version = "0.1.0"
description = "zorinhp-thetagang"
authors = ["Your Name <[email protected]>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "3.11.6"
thetagang = "^1.8.1"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

What is missing to run the program properly in Poetry? Generally I'm a bit new in linux so hope I didn't miss some obvious things. Thanks a lot for any feedback

Facing some issues

Hello,

Thank you for making such algo. I was planning to create my own wheel strategy for algotrading but didn't have time to get on it.

Anyways, when i run the following:
thetagang -c thetagang.toml
it seems to work well along with the api gateway with IBC, but i get errors. I can't narrow it down which might be causing the errors and force quitting the gateway. Attached the log for reference.

I'm running Win 10 and i have seamless 2FA enabled and no SLS. Read-only API is disabled.

ib_insync.log

Attached the logs from gateway as well just in case it narrows down the problem.

gateway-exported-logs.txt

Requested market data is not subscribed

First, thanks for making this tool!

I finally got docker/wsl running along with the mounted paths and config, but I'm hitting an issue and I'm a bit stuck. Unsure if I need to modify part of the config or if there is a bug. I've basically copied the sample config files - the only changes are the addition of my username/password to both, although it wasn't clear to me if I needed to add it to both or just thetagang.toml.

See the stack below:

2021-02-11 16:55:41,008 ib_insync.Watchdog INFO Starting
2021-02-11 16:55:41,008 ib_insync.IBC INFO Starting
2021-02-11 16:56:11,035 ib_insync.client INFO Connecting to 127.0.0.1:4002 with clientId 1...
2021-02-11 16:56:11,036 ib_insync.client INFO Connected
2021-02-11 16:56:11,058 ib_insync.client INFO Logged on to server version 152
2021-02-11 16:56:11,061 ib_insync.client INFO API connection ready
2021-02-11 16:56:11,067 ib_insync.wrapper INFO Warning 2158, reqId -1: Sec-def data farm connection is OK:secdefil
2021-02-11 16:56:12,075 ib_insync.ib INFO Synchronization complete
2021-02-11 16:56:12,271 ib_insync.wrapper INFO Warning 2119, reqId -1: Market data farm is connecting:usfarm
2021-02-11 16:56:12,318 ib_insync.wrapper INFO Warning 2104, reqId -1: Market data farm connection is OK:usfarm
2021-02-11 16:56:12,383 ib_insync.wrapper ERROR Error 354, reqId 4: Requested market data is not subscribed.Delayed market data is available.Error&BEST/STK/Top&BEST/STK/Top, contract: Stock(symbol='SPY', exchange='SMART', currency='USD')
2021-02-11 16:56:42,415 eventkit.event ERROR Value () caused exception for event Event<connectedEvent, [[None, None, <function start.<locals>.onConnected at 0x7fa844652c80>]]>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/eventkit/event.py", line 181, in emit
    func(*args)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/thetagang.py", line 113, in onConnected
    portfolio_manager.manage()
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 271, in manage
    self.check_if_can_write_puts(account_summary, portfolio_positions)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 443, in check_if_can_write_puts
    self.wait_for_market_price(ticker)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 60, in wait_for_market_price
    10,
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  [Previous line repeated 7 more times]
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 57, in while_n_times
    raise "Exhausted retries waiting on predicate. This shouldn't happen. "
TypeError: exceptions must derive from BaseException

IBC setup on Docker

Hi, I read through the documentation a couple of times but I'm still a little lost. Looking at the dockerfile I noticed that It sets up IBC inside its container as well as looking at the following line:

if not without_ibc:

I can deduce that the docker container will start the ibc gateway for me. Well, good. I followed the instructions, changed the configs, added my logins for IBKR but I'm getting the following when trying to run it:

2023-07-19 16:17:14,918 ib_insync.client INFO Connecting to 127.0.0.1:7497 with clientId 1...
2023-07-19 16:17:14,919 ib_insync.client INFO Disconnecting
2023-07-19 16:17:14,919 ib_insync.client ERROR API connection failed: ConnectionRefusedError(111, "Connect call failed ('127.0.0.1', 7497)")
2023-07-19 16:17:14,919 ib_insync.client ERROR Make sure API port on TWS/IBG is open
2023-07-19 16:17:14,919 ib_insync.IBC INFO Terminating
2023-07-19 16:17:17,282 ib_insync.IBC INFO Starting```

Another question is regarding the -v level. Does it work? It didn't change the logs on any of the levels. Thanks!
 docker run --pull always --rm -i --net host -v ~/thetagang:/etc/thetagang brndnmtthws/thetagang:main -v DEBUG --config /etc/thetagang/thetagang.toml

-- I'm trying to run the application targeting a paper account for now.

Position not rolled - conditions met

Currently have a position that is just ITM but with a positive P&L. Still not beeing rolled.

โ”‚ โ”‚ Roll options when โ”‚ โ”‚ โ”‚ either condition is โ”‚ โ”‚ โ”‚ true โ”‚ โ”‚ โ”‚ Days to expiry <= 15 and P&L >= 0.0 โ”‚ โ”‚ โ”‚ (0%)


SBUX โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ โ”‚P โ”‚ -1 โ”‚$0.85 โ”‚$0.93 โ”‚ $-85 โ”‚$-93 โ”‚ $8 โ”‚9.0% โ”‚$92.โ€ฆ โ”‚202โ€ฆ โ”‚ 3 โ”‚โœ”๏ธ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”˜


0 puts can be rolled

Full log in the attachment.

SBUX_NoRoll.txt

ValueError: cannot convert float NaN to integer

Calculating target positions... 0% -:--:--
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Traceback (most recent call last) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ /usr/local/lib/python3.10/dist-packages/thetagang/portfolio_manager.py:581 โ”‚
โ”‚ in manage โ”‚
โ”‚ โ”‚
โ”‚ 578 โ”‚ โ”‚ โ”‚ โ”‚ positions_table, โ”‚
โ”‚ 579 โ”‚ โ”‚ โ”‚ โ”‚ put_actions_table, โ”‚
โ”‚ 580 โ”‚ โ”‚ โ”‚ โ”‚ puts_to_write, โ”‚
โ”‚ โฑ 581 โ”‚ โ”‚ โ”‚ ) = self.check_if_can_write_puts(account_summary, portfol โ”‚
โ”‚ 582 โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ 583 โ”‚ โ”‚ โ”‚ # Look for lots of stock that don't have covered calls โ”‚
โ”‚ 584 โ”‚ โ”‚ โ”‚ (call_actions_table, calls_to_write) = self.check_for_unc โ”‚
โ”‚ โ”‚
โ”‚ /usr/local/lib/python3.10/dist-packages/thetagang/portfolio_manager.py:983 โ”‚
โ”‚ in check_if_can_write_puts โ”‚
โ”‚ โ”‚
โ”‚ 980 โ”‚ โ”‚ โ”‚ targets[symbol] = round( โ”‚
โ”‚ 981 โ”‚ โ”‚ โ”‚ โ”‚ self.config["symbols"][symbol]["weight"] * total_buyi โ”‚
โ”‚ 982 โ”‚ โ”‚ โ”‚ ) โ”‚
โ”‚ โฑ 983 โ”‚ โ”‚ โ”‚ target_quantity = math.floor(targets[symbol] / ticker.mar โ”‚
โ”‚ 984 โ”‚ โ”‚ โ”‚ โ”‚
โ”‚ 985 โ”‚ โ”‚ โ”‚ # Current number of short puts โ”‚
โ”‚ 986 โ”‚ โ”‚ โ”‚ put_count = count_short_option_positions(symbol, portfoli โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Rolling puts with increasing Delta

So when configuring with delta of 0.3 (which is the default), first time writing a put looks fine, say the put is written for the strike of S. But as the price of the underlying rises, looks like rolling just happens and we are stuck with the strike of S, now floating with delta way below 0.3. Is this intended? I looked at the code and I see the roll_positions() just does a min of config's strike_limit and position.contract.strike (plus cost so downward moving of price works correctly) to get a strike_limit, so I don't think the is a way to configure it so that we actually increase the strike and follow our 0.3 configuration for rolling puts?

only support USD currency

Hi @brndnmtthws ,

I saw USD is hard coded in multiple different places. I want to help and make this currency optional.

The easiest solution would be user can create multiple configuration files for different currencies. If that sounds good, I can help implement this change.

Thanks,

Junyuan

New coder, issue getting the bot to run.

I am pretty much teaching myself how to code. I am trying to build the bot using wsl ubuntu and have it run on my windows tower all day while i work. I am trying to build the docker file and then pass the two libraries into my copy of the code saved on my desktop, i keep getting this error when building, it seems to have to do with the tws installer for interactive brokers.
Do i need a desktop interface, can i install this using wsl? Where am i going wrong?

#6 155.5 Downloading SecretStorage-3.3.0-py3-none-any.whl (14 kB)
#6 155.5 Collecting ptyprocess>=0.5
#6 155.5 Downloading ptyprocess-0.7.0-py2.py3-none-any.whl (13 kB)
#6 155.6 Collecting pyparsing>=2.0.2
#6 155.7 Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
#6 155.7 Collecting msgpack>=0.5.2
#6 155.8 Downloading msgpack-1.0.2-cp39-cp39-manylinux1_x86_64.whl (294 kB)
#6 155.9 Collecting lockfile>=0.9; extra == "filecache"
#6 155.9 Downloading lockfile-0.12.2-py2.py3-none-any.whl (13 kB)
#6 156.2 Collecting cryptography>=2.0
#6 156.2 Downloading cryptography-3.3.1-cp36-abi3-manylinux2010_x86_64.whl (2.6 MB)
#6 157.2 Collecting cffi>=1.12
#6 157.3 Downloading cffi-1.14.4-cp39-cp39-manylinux1_x86_64.whl (405 kB)
#6 157.4 Collecting pycparser
#6 157.4 Downloading pycparser-2.20-py2.py3-none-any.whl (112 kB)
#6 157.6 Installing collected packages: pip, crashtest, poetry-core, filelock, appdirs, distlib, six, virtualenv, pylev, pastel, clikit, tomlkit, idna, certifi, chardet, urllib3, requests, webencodings, html5lib, shellingham, jeepney, pycparser, cffi, cryptography, SecretStorage, keyring, ptyprocess, pexpect, requests-toolbelt, pkginfo, cleo, pyparsing, packaging, cachy, msgpack, lockfile, cachecontrol, poetry
#6 157.6 Attempting uninstall: pip
#6 157.6 Found existing installation: pip 20.1.1
#6 157.6 Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
#6 157.6 Can't uninstall 'pip'. No files were found to uninstall.
#6 160.2 Successfully installed SecretStorage-3.3.0 appdirs-1.4.4 cachecontrol-0.12.6 cachy-0.3.0 certifi-2020.12.5 cffi-1.14.4 chardet-4.0.0 cleo-0.8.1 clikit-0.6.2 crashtest-0.3.1 cryptography-3.3.1 distlib-0.3.1 filelock-3.0.12 html5lib-1.1 idna-2.10 jeepney-0.6.0 keyring-21.8.0 lockfile-0.12.2 msgpack-1.0.2 packaging-20.8 pastel-0.2.1 pexpect-4.8.0 pip-21.0 pkginfo-1.7.0 poetry-1.1.4 poetry-core-1.0.0 ptyprocess-0.7.0 pycparser-2.20 pylev-1.3.0 pyparsing-2.4.7 requests-2.25.1 requests-toolbelt-0.9.1 shellingham-1.3.2 six-1.15.0 tomlkit-0.7.0 urllib3-1.26.2 virtualenv-20.4.0 webencodings-0.5.1
#6 160.3 30ed8944d4df950e8802b0f127f6445cd23fbfd6ecc42b375fe222f6714669fa tws-installer.sh
#6 160.3 % Total % Received % Xferd Average Speed Time Time Time Current
#6 160.3 Dload Upload Total Spent Left Speed
100 74.0M 100 74.0M 0 0 3566k 0 0:00:21 0:00:21 --:--:-- 3664k
#6 181.8 sha256sum: WARNING: 1 computed checksum did NOT match
#6 181.8 tws-installer.sh: FAILED

executor failed running [/bin/sh -c apt-get update && apt-get install -qy python3-pip xvfb libxrender1 openjfx unzip curl && pip3 install --upgrade pip poetry && echo '30ed8944d4df950e8802b0f127f6445cd23fbfd6ecc42b375fe222f6714669fa tws-installer.sh' | tee tws-installer.sh.sha256 && curl -qL https://download2.interactivebrokers.com/installers/tws/stable-standalone/tws-stable-standalone-linux-x64.sh -o tws-installer.sh && sha256sum -c tws-installer.sh.sha256 && echo 'c079e0ade7e95069e464859197498f0abb4ce277b2f101d7474df4826dcac837 ibc.zip' | tee ibc.zip.sha256 && curl -qL https://github.com/IbcAlpha/IBC/releases/download/3.8.4-beta.2/IBCLinux-3.8.4-beta.2.zip -o ibc.zip && sha256sum -c ibc.zip.sha256 && yes "" | sh tws-installer.sh && unzip ibc.zip -d /opt/ibc && chmod o+x /opt/ibc/.sh /opt/ibc//.sh && rm tws-installer.sh ibc.zip && apt-get clean && rm -rf /var/lib/apt/lists/]: exit code: 1
The terminal process "/bin/bash '-c', 'docker build --pull --rm -f "Dockerfile" -t thetagangmain:latest "."'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

One final request and everything should be good.

Sorry for the delay boss, just got home. I ran the code you sent and got this output.

delos@DESKTOP-087HUSE:~/thetagang$ docker run --rm -it \

-v ~/thetagang:/etc/thetagang \
brndnmtthws/thetagang:latest \
--config /etc/thetagang/thetagang.toml
  • export DISPLAY=:1
  • DISPLAY=:1
  • Xvfb :1 -ac -screen 0 1024x768x24
  • exec poetry run thetagang --config /etc/thetagang/thetagang.toml

Config:

Account details:
Number = DU2275067
Cancel existing orders = True
Margin usage = 0.3 (30.0%)
Market data type = 1

Roll options when either condition is true:
Days to expiry <= 15
P&L >= 0.9 (90.0%)

Write options with targets of:
Days to expiry >= 45
Delta <= 0.3
Minimum open interest >= 10

Symbols:
GLD, weight = 0.4 (40.0%)
TECL, weight = 0.3 (30.0%)
SLV, weight = 0.3 (30.0%)

My info is there, user id and all, no param for password except here.

[ibc]

IBC configuration parameters. See

https://ib-insync.readthedocs.io/api.html#ibc for details.

gateway = true
ibcPath = '/opt/ibc'
password = 'demo'
tradingMode = 'paper'
userid = 'demo'

Change this to point to your config.ini for IBC

ibcIni = '/etc/thetagang/config.ini'

Do i fill this out too boss, including the path to where anything may be installedc, or is the bot running and does it self populate it all? Its probably because markets are closed that its not running fully?

ValueError: cannot convert float NaN to integer

so boss, here is what i got, how to fix

2022-12-17 09:59:16,079 ib_insync.wrapper INFO Warning 2104, reqId -1: Market data farm connection is OK:usfarm
2022-12-17 10:01:25,149 eventkit.event ERROR Value () caused exception for event Event<connectedEvent, [[None, None, <function start..onConnected at 0x7f905169fac0>]]>
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/eventkit/event.py", line 198, in emit
result = func(*args)
File "/usr/local/lib/python3.10/dist-packages/thetagang/thetagang.py", line 172, in onConnected
portfolio_manager.manage()
File "/usr/local/lib/python3.10/dist-packages/thetagang/portfolio_manager.py", line 460, in manage
self.check_if_can_write_puts(account_summary, portfolio_positions)
File "/usr/local/lib/python3.10/dist-packages/thetagang/portfolio_manager.py", line 762, in check_if_can_write_puts
target_quantity = math.floor(targets[symbol] / ticker.marketPrice())
ValueError: cannot convert float NaN to integer
2022-12-17 10:01:25,152 ib_insync.Watchdog INFO Stopping
2022-12-17 10:01:25,153 ib_insync.ib INFO Disconnecting from 127.0.0.1:7497, 928 B sent in 16 messages, 84.5 kB received in 1756 messages, session time 163 s.

Error when running Docker

I am trying to run the Docker Image without changing any params. I just entered my login info in the .toml file and config.ini
and I am getting the following error. This is on IBKR paper trading account.


_**+ export DISPLAY=:1

  • DISPLAY=:1

  • exec /usr/local/bin/thetagang -c /etc/thetagang/thetagang.toml

  • Xvfb :1 -ac -screen 0 1024x768x24
    Config:

    Account details:
    Number = -
    Cancel existing orders = True
    Margin usage = 0.5 (50.0%)
    Market data type = 1

    Roll options when either condition is true:
    Days to expiry <= 15 and P&L >= 0.0 (0.0%)
    P&L >= 0.9 (90.0%)

    When contracts are ITM:
    Roll puts = False
    Roll calls = True

    Write options with targets of:
    Days to expiry >= 45
    Default delta <= 0.3
    Maximum new contracts = 15
    Minimum open interest = 10

    Symbols:
    SPY weight = 0.40 (40.0%), delta = 0.30p, 0.30c
    QQQ weight = 0.30 (30.0%), delta = 0.50p, 0.30c, call strike >= $100.00, put strike <= $1000.00
    TLT weight = 0.20 (20.0%), delta = 0.40p, 0.40c
    ABNB weight = 0.05 ( 5.0%), delta = 0.30p, 0.30c
    BRK.B weight = 0.05 ( 5.0%), delta = 0.30p, 0.30c

Traceback (most recent call last):
File "/usr/local/bin/thetagang", line 8, in
sys.exit(cli())
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 829, in call
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/thetagang/main.py", line 35, in cli
start(config)
File "/usr/local/lib/python3.7/dist-packages/thetagang/thetagang.py", line 128, in start
ibc = IBC(981, ibc_config)
TypeError: init() got an unexpected keyword argument 'RaiseRequestErrors'
_

Issue on Ubuntu Mate.

Want to run it on my Raspberry Pi and have the crontab execute every 10 minutes. Getting this issue when building the image.

standard_init_linux.go:211: exec user process caused "exec format error"
failed to resize tty, using default size.

How could this error be rectified? Everything else is cool on the windows version.

RFC: Create a futures-based version of ThetaGang?

Leaving this issue here as a request for comments. If I decide to get around to this, it probably won't happen for a while. Alternatively if you have a lot of money and want me to implement this, you can hire me (send me an email).

I'm considering creating a futures version of this, which would allow you to specify a leverage ratio and use futures instead of stocks or ETFs to accomplish the same thing on major indices.

The main advantages of using futures rather than ETFs are that:

  • they're slightly more efficient than using ETFs in most cases (i.e., lower fees)
  • Bitcoin futures are available
  • you can relatively easily obtain arbitrary leverage factors (up to the maximum leverage per the contract) if you have enough capital, with low costs
  • futures are taxed at 60/40 long/short cap gains

The main downside of futures are that:

  • the contracts are HUGE: the nominal value for the NASDAQ-100 is around $250k at the time of writing, so you'd need to set aside $250k to hold 1 contract and 20x that amount to write puts or calls
  • option volume is not great, due to the massive size of the contracts
  • you can never get 100% long term cap gains, you must roll the contract every quarter (or whatever the contract requires)

Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.