mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-20 23:34:21 +01:00
examples: add rest create order example
This commit is contained in:
committed by
Jacob Plaster
parent
f6837452fb
commit
9a84dd7114
43
bfxapi/examples/rest/create_order.py
Normal file
43
bfxapi/examples/rest/create_order.py
Normal file
@@ -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)
|
||||||
@@ -10,20 +10,18 @@ API_SECRET=os.getenv("BFX_SECRET")
|
|||||||
bfx = Client(
|
bfx = Client(
|
||||||
API_KEY=API_KEY,
|
API_KEY=API_KEY,
|
||||||
API_SECRET=API_SECRET,
|
API_SECRET=API_SECRET,
|
||||||
logLevel='INFO'
|
logLevel='DEBUG'
|
||||||
)
|
)
|
||||||
|
|
||||||
@bfx.ws.on('order_closed')
|
@bfx.ws.on('order_closed')
|
||||||
def order_cancelled(order, trade):
|
def order_cancelled(order):
|
||||||
print ("Order cancelled.")
|
print ("Order cancelled.")
|
||||||
print (order)
|
print (order)
|
||||||
print (trade)
|
|
||||||
|
|
||||||
@bfx.ws.on('order_confirmed')
|
@bfx.ws.on('order_confirmed')
|
||||||
async def trade_completed(order, trade):
|
async def trade_completed(order):
|
||||||
print ("Order confirmed.")
|
print ("Order confirmed.")
|
||||||
print (order)
|
print (order)
|
||||||
print (trade)
|
|
||||||
await bfx.ws.cancel_order(order.id)
|
await bfx.ws.cancel_order(order.id)
|
||||||
|
|
||||||
@bfx.ws.on('error')
|
@bfx.ws.on('error')
|
||||||
@@ -32,7 +30,7 @@ def log_error(msg):
|
|||||||
|
|
||||||
@bfx.ws.once('authenticated')
|
@bfx.ws.once('authenticated')
|
||||||
async def submit_order(auth_message):
|
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)
|
await bfx.ws.submit_order('tBTCUSD', 10, 1, Order.Type.EXCHANGE_LIMIT)
|
||||||
|
|
||||||
bfx.ws.run()
|
bfx.ws.run()
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ async def test_closed_callback_on_submit_order_closed():
|
|||||||
client.ws._emit('c1', order)
|
client.ws._emit('c1', order)
|
||||||
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
||||||
# override cid generation
|
# 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.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]])
|
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()
|
callback_wait.wait_until_complete()
|
||||||
@@ -139,7 +139,7 @@ async def test_confirmed_callback_on_submit_order_closed():
|
|||||||
client.ws._emit('c1', order)
|
client.ws._emit('c1', order)
|
||||||
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
||||||
# override cid generation
|
# 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.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]])
|
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()
|
callback_wait.wait_until_complete()
|
||||||
@@ -155,7 +155,7 @@ async def test_confirmed_callback_on_submit_new_order():
|
|||||||
client.ws._emit('c1', order)
|
client.ws._emit('c1', order)
|
||||||
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
||||||
# override cid generation
|
# 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.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]])
|
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()
|
callback_wait.wait_until_complete()
|
||||||
@@ -171,7 +171,7 @@ async def test_confirmed_callback_on_submit_order_update():
|
|||||||
client.ws._emit('c1', order)
|
client.ws._emit('c1', order)
|
||||||
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
||||||
# override cid generation
|
# 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.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]])
|
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()
|
callback_wait.wait_until_complete()
|
||||||
@@ -187,7 +187,7 @@ async def test_confirmed_callback_on_submit_cancel_order():
|
|||||||
client.ws._emit('c1', order)
|
client.ws._emit('c1', order)
|
||||||
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
||||||
# override cid generation
|
# 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.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]])
|
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()
|
callback_wait.wait_until_complete()
|
||||||
@@ -203,7 +203,7 @@ async def test_confirmed_callback_on_submit_cancel_group_order():
|
|||||||
client.ws._emit('c1', order)
|
client.ws._emit('c1', order)
|
||||||
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
callback_wait = EventWatcher.watch(client.ws, 'c1')
|
||||||
# override cid generation
|
# 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.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]])
|
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()
|
callback_wait.wait_until_complete()
|
||||||
|
|||||||
Reference in New Issue
Block a user