Skip to content

Commit

Permalink
Timestamp format made consistent with other time formats. Added a lis…
Browse files Browse the repository at this point in the history
…tener name to Verbose Listeners.
  • Loading branch information
akapur committed Mar 8, 2016
1 parent c951d71 commit 2281d94
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 37 deletions.
52 changes: 42 additions & 10 deletions conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
from collections import deque, namedtuple
from typing import Sequence, List, Callable, Tuple
from listeners import VerboseIQFeedListener, VerboseAdminListener, VerboseQuoteListener


def blob_to_str(val) -> str:
Expand Down Expand Up @@ -119,6 +120,26 @@ def read_mmddccyy(field: str) -> np.datetime64:
return np.datetime64(datetime.date(year=1, month=1, day=1), 'D')


def read_ccyymmdd(field: str) -> np.datetime64:
if field != "":
year = int(field[0:4])
month = int(field[4:6])
day = int(field[6:8])
return np.datetime64(datetime.date(year=year, month=month, day=day), 'D')
else:
return np.datetime64(datetime.date(year=1, month=1, day=1), 'D')


def read_dtn_timestamp(dt_tm: str) -> Tuple[np.datetime64, int]:
if dt_tm != "":
(date_str, time_str) = dt_tm.split(' ')
dt = read_ccyymmdd(date_str)
tm = read_hhmmss(time_str)
return dt, tm
else:
return np.datetime64(datetime.date(year=1, month=1, day=1), 'D'), 0


# noinspection PyUnresolvedReferences
def read_yyyymmdd_hhmmss(dt_tm: str) -> Tuple[datetime.date, int]:
if dt_tm != "":
Expand Down Expand Up @@ -416,12 +437,12 @@ def process_conn_stats(self, fields: Sequence[str]) -> None:
listener.process_conn_stats(conn_stats)

def process_timestamp(self, fields: Sequence[str]) -> None:
# T,[YYYYMMDD HH:MM:SS]
assert fields[0] == "T"
assert len(fields) > 1
# TODO: Change to a tuple of np.datetime64 and millisecs since midnight
time_val = time.strptime("%s EST" % fields[1], "%Y%m%d %H:%M:%S %Z")
dt_tm_tuple = read_dtn_timestamp(fields[1])
for listener in self._listeners:
listener.process_timestamp(time_val)
listener.process_timestamp(dt_tm_tuple)

def process_error(self, fields: Sequence[str]) -> None:
assert fields[0] == "E"
Expand Down Expand Up @@ -2003,14 +2024,15 @@ def request_equity_option_chain(self, symbol: str, opt_type: str = 'pc',
svc.launch()

admin_conn = AdminConn(name="RunningInIde")
admin_listener = VerboseAdminListener("AdminListener")
admin_conn.add_listener(admin_listener)
admin_conn.start_runner()
admin_conn.set_admin_variables_from_dict(svc.admin_variables())
admin_conn.client_stats_on()
time.sleep(30)
admin_conn.client_stats_off()
admin_conn.stop_runner()

quote_conn = QuoteConn(name="RunningInIDE")
quote_listener = VerboseQuoteListener("QuoteListener")
quote_conn.add_listener(quote_listener)
quote_conn.start_runner()

quote_conn.request_all_update_fieldnames()
Expand All @@ -2020,12 +2042,10 @@ def request_equity_option_chain(self, symbol: str, opt_type: str = 'pc',
quote_conn.select_update_fieldnames(all_fields)
quote_conn.watch("@VXH16")
time.sleep(30)
quote_conn.unwatch("@VXH16")
print("Unwatched")
time.sleep(3)
quote_conn.stop_runner()

hist_conn = HistoryConn(name="RunningInIde")
hist_listener = VerboseIQFeedListener("HistListener")
hist_conn.add_listener(hist_listener)
hist_conn.start_runner()

ticks = hist_conn.request_ticks("INTC", 10)
Expand Down Expand Up @@ -2070,6 +2090,8 @@ def request_equity_option_chain(self, symbol: str, opt_type: str = 'pc',
print(monthly)

table_conn = TableConn(name="RunningInIDE")
table_listener = VerboseIQFeedListener("TableListener")
table_conn.add_listener(table_listener)
table_conn.update_tables()
print(table_conn.get_markets())
print(table_conn.get_security_types())
Expand All @@ -2078,6 +2100,8 @@ def request_equity_option_chain(self, symbol: str, opt_type: str = 'pc',
print(table_conn.get_naic_codes())

lookup_conn = LookupConn(name="RunningInIDE")
lookup_listener = VerboseIQFeedListener("LookupListener")
lookup_conn.add_listener(lookup_listener)
lookup_conn.start_runner()

tesla_syms = lookup_conn.request_symbols_by_filter(search_term='TSLA', search_field='s')
Expand Down Expand Up @@ -2121,7 +2145,15 @@ def request_equity_option_chain(self, symbol: str, opt_type: str = 'pc',
timeout=None)
print(e_opt)

time.sleep(30)
admin_conn.client_stats_off()
quote_conn.unwatch("@VXH16")
print("Unwatched")
time.sleep(3)

lookup_conn.stop_runner()
quote_conn.stop_runner()
admin_conn.stop_runner()



Expand Down
64 changes: 37 additions & 27 deletions listeners.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import time
from typing import Tuple


class SilentIQFeedListener:
Expand All @@ -16,7 +16,7 @@ def feed_has_error(self) -> None:
def process_conn_stats(self, stats: dict) -> None:
pass

def process_timestamp(self, time_val: time.struct_time) -> None:
def process_timestamp(self, time_val: Tuple[np.datetime64, int]) -> None:
pass


Expand Down Expand Up @@ -85,91 +85,101 @@ def process_client_stats(self, client_stats: dict) -> None:
# noinspection PyMethodMayBeStatic
class VerboseIQFeedListener:

def __init__(self, name: str):
self._name = name

def feed_is_stale(self) -> None:
print("Feed Disconnected")
print("%s: Feed Disconnected" % self._name)

def feed_is_fresh(self) -> None:
print("Feed Connected")
print("%s: Feed Connected" % self._name)

def feed_has_error(self) -> None:
print("Feed Reconnect Failed")
print("%s: Feed Reconnect Failed" % self._name)

def process_conn_stats(self, stats: dict) -> None:
print("Connection Stats:")
print("%s: Connection Stats:" % self._name)
print(stats)

def process_timestamp(self, time_val: time.struct_time):
print("Timestamp:")
def process_timestamp(self, time_val: Tuple[np.datetime64, int]):
print("%s: Timestamp:" % self._name)
print(time_val)


# noinspection PyMethodMayBeStatic
class VerboseQuoteListener(VerboseIQFeedListener):

def __init__(self, name: str):
super().__init__(name)

def process_invalid_symbol(self, bad_symbol: str) -> None:
print("Invalid Symbol: %s" % bad_symbol)
print("%s: Invalid Symbol: %s" % (self._name, bad_symbol))

def process_news(self, news_item: dict) -> None:
print("News Item Received")
print("%s: News Item Received" % self._name)
print(news_item)

def process_regional_quote(self, quote: np.array) -> None:
print("Regional Quote:")
print("%s: Regional Quote:" % self._name)
print(quote)

def process_summary(self, summary: np.array) -> None:
print("Data Summary")
print("%s: Data Summary" % self._name)
print(summary)

def process_update(self, update: np.array) -> None:
print("Data Update")
print("%s: Data Update" % self._name)
print(update)

def process_fundamentals(self, fund: np.array) -> None:
print("Fundamentals Received:")
print("%s: Fundamentals Received:" % self._name)
print(fund)

def process_auth_key(self, key: str) -> None:
print("Authorization Key Received: %s" % key)
print("%s: Authorization Key Received: %s" % (self._name, key))

def process_keyok(self) -> None:
print("Authorization Key OK")
print("%s: Authorization Key OK" % self._name)

def process_customer_info(self, cust_info: dict) -> None:
print("Customer Information:")
print("%s: Customer Information:" % self._name)
print(cust_info)

def process_symbol_limit_reached(self, sym: str) -> None:
print("Symbol Limit Reached with subscription to %s" % sym)
print("%s: Symbol Limit Reached with subscription to %s" % (self._name, sym))

def process_ip_addresses_used(self, ip: str) -> None:
print("IP Addresses Used: %s" % ip)
print("%s: IP Addresses Used: %s" % (self._name, ip))


# noinspection PyMethodMayBeStatic
class VerboseAdminListener(VerboseIQFeedListener):

def __init__(self, name: str):
super().__init__(name)

def process_register_client_app_completed(self) -> None:
print("Register Client App Completed")
print("%s: Register Client App Completed" % self._name)

def process_remove_client_app_completed(self) -> None:
print("Remove Client App Completed")
print("%s: Remove Client App Completed" % self._name)

def process_current_login(self, login: str) -> None:
print("Current Login: %s" % login)
print("%s: Current Login: %s" % (self._name, login))

def process_current_password(self, password: str) -> None:
print("Current Password: %s" % password)
print("%s: Current Password: %s" % (self._name, password))

def process_login_info_saved(self) -> None:
print("Login Info Saved")
print("%s: Login Info Saved" % self._name)

def process_autoconnect_on(self) -> None:
print("Autoconnect On")
print("%s: Autoconnect On" % self._name)

def process_autoconnect_off(self) -> None:
print("Autoconnect Off")
print("%s: Autoconnect Off" % self._name)

def process_client_stats(self, client_stats: dict) -> None:
print("Client Stats:")
print("%s: Client Stats:" % self._name)
print(client_stats)

0 comments on commit 2281d94

Please sign in to comment.