Getting familiar with your trading data

Financial Trading in Python

Chelsea Yang

Data Science Instructor

Different types of traders

  • Day Trader: holds positions throughout the day but usually not overnight
  • Swing Trader: holds positions from a few days to several weeks
  • Position Trader: holds positions from a few months to several years
Financial Trading in Python

Resample the data

Date                   Close
2019-11-29 04:00:00    1.1010
2019-11-29 08:00:00    1.1005
2019-11-29 12:00:00    1.0993
2019-11-29 16:00:00    1.1016
2019-11-29 20:00:00    1.1020
# Resample from hourly to daily
eurusd_daily = eurusd_h.resample('D').mean()
Date          Close
2019-11-25    1.10165
2019-11-26    1.10165
2019-11-27    1.10058
2019-11-28    1.10083
2019-11-29    1.10093
# Resample from hourly to weekly
eurusd_weekly = eurusd_h.resample('W').mean()
Date          Close
2019-11-03    1.11248
2019-11-10    1.10860
2019-11-17    1.10208
2019-11-24    1.10659
2019-12-01    1.10113
Financial Trading in Python

Calculate daily returns

# Calculate daily returns
stock_data['daily_return'] 
= stock_data['Close'].pct_change() * 100
             Close  daily_return
Date                            
2020-12-11  609.99     -2.723779
2020-12-14  639.83      4.891883
2020-12-15  633.25     -1.028398
2020-12-16  622.77     -1.654955
2020-12-17  655.90      5.319781
# Plot the data
plt.plot(stock_data['daily_return'])
plt.show()

Plot of daily returns

Financial Trading in Python

Plot a histogram of daily returns

stock_data['daily_return'].hist(bins=100)
plt.show()

Daily return histogram

Financial Trading in Python

Data transformation

Technical indicators: various types of data transformations

Simple moving average (SMA): arithmetic mean price over a specified n-period

stock_data['sma_50'] = stock_data['Close'].rolling(window=50).mean()
            Close    sma_50
Date                                                                      
2020-12-11  122.41   117.7474
2020-12-14  121.78   117.9226
2020-12-15  127.88   118.1502
2020-12-16  127.81   118.4432
2020-12-17  128.70   118.7156
Financial Trading in Python

Plot the rolling average

import matplotlib.pyplot as plt

plt.plot(stock_data['Close'], 
         label='Close')
plt.plot(stock_data['sma_50'], 
         label='SMA_50')
plt.legend()
plt.show()

A plot of SMA with the Close price

Financial Trading in Python

Let's practice!

Financial Trading in Python

Preparing Video For Download...