13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

320

Obtains a pandas DataFrame containing open normalised prices

for high, low and close for a particular equities symbol

from Yahoo Finance. That is, it creates High/Open, Low/Open

and Close/Open columns.

"""

df = web.DataReader(symbol, "yahoo", start, end)

df["H/O"] = df["High"]/df["Open"]

df["L/O"] = df["Low"]/df["Open"]

df["C/O"] = df["Close"]/df["Open"]

df.drop(

[

"Open", "High", "Low",

"Close", "Volume", "Adj Close"

],

axis=1, inplace=True

)

return df

def plot_candlesticks(data, since):

"""

Plot a candlestick chart of the prices,

appropriately formatted for dates

"""

# Copy and reset the index of the dataframe

# to only use a subset of the data for plotting

df = copy.deepcopy(data)

df = df[df.index >= since]

df.reset_index(inplace=True)

df[’date_fmt’] = df[’Date’].apply(

lambda date: mdates.date2num(date.to_pydatetime())

)

# Set the axis formatting correctly for dates

# with Mondays highlighted as a "major" tick

mondays = WeekdayLocator(MONDAY)

alldays = DayLocator()

weekFormatter = DateFormatter(’%b %d’)

fig, ax = plt.subplots(figsize=(16,4))

fig.subplots_adjust(bottom=0.2)

ax.xaxis.set_major_locator(mondays)

ax.xaxis.set_minor_locator(alldays)

ax.xaxis.set_major_formatter(weekFormatter)

# Plot the candlestick OHLC chart using black for

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!