Skip to content

Commit

Permalink
better IBKR closing + basic sp500 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Feb 29, 2024
1 parent 1420d04 commit 6414ae6
Show file tree
Hide file tree
Showing 8 changed files with 2,567 additions and 18 deletions.
7 changes: 5 additions & 2 deletions roboquant/brokers/ibkrbroker.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,16 @@ def __init__(self, host="127.0.0.1", port=4002, account=None, client_id=123) ->
self.__has_new_orders_since_sync = False

# Start the handling in a thread
self.__api_thread = threading.Thread(target=api.run, daemon=False)
self.__api_thread = threading.Thread(target=api.run, daemon=True)
self.__api_thread.start()
time.sleep(3.0)

def disconnect(self):
self.__api.reader.conn.disconnect()

def _should_sync(self, now: datetime):
"""Avoid too many API calls"""
return self.__has_new_orders_since_sync or now - self.__account.last_update > timedelta(seconds=30)
return self.__has_new_orders_since_sync or now - self.__account.last_update > timedelta(seconds=1)

def sync(self, event: Event | None = None) -> Account:
"""Sync with the IBKR account
Expand Down
17 changes: 14 additions & 3 deletions roboquant/feeds/feedutil.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json
import pathlib
import threading
from datetime import datetime

from roboquant.event import Candle
from roboquant.timeframe import Timeframe
from .eventchannel import EventChannel, ChannelClosed
from .feed import Feed
from roboquant.feeds.eventchannel import EventChannel, ChannelClosed
from roboquant.feeds.feed import Feed


def play_background(feed: Feed, channel: EventChannel):
Expand All @@ -21,7 +23,7 @@ def __background():
finally:
channel.close()

thread = threading.Thread(None, __background, daemon=False)
thread = threading.Thread(None, __background, daemon=True)
thread.start()


Expand Down Expand Up @@ -65,6 +67,15 @@ def get_symbol_dataframe(feed: Feed, symbol: str, timeframe: Timeframe | None =
return pd.DataFrame(ohlcv, columns=["Date", "Open", "High", "Low", "Close", "Volume"]).set_index("Date")


def get_sp500_symbols():
full_path = pathlib.Path(__file__).parent.resolve().joinpath("sp500.json")
with open(full_path) as f:
content = f.read()
j = json.loads(content)
symbols = [elem["Symbol"] for elem in j]
return symbols


def print_feed_items(feed: Feed, timeframe: Timeframe | None = None, timeout: float | None = None):
"""Print the items in a feed to the console.
This is mostly useful for debugging purposes to see what events a feed generates.
Expand Down
Loading

0 comments on commit 6414ae6

Please sign in to comment.