Intro Finance


1. Stocks

From Investopedia

  • A stock is a form of security that indicates the holder has proportionate ownership in the issuing corporation.
  • Corporations issue (sell) stock to raise funds to operate their businesses. There are two main types of stock: common and preferred.
  • Stocks are bought and sold predominantly on stock exchanges, though there can be private sales as well, and they are the foundation of nearly every portfolio.
  • Historically, they have outperformed most other investments over the long run.
  • Stocks are quoted on exchanges. The current market price is referred to as the spot price of the stock.
  • A good primer on finance and the stock market is Malkiel's A Random Walk Down Wall Street.
In [2]:
goog = pdr.get_data_tiingo("GOOG", api_key=api_key)
goog.index = goog.index.get_level_values("date")
In [3]:
goog.iloc[:-10:-1]
Out[3]:
adjClose adjHigh adjLow adjOpen adjVolume close divCash high low open splitFactor volume
date
2019-04-04 00:00:00+00:00 1215.00 1215.67 1204.13 1205.94 950747 1215.00 0.0 1215.67 1204.13 1205.94 1.0 950747
2019-04-03 00:00:00+00:00 1205.92 1216.30 1200.50 1207.48 1017838 1205.92 0.0 1216.30 1200.50 1207.48 1.0 1017838
2019-04-02 00:00:00+00:00 1200.49 1201.35 1185.71 1195.32 827973 1200.49 0.0 1201.35 1185.71 1195.32 1.0 827973
2019-04-01 00:00:00+00:00 1194.43 1196.66 1182.00 1184.10 1252757 1194.43 0.0 1196.66 1182.00 1184.10 1.0 1252757
2019-03-29 00:00:00+00:00 1173.31 1178.99 1162.88 1174.90 1269979 1173.31 0.0 1178.99 1162.88 1174.90 1.0 1269979
2019-03-28 00:00:00+00:00 1168.49 1171.57 1159.43 1171.54 1012430 1168.49 0.0 1171.57 1159.43 1171.54 1.0 1012430
2019-03-27 00:00:00+00:00 1173.02 1187.56 1159.37 1185.50 1400240 1173.02 0.0 1187.56 1159.37 1185.50 1.0 1400240
2019-03-26 00:00:00+00:00 1184.62 1202.83 1176.72 1198.53 1901156 1184.62 0.0 1202.83 1176.72 1198.53 1.0 1901156
2019-03-25 00:00:00+00:00 1193.00 1206.40 1187.04 1196.93 1496843 1193.00 0.0 1206.40 1187.04 1196.93 1.0 1496843
  • close is the end-of-day (EOD) price
  • open is the first price for the day
  • volume is the number of stocks traded
  • adj* columns are adjusted prices for stock splits/dividend disbursements
In [4]:
goog["close"].plot(figsize=(16,8))
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x112b41630>
In [2]:
tickers = ["AAPL", "MSFT", "TSLA", "IBM", "GOOG"]
symbols = pdr.get_data_tiingo(tickers, api_key=api_key)
In [3]:
close_data = symbols["adjClose"].unstack(level="symbol")
close_data.tail()
Out[3]:
symbol AAPL GOOG IBM MSFT TSLA
date
2019-04-01 00:00:00+00:00 191.24 1194.43 143.30 119.02 289.18
2019-04-02 00:00:00+00:00 194.02 1200.49 143.00 119.19 285.88
2019-04-03 00:00:00+00:00 195.35 1205.92 143.63 119.97 291.81
2019-04-04 00:00:00+00:00 195.69 1215.00 142.78 119.36 267.78
2019-04-05 00:00:00+00:00 197.00 1207.15 143.28 119.89 274.96
In [4]:
close_data.plot(figsize=(16, 10))
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x1123056d8>

Key concepts

  • Return
    $$R(S, t, t_0) = \dfrac{S_t - S_0}{S_0} = \dfrac{S_t}{S_0} - 1$$

  • Volatility
    $$\sigma = \sqrt{\frac{\sum\limits_{i=0}^t (S_i -\mu)^2}{t}}$$
    Where stock $S_t$ is the price of the stock at time $t$ and $\mu$ is the mean return.

In [9]:
close_data.std()
Out[9]:
symbol
AAPL     48.947991
GOOG    222.109105
IBM      18.555292
MSFT     27.015432
TSLA    115.476579
dtype: float64

Lets look at the daily returns for each symbol.

In [5]:
returns = close_data.pct_change()
returns.tail()
Out[5]:
symbol AAPL GOOG IBM MSFT TSLA
date
2019-04-01 00:00:00+00:00 0.006791 0.018000 0.015592 0.009157 0.033302
2019-04-02 00:00:00+00:00 0.014537 0.005074 -0.002094 0.001428 -0.011412
2019-04-03 00:00:00+00:00 0.006855 0.004523 0.004406 0.006544 0.020743
2019-04-04 00:00:00+00:00 0.001740 0.007530 -0.005918 -0.005085 -0.082348
2019-04-05 00:00:00+00:00 0.006694 -0.006461 0.003502 0.004440 0.026813
In [12]:
returns.hist(bins=50, color="red", figsize=(16, 8))
Out[12]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x1173ad2e8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x1174013c8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x118d33d30>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x117cbd6d8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x118712080>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x118ccf9e8>]],
      dtype=object)
In [6]:
returns.std()
Out[6]:
symbol
AAPL    0.016304
GOOG    0.014808
IBM     0.012312
MSFT    0.014484
TSLA    0.032607
dtype: float64

Indeces and ETFs

Index

  • An index consists of a hypothetical portfolio of securities representing a particular market or a segment of it.
  • Common examples: S&P500, FTSE100, Russell 2000, US Aggregate Bond Index.
  • S&P500 is a commonly used benchmark for the stock market. It aggregates 500 of the largest US publicly traded companies weighted by market cap. It includes 75% of the total stocks traded in the US.

ETF

  • An exchange traded fund is is a marketable security that tracks a stock index, a commodity, bonds, or a basket of assets.
  • Benefits: Low fees, lower taxes.
  • They allow investors to trade a portfolio of stocks with lower capital margin.
  • Common examples: SPDR (Spider, tracks S&P500), VOO (Vanguard S&P500 Index Fund), iShares Russell 2000, BLV (Vanguard Long-Term Bond)

2. Options

From Investopedia

  • Options are financial derivatives that give buyers the right, but not obligation, to buy or sell an underlying asset at an agreed upon price during a certain period of time.
  • Two exercise types: European (may exercise at expiration) and American (may exercise at any moment prior to expiration)
  • Basic options come in two types: Calls and Puts
  • Calls and puts form the basis for a wide range of option strategies designed for hedging, protection and speculation.
  • They are also used as a leveraging tool. Option contracts represent (in general) 100 shares of the underlying stock while costing only a fraction of the price.
  • For more information on options, a good source is Natenberg's Option Volatility and Pricing. Also, Hull's Options, Futures, and Other Derivatives is a comprehensive textbook on the subject of financial derivatives.

Option contracts specify the following parts:

$S$ - Underlying asset
$K$ - Strike price
$\text{Expiration}$ - Days to expiration (dte)
$\text{Price}$ - Price of the option, also called premium

Key concepts

  • OTM, Out of the money : $K < S_t$ for puts, $K > S_t$ for calls
  • ATM, At the money : When $K \approx S_t$
  • ITM, In the money : $K > S_t$ for puts, $K < S_t$ for calls
  • Cash-settled options are payed in cash, no physical delivery of the underlying asset takes place. The amount payed is the difference between the strike price of the option and the value of the asset at exercise date.

Calls

  • Give the bearer the right to buy the underlying asset at a given date for an agreed upon price.
  • Can be thought of as an insurance against asset price increases.

    $$\text{Payoff} \equiv max(S_t - K, 0)$$
    $$\text{Profit} \equiv max(S_t - K, 0) - C(K, t, t_0)$$
    Where $S_t$ is the price of the asset at the day of expiration $t$ and $C(K, t, t_0)$ is the premium payed for the call purchased at time $t_0$ with strike price $K$.
In [9]:
def call(K, S_t, C):
    return np.maximum(S_t - K, 0) - C
In [10]:
C = 5
K = 200
S_t = np.linspace(190, 220, num=100)

with sns.axes_style("white"):
    _fig, ax = plt.subplots(figsize=(8, 6))
    ax.axhline(y=0, color="k")
    ax.axvline(x=K+C, color="r", linestyle="--")
    ax.set(xlabel="Spot price", ylabel="Profit")
    sns.lineplot(x=S_t, y=call(K, S_t, C), ax=ax).set_title("Call")

Puts

  • Give the bearer the right to sell the underlying asset at a given date for an agreed upon price.
  • Can be thought of as an insurance against asset price decreases.

    $$\text{Payoff} \equiv max(K - S_t, 0)$$
    $$\text{Profit} \equiv max(K - S_t, 0) - P(K, t, t_0)$$
    Where $S_t$ is the price of the asset at the day of expiration $t$ and $P(K, t, t_0)$ is the premium payed for the put purchased at time $t_0$ with strike price $K$.
In [11]:
def put(K, S_t, P):
    return np.maximum(K - S_t, 0) - P
In [12]:
P = 5
K = 200
S_t = np.linspace(180, 210, num=100)

with sns.axes_style("white"):
    _fig, ax = plt.subplots(figsize=(8, 6))
    ax.axhline(y=0, color="k")
    ax.axvline(x=K-P, color="r", linestyle="--")
    ax.set(xlabel="Spot price", ylabel="Profit")
    sns.lineplot(x=S_t, y=put(K, S_t, P), ax=ax).set_title("Put")

Selling options

  • A trader can choose to sell options, also referred as writing options.
  • Investor earns the option premium and must settle at expiration date or cover the position by buying the contract.

Selling calls

  • Also known as naked call selling
  • Exposed to price increases of the underlying. Unlimited risk.

    $$\text{Profit} \equiv C(K, t, t_0) - max(S_t - K, 0)$$
In [13]:
def sell_call(K, S_t, C):
    return -call(K, S_t, C)
In [14]:
C = 5
K = 200
S_t = np.linspace(190, 220, num=100)

with sns.axes_style("white"):
    _fig, ax = plt.subplots(figsize=(8, 6))
    ax.axhline(y=0, color="k")
    ax.axvline(x=C+K, color="r", linestyle="--")
    ax.set(xlabel="Spot price", ylabel="Profit")
    sns.lineplot(x=S_t, y=sell_call(K, S_t, C), ax=ax).set_title("Call Sell")

Selling puts

  • Less risky than selling calls.
  • Exposed to price decreases of the underlying.
  • Max loss capped at $K - P$.

    $$\text{Profit} \equiv P(K, t, t_0) - max(K - S_t, 0)$$
In [15]:
def sell_put(K, S_t, C):
    return -put(K, S_t, C)
In [16]:
P = 5
K = 200
S_t = np.linspace(180, 210, num=100)

with sns.axes_style("white"):
    _fig, ax = plt.subplots(figsize=(8, 6))
    ax.axhline(y=0, color="k")
    ax.axvline(x=K-P, color="r", linestyle="--")
    ax.set(xlabel="Spot price", ylabel="Profit")
    sns.lineplot(x=S_t, y=sell_put(K, S_t, P), ax=ax).set_title("Put Sell")
In [17]:
options_data = pd.read_hdf("../data/options_data_v2.h5", key="/SPX", where="quotedate>2017 & quotedate<2018")
In [18]:
options_data.head()
Out[18]:
underlying underlying_last exchange optionroot optionext type expiration quotedate strike last bid ask volume openinterest impliedvol delta gamma theta vega optionalias
13328880 SPX 2257.83 * SPX170120C00100000 NaN call 2017-01-20 2017-01-03 100.0 2161.50 2154.3 2158.7 0 5615 0.1214 1.0 0.0 -1.0239 0.0 SPX170120C00100000
13328881 SPX 2257.83 * SPX170120C00200000 NaN call 2017-01-20 2017-01-03 200.0 2060.95 2054.4 2058.8 0 6355 0.1214 1.0 0.0 -2.0479 0.0 SPX170120C00200000
13328882 SPX 2257.83 * SPX170120C00300000 NaN call 2017-01-20 2017-01-03 300.0 1960.45 1954.4 1958.8 0 2000 0.1214 1.0 0.0 -3.0718 0.0 SPX170120C00300000
13328883 SPX 2257.83 * SPX170120C00400000 NaN call 2017-01-20 2017-01-03 400.0 1760.55 1854.6 1858.9 0 25 0.1214 1.0 0.0 -4.0957 0.0 SPX170120C00400000
13328884 SPX 2257.83 * SPX170120C00500000 NaN call 2017-01-20 2017-01-03 500.0 1749.33 1754.6 1758.9 0 3813 0.1214 1.0 0.0 -5.1197 0.0 SPX170120C00500000
  • optionroot is the name of the contract (think id)
  • underlying_last is the last seen price for the underlying
  • type either call or put
  • strike is the strike price
  • bid price offered by the market maker for the contract
  • ask price wanted by the market maker for the contract
  • volume the number of contracts traded that day
  • openinterest the total number of contracts open since it started trading
In [19]:
contracts = options_data.groupby("quotedate").size()
_fig, ax = plt.subplots(figsize=(16,8))
ax.set_xlim([pd.Timestamp(2017, 1, 1), pd.Timestamp(2017, 12, 31)])
sns.scatterplot(x=contracts.index, y=contracts, ax=ax).set_title("Number of option contracts")
Out[19]:
Text(0.5, 1.0, 'Number of option contracts')