How to Calculate the 20-EMA on Binance with Python and Pandas
Python Trading Bot
One of the more popular indicators day traders use is the EMA (Exponential Moving Average). As the crypto market becomes more mature, the opportunity to apply well-known trading patterns is immense, so I thought it would be great to share how to calculate this indicator.
You can easily plug this code straight into your crypto trading bot. I’ll be using Python 3, Python Pandas, and data from Binance. I’ve provided links to these resources at the bottom of this article.
What is an EMA
An exponential moving average is a weighted average of a series of prices. Many traders consider it to be more accurate than an SMA as the weighting makes it more sensitive to price movements. This makes it a potentially faster way to identify market direction changes, with many traders associating the words “lots more money” with the word “faster”.
Why it Might Be Useful
Including an EMA in your trading bot can be a great way to inform your automated trading strategy about upcoming BUY and SELL signals. Using the power of computing, you can rapidly (as in microseconds) inform your bot of direction changes.
Another powerful use of this code is the ability to calculate multiple timeframes across multiple trading pairs. For instance, many traders believe that if you observe a direction change in lower timeframes, you can have a higher level of confidence in a similar direction change in higher timeframes. Alternatively, if you believe that Ethereum (ETH) is sensitive to price movements in Bitcoin (BTC), calculating the EMA for BTC while trading ETH might be handy.
Regardless, converting this calculation into code gives you tremendous flexibility which you just cannot get from using the standard charts available.
How to Code
EMA Formula
The formula for an EMA is as follows:
EMA = Todays_Price * multiplier + Yesterdays_EMA * (1 — multiplier)
Where the multiplier is:
Smoothing Factor / (Number of Periods + 1)
Typically the Smoothing Factor is 2.
Because this calculation requires historic values to calculate, we always start the very first EMA with the associated SMA. I.e. if calculating a 20-EMA, then the very first EMA is actually the 20-SMA. Check out this article if you want to learn how to calculate an SMA.
Calculation Caution
The dependence on previous values to calculate the next values means that you need to be careful with how many rows of data you pass to your EMA calculator.
In my experience, the sweet spot is around 1,000 rows. Any more than this and your calculation slows down significantly. Any less, and your calculation loses precision. The number does change depending on the size of your EMA, so feel free to experiment.
Retrieve Data from Binance
Let’s start by retrieving the data from Binance. We’ll take this opportunity to normalize the returned values to comply with the data standards of the Python Trading Bot. Here’s how:
- Import the official Binance Python Connector
from binance.spot import Spot()
- Add the following function:
# Function to query Binance for candlestick data
def get_candlestick_data(symbol, timeframe, qty):
# Retrieve the raw data
raw_data = Spot().klines(symbol=symbol, interval=timeframe, limit=qty)
# Set up the return array
converted_data = []
# Convert each element into a Python dictionary object, then add to converted_data
for candle in raw_data:
# Dictionary object
converted_candle = {
'time': candle[0],
'open': float(candle[1]),
'high': float(candle[2]),
'low': float(candle[3]),
'close': float(candle[4]),
'volume': float(candle[5]),
'close_time': candle[6],
'quote_asset_volume': float(candle[7]),
'number_of_trades': int(candle[8]),
'taker_buy_base_asset_volume': float(candle[9]),
'taker_buy_quote_asset_volume': float(candle[10])
}
# Add to converted_data
converted_data.append(converted_candle)
# Return converted data
return converted_data
Create the Generic EMA Function
Next, create the EMA function as below:
import pandas
# Define function to calculate an arbitrary EMA
def calc_generic_ema(symbol, timeframe, ema_size):
raw_data = get_candlestick_data(symbol=symbol, timeframe=timeframe, qty=1000)
# Convert into Dataframe
dataframe = pandas.DataFrame(raw_data)
# Create column string
ema_name = "ema_" + str(ema_size)
# Create the multiplier
multiplier = 2/(ema_size + 1)
# Calculate the initial value (SMA)
# pandas.set_option('display.max_columns', None) # <- use this to show all columns
# pandas.set_option('display.max_rows', None) # <- use this to show all the rows
initial_mean = dataframe['close'].head(ema_size).mean()
# Iterate through Dataframe
for i in range(len(dataframe)):
if i == ema_size:
dataframe.loc[i, ema_name] = initial_mean
elif i > ema_size:
ema_value = dataframe.loc[i, 'close'] * multiplier + dataframe.loc[i-1, ema_name]*(1-multiplier)
dataframe.loc[i, ema_name] = ema_value
else:
dataframe.loc[i, ema_name] = 0.00
# print(dataframe) # <- use this to print the dataframe if you want to inspect
return dataframe
And that’s it!
20-Day EMA on BTCETH
To calculate a 20-Day EMA on the BTCETH pair, you could simply do the following:
calc_generic_ema("BTCETH", "1d", 20)
Why Not Follow Me For More
I love what I do, and I’d love to hear from you! Why not follow me on Medium, LinkedIn, or Twitter, and sign up for my email distribution?