Algorithmic Trading and the Power of Python
Algorithmic trading has redefined today’s financial markets. It not only helps in making trades faster but also helps in implementing high-level strategies and analysis with accuracy. In this blog, we will learn how to use Python and Pandas Series, and how you can create trading strategies, especially with tools like Freqtrade. We will also talk about top trading software and quant analysis in the US and Singapore.
What is Pandas Series in Python?
A Pandas Series is like a one-dimensional array, which contains data and labeled index. You can also think of a series as a column or row of a data frame.
Printing and checking a Series
type(DF)
If the output is <class ‘pandas.core.series.Series’>, it means that your data is a Series.
Important functions of Series
1. head() and tail():
- Using head() you can see the top 5 values.
- Using tail() you get the bottom 5 values.
If you want custom values (like last 20 values), then you can use tail(20).
Example:
DF.head(10) # Top 10 values
DF.tail(15) # Last 15 values
2. sample(): Get a random sample of data
If you have 100 values and you want to see a random value, then use DF.sample().
For many random values do DF.sample(5).
Why is Sample important?
When your data is sorted (like small numbers at the top and big numbers at the bottom), then head() or tail() will give biased information. In such a case sample() gives unbiased estimate.
3. value_counts(): Know the frequency of a value
DF.value_counts() will give you information about which value has occurred how many times.
Example:
DF.value_counts()
This method is useful when you need to count the number of prices or values that occur frequently.
4. sort_values(): Sort the values
If you want to see the values in ascending or descending order, use sort_values().
Example:
DF.sort_values(ascending=True)
DF.sort_values(ascending=False)
You can make these changes permanent by adding inplace=True, but use it carefully.
5. sort_index(): Sort the index
Use sort_index() to sort the index.
DF.sort_index()
DF.sort_index(ascending=False)
This is especially helpful when the Series has labels like ‘a’, ‘b’, ‘c’ etc.
How to plot a chart in Python?
In Python, you can create many types of charts through libraries like Pandas and Matplotlib:
- Line chart
- Bar chart
- Pie chart
import matplotlib.pyplot as plt
DF.plot(kind=’bar’) # Bar chart
plt.show()
Charts are a must for visualizing data, especially when you are creating trading strategies.