Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
close positions
'use strict'
/**
* Test steps
*
* Create a position with dust amount:
* 1. Put $10 in margin wallet
* (in Order form, switch to margin tab)
* 2. Place market order for BTC, amount 0.001. Click buy button
* 3. You will see the position
* 4. Place a market order for btc, amount 0.00099999. Click sell button
* 5. Remaining position size is 0.00000001
*
* Create a position close order with bitfinex-api-node:
* 1. put this script in `bitfinex-api-node/examples/ws2/close_orders.js`
* (this scrpit is modified from examples/ws2/orders.js)
* 2. switch to examples folder. Add .env with API_KEY and API_SECRET
* 3. run this script with node close_order.js
*
*/
process.env.DEBUG = 'bfx:examples:*'
const debug = require('debug')('bfx:examples:ws2_orders')
const { Order } = require('bfx-api-node-models')
const bfx = require('../bfx')
const ws = bfx.ws(2)
ws.on('error', (err) => {
console.log(err)
})
ws.on('open', () => {
debug('open')
ws.auth()
})
ws.once('auth', () => {
debug('authenticated')
// Build new order
// const o = new Order({
// cid: Date.now(),
// symbol: 'tBTCUSD',
// price: 589.10,
// amount: -0.02,
// type: Order.type.EXCHANGE_LIMIT
// }, ws)
const o = new Order({
// cid: Date.now(),
symbol: 'tBTCUSD',
amount: -1e-8, // counter amount
type: Order.type.MARKET,
flags: 512,
}, ws)
let closed = false
// Enable automatic updates
o.registerListeners()
o.on('update', () => {
debug('order updated: %j', o.serialize())
})
o.on('close', () => {
debug('order closed: %s', o.status)
closed = true
})
debug('submitting order %d', o.cid)
o.submit().then(() => {
debug('got submit confirmation for order %d [%d]', o.cid, o.id)
// wait a bit...
setTimeout(() => {
if (closed) return
debug('canceling...')
// o.cancel().then(() => {
// debug('got cancel confirmation for order %d', o.cid)
// ws.close()
// }).catch((err) => {
// debug('error cancelling order: %j', err)
// ws.close()
// })
ws.close()
}, 2000)
}).catch((err) => {
console.log(err)
ws.close()
})
})
ws.open()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment