def sfd_process(sfd): FX_CHANNEL = "lightning_ticker_FX_BTC_JPY" BTC_CHANNEL = "lightning_ticker_BTC_JPY" class BitflyerSubscriberCallback(SubscribeCallback): def message(self, pubnub, message): tick = message.message timestamp = dateutil.parser.parse(tick["timestamp"]) with sfd.lock: changed = False if message.channel == FX_CHANNEL: if sfd.fx_ltp != tick["ltp"]: sfd.fx_ltp = tick["ltp"] changed = True elif message.channel == BTC_CHANNEL: if sfd.btc_ltp != tick["ltp"]: sfd.btc_ltp = tick["ltp"] changed = True if sfd.ready and changed: sfd.disparity = (sfd.fx_ltp / sfd.btc_ltp) * 100 - 100 if (sfd.fx_ltp > sfd.btc_ltp and sfd.disparity >= 10.0) or \ (sfd.fx_ltp < sfd.btc_ltp and sfd.disparity <= -10.0): sfd.last_sfd_at = timestamp sfd.updated_at = timestamp sfd.event.set() config = PNConfiguration() config.subscribe_key = 'sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f' config.reconnect_policy = PNReconnectionPolicy.LINEAR config.ssl = False config.set_presence_timeout(60) pubnub = PubNubTornado(config) listener = BitflyerSubscriberCallback() pubnub.add_listener(listener) pubnub.subscribe().channels([FX_CHANNEL, BTC_CHANNEL]).execute() pubnub.start()
class RealtimeData(Settings): last_price = 0 def __init__(self): super().__init__() self.c = PNConfiguration() self.c.subscribe_key = 'sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f' self.c.reconnect_policy = PNReconnectionPolicy.LINEAR self.pubnub = PubNubTornado(self.c) channels = [ self.realtime_product, ] self.main(channels) self.pubnub.start() @gen.coroutine def main(self, channels): class Callback(SubscribeCallback): def message(self, pubnub, message): RealtimeData.last_price = message.message['ltp'] pubnub.stop() # 必要なデータを取得したら一度停止する s = Callback() self.pubnub.add_listener(s) self.pubnub.subscribe().channels(channels).execute() def stop(self): self.pubnub.stop() def get_current_data(self): self.pubnub.start()
class BitcoinTickServer(): def __init__(self, fxevents, tick_interval): self.fxevents = fxevents self.tick_interval = tick_interval self.instruments = 'lightning_ticker_BTC_JPY' config = PNConfiguration() config.subscribe_key = 'sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f' config.reconnect_policy = PNReconnectionPolicy.LINEAR self.pubnub = PubNubTornado(config) self.run = True def get_ticks(self): ticks = self.listener.get_ticks() return ticks def get_time(self): time = self.listener.get_time() return time @gen.coroutine def thread(self): self.listener = BitflyerSubscriberCallback(self.tick_interval) self.pubnub.add_listener(self.listener) self.pubnub.subscribe().channels(self.instruments).execute() self.pubnub.start() try: while self.run: self.do_unsubscribe() except: print('@BitcoinTickServerThread : exception raised.') traceback.print_exc() self.fxevents.put(event_thread.EventContainer(name='exception', value=[])) def thread_close(self): self.run = False def do_unsubscribe(self): if self.run==False: self.pubnub.unsubscribe({'channel': self.instruments}) raise Exception
class TestPubNubAsyncPublish(AsyncTestCase): def setUp(self): AsyncTestCase.setUp(self) self.env = None def callback(self, tornado_res): self.env = tornado_res.result() self.pubnub.stop() self.stop() def assert_success(self, pub): pub.future().add_done_callback(self.callback) self.pubnub.start() self.wait() assert isinstance(self.env, TornadoEnvelope) assert isinstance(self.env.result, PNPublishResult) assert isinstance(self.env.status, PNStatus) assert self.env.result.timetoken > 0 assert len(self.env.status.original_response) > 0 @tornado.testing.gen_test def assert_success_yield(self, pub): envelope = yield pub.future() assert isinstance(envelope, TornadoEnvelope) assert isinstance(envelope.result, PNPublishResult) assert isinstance(envelope.status, PNStatus) assert envelope.result.timetoken > 0 assert len(envelope.status.original_response) > 0 def assert_success_publish_get(self, msg): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_success(self.pubnub.publish().channel(ch).message(msg)) self.assert_success_yield(self.pubnub.publish().channel(ch).message(msg)) def assert_success_publish_post(self, msg): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_success(self.pubnub.publish().channel(ch).message(msg).use_post(True)) self.assert_success_yield(self.pubnub.publish().channel(ch).message(msg).use_post(True)) def assert_success_publish_get_encrypted(self, msg): self.pubnub = PubNubTornado(pnconf_enc, custom_ioloop=self.io_loop) self.assert_success(self.pubnub.publish().channel(ch).message(msg)) self.assert_success_yield(self.pubnub.publish().channel(ch).message(msg)) def assert_success_publish_post_encrypted(self, msg): self.pubnub = PubNubTornado(pnconf_enc, custom_ioloop=self.io_loop) self.assert_success(self.pubnub.publish().channel(ch).message(msg).use_post(True)) self.assert_success_yield(self.pubnub.publish().channel(ch).message(msg).use_post(True)) def assert_client_side_error(self, pub, expected_err_msg): try: yield pub.future() self.pubnub.start() self.wait() except PubNubException as e: assert expected_err_msg in str(e) self.pubnub.stop() self.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/mixed_via_get.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_publish_mixed_via_get(self): self.assert_success_publish_get("hi") self.assert_success_publish_get(5) self.assert_success_publish_get(True) self.assert_success_publish_get(["hi", "hi2", "hi3"]) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/object_via_get.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['method', 'scheme', 'host', 'port', 'object_in_path', 'query']) def test_publish_object_via_get(self): self.assert_success_publish_get({"name": "Alex", "online": True}) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/mixed_via_post.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['method', 'scheme', 'host', 'port', 'path', 'query']) def test_publish_mixed_via_post(self): self.assert_success_publish_post("hi") self.assert_success_publish_post(5) self.assert_success_publish_post(True) self.assert_success_publish_post(["hi", "hi2", "hi3"]) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/object_via_post.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['host', 'method', 'path', 'query', 'object_in_body']) def test_publish_object_via_post(self): self.assert_success_publish_post({"name": "Alex", "online": True}) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/mixed_via_get_encrypted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_publish_mixed_via_get_encrypted(self): self.assert_success_publish_get_encrypted("hi") self.assert_success_publish_get_encrypted(5) self.assert_success_publish_get_encrypted(True) self.assert_success_publish_get_encrypted(["hi", "hi2", "hi3"]) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/object_via_get_encrypted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['host', 'method', 'query', 'object_in_path'], match_on_kwargs={'object_in_path': { 'decrypter': gen_decrypt_func('testKey')}}) def test_publish_object_via_get_encrypted(self): self.assert_success_publish_get_encrypted({"name": "Alex", "online": True}) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/mixed_via_post_encrypted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['method', 'path', 'query', 'body']) def test_publish_mixed_via_post_encrypted(self): self.assert_success_publish_post_encrypted("hi") self.assert_success_publish_post_encrypted(5) self.assert_success_publish_post_encrypted(True) self.assert_success_publish_post_encrypted(["hi", "hi2", "hi3"]) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/object_via_post_encrypted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['method', 'path', 'query', 'object_in_body'], match_on_kwargs={'object_in_body': { 'decrypter': gen_decrypt_func('testKey')}}) def test_publish_object_via_post_encrypted(self): self.assert_success_publish_post_encrypted({"name": "Alex", "online": True}) def test_error_missing_message(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_client_side_error(self.pubnub.publish().channel(ch).message(None), "Message missing") def test_error_missing_channel(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_client_side_error(self.pubnub.publish().channel("").message("hey"), "Channel missing") def test_error_non_serializable(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) def method(): pass self.assert_client_side_error(self.pubnub.publish().channel(ch).message(method), "not JSON serializable") def sserr_cb(self, env): assert isinstance(env, Future) exception = env.exception() self.pubnub.stop() # this kind of assertion will not fail the test if'll be moved below `self.stop()` call # but also not raises correct exception, timeout exception will be raised on fail instead assert self.expected_err_msg in str(exception) self.stop() def assert_server_side_error(self, pub, expected_err_msg): self.expected_err_msg = expected_err_msg pub.result().add_done_callback(self.sserr_cb) self.pubnub.start() self.wait() @tornado.testing.gen_test def assert_server_side_error_yield(self, pub, expected_err_msg): try: yield pub.result() self.pubnub.start() self.wait() except PubNubException as e: assert expected_err_msg in str(e) self.pubnub.stop() self.stop() @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/invalid_key.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_error_invalid_key(self): conf = PNConfiguration() conf.publish_key = "fake" conf.subscribe_key = "demo" self.pubnub = PubNubTornado(conf, custom_ioloop=self.io_loop) self.assert_server_side_error(self.pubnub.publish().channel(ch).message("hey"), "Invalid Key") self.assert_server_side_error_yield(self.pubnub.publish().channel(ch).message("hey"), "Invalid Key") @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/not_permitted.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_error_not_permitted_403(self): my_pnconf = pnconf_pam_copy() my_pnconf.secret_key = None self.pubnub = PubNubTornado(my_pnconf, custom_ioloop=self.io_loop) self.assert_server_side_error( self.pubnub.publish().channel("not_permitted_channel").message("hey"), "HTTP Client Error (403)") self.assert_server_side_error_yield( self.pubnub.publish().channel("not_permitted_channel").message("hey"), "HTTP Client Error (403)") @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/meta_object.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub'], match_on=['host', 'method', 'path', 'meta_object_in_query']) def test_publish_with_meta(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_success( self.pubnub.publish().channel(ch).message("hey").meta({'a': 2, 'b': 'qwer'})) self.assert_success_yield( self.pubnub.publish().channel(ch).message("hey").meta({'a': 2, 'b': 'qwer'})) @pn_vcr.use_cassette( 'tests/integrational/fixtures/tornado/publish/do_not_store.yaml', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'l_pub']) def test_publish_do_not_store(self): self.pubnub = PubNubTornado(pnconf, custom_ioloop=self.io_loop) self.assert_success( self.pubnub.publish().channel(ch).message("hey").should_store(False)) self.assert_success_yield( self.pubnub.publish().channel(ch).message("hey").should_store(False))
if chumon_umu == 0: my_position = buy_or_sell(buy_1hour_vol, buy_vol, buy_5sec_vol, sell_1hour_vol, sell_vol, sell_5sec_vol) #"BUY"か"SELL"posがあればentry if my_position != "NONE" and my_position != my_last_position: my_position = entry(my_position, my_order_size) my_last_position = my_position print("POSITION: ", my_position) chumon_umu = 1 elif chumon_umu == 1: close_flag = close_or_dont_close(buy_vol, buy_5sec_vol, sell_vol, sell_5sec_vol, my_position) print(close_flag) if "CLOSE" == close_flag: close_result = close(my_position, my_order_size) if close_result == "NONE": chumon_umu = 0 print("POSITION: ", close_result) my_position = "NONE" else: print(my_position) except Exception as e: print(e) if __name__ == "__main__": main(['lightning_executions_FX_BTC_JPY']) pubnub.start()
# pubnub receive @gen.coroutine def main(channels): class BitflyerSubscriberCallback(SubscribeCallback): def presence(self, pubnub, presence): pass def status(self, pubnub, status): if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory: pass elif status.category == PNStatusCategory.PNConnectedCategory: pass elif status.category == PNStatusCategory.PNReconnectedCategory: pass elif status.category == PNStatusCategory.PNDecryptionErrorCategory: pass def message(self, pubnub, message): try: received_message_task(message.channel, message.message) except: print('Could not do received_message_task.') listener = BitflyerSubscriberCallback() pubnub.add_listener(listener) pubnub.subscribe().channels(channels).execute() if __name__ == '__main__': main(['lightning_executions_FX_BTC_JPY']) pubnub.start()
class FastTrader(Settings): last_price = 0 hd = ConditionChecker() hd.market_reader() # マーケットの流れを確認 hd.board_status_checker() # サーバー状態を確認 hd.sfd_status_checker() # SFDを確認 rec = Recorder() count = 0 count2 = 0 last_balance = 0 order = OrderMaker() def __init__(self): super().__init__() self.c = PNConfiguration() self.c.subscribe_key = 'sub-c-52a9ab50-291b-11e5-baaa-0619f8945a4f' self.c.reconnect_policy = PNReconnectionPolicy.LINEAR self.pubnub = PubNubTornado(self.c) channels = [ self.realtime_product, ] self.main(channels) self.pubnub.start() @gen.coroutine def main(self, channels): class Callback(SubscribeCallback): def message(self, pubnub, message): current_price = message.message['ltp'] FastTrader.hd.child_order_checker() FastTrader.hd.position_checker() if FastTrader.count == 0: FastTrader.hd.market_reader() print(current_price) if FastTrader.hd.positioning and not FastTrader.hd.ordering: FastTrader.hd.position_checker_for_market_ordering( current_price) print('aiming to place execution order') FastTrader.count += 1 if FastTrader.hd.signal: print('aiming to place a new order') FastTrader.hd.order_information_checker("MARKET") if FastTrader.count == 40: FastTrader.count2 += 1 FastTrader.hd.sfd_status_checker() if FastTrader.count2 == 10: FastTrader.rec.balance_recorder( FastTrader.hd.balance, current_price) FastTrader.count2 = 0 FastTrader.count = 0 s = Callback() self.pubnub.add_listener(s) self.pubnub.subscribe().channels(channels).execute() def stop(self): self.pubnub.stop() def get_current_data(self): self.pubnub.start()