okcoin-fix
Version:
OKCoin FIX API wrapper
219 lines (180 loc) • 6.18 kB
JavaScript
/**
* Created by robi on 17/2/18.
*/
var OKCoin = require("../index.js");
var math = require('mathjs');
var key = process.argv[2] || 'your-api-key';
var secret = process.argv[3] || 'your-api-secret';
var subscribeUserBase =false;//true;
var subscribeUserPosition= false;//true;
var subscribeUserOrders= false;
var subscribeMarket= false;//true;
var subscribeTrades= false;
var subscribeOrderbook= false;
var longPos = {};
var shortPos = {};
var okclient = new OKCoin({
key: key,
secret: secret,
host: 'fix.okex.com',
port: 9880,
//symbol: 'BTC/USD',
symbol: 'btc_usd',
contract: 'this_week',
subscribeUserBase: subscribeUserBase,
subscribeUserPosition: subscribeUserPosition,
subscribeUserOrders: subscribeUserOrders,
subscribeMarket: subscribeMarket,
subscribeTrades: subscribeTrades,
subscribeOrderbook: subscribeOrderbook,
log: false,
debug: false,
onLogon : function (status) {
console.log(`OKCoin FIX API logon status: ${status} \nSubscriptions[Base|Pos|MyOrders|Market|Trades|OrderBook] = [${subscribeUserBase ? 'Y':'N'}|${subscribeUserPosition ? 'Y':'N'}|${subscribeUserOrders ? 'Y':'N'}|${subscribeMarket ? 'Y':'N'}|${subscribeTrades ? 'Y':'N'}|${subscribeOrderbook ? 'Y':'N'}]`)
//okclient.executeSpotsOrder('123', 2000, 0.05, true);
//okclient.executeSpotsOrder({
// oid: '124',
// price: 5000,
// amount: 0.01,
// side: 2
//}, function (data) {
// var d = data;
//});
//okclient.listSpotsOpenOrders(function (data) {
// console.log(`Found open order : ${data.exoid} and cancel it now!`);
//})
okclient.listFutureOpenOrders();
},
onOrderUpdated: function (status, order) {
//okclient.executeSpotsCancelOrder(order, function(data) {
// console.log(`Order ${data.exoid} cancelled`);
//});
console.log(`Order ${status}:`);
console.log(JSON.stringify(order));
//testOpenPositionOrderMakeAndCancel(status, order);
//testClosePositionOrderMakeAndCancel(status, order);
},
onUserInfo: function (data) {
console.log(JSON.stringify(data));
//okclient.listFutureOpenOrders();
},
onPositionInfo: function (data) {
console.log(JSON.stringify(data));
var pos = data.position;
for (var i = 0 ; i < pos.length; ++i) {
if (pos[i].direction < 0) {
shortPos = pos[i];
} else {
longPos = pos[i];
}
}
// to trigger testClosePositionOrderMakeAndCancel()
//okclient.listFutureOpenOrders();
},
processTrades: function (data) {
console.log(JSON.stringify(data));
},
processOrderbook: function (data) {
console.log(JSON.stringify(data));
}
});
okclient.start(function() {
console.log("OKCoin FIX started!");
});
var step = 0;
var current_price = 1600;
var openbtc = 0.005;
function testOpenPositionOrderMakeAndCancel(status, order) {
// test long position
if (status === 'empty' && step == 0) {
var direction = 1;
var openPrice = current_price - 100;
var order = {
oid: '_O' + new Date().getTime(),
amount: calcAmount(openbtc, openPrice),
price: openPrice,
position: 'O',
side: direction < 0 ? 2 : 1,
lever: 20
};
okclient.executeFutureOrder(order);
}
if (status === 'new' && order.ordertype === 'open_long') {
okclient.executeFutureCancelOrder(order);
step = 1;
}
// test short position
if (status === 'empty' && step == 1) {
var direction = -1;
var order = {
oid: new Date().getTime(),
amount: 1,
price: current_price + 500,
position: 'O',
side: direction < 0 ? 2 : 1,
lever: 20
};
okclient.executeFutureOrder(order);
}
if (status === 'new' && order.ordertype === 'open_short') {
okclient.executeFutureCancelOrder(order);
step = 2;
}
}
var newlong = false;
var newshort = false;
function testClosePositionOrderMakeAndCancel(status, order) {
// test long position
if (longPos.amount > 0 && !newlong) {
var direction = longPos.direction;
var order = {
oid: new Date().getTime(),
amount: longPos.amount,
price: longPos.avgOpenPrice + 50,
position: 'C',
side: direction < 0 ? 2 : 1,
lever: 20
};
okclient.executeFutureOrder(order);
newlong = true;
}
if (status === 'new' && order.ordertype === 'close_long') {
okclient.executeFutureCancelOrder(order);
}
// test short position
if (shortPos.amount > 0 && !newshort) {
var direction = shortPos.direction;
var order = {
oid: new Date().getTime(),
amount: shortPos.amount,
price: shortPos.avgOpenPrice - 50,
position: 'C',
side: direction < 0 ? 2 : 1,
lever: 20
};
okclient.executeFutureOrder(order);
newshort = true;
}
if (status === 'new' && order.ordertype === 'close_short') {
okclient.executeFutureCancelOrder(order);
}
}
function calcAmount(asset, price) {
// Open 1BTC BOM
var bom = calcMargin(1, price);
// Open 1FUT BOM
var fut = bom * (100/price);
// How many fut can open
return asset / fut;
}
function calcFee(openAmount, openPrice, currentPrice) {
openAmount = math.floor(openAmount / (100 / openPrice));
var f = math.divide(math.multiply(math.multiply(100,openAmount),0.0003),currentPrice);
return math.round(f,5);
}
function calcMargin(openAmount, openPrice) {
openAmount = math.floor(openAmount / (100 / openPrice));
var fm = math.multiply(math.multiply(math.divide(100, openPrice),openAmount), math.divide(1,20));
return math.round(fm,4);
}
process.stdin.resume();