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.

Overview
 _     _                                        _                _              _              _           _   
| |__ (_)_ __   __ _ _ __   ___ ___    ___ ___ (_)_ __   /\/\   | |__   ___  __| | __ _  ___  | |__   ___ | |_ 
| '_ \| | '_ \ / _` | '_ \ / __/ _ \  / __/ _ \| | '_ \ /    \  | '_ \ / _ \/ _` |/ _` |/ _ \ | '_ \ / _ \| __|
| |_) | | | | | (_| | | | | (_|  __/ | (_| (_) | | | | / /\/\ \ | | | |  __/ (_| | (_| |  __/ | |_) | (_) | |_ 
|_.__/|_|_| |_|\__,_|_| |_|\___\___|  \___\___/|_|_| |_\/    \/ |_| |_|\___|\__,_|\__, |\___| |_.__/ \___/ \__|
                                                                                  |___/                        

Developed by Twitter URL - https://twitter.com/Convexical

Binance Futures Coin-M Hedger

Other Languages / Ported code

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.

What is this project?

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.

Why would I use this?

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.

How do I use this and run it?

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:

  • Setup a Linux VPS or have a Linux server available - the below assumes a new version of Linux
  • Install PHP 7 - in the terminal: apt install php
  • Install PHP Curl - in the terminal: apt install php-curl
  1. have an understanding of how to use a linux server and have PHP 7.0+ installed with the required modules of curl.
  2. create an API key on Binance with trade permissions for Futures
  3. update the config.php file with your API keys and set the various variables to your own preferences - order sizes for buy / sell side orders and the BTC delta amount distance when you want the bot to stop executing
  4. setup a cronjob that executes the main.php at your specified interval or run php main.php to test and review the output
  5. enjoy the magic / simplicitiy of the program running to consistently keep your hedge optimized correctly

Example Cronjob

This executes every minute. Please change to your desired time interval.

* * * * * /usr/bin/php /<DIRECTORY_WHERE_SCRIPT_IS_LOCATED>/main.php > /dev/null 2>&1

Can you explain in detail what this code does

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:

  1. The script then checks if there are existing orders open - if there's an order open that is at the top of the book (either the bid or ask side depending on the delta of the current position against the wallet size), then it will stop / exit. If the current open limit order isn't at the top of the order book, it will cancel it and place a new limit order to hopefully get filled.
  2. If the current open position is less than the wallet balance, it will create orders in the order book (short / sell) until the position is hedged / matches closely with the wallet balance
  3. If the current open position is greater than the wallet balance, (because you moved coins out of the Coin-M futures wallet), then the bot will create orders (buy / long) in the order book to get close to the wallet balance.

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.

Are there any online resources where I can learn more about Synthetic USD / hedging / delta neutral trading?

Does this work for other coins / tokens under Binance?

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.

How do I get support for this program?

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.

Important notes

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.

Disclaimer

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.

                                             .__                 .__   
  ____   ____    ____ ___  __  ____  ___  ___|__|  ____  _____   |  |  
_/ ___\ /  _ \  /    \\  \/ /_/ __ \ \  \/  /|  |_/ ___\ \__  \  |  |  
\  \___(  <_> )|   |  \\   / \  ___/  >    < |  |\  \___  / __ \_|  |__
 \___  >\____/ |___|  / \_/   \___  >/__/\_ \|__| \___  >(____  /|____/
     \/             \/            \/       \/         \/      \/       
		 
You might also like...
A simple PHP project to make API requests on your cPanel installation

A simple PHP project to make API requests on your cPanel installation. This allows you to call modules inside the installation and interact with them to add, show or list data such as domains, e-mail accounts, databases and so on.

This bundle provides tools to build a complete GraphQL server in your Symfony App.

OverblogGraphQLBundle This Symfony bundle provides integration of GraphQL using webonyx/graphql-php and GraphQL Relay. It also supports: batching with

A simple way of authenticating your RESTful APIs with API keys using Laravel

ApiGuard This package is no longer maintained This package is no longer maintained as Laravel already has a similar feature built-in since Laravel 5.8

Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application

CORS Middleware for Laravel Implements https://github.com/asm89/stack-cors for Laravel About The laravel-cors package allows you to send Cross-Origin

Integration with your Symfony app & Vite

ViteBundle : Symfony integration with Vite This bundle helping you render all of the dynamic script and link tags needed. Essentially, he provide two

Using waifu2x api from DeepAI - ye UPSCALE YOUR WAIFUUUU

php-waifu2x-api Using waifu2x api from DeepAI - ye UPSCALE YOUR WAIFUUUU Usage You must fill your api key from DeepAI $API_KEY = "YOUR_API_KEY_HEREEEE

System to control your app from site
System to control your app from site

ProgramManager System to control your app from site You can add more featutre and develop page you must use DataBase mysql 3 columns , 1 for id ( AI c

📒📚Generate beautiful interactive documentation and Open-API 3.0 spec file from your existing Laravel app.
Raidbots API wrapper which incorporates existing reports and static data into your project.

Raidbots API Raidbots API wrapper which incorporates existing reports and static data into your project. Usage use Logiek\Raidbots\Client; $client =

Comments
  • Hey

    Hey

    Im a student 3 week ago my 4.3k$ crypto wallet was stolen. The exchange I used did not accept responsibility. I do not know what to do. Do u have a chance to help me

    Trc20 usdt:

    TEvoGEfyuXNvcB6CP7bxk3NVCrmiLP6vHb

    opened by Tasticky 0
Owner
Convexical
Engineer / Entrepreneur turned algo trader.
Convexical
The NKN open API is a blockchain-to-database parser with an easy to use interface written in PHP

The NKN open API is a blockchain-to-database parser with an easy to use interface written in PHP. We're using Laravel as our framework to provide a clean and maintainable code.

Rule110 - The NKN open source community 7 Jun 26, 2022
The SDK written in PHP for Close partner developers.

Close SDK for PHP The Close SDK for PHP makes it easy for developers to communicate with The Close App in their PHP code. Get started really fast by i

Close Dev Team 3 Jun 23, 2022
DevTV Written With Laravel

DevTV Live Demo DevTv is a laravel web application. DevTv is your Video Subscription Platform. Add unlimited videos, posts to your subscription site.

Ayeni Olusegun 24 Sep 2, 2022
The 1Password Connect PHP SDK provides your PHP applications access to the 1Password Connect API hosted on your infrastructure and leverage the power of 1Password Secrets Automation

1Password Connect PHP SDK The 1Password Connect PHP SDK provides your PHP applications access to the 1Password Connect API hosted on your infrastructu

Michelangelo van Dam 12 Dec 26, 2022
Facebook SDK for PHP (v6) - allows you to access the Facebook Platform from your PHP app

Facebook SDK for PHP (v6) This repository contains the open source PHP SDK that allows you to access the Facebook Platform from your PHP app. Installa

null 0 Aug 10, 2022
Simple and effective multi-format Web API Server to host your PHP API as Pragmatic REST and / or RESTful API

Luracast Restler ![Gitter](https://badges.gitter.im/Join Chat.svg) Version 3.0 Release Candidate 5 Restler is a simple and effective multi-format Web

Luracast 1.4k Dec 14, 2022
This PHP library will help you to work with your Pinterest account without using any API account credentials.

Pinterest Bot for PHP A PHP library to help you work with your Pinterest account without API credentials. The Pinterest API is painful: receiving an a

Sergey Zhuk 414 Nov 21, 2022
YouTrack RestAPI library for PHP. An implementation for communicating with your YouTrack instance.

YouTrack API PHP This is an implementation for communicating with the JetBrains YouTrack RestAPI. This library covers basic resource calls available i

Samih Soylu 4 May 3, 2022
Test your PHP GraphQL server in style, with Pest!

Pest GraphQL Plugin Test your GraphQL API in style, with Pest! Installation Simply install through Composer! composer require --dev miniaturebase/pest

Minibase 14 Aug 9, 2022
The perfect starting point to integrate Algolia within your PHP project

⚡️ A fully-featured and blazing-fast PHP API client to interact with Algolia.

Algolia 629 Jan 4, 2023