本文整理汇总了Python中clock.Clock.is_market_open方法的典型用法代码示例。如果您正苦于以下问题:Python Clock.is_market_open方法的具体用法?Python Clock.is_market_open怎么用?Python Clock.is_market_open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类clock.Clock
的用法示例。
在下文中一共展示了Clock.is_market_open方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from clock import Clock [as 别名]
# 或者: from clock.Clock import is_market_open [as 别名]
class TheLoop:
def __init__(self):
self.clock = Clock()
self.logger = logging.getLogger('mats')
self.accepting_new_quote = True
self.accepting_new_trade = True
self.ticker_handlers = {
'incomplete_read': self.incomplete_read_handler,
'ssl_error': self.ssl_error_handler,
'unknown_error': self.unknown_error_handler,
'connected': self.connected_handler,
'new_quote': self.new_quote_handler,
'new_trade': self.new_trade_handler,
'empty_watchlist': self.empty_watchlist_handler
}
def loop(self):
self.ticker_p, self.ticker_conn = self.launch_ticker()
while True:
self.loop_iter()
def loop_iter(self):
if not self.clock.is_market_open():
self.logger.info('theloop: market is closed. killing ticker.')
self.ticker_p.terminate()
# block until ticker child has time to exit
while self.ticker_p.is_alive():
pass
# now go to sleep
self.go_to_sleep()
elif not self.ticker_p.is_alive():
self.ticker_p, self.ticker_conn = self.launch_ticker()
if self.ticker_conn.poll():
data = self.ticker_conn.recv()
self.ticker_handlers[data.name](data)
def ticker(self, conn):
ticker = Ticker(conn)
ticker.start()
def launch_ticker(self):
self.logger.info('theloop: spawning ticker child process')
parent, child = Pipe()
p = Process(target=self.ticker, name='ticker', args=(child,))
p.start()
return (p, parent)
def incomplete_read_handler(self, data):
self.logger.error('theloop: ticker had incompleteread error, attempting to respawn ticker.')
self.ticker_p, self.ticker_conn = self.launch_ticker()
def ssl_error_handler(self, data):
self.logger.error('theloop: ticker had ssl_error, attempting to respawn ticker.')
self.ticker_p, self.ticker_conn = self.launch_ticker()
def unknown_error_handler(self, data):
self.logger.error('theloop: ticker had an unknown error, attempting to respawn ticker.')
self.ticker_p, self.ticker_conn = self.launch_ticker()
def connected_handler(self, event):
self.logger.info('theloop: ticker connected to stream')
def go_to_sleep(self):
secs = self.clock.secs_until_open()
time_slept = time.time()
self.logger.info('theloop: sleeping for ' + str(secs) + ' seconds.')
time.sleep(secs)
secs_asleep = time.time() - time_slept
self.logger.info('theloop: waking up after ' + str(secs_asleep) + ' seconds')
self.logger.info('theloop: attempting to respawn ticker.')
self.ticker_p, self.ticker_conn = self.launch_ticker()
def new_quote_handler(self, event):
self.logger.debug('theloop: ticker reports new quote')
def new_trade_handler(self, event):
self.logger.debug('theloop: ticker reports new trade')
def empty_watchlist_handler(self, event):
self.logger.info('theloop: exiting.')
exit(0)