Fetch All Contract Positions on Binance At Once and Format Them Into an Array of Inventor Position Structures
JavaScript strategy by 夏天不打你.
JavaScriptOther
The original spec has no written description. Read the source below.
Source JavaScript
// Get the positions of a specific currency from the position list
function getPositionBySymbol(positions, symbol) {
var index = -1;
if (positions && positions.length > 0) {
for (var i = 0; i < positions.length; i++) {
if (positions[i][0].Symbol == symbol) {
index = i;
break;
}
}
}
return index == -1 ? null : positions[index];
}
// Get all position lists
function getAllPositionInBinance() {
var ret = exchange.IO("api", "GET", "/fapi/v2/account");
var positions = [];
var i = 0;
if (!ret || !ret.positions) {
return null;
}
// Get all positions
for (i = 0; i < ret.positions.length; i++) {
if (ret.positions[i].positionAmt != 0 && ret.positions[i].symbol.endsWith("USDT")) {
positions.push([{
Symbol: ret.positions[i].symbol.substring(0, ret.positions[i].symbol.lastIndexOf("USDT")) + "_USDT",
Amount: Number(Math.abs(ret.positions[i].positionAmt)),
FrozenAmount: 0,
Price: Number(ret.positions[i].entryPrice),
Profit: Number(ret.positions[i].unrealizedProfit),
Type: ret.positions[i].positionAmt < 0 ? PD_SHORT : PD_LONG,
ContractType: "swap",
Margin: Number(ret.positions[i].positionInitialMargin),
LevelRate: Number(ret.positions[i].leverage)
}]);
}
}
// Merge positions of the same currency(Same currency, long and short two-way positions)
if (positions.length >= 2) {
for (i = 0; i < positions.length; i++) {
for (var j = i + 1; j < positions.length; j++) {
if (positions[i][0].Symbol == positions[j][0].Symbol) {
positions[i].push(JSON.parse(JSON.stringify(positions[j][0])));
positions.splice(j, 1); // Delete the same currency
break;
}
}
}
}
return positions;
}Published by 夏天不打你 on the FMZ strategy square · last modified 2021-09-26. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Fetch-All-Contract-Positions-on-Binance-At-Once-and-Format-Them-Into-an-Array-of-Inventor-Position-Structures.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.