Design a site like this with WordPress.com
Get started

How to download S&P 500 data from Yahoo Finance using Python

Make sure you have the yfinance package installed first. If you don’t, you can run the following command in your Jupyter notebook:

!pip install yfinance

Input:

import pandas as pd
import numpy as np
import yfinance as yf

Input:

spy_ohlc_df = yf.download('SPY', start='1993-02-01', end='2019-12-01')

Output:

[*********************100%***********************]  1 of 1 downloaded

Input:

spy_ohlc_df.head()

Output:

spy 12-1-19

Conclusion

This is a very brief summary of how you can download stock data information for the S&P 500 from Yahoo Finance, using the Python programming language. If there is something you want to learn about, please let me know in the comments below and I can cover it in a future blog post.

Advertisement

When I stopped trying to be right, I became a better trader

I began my journey trading stock options in September of 2018. When I entered the trading arena, market volatility was picking up. This was the environment I traded and adapted to over the three months from October to December of last year, 2018.

During this time period I learned to trade options in an extremely volatile environment. I was able to successfully gain over ten thousand dollars on my small account in December by being short on the market.

I primarily bought put options on bank stocks and other stocks that were experiences drastic declines at the time. My portfolio saw outsized gains which I had experienced up until that point. Enjoying the outsized (mostly unrealized) profits that I reaped up to Christmas Eve, I continued to buy put options on the market.

This turned out to be disastrous.

The subsequent decline

All through January until April, I had the thought that the market shouldn’t be going higher. I absolutely knew what was going to happen next. Or so I thought…

At the time, I was following Fintech Twitter. I wanted to know what was “happening” in the market, and took to Twitter to see what people were saying.

90% of the people on there seem to be bearish during the first quarter of this year. This influenced my investing and my perspective of the market.

The illusion of being a good trader

It took a while, but at some point I realized that being right, or the illusion of being right, doesn’t put money in the bank account.

I called it!

On Twitter it’s easy for people to create the illusion that they called many good trades. It’s easy to do. All you have to do is post a bunch of charts calling bullish or bearish, put them on Twitter, let the market play out, and then delete your tweets that were wrong. Then you can easily pin your tweets where you “called” the market top or Market bottom.

I got caught up in the Twitter atmosphere and my trading suffered. Being right doesn’t matter.

Hedging instead of guessing

In recent weeks I’ve developed better hedges on my trades.

I like to build up large put options positions at times when I believe the market could go down over the next few months.

In the first quarter of this year, I built up put option positions, and lost a significant amount of the value that I put into those positions. It’s only after reflecting and experiencing the pain of those few months then I realize how important it is to hedge my trades.

I was concerned with effectively predicting what was going to happen in the market. I wanted to be right, and I would only enter trades on one side of the mark get out of time. This was stupid. I lost out on a ton of games I could have gotten all throughout January and February and even into March and April.

Current endeavors

I’m currently building up put option positions on SPY and IWM.  I’m hedging the position by buying shorter-dated call options, debit spreads actually, and then collecting profits when the underlying stock price land somewhere in between the two strike prices of my debit spread. This has been a much more effective strategy and has kept my portfolio more even overtime while I continue to build up a larger put option positions.

Ideally, call options on the S&P 500 and the Russell 2000 Index will continue to gain value in excess of the value that I lose on those longer-dated put options while the market continue to rip higher. Then, when the market does turn, I’ll have built up a decent size put option position to benefit from larger price declines.

Conclusion

I believed I had to try to predict the market and be correct. Instead I have to be flexible, I have to adapt, and I have to head to effectively on my positions.