A cryptocurrency trading bot is a software program that automatically buys and/or sells cryptocurrencies according to predetermined criteria. Cryptocurrency trading bots are designed to remove the emotion from trading decisions, as well as constant market monitoring and trade execution.
Bots can be used to take advantage of arbitrage opportunities, as well as to automate trades based on technical analysis indicators.
There are many different types of cryptocurrency trading bots available, each with their own strengths and weaknesses. In this article, we’ll take a look at how to build a simple cryptocurrency trading bot using the Python programming language and the Binance API.
The first thing we need to do is create a new Python file and import the following libraries:
import time import requests import json import pandas as pd import numpy as np from binance.client import Client client = Client(“”, “”) #API Key and Secret
We also need to set up a few variables that will be used throughout the script:
symbol = ‘ETHBTC’ #Symbol to trade interval = ‘1m’ #1 minute candlesticks kline = client.get_historical_klines(symbol, interval, “1 day ago UTC”) #Get 1 day of 1 minute ETHBTC candlesticks df = pd.DataFrame(kline,columns=[‘Open time’,’Open’,’High’,’Low’,’Close’,’Volume’,’Close time’,’Quote asset volume’,’Number of trades’,’Taker buy base asset volume’,’Taker buy quote asset volume’,’Ignored’]) df[‘SMA5’]=df[‘Close’].rolling(window=5).
mean() df[‘SMA20’]=df[‘Close’].rolling(window=20).mean() df = df[20:] #Drop first 20 rows because they are incomplete due to SMA calculation last_row = df.tail(1) last_close = float(last_row[‘Close’]) print(“Last close price: “+str(last_close)).
In the above code, we’ve imported the necessary libraries and set up our symbol, interval, and candlestick dataframe variables. We’ve also added two Simple Moving Average (SMA) columns to our dataframe, one for a 5 period SMA and one for a 20 period SMA.
NOTE: Warning: Building a simple cryptocurrency live trading bot with Python using the Binance API can be a complicated and risky endeavor. It is important to understand the programming language and the cryptocurrency market in order to make an informed decision. Before attempting to build such a bot, it is recommended that you thoroughly research how to use the Binance API, as well as any other relevant information about trading cryptocurrencies. Additionally, it is important to understand the risks associated with trading cryptocurrencies, including but not limited to financial loss.
The last_row and last_close variables will be used later on in the script.
Next, we need to define our buy() and sell() functions:
def buy(): quantity = float(client.get_asset_balance(asset)[‘free’]) order = client.
order_market_buy(symbol=symbol, quantity=quantity) print(“Buy order placed: “+str(quantity)+” “+symbol+” at “+str(order[‘fills’][0][‘price’])) def sell(): quantity = float(client.order_market_sell(symbol=symbol, quantity=quantity) print(“Sell order placed: “+str(quantity)+” “+symbol+” at “+str(order[‘fills’][0][‘price’])).
These functions will place market buy/sell orders for the specified asset/quantity pair. We’re using the Binance API’s get_asset_balance() function to get our account’s current balance of the asset we’re trading (ETH in this case), and then using the order_market_buy() or order_market__sell() function to place our trade orders accordingly.
The print statements are optional but useful for debugging purposes.
Now we need to define our main() function:
def main(): while True: try: row = df.tail(1) close = float(row[‘Close’]) sma5 = float(row[‘SMA5’]) sma20 = float((row[‘SMA20’])) if close > sma5 > sma20: if not client.
get_open_orders(): buy() elif close < sma5 < sma20: if not client.get_open_orders(): sell() time.sleep(60) except KeyboardInterrupt: print('ctr-c detected') break except Exception as e: print('Exception detected: '+str(e)) break if __name__ == '__main__': main().
[related-posts id="28574, 11482, 25028, 39178, 39194, 24978, 29804, 32976"]