erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.84k stars 770 forks source link

marketPrice #109

Closed ejpjapan closed 6 years ago

ejpjapan commented 6 years ago

I think there may be a very minor issue with the marketPrice() method in the Ticker class. When the market is closed the bid and and ask will return a -1. When that happens the first conditional statement will return nan instead of last. As a result the next conditional statement returns the close. Instead the first conditional statement on line 109 should include an additional condition such as below:

"""
        Return the first available one of

        * last price if within current bid/ask or bid/ask return -1
        * average of bid and ask (midpoint);
        * close price.
        """
midpoint = self.midpoint()
        price = self.last if (
            isNan(midpoint) or self.bid <= self.last <= self.ask or
            (self.bid == -1 or self.ask == -1)) else nan    
        if isNan(price):
            price = midpoint
        if isNan(price) or price == -1:
            price = self.close
        return price

Instead of:

    """
 """
        Return the first available one of

        * last price if within current bid/ask;
        * average of bid and ask (midpoint);
        * close price.
midpoint = self.midpoint()
        price = self.last if (
            isNan(midpoint) or self.bid <= self.last <= self.ask) else nan
        if isNan(price):
            price = midpoint
        if isNan(price) or price == -1:
            price = self.close
        return price
erdewit commented 6 years ago

You propose to use last price instead of close price when the market is closed. The issue with this is that a client that was connected since before the market closed will have a last price but a client connected after will not. They will then show different market prices. So it's more consistent to use the close price.