## Code snippets: Stata

Category : Uncategorized

# Code snippets: Stata

### Learning Stata

- Working with Stata #1 (variable, observation, gen, replace, directory, save, help)
- Working with Stata #2 (constant, foreach loop, local, variable, display)
- Working with Stata #3 (Download the historical prices for IBM)
- Working with Stata #4 (Download the historical prices for a symbol)
- Working with Stata #5 (Download the historical prices for a list of symbols)
- Working with Stata #6 (Merging downloaded historical prices for list of symbols)
- Working with Stata #7 (Creating a portfolio and descriptive statistics)
- Working with Stata #8 (Return difference of two portfolios)
- Working with Stata #9 (Download the key statistics for IBM)
- Working with Stata #10 (Download the key statistics for list of symbols)
- Working with Stata #11 (Appending downloaded key statistics for list of symbols)
- tabstat and outsheet
- collapse
- fetchyahooquotes and pwcorr: Download historical prices from Yahoo! Finance
- fetchyahookeystats: Download key statistics from Yahoo! Finance
- components: Download components (list of symbols) of indices
- components_etf: Download list of ETFs that are available on Yahoo! Finance
- pwcorr: Correlation matrix for list of symbols

### Intermediate Stata code related to finance

- Download portfolio statistics for all ETFs on Yahoo! Finance
- Download key statistics for components of an index
- Calculate implied volatility for a stock using all options written on it
- Simple t-test of (average daily return = 0)
- Test whether a daily return came from a sample of daily returns (95% confidence interval)
- Moving average of price and intraday volatility

### Stata code for mapping symbol, suffix, prefix to name, country, exchange

### CAPM

- Daily risk-free rate
- Estimate CAPM (single factor, single security)
- Estimate CAPM (single factor, multiple securities)
- Estimate CAPM (multi factor, single security)

### Derivatives

- Add a Put option into a portfolio
- Add a Call option into a portfolio
- Add both a Put and a Call options into a portfolio
- Option outcomes (long call, short call, long put, short put)

### Derivatives: Option Trading Strategies

- Bull spread using calls
- Bull spread using puts
- Bear spread using puts
- Bear spread using calls
- Box spread
- Butterfly spread using calls
- Butterfly spread using puts
- Straddle
- Strip
- Strap
- Strangle

### Derivatives: Option Trading Strategies Using Real Data

- Bull spread using calls
- Bull spread using calls (using the entire data)
- Bull spread using puts
- Bull spread using puts (using the entire data)
- Bear spread using puts
- Bear spread using calls
- Box spread
- Calendar spread
- Bear Calendar spread
- Butterfly spread using calls
- Butterfly spread using puts
- Straddle
- Strip
- Strap
- Strangle

### Testing…

- Monday and Friday effect in daily returns
- Autoregressive effect in daily returns
- Causal effect in daily returns
- Are returns for dividend paying companies higher?
- Are returns for dividend paying companies higher? (Bigger sample with a loop)
- Can intraday volatility predict stock returns / option premium returns?
- Which strike price is more sensitive to price changes?
- Which strike price is more sensitive to price changes? (using the entire data)
- What is the distribution of the liquidity across strike prices?
- Causality between implied volatility, intraday volatility and returns

### Simple event tests

### Research…

- Download ‘Penn World Table’
- Standardize country names and limit the sample to two countries
- Convert sample ‘Penn World Table’ into dyadic sample dataset for real GDP per capita
- Graph for the annual real GDP per capita for each country and their comparative graph
- Graph for the difference in annual real GDP per capita between two countries
- Regression of difference in annual real GDP per capita between two countries on time trend
- Evaluating whether common religion explains the convergence partners of a country