How To Build A Crypto Trading - ABM
How To Build A Crypto Trading - ABM
Search Medium
Published in Coinmonks
Save
All activity is demonstrated in Python. Each episode contains working code samples,
with the full code accessible on GitHub here.
Note: all code is for example use only and I make no guarantees about the strategy. Do
your own research and use it at your own risk 😊.
In This Episode
By the end of this episode, you’ll have built your very own trading strategy and to
actively analyze market data.
Strategy Development
A Quick Note
Before diving into strategy development, I want to make a quick note about the
strategy. The strategy I’m demonstrating should only be used at your own risk — after
you do your own research. I cannot and do not make any statements about its viability.
If you do choose to use it, I only ask that you give me a quick shout-out on LinkedIn ❤
Strategy
The strategy I’ll be demonstrating is designed to take advantage of momentum-based
trading events. Here are the rules:
Condition 1: If the closing price of a token experiences a 10% or more price rise per
hour, for three consecutive hours, then open a trade. Call this a pump.
Restriction 1: Only trade in tokens with a Quote Asset of Binance USD (BUSD) pairs.
This helps us avoid having to dynamically shift around our liquid capital to esoteric
pairs.
Note. If you’re someone who tracks crypto pump and dump events, this strategy could
be easily modified to identify these events. You could then filter these out from your
trading.
Getting Data
Strategy implementation starts with correctly formatting data.
I’ll start by updating the function get_candlestick_data from Episode 1 (episode list at
the bottom of the article). Readers who have seen my previous series How to Build a
MetaTrader 5 Python Trading Bot will recognize the data format. Without giving too
much away, I’ve standardized the format in preparation for a future series 😉
The Binance API documentation for Kline/Candlestick Data explains how to interpret
the results from the queries. Expressed in code, it looks like this:
or24‘Green’ (trending
converted_data.append(converted_candle)
up):
25 # Return converted data
26 return converted_data
1 import pandas
binance_interaction.py hosted with ❤ by GitHub view raw
2 import numpy
3 import binance_interaction
4
5
6 # Function to convert candle data from Binance into a dataframe
7 def get_and_transform_binance_data(symbol, timeframe, number_of_candles):
8 # Retrieve the raw data from Binance
9 raw_data = binance_interaction.get_candlestick_data(symbol=symbol, timeframe=timeframe, q
10 print(raw_data[0]['time'])
11 # Transform raw_data into a Pandas DataFrame
12 df_data = pandas.DataFrame(raw_data)
13 # Convert the time to human readable format. Note that Binance uses micro-seconds.
14 df_data['time'] = pandas.to_datetime(df_data['time'], unit='ms')
15 # Convert the close time to human readable format. Note that Binance uses micro-seconds.
16 df_data['close_time'] = pandas.to_datetime(df_data['close_time'], unit='ms')
17 # Calculate if Red or Green
18 df_data['RedOrGreen'] = numpy.where((df_data['open'] < df_data['close']), 'Green', 'Red')
19 # Return the dataframe
20 return df_data
Update strategy.py with a function to convert raw Binance Data into a Pandas DataFrame. Part of the series How
to Build a Crypto Trading Bot with Binance and Python.
Identifying a Buy
The strategy provides a series of rules which can be used to identify a buy . Here they
2. If they are, ensure the closing price rise for each was ≥ 10%
3. If true, buy
Note. Astute programmers will note the use of hard-coded functions for determining
buy events. This could certainly be abstracted into a simple for loop. I’ve done it this
way to emphasize the calculation.
Binance has strict request limits for its API. They are certainly generous but need to be
respected. To see the latest updates on the limit, check this webpage out. At the time of
writing, this is 1200 weighted requests per minute (reasonably generous).
To ensure that you stay below the request limit, I’ve implemented a time.sleep of 1
second per iteration. This doesn’t impact the performance of the algorithm, but stops
your API Key from getting banned ❤
If you’d like to play with what you’ve developed so far, update your main as below:
__main__ updated to incorporate the analysis functions described so far. Part of the series ‘How to Build a Crypto
Trading Bot with Binance and Python’.
Make sure you’ve imported the functions we’ve developed import binance_interaction
Demonstration of strategy for ‘How to Build a Crypto Trading Bot with Binance and Python’
Have some fun with this. Because of the design, you can modify the percentage gain
and quote assets pretty easily. See if you can find some trending tokens and then check
them out on Binance.
Wrapping Up
What a big episode! If you’ve been following along, you’re now analyzing symbols on
Binance to see if they’re trending!
We’ve developed a cohesive strategy, and now it’s time for us to take the next step by
learning how to place trades!
List of Episodes
1. Connecting to Binance
Say Hi!
I love hearing from my readers, so feel free to reach out. It means a ton to me when
you clap for my articles or drop a friendly comment — it helps me know that my
content is helping.
A newsletter that brings you day's best crypto news, Technical analysis, Discount Offers, and MEMEs directly in your
inbox, by CoinCodeCap.com Take a look.