Created
June 6, 2019 04:00
-
-
Save gasolin/8233c0c6b818be9b9c6b4d779224cbd1 to your computer and use it in GitHub Desktop.
close positions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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