Skip to content

Instantly share code, notes, and snippets.

@tegila
Created June 15, 2021 15:11
Show Gist options
  • Save tegila/fbe7c0576a01c0cbd0cb65f41ab257ae to your computer and use it in GitHub Desktop.
Save tegila/fbe7c0576a01c0cbd0cb65f41ab257ae to your computer and use it in GitHub Desktop.
const fetch = require("node-fetch"); // "Fetch" HTTP req library
const finex = require("./finex");
let params = {
sku: 'start',
symbol: "tBTCUSD",
type: "LIMIT",
// target: 25500,
limit: 100,
// amount: 0.002, //0.0052, //200
algo: "TRACKER",
};
// node index.js '{"target":36600, "amount": 0.022, "limit":250}'
console.log(JSON.stringify(params));
const myArgs = JSON.parse(process.argv.slice(2));
console.log("myArgs: ", myArgs);
if(!myArgs.target || !myArgs.amount) return;
params = Object.assign({}, params, myArgs);
console.log(JSON.stringify(params));
finex.__set_api(
"",
""
);
finex.__send("v2/auth/r/positions", {}, (err_positions, positions) => {
if (err_positions) return console.log(err_positions);
//console.log(positions);
const old_position = positions.find((e) => e[0] === params.symbol);
const [,, start_amount] = old_position || [,, 0];
console.log(start_amount);
const diff = (Number(params.amount)/1000) - (start_amount || 0);
console.log(diff);
const amount = diff.toFixed(8);
console.log(amount);
if(!amount) return;
//process.exit();
finex.__send("v2/auth/r/orders", {}, (err_orders, orders) => {
const robot_order = orders.find((e) => e[31].sku === params.sku);
// NEW ORDER
if (!robot_order) {
finex.__send(
"v2/auth/w/order/submit",
{
type: params.type,
symbol: params.symbol,
price: `${params.target}`,
amount,
meta: {
sku: params.sku,
target: params.target,
limit: params.limit,
algo: params.algo
}, // optional param to pass an affiliate code
},
(err_new_order, new_order) => {
if (err_new_order) console.log(err_new_order);
console.log(JSON.stringify(new_order));
}
);
// UPDATE ORDER
} else {
finex.__send(
"v2/auth/w/order/update",
{
id: robot_order[0],
price: `${params.target}`,
amount,
},
(err_update, updated_order) => {
if (err_update) console.log(err_update);
console.log(JSON.stringify(updated_order));
}
);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment