From 9a84dd71148309e8c4c42a411a29675199ec4d01 Mon Sep 17 00:00:00 2001 From: Jacob Plaster Date: Wed, 11 Sep 2019 11:15:53 +0100 Subject: [PATCH] examples: add rest create order example --- bfxapi/examples/rest/create_order.py | 43 ++++++++++++++++++++++++++++ bfxapi/examples/ws/cancel_order.py | 10 +++---- bfxapi/tests/test_ws_orders.py | 12 ++++---- 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 bfxapi/examples/rest/create_order.py diff --git a/bfxapi/examples/rest/create_order.py b/bfxapi/examples/rest/create_order.py new file mode 100644 index 0000000..ed0a927 --- /dev/null +++ b/bfxapi/examples/rest/create_order.py @@ -0,0 +1,43 @@ +import os +import sys +import asyncio +import time +sys.path.append('../') + +from bfxapi import Client + +API_KEY=os.getenv("BFX_KEY") +API_SECRET=os.getenv("BFX_SECRET") + +bfx = Client( + API_KEY=API_KEY, + API_SECRET=API_SECRET, + logLevel='DEBUG' +) + +async def create_order(): + response = await bfx.rest.submit_order("tBTCUSD", 10, 0.1) + # response is in the form of a Notification object + for o in response.notify_info: + # each item is in the form of an Order object + print ("Order: ", o) + +async def cancel_order(): + response = await bfx.rest.submit_cancel_order(1185510865) + # response is in the form of a Notification object + # notify_info is in the form of an order object + print ("Order: ", response.notify_info) + +async def update_order(): + response = await bfx.rest.submit_update_order(1185510771, price=15, amount=0.055) + # response is in the form of a Notification object + # notify_info is in the form of an order object + print ("Order: ", response.notify_info) + +async def run(): + await create_order() + await cancel_order() + await update_order() + +t = asyncio.ensure_future(run()) +asyncio.get_event_loop().run_until_complete(t) diff --git a/bfxapi/examples/ws/cancel_order.py b/bfxapi/examples/ws/cancel_order.py index 18d0703..2c50b25 100644 --- a/bfxapi/examples/ws/cancel_order.py +++ b/bfxapi/examples/ws/cancel_order.py @@ -10,20 +10,18 @@ API_SECRET=os.getenv("BFX_SECRET") bfx = Client( API_KEY=API_KEY, API_SECRET=API_SECRET, - logLevel='INFO' + logLevel='DEBUG' ) @bfx.ws.on('order_closed') -def order_cancelled(order, trade): +def order_cancelled(order): print ("Order cancelled.") print (order) - print (trade) @bfx.ws.on('order_confirmed') -async def trade_completed(order, trade): +async def trade_completed(order): print ("Order confirmed.") print (order) - print (trade) await bfx.ws.cancel_order(order.id) @bfx.ws.on('error') @@ -32,7 +30,7 @@ def log_error(msg): @bfx.ws.once('authenticated') async def submit_order(auth_message): - # create an inital order a really low price so it stays open + # create an initial order at a really low price so it stays open await bfx.ws.submit_order('tBTCUSD', 10, 1, Order.Type.EXCHANGE_LIMIT) bfx.ws.run() diff --git a/bfxapi/tests/test_ws_orders.py b/bfxapi/tests/test_ws_orders.py index ebf5e5b..23ee04e 100644 --- a/bfxapi/tests/test_ws_orders.py +++ b/bfxapi/tests/test_ws_orders.py @@ -122,7 +122,7 @@ async def test_closed_callback_on_submit_order_closed(): client.ws._emit('c1', order) callback_wait = EventWatcher.watch(client.ws, 'c1') # override cid generation - client.ws.orderManager._gen_unqiue_cid = lambda: 123 + client.ws.orderManager._gen_unique_cid = lambda: 123 await client.ws.submit_order('tBTCUSD', 19000, 0.01, 'EXCHANGE MARKET', onClose=c) await client.ws.publish([0,"oc",[123,None,1548262833910,"tBTCUSD",1548262833379,1548262888016,0,-1,"EXCHANGE LIMIT",None,None,None,0,"EXECUTED @ 15980.0(-0.5): was PARTIALLY FILLED @ 15980.0(-0.5)",None,None,15980,15980,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]]) callback_wait.wait_until_complete() @@ -139,7 +139,7 @@ async def test_confirmed_callback_on_submit_order_closed(): client.ws._emit('c1', order) callback_wait = EventWatcher.watch(client.ws, 'c1') # override cid generation - client.ws.orderManager._gen_unqiue_cid = lambda: 123 + client.ws.orderManager._gen_unique_cid = lambda: 123 await client.ws.submit_order('tBTCUSD', 19000, 0.01, 'EXCHANGE MARKET', onConfirm=c) await client.ws.publish([0,"oc",[123,None,1548262833910,"tBTCUSD",1548262833379,1548262888016,0,-1,"EXCHANGE LIMIT",None,None,None,0,"EXECUTED @ 15980.0(-0.5): was PARTIALLY FILLED @ 15980.0(-0.5)",None,None,15980,15980,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]]) callback_wait.wait_until_complete() @@ -155,7 +155,7 @@ async def test_confirmed_callback_on_submit_new_order(): client.ws._emit('c1', order) callback_wait = EventWatcher.watch(client.ws, 'c1') # override cid generation - client.ws.orderManager._gen_unqiue_cid = lambda: 123 + client.ws.orderManager._gen_unique_cid = lambda: 123 await client.ws.submit_order('tBTCUSD', 19000, 0.01, 'EXCHANGE MARKET', onConfirm=c) await client.ws.publish([0,"on",[123,None,1548262833910,"tBTCUSD",1548262833379,1548262833410,-1,-1,"EXCHANGE LIMIT",None,None,None,0,"ACTIVE",None,None,15980,0,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]]) callback_wait.wait_until_complete() @@ -171,7 +171,7 @@ async def test_confirmed_callback_on_submit_order_update(): client.ws._emit('c1', order) callback_wait = EventWatcher.watch(client.ws, 'c1') # override cid generation - client.ws.orderManager._gen_unqiue_cid = lambda: 123 + client.ws.orderManager._gen_unique_cid = lambda: 123 await client.ws.update_order(123, price=100, onConfirm=c) await client.ws.publish([0,"ou",[123,None,1548262833910,"tBTCUSD",1548262833379,1548262846964,-0.5,-1,"EXCHANGE LIMIT",None,None,None,0,"PARTIALLY FILLED @ 15980.0(-0.5)",None,None,15980,15980,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]]) callback_wait.wait_until_complete() @@ -187,7 +187,7 @@ async def test_confirmed_callback_on_submit_cancel_order(): client.ws._emit('c1', order) callback_wait = EventWatcher.watch(client.ws, 'c1') # override cid generation - client.ws.orderManager._gen_unqiue_cid = lambda: 123 + client.ws.orderManager._gen_unique_cid = lambda: 123 await client.ws.cancel_order(123, onConfirm=c) await client.ws.publish([0,"oc",[123,None,1548262833910,"tBTCUSD",1548262833379,1548262888016,0,-1,"EXCHANGE LIMIT",None,None,None,0,"EXECUTED @ 15980.0(-0.5): was PARTIALLY FILLED @ 15980.0(-0.5)",None,None,15980,15980,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]]) callback_wait.wait_until_complete() @@ -203,7 +203,7 @@ async def test_confirmed_callback_on_submit_cancel_group_order(): client.ws._emit('c1', order) callback_wait = EventWatcher.watch(client.ws, 'c1') # override cid generation - client.ws.orderManager._gen_unqiue_cid = lambda: 123 + client.ws.orderManager._gen_unique_cid = lambda: 123 await client.ws.cancel_order_group(123, onConfirm=c) await client.ws.publish([0,"oc",[1548262833910,123,1548262833910,"tBTCUSD",1548262833379,1548262888016,0,-1,"EXCHANGE LIMIT",None,None,None,0,"EXECUTED @ 15980.0(-0.5): was PARTIALLY FILLED @ 15980.0(-0.5)",None,None,15980,15980,0,0,None,None,None,0,0,None,None,None,"API>BFX",None,None,None]]) callback_wait.wait_until_complete()