Coder Social home page Coder Social logo

binance-futures-trading-bot's Introduction

Binance-Futures-Trading-Bot Tweet

Technical Analysis driven Crypto Trading bot on Binance Futures ๐Ÿ“ˆ โ‚ฟ ๐Ÿš€

  • Utilizes python-Binance Client to execute orders and pull data from Binance
  • Utilizes ta library to calculate Technical indicators
  • Write your own functions/classes to implement your TA strategies
  • Comes with some pre-coded strategies found in TradingStrats.py
  • If you enjoy the repo please share it around to friends & tweet about it using the tweet button above ๐Ÿ˜ƒ
    or Buy me a Coffee

Binance Setup


  • Create a Binance Account (This link uses my referral which gives you 5% kickback on trades & would be greatly appreciated)
  • Enable Two-factor Authentication in the security section to keep your crypto safe.
  • Create a new API key under the API Management section.
  • [โœ“] Read Info [โœ“] Enable Trading [X] Enable Withdrawals
  • Whitelist your IP address to further secure your account, and so that your API keys don't expire after 1 month.
  • Fill in your api keys into api_secret and api_key in API_keys.py.

Back test strategies in Bot.py


  • To back test ensure Trading is switched off on line 95.
  • Back test section starts at line 719.
  • Create a list named 'symbol' of coin/coins you wish to run a strategy on ie. symbol = ['BTCUSDT' , 'ETHUSDT'] , this would run your strategy on BTC and ETH. Whereas symbol = ['BTCUSDT'] would run the strategy on BTC only.
  • Ignore the pair-trading section and ensure pair_Trading = 0, if you are executing a TA strategy
  • The data is split into an in-sample set and a test set, the flag test_set decides which set we are running the strategy on, both sets are in same units as test_set_length but we adjust time_period variable to change the in-sample data set length. The reason for splitting the data like this is to optimize parameters on the in-sample set and then once optimized run the strategy on the test-set to see if you have overfit your model by cherry picking values for parameters that suit the in-sample data.
  • The time_period variable is the length of time in the past from today excluding the test-set, to run the strategy on. This is in the same units as the test_set_length.
  • The TIME_INTERVAL variable is the interval for the candlesticks we want to trade on.
  • Next we want to choose our TA strategy, this is done after line 925 , uncomment a strategy or call a new strategy you have written yourself here, the 'prediction' variable is used to tell the script to go short (0), go long (1), or go flat (-99). This should be returned by custom strategy functions/classes you write for the strategy to be executed correctly
  • Some of the pre-coded strategies return a 'Type' variable, if a strategy returns the 'Type' variable you must call the SetSLTP() function from TradingStrats.py in order to set the corresponding Stop loss value, and Take profit value, this function is found in TradingStrats.py
  • Now just run the script and wait a few minutes for it to pull the data and begin backtesting

Using Downloaded data for backtesting


  • Reason to do this is to speed up backtesting
  • Create a folder on the desktop called price_data.
  • In download_Data.py change the path to f"C:\Users\your_name\Desktop\price_data
    replacing your_name with the user that you are logged into.
  • Switch load_data on in Bot.py on line 750, now when you run the script it will load from the folder & if the specified candlestick data isn't present in the folder then it will be downloaded and saved for future use.
    NOTE: this data is static so if you want up to date data in future you will have to manually delete the data from the folder on your desktop and then run the script again. Otherwise you can just turn load_data off and pull data from the server everytime you want to run a backtest.

Strategy_Tester.py

This script will run a strategy on every coin on Binance & then generate graphs and statistics which are saved to a folder on the desktop.

  • Line 29 to Line 43 are the settings which should be clear with the comments I've provided
  • Create a folder on the desktop called Strategy_tester with another folder matching the Strategy_name variable, this is where the data will be stored when you run.
  • Change path1 on line 44 to reflect the location of the Strategy_tester folder you've just created.
  • Uncomment a strategy after line 258 or else call your custom strategy here, following the same guidelines layed out in the backtesting section above.
  • Now run the script with the settings you've chosen and check up on the graphs and statistics that are created soon after.

Run strategies live in Bot.py


Run strategies at your own risk I am not responsible for your trading decisions, futures are risky and proper risk management should be adhered to at all times, always have a stoploss

  • Switch Trading On at line 95.
  • Choose the Interval of candle sticks you wish to trade over.
  • Now un-comment a symbol or add a new symbol you want to trade after line 110.
  • To trade a coin not listed at the top of the script we must add an elif symbol clause in get_coin_attrib() in Helper.py specifying Coin_precision (how many decimal point places the price of the coin is measured in) & Order_precision (how many decimal point places orders are measured in). Note: some coins have no decimal places in order quantity, in this case we set Order_precision = 0.
  • Adjust 'OrderSIZE' line 98 and 'leverage' line 97 as you see fit, adjusting the leverage only changes the EffectiveAccountBalance variable in the script which will affect the order quantity, you must manually adjust the leverage on the exchange currently. Note: OrderSIZE just helps scale trades in reality you may be risking more than your desired percentage, this will be decided by your stoploss values implemented
  • The Market_Orders switch on line 94 decides whether to use market orders (1) or to use limit orders (0).
  • Now we select our trading strategy, one which we have thoroughly Backtested in the section starting on line 367.
  • Custom Strategies must return a 'prediction' parameter either 1, 0, or -99 to go long, short or flat respectively.
  • Again like in Backtesting we call our trading strategy in the form of a function/class, If the function/class returns a Type then we must call the SetSLTP() function to set our Stop loss and Take profit before sending our orders.
  • Now just run the script and your strategy is up and running.

Create Custom Strategies


  • Custom Strategies Can be implemented in TradingStrats.py
  • Custom Strategies should return 'prediction' parameter to indicate the strategy's decision to go short (0), go long (1), go flat (-99).
  • For stop loss and take profit you must calculate and assign values to stoplossval and takeprofitval respectively, some pre-coded stoploss functions are already encapsulated in the SetSLTP() function in TradingStrats.py. You may use these by getting your custom strategy to return a Type parameter corresponding to the Type in SetSLTP() and then call SetSLTP with Type as one of the parameters, or else set these in a custom function of your own. Note: Stop loss and Take profit should be the margin of increase/decrease not the target price.

Contact me

  • If you have any querys about anything, or need me to explain any blocks of code please reach out to me at [email protected].
  • If you have any suggestions or requests please reach out to me as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.