当前位置: 首页>>代码示例>>Python>>正文


Python Clock.is_market_open方法代码示例

本文整理汇总了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)
开发者ID:travishoffman,项目名称:mats,代码行数:89,代码来源:theloop.py


注:本文中的clock.Clock.is_market_open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。