Binance.com Futures Coin Margined Hedge Bot written in PHP. Creates Limit orders consistently on the BTCUSD_PERP contract to hedge the BTC in your wallet.
_ _ _ _ _ _ _ | |__ (_)_ __ __ _ _ __ ___ ___ ___ ___ (_)_ __ /\/\ | |__ ___ __| | __ _ ___ | |__ ___ | |_ | '_ \| | '_ \ / _` | '_ \ / __/ _ \ / __/ _ \| | '_ \ / \ | '_ \ / _ \/ _` |/ _` |/ _ \ | '_ \ / _ \| __| | |_) | | | | | (_| | | | | (_| __/ | (_| (_) | | | | / /\/\ \ | | | | __/ (_| | (_| | __/ | |_) | (_) | |_ |_.__/|_|_| |_|\__,_|_| |_|\___\___| \___\___/|_|_| |_\/ \/ |_| |_|\___|\__,_|\__, |\___| |_.__/ \___/ \__| |___/
Developed by - https://twitter.com/Convexical
Binance Futures Coin-M Hedger
This code has been currently ported to 1 different language -
https://github.com/Xpipe/Binance_Coin-M_Futures_Hedger - python
If you port this code to another language (NodeJS/Javascript anyone?), please contact me on Twitter.
This project contains code that automates / places various trades (long [buy] & short [sell]) to create a hedge (synthetic USD) of your COIN-M Futures Wallet on Binance. For example, if you deposit 1 BTC on Binance under your spot wallet and transfer it into your Binance COIN-M futures account, it will automatically place trades until you are hedged (aka it will create short limit orders). You can also set a min/max amount per order that the bot will randomize with to get your current open position to be hedged to your Coin-M Futures wallet balance. If you choose to run the code with a cronjob or scheduler, it will continuously monitor your wallet balance and appropriately buy or sell to get the current open position to match your wallet balance.
Creating a hedge for your BTC can be important in certain situations. If you want more info on why you'd do this, please look at the resources section below. This code allows you to hedge off your BTC using (1) limit orders which save you money [because you're not submiting market orders which have higher fees than limit orders], (2) manage your hedge by consistently checking your Coin-M wallet balance and buying or selling more of the derivative to match your futures wallet. The reason I created this script is that in certain situations, you want to hedge your position and protect the FIAT value. If your wallet balance is a lower amount, it's likely easy to create a hedged position, but when you have larger BTC amounts the ability to hedge your position can be difficult because creating large market orders to hedge your wallet balance would (1) result in higher fees (which is money you're just giving to the exchange willingly) and (2) bad pricing of the hedge since it's possible you'd affect the market.
This code is written in PHP. I am a versatile developer / engineer but I still love PHP. While I could code it in Python, C, C++, etc. - PHP is something that I can move faster in and for me it just works. If you're a coder and want to port it, I'd love to work with you.
In order to get this code to work correctly, you have to do the following:
apt install php
apt install php-curl
php main.php
to test and review the outputThis executes every minute. Please change to your desired time interval.
* * * * * /usr/bin/php /<DIRECTORY_WHERE_SCRIPT_IS_LOCATED>/main.php > /dev/null 2>&1
At a high level - this code creates synthetic USD or hedges your BTC Coin-M futures wallet. When the code is executed (main.php) it connects to your Binance wallet and gets the Coin-M wallet balance and open positions. Once it has that information, it checks what the delta / difference is between the two. The script then does the following:
It's important to note that in order to create a hedge - you are creating a position that is opposite your wallet balance - so if you have 1 BTC in your Binance.com Futures Coin-M wallet, this code will create / place trades until the open position is close to -1 BTC.
As of the release of this code, I have not tested it fully on other coins / tokens. However, it wouldn't be that difficult to modify this code for other coins / tokens. If you're interested in getting this code to work with other coins / tokens, please reach out to me.
If you're looking for support on how to set this up or you just want to reach out, please contact me on Twitter - https://twitter.com/Convexical.
This code utilizes the JaggedSoft PHP Binance API library (https://github.com/jaggedsoft/php-binance-api). I've added various pieces of code to bring that library up to date to interact with the Binance Coin-M futures.
Please use this code at your own risk. If you modify certain variables to be beyond certain ranges, you may end up with bad results. Always test first and optimize after.
.__ .__ ____ ____ ____ ___ __ ____ ___ ___|__| ____ _____ | | _/ ___\ / _ \ / \\ \/ /_/ __ \ \ \/ /| |_/ ___\ \__ \ | | \ \___( <_> )| | \\ / \ ___/ > \____/ |___| / \_/ \___ >/__/\_ \|__| \___ >(____ /|____/ \/ \/ \/ \/ \/ \/