question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Buy at market, then sell or close a specific futures order.

See original GitHub issue

I am trying to make an extremely simple bot that just buys with a % of my money in futures, then sells the full contract based on some signals from a webhook.

My current buy states is something like:

    let maxBuyIn = await futuresBusdBalance();
    maxBuyIn = maxBuyIn * 0.85;
    console.info( await binance.futuresMarketBuy( 'ADABUSD', maxBuyIn ) );

I think this should work fine, although I’d be happy to hear suggestions if there is a better way, or maybe a function for a max buy in already.

For my sell order on the other hand, I am getting a bit stuck. I can’t find anywhere in the API where I can simply close the full order at market. I will admit I am not amazing javascript. So far my only thoughts might be to use await binance.futuresBalance() and somehow find the object containing my order, then selling the quantity from it? Am I on the right track? Am I completely off? Maybe people have suggestions.

A simple full market buy and full market sell in futures would suffice for me.

Thanks for any help, and I apologize if this isn’t the place to ask this.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
sleepyhollowcommented, Oct 16, 2021

Thanks!! Can I ask what does reduceOnly exactly do?

It just means that the order will only be reduced, it will not add anything to your order or it will fail. Just leave it in unless you have a very specific case where this isn’t what you want.

1reaction
sleepyhollowcommented, Oct 16, 2021

Closing the question figured it out.

Hey, I am in the same boat - I can’t find how I can close the future position. If you solved that could you share it please?

Sure!

// side should be long/short async function sellPosition(side) { // coinPair example: ‘ADAUSDT’ let position = await binance.futuresPositionRisk({ symbol: coinPair });

// position 0 is my first trade (I only do trades on one pair, you can change this accordingly)
// you could also do something like positionAmt*0.8 if you wanted to sell a partial 80% of your trade if you wanted.
if (side == 'LONG') {
    console.info(await binance.futuresMarketSell(coinPair, position[0]['positionAmt'], { reduceOnly: true }));
    return;
}

if (side == 'SHORT') {
    console.info(await binance.futuresMarketBuy(coinPair, Math.abs(position[0]['positionAmt']), { reduceOnly: true }));
    return;
}

}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Market Order: Definition, Example, Vs. Limit Order
A market order is an instruction to a broker to buy or sell a stock or other asset immediately at the best available...
Read more >
3 Order Types: Market, Limit and Stop Orders | Charles Schwab
Market orders, limit orders, and stop orders are common order types used to buy or sell stocks and ETFs. Learn how and when...
Read more >
Buying and Selling Futures Contracts on an Exchange
Types of exchange orders for buying and selling futures, with examples, including: market-on-close orders; market-on-open orders (MOO); good-till-canceled ...
Read more >
Types of Orders
A market order generally will execute at or near the current bid (for a sell order) or ask (for a buy order) price....
Read more >
Market Order vs. Limit Order: When to Use Which
The main difference between a market order and a limit order is that market orders trigger the immediate purchase or sale of a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found