Giter Club home page Giter Club logo

Comments (11)

brndnmtthws avatar brndnmtthws commented on May 26, 2024

Was the market open when you ran this? The ticker is not returning a price, hence the error.

from thetagang.

robby28-11 avatar robby28-11 commented on May 26, 2024

Yes. Also at this moment same error. Even with 1.8.1

from thetagang.

brndnmtthws avatar brndnmtthws commented on May 26, 2024

Odd. Does IBKR return a price for the same symbol? There are some weird edge cases (like things with very low liquidity) where sometimes it doesn't return data. It should normally return the midpoint price, or the last trade price if that's not available. This is a tricky case because it's not safe to keep going if you can't get a valid price for the symbol, it will lead to undefined behaviour.

See here: https://ib-insync.readthedocs.io/api.html#ib_insync.ticker.Ticker.marketPrice

from thetagang.

robby28-11 avatar robby28-11 commented on May 26, 2024

Argh, yes there was one option with a price of 0.00. Just bought it for 0.01 to close that position. Fixed the issue instantly. Thanks for the hint.

from thetagang.

brndnmtthws avatar brndnmtthws commented on May 26, 2024

That's odd, I guess there must have been no bids or asks, and no trades. Maybe I will have it handle that one particular case more gracefully by skipping over contracts with no price, but I want to think about it for a bit to make sure I don't inadvertently introduce a footgun. I think it's probably safe to just ignore this specific case, but that means it won't be able to roll the contract (ideally it would expire worthless).

from thetagang.

robby28-11 avatar robby28-11 commented on May 26, 2024

The put option (T Feb16 14Put) was sold with a very low commission. Unfortunately, I hadn't used minimum_credit yet, which could probably have prevented it (which is btw just working in the latest tag, not main).
As a result, the option was too far out of the money.

Of course, the option would have simply expired - that's not a problem. The problem, however, is that all other options were set to "pause" because the script was always interrupted as a result.

So maybe it's not such a bad idea to distinguish whether the option price is 0.00 or does not return a value at all?

Screenshot 2024-02-15 081029

from thetagang.

brndnmtthws avatar brndnmtthws commented on May 26, 2024

What's especially confusing to me is that it's actually failing on getting the market price for the stock, not the option itself. That should certainly not be returning 0 or NaN. It should already gracefully handle cases where the option has no price, usually it'll print an error and continue anyway.

So, I'm either failing to understand the problem, or I've missed some key piece of information.

Was there any other detail in the stack trace from before? I'm thinking that stack trace is actually a red herring and the error is somewhere else.

If I were to guess, it's failing after this function (which tries to handle this as gracefully as possible, but could still return a 0 or NaN in some cases):

thetagang/thetagang/util.py

Lines 207 to 222 in 42af123

def midpoint_or_market_price(ticker: Ticker) -> float:
# As per the ib_insync docs, marketPrice returns the last price first, but
# we often prefer the midpoint over the last price. This function pulls the
# midpoint first, then falls back to marketPrice() if midpoint is nan.
if util.isNan(ticker.midpoint()):
if (
util.isNan(ticker.marketPrice())
and ticker.modelGreeks
and ticker.modelGreeks.optPrice
):
# Fallback to the model price if the greeks are available
return ticker.modelGreeks.optPrice
else:
return ticker.marketPrice()
return ticker.midpoint()

The error itself is caused by a failed type conversion from a float to an integer, which doesn't quite make sense because the prices are handled as floats. I will make a tiny correction so that in the case that ticker.marketPrice() is NaN, it'll return 0.0 instead.

from thetagang.

robby28-11 avatar robby28-11 commented on May 26, 2024

Something odd is happening here:

Log from yesterday attached. It just happens in my paper account. The live account does not have any issues.
I'm using the cash management in the paper only. Currently the paper only have SGOV in account.
It somehow fails to caluclate target positions.

Maybe this helps to find whats happening.
Error.txt

from thetagang.

brndnmtthws avatar brndnmtthws commented on May 26, 2024

One of those two values (either the market price, or the weights) has to be NaN. It's not the weight, because it prints that earlier (showing 20%), so it must be the market price.

What do you have this setting set to:

api_response_wait_time = 60

The only thing I can think of, is that if ib_insync.api_response_wait_time is too small, it won't wait long enough for the API to get the market prices. Anything less than 5 is probably going to frequently cause problems.

In any case, I'll make one more tweak to this code, which will hopefully handle this situation better.

from thetagang.

robby28-11 avatar robby28-11 commented on May 26, 2024

api_response_wait_time = 60

for both paper/live

from thetagang.

robby28-11 avatar robby28-11 commented on May 26, 2024

Now it is just working. Haven't changed anything. But thanks for digging into this rare issue.

from thetagang.

Related Issues (20)

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.