Hephyrius / binance_futures_bot

A bot that trades leveraged USDT futures markets on binance.
299 stars 123 forks source link
binance binance-bots bot cryptocurrency-trading-bot trading-bot

Binance Futures Bot

A simple bot that trades futures on binance.

Contributing

Feel free to contribute to the project and propose changes. I will review and accept the good PRs

Table of Contents

Disclaimer

This bot is intended to be a Proof-of-concept. The developer will not be responsible for any losses that are a result of using this program. This program or anything related to it, should not be considered investment advice. Understand the risks involved, do your own research and only trade with amounts you are willing to lose.

Trading Strategy

The bot trades a strategy called the 'TalonSniper'. It can be modified to take your own stragies.

image of strategy

Why this strategy?

I chose the strategy as a demonstration strategy. I came across it while browsing trading view. On the surface the strategy seems to find points where trends can be considered "confirmed". The strategy was reimplemented by looking at the code in the trading view pine editor and then recreating it in python. This can be found in the trading_signal() function of the bot_functions.py file.

Heikin Ashi Candles

The Bot makes use of Heikin Ashi candles for it's candle representations. This is because Heikin Ashi candles make it easier to spot trends. You can read more about them here

Bot Specifics

Position Entry

Positions are given to a bot as a list of historic "signals". These tell the bot when to go long, short or stay in whatever it was in before.

The bot uses the second last signal from the list of historic signals to make it's choice of action. This is because the last signal in the list is the current unresolved candle, as this is still in flux.

Position Exit

The bot attempts to stay in positions as long as possible.

Changing Strategies

If you want to swap out the falcon sniper strategy with one of your own, I recommend doing it like so.

First Create a function that calculates some sort of signal and returns it as a list of integers. Taking Heikin Ashi open, high, low, and close as inputs.

def new_trading_signal(h_o, h_h, h_l, h_c, use_last=False):

    entry = [0]
    last = 0
    for i, v in enumerate(h_o):
        if i != 0:
            if h_o[i-1] < h_c[i-1]:
                entry.append(1)
                last = 1

            elif h_o[i-1] > h_c[i-1]:
                entry.append(-1)
                last = -1

            else:
                if use_last:
                    entry.append(last)
                else:
                    entry.append(0)

    return entry

Then modify the function the bot calls for its entry signalling.

def get_signal(client, _market="BTCUSDT", _period="15m", use_last=False):
    candles = client.get_candlestick_data(_market, interval=_period)
    o, h, l, c, v = convert_candles(candles)
    h_o, h_h, h_l, h_c = construct_heikin_ashi(o, h, l, c)
    ohlcv = to_dataframe(h_o, h_h, h_l, h_c, v)
    entry = new_trading_signal(h_o, h_h, h_l, h_c, use_last)
    return entry

NOTE: the use_last variable allows you to constantly pump out the last non-zero bot signal. This will allow the bot to enter a position as soon as the bot is initiated, rather than it waiting for a new signal to appear. This can also be chained with multi-timescale strategies for better potential performance.

Installation

Requirements

The bot doesn't require much to operate. Simple Numpy, Pandas and binance futures python

you can install some of the requirements as such:

pip install -r requirements.txt

followed by the installation of binance futures python

python -m pip install git+https://github.com/Binance-docs/Binance_Futures_python.git

Setup

The bot can be modified for use with any USDT futures market, leverage and time frame combinatiob by editing the settings.json file.

Settings.json

You should replace the market, leverage and period with values that are relevant.

Keys.json

This file is where you should put your API keys. The API Keys should have Futures access enabled, or the bot won't work. You can generate a new api key here when logged in to binance

{
    "api_key": "fill_api_key_here",
    "api_secret": "fill_api_secret_here"
}

Usage

Once you've modified Keys.json and Settings.json you should be ready to go.

Running locally

python bot.py

Running on a linux server/cloud insance

nohup python bot.py &

Misc

Does this make $$$?

tl;dr - Short answer yes and no!

This really depends on market conditions, leverage and time period! The bot seems to struggle when the market is side-ways, but seems to excell when trending. But again this depends on settings and specific markets. Nothing is guaranteed ( refer to Disclaimer ).

Liquidation Price

The bot does not take into account liquidation price, although a function has been created for this purpose. Be careful when using any leverage where a 2% move can lead to liquidation of positions - I advice against using leverage sizes that are crazy high (i.e. 30x+)