examples: seperate ws and rest examples

This commit is contained in:
Jacob Plaster
2018-12-03 14:40:44 +00:00
parent 405c1a7b7b
commit d683faf459
9 changed files with 207 additions and 207 deletions

View File

@@ -0,0 +1,38 @@
import os
import sys
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='INFO'
)
@bfx.ws.on('order_closed')
def order_cancelled(order, trade):
print ("Order cancelled.")
print (order)
print (trade)
@bfx.ws.on('order_confirmed')
async def trade_completed(order, trade):
print ("Order confirmed.")
print (order)
print (trade)
await bfx.ws.cancel_order(order.id)
@bfx.ws.on('error')
def log_error(msg):
print ("Error: {}".format(msg))
@bfx.ws.once('authenticated')
async def submit_order(auth_message):
# create an inital order a really low price so it stays open
await bfx.ws.submit_order('tBTCUSD', 10, 1, 'EXCHANGE LIMIT')
bfx.ws.run()

View File

@@ -0,0 +1,19 @@
import os
import sys
sys.path.append('../')
from bfxapi import Client
bfx = Client(
logLevel='DEBUG'
)
@bfx.ws.on('error')
def log_error(msg):
print ("Error: {}".format(msg))
@bfx.ws.on('all')
async def log_output(output):
print ("WS: {}".format(output))
bfx.ws.run()

View File

@@ -0,0 +1,16 @@
import os
import sys
import asyncio
sys.path.append('../')
from bfxapi import Client
bfx = Client(
logLevel='INFO'
)
async def get_seeds():
candles = await bfx.rest.get_seed_candles('tBTCUSD')
print (candles)
asyncio.get_event_loop().run_until_complete(get_seeds())

View File

@@ -0,0 +1,37 @@
import os
import sys
sys.path.append('../')
from bfxapi import Client
bfx = Client(
logLevel='INFO'
)
@bfx.ws.on('error')
def log_error(err):
print ("Error: {}".format(err))
@bfx.ws.on('unsubscribed')
async def on_unsubscribe(subscription):
print ("Unsubscribed from {}".format(subscription.symbol))
# await subscription.subscribe()
@bfx.ws.on('subscribed')
async def on_subscribe(subscription):
print ("Subscribed to {}".format(subscription.symbol))
# await subscription.unsubscribe()
# or
# await bfx.ws.unsubscribe(subscription.chanId)
@bfx.ws.once('subscribed')
async def on_once_subscribe(subscription):
print ("Performig resubscribe")
await bfx.ws.resubscribe(subscription.chanId)
async def start():
await bfx.ws.subscribe('book', 'tBTCUSD')
bfx.ws.on('connected', start)
bfx.ws.run()

View File

@@ -0,0 +1,48 @@
import os
import sys
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'
)
@bfx.ws.on('order_snapshot')
async def close_all(data):
await bfx.ws.close_all_orders()
@bfx.ws.on('order_confirmed')
async def trade_completed(order, trade):
print ("Order confirmed.")
print (order)
print (trade)
## close the order
# await order.close()
# or
# await bfx.ws.close_order(order.id)
# or
# await bfx.ws.close_all_orders()
@bfx.ws.on('error')
def log_error(msg):
print ("Error: {}".format(msg))
@bfx.ws.on('authenticated')
async def submit_order(auth_message):
await bfx.ws.submit_order('tBTCUSD', 19000, 0.01, 'EXCHANGE LIMIT')
# If you dont want to use a decorator
# ws.on('authenticated', submit_order)
# ws.on('error', log_error)
# You can also provide a callback
# await ws.submit_order('tBTCUSD', 0, 0.01,
# 'EXCHANGE MARKET', onComplete=trade_complete)
bfx.ws.run()

View File

@@ -0,0 +1,31 @@
import os
import sys
sys.path.append('../')
from bfxapi import Client
bfx = Client(
logLevel='DEBUG',
# Verifies that the local orderbook is up to date
# with the bitfinex servers
manageOrderBooks=True
)
@bfx.ws.on('error')
def log_error(err):
print ("Error: {}".format(err))
@bfx.ws.on('order_book_update')
def log_update(data):
print ("Book update: {}".format(data))
@bfx.ws.on('order_book_snapshot')
def log_snapshot(data):
print ("Initial book: {}".format(data))
async def start():
await bfx.ws.subscribe('book', 'tBTCUSD')
# bfx.ws.subscribe('book', 'tETHUSD')
bfx.ws.on('connected', start)
bfx.ws.run()

View File

@@ -0,0 +1,28 @@
import os
import sys
sys.path.append('../')
from bfxapi import Client
bfx = Client(
logLevel='DEBUG'
)
@bfx.ws.on('error')
def log_error(err):
print ("Error: {}".format(err))
@bfx.ws.on('new_candle')
def log_candle(candle):
print ("New candle: {}".format(candle))
@bfx.ws.on('new_trade')
def log_trade(trade):
print ("New trade: {}".format(trade))
async def start():
await bfx.ws.subscribe('candles', 'tBTCUSD', timeframe='1m')
await bfx.ws.subscribe('trades', 'tBTCUSD')
bfx.ws.on('connected', start)
bfx.ws.run()

View File

@@ -0,0 +1,43 @@
import os
import sys
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'
)
@bfx.ws.on('order_update')
def order_updated(order, trade):
print ("Order updated.")
print (order)
print (trade)
@bfx.ws.once('order_update')
async def order_once_updated(order, trade):
# update a second time using the object function
await order.update(price=80, amount=0.02, flags="2nd update")
@bfx.ws.once('order_confirmed')
async def trade_completed(order, trade):
print ("Order confirmed.")
print (order)
print (trade)
await bfx.ws.update_order(order.id, price=100, amount=0.01)
@bfx.ws.on('error')
def log_error(msg):
print ("Error: {}".format(msg))
@bfx.ws.once('authenticated')
async def submit_order(auth_message):
# create an inital order a really low price so it stays open
await bfx.ws.submit_order('tBTCUSD', 10, 1, 'EXCHANGE LIMIT')
bfx.ws.run()

View File

@@ -0,0 +1,31 @@
import os
import sys
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='INFO'
)
@bfx.ws.on('wallet_snapshot')
def log_snapshot(wallets):
for wallet in wallets:
print (wallet)
# or bfx.ws.wallets.get_wallets()
@bfx.ws.on('wallet_update')
def log_update(wallet):
print ("Balance updates: {}".format(wallet))
@bfx.ws.on('error')
def log_error(msg):
print ("Error: {}".format(msg))
bfx.ws.run()