Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict'
process.env.DEBUG = 'bfx:examples:*'
const debug = require('debug')('bfx:examples:submit_order')
const bfx = require('../bfx')
const { Order } = require('bfx-api-node-models')
const rest = bfx.rest(2)
debug('Submitting new order...')
// Build new order
const o = new Order({
cid: Date.now(),
symbol: 'tBTCUSD',
price: 18000,
amount: -0.02,
type: Order.type.LIMIT,
lev: 10
}, rest)
o.submit().then(() => {
debug(
'got submit confirmation for order %d [%d] [tif: %d]',
o.cid, o.id, o.mtsTIF
)
})
.catch((err) => console.log(err))
ws.onOrderBook({ symbol: SYMBOL }, (ob) => {
const topBidL = ob.topBidLevel()
if (topBidL === null || orderSent) {
return
}
debug('taking out price level: %j', topBidL)
const o = new Order({
symbol: SYMBOL,
type: Order.type.EXCHANGE_LIMIT,
price: topBidL[0],
amount: topBidL[2] * -1.1 // sell through top bid
}, ws)
o.registerListeners()
o.submit().then(() => debug('order submitted'))
o.once('update', (o) => {
debug('got order update: %s', o.status)
if (o.isPartiallyFilled()) {
debug('order is partially filled, amount %f', o.amount)
debug('increasing amount w/ delta %f', o.amount * 2)
o.update({ delta: `${o.amount * 2}` }).then(() => {
submitOrder (order) {
if (!this._isAuthenticated) {
return Promise.reject(new Error('not authenticated'))
}
const packet = Array.isArray(order)
? order
: order instanceof Order
? order.toNewOrderPacket()
: new Order(order).toNewOrderPacket()
if (this._affCode) {
if (!packet.meta) {
packet.meta = {}
}
packet.meta.aff_code = packet.meta.aff_code || this._affCode // eslint-disable-line
}
this._sendOrderPacket([0, 'on', null, packet])
return this._getEventPromise(`order-new-${packet.cid}`)
}
wsSingle.once('auth', () => {
const o = new Order({
id: Date.now(),
type: 'EXCHANGE LIMIT',
price: 100,
amount: 1,
symbol: 'tBTCUSD'
}, wsSingle)
wsSingle._ws.send = (msgJSON) => {
const msg = JSON.parse(msgJSON)
assert.strictEqual(msg[0], 0)
assert.strictEqual(msg[1], 'ou')
assert(msg[3])
assert.strictEqual(msg[3].id, o.id)
assert.strictEqual(+msg[3].delta, 1)
assert.strictEqual(+msg[3].price, 200)
wsMulti.once('auth', () => {
const oA = new Order({
gid: null,
cid: Date.now(),
type: 'EXCHANGE LIMIT',
price: 100,
amount: 1,
symbol: 'tBTCUSD'
})
const oB = new Order({
gid: null,
cid: Date.now(),
type: 'EXCHANGE LIMIT',
price: 10,
amount: 1,
symbol: 'tETHUSD'
})
ws.once('auth', () => {
debug('authenticated')
const oA = new Order({
symbol: 'tBTCUSD',
price: 200,
amount: 1,
type: 'EXCHANGE LIMIT'
}, ws)
const oB = new Order({
symbol: 'tETHUSD',
price: 50,
amount: 1,
type: 'EXCHANGE LIMIT'
}, ws)
oA.submit().then(() => {
debug('created order A')
return oB.submit()
const oA = new Order({
symbol: 'tBTCUSD',
price: 200,
amount: 1,
type: 'EXCHANGE LIMIT'
}, ws)
const oB = new Order({
symbol: 'tETHUSD',
price: 50,
amount: 1,
type: 'EXCHANGE LIMIT'
}, ws)
const oC = new Order({
symbol: 'tETHBTC',
price: 1,
amount: 1,
type: 'EXCHANGE LIMIT'
}, ws)
oA.submit().then(() => {
debug('created order A')
return oB.submit()
}).then(() => {
debug('created order B')
return oC.submit()
}).then(() => {
debug('created order C')
ws.submitOrderMultiOp([
ws.once('auth', () => {
debug('authenticated')
const oA = new Order({
symbol: 'tBTCUSD',
price: 200,
amount: 1,
type: 'EXCHANGE LIMIT'
}, ws)
const oB = new Order({
symbol: 'tETHUSD',
price: 50,
amount: 1,
type: 'EXCHANGE LIMIT'
}, ws)
const oC = new Order({
symbol: 'tETHBTC',
price: 1,
amount: 1,
type: 'EXCHANGE LIMIT'
}, ws)
oA.submit().then(() => {
debug('created order A')
return oB.submit()