import { createRequire, runMain } from "module";
const require = createRequire(import.meta.url);
const Binance = require("node-binance-api");
const binance = new Binance().options({
APIKEY: "<API-KEY>",
APISECRET: "<API-SECRET>",
useServerTime: true,
});
async function Trade(interval, { symbol, EnterAt, Type }) {
try {
if ((interval == "1h" || interval == "4h") & (Type == "SHORT")) {
let response = await binance.futuresBalance();
let pair = response.filter((el) => el.asset == "USDT");
const leverage = 6;
let quantity = Math.round(
((parseFloat(pair[0].availableBalance) * leverage) / EnterAt).toFixed(2)
);
let takeProfit = EnterAt - (EnterAt * 0.6) / 100;
let stopMarket = EnterAt + (EnterAt * 1.1) / 100;
// Adjust leverage
let adjustLeverage = await binance.futuresLeverage(symbol, leverage);
// Futures Market Sell
let short = await binance.futuresMarketSell(symbol, quantity);
// Set TAKE PROFIT
let TP = await binance.futuresOrder("BUY", symbol, quantity, false, {
type: "TAKE_PROFIT_MARKET",
stopPrice: takeProfit.toFixed(2),
closePosition: true,
});
// Set STOP LOSS
let SL = await binance.futuresOrder("BUY", symbol, quantity, false, {
type: "STOP_MARKET",
workingType: "MARK_PRICE",
stopPrice: stopMarket.toFixed(2),
closePosition: true,
});
console.log("SHORT: ", short);
console.log("TP: ", TP);
console.log("SL: ", SL);
}
} catch (error) {
console.log(error.message);
}
}
function wrapUpTrade(interval, { symbol, EnterAt }) {
binance.useServerTime(() =>
Trade(interval, { symbol: symbol, EnterAt: EnterAt })
);
}
// wrapUpTrade("1h", { symbol: "XRPUSDT", shortAt: 0.774 });
export { wrapUpTrade };
This code is only for futures Market Sell (sell short). Why do I get an error of Insufficient Margin on placing an order of coin having a price of $4.5 and I have $2.9 in my wallet? On leveraging(6x) I will be having (6 * $2.9 = $17.4) that is greater than $4.5 still I get the error of Insufficient error. Function Trade takes some arguments:
Interval: "4h" or "1h"
symbol: "PAIR/(USDT)" // BTCUSDT, ETHUSDT
EnterAt: It is used to calculate Stoploss and TakeProfit. The type is int.
Type: "SHORT", means futuresSell