Financial Trading in Python
Chelsea Yang
Data Science Instructor
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
# 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()
stock_data['daily_return'].hist(bins=100)
plt.show()
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
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()
Financial Trading in Python