### My New Udemy Class: Fundamental Market Analysis with Python

Fundamental Market Analysis with Python - Find Your Own Answers On What Is Going on in the Financial Markets

### Hot off the Press!

My New Book: "The Little Book of Fundamental Analysis: Hands-On Market Analysis with Python" is Out!

### Grow Your Web Brand, Visibility & Traffic Organically

5 Years of amunategui.github.Io and the Lessons I Learned from Growing My Online Community from the Ground Up

### Introduction

Lets get back to the basics of market analysis and quantify how many times the markets go up, down and for how much. Its not always intuitive, and whether you are a trader or not, you need to know this. Casino math: https://gaming.unlv.edu/casinomath.html

### Code

ViralML-How-Often-Do-Markets-Go-Up
In [218]:
from IPython.display import Image
Image(filename='viralml-book.png')

Out[218]:

# Fundamental and Technical Indicators - Hands-On Market Analysis¶

Get the book "The Little Book of Fundamental Market Indicators":

https://amzn.to/2DERG3d

More at:

https://www.viralml.com/

### So, How Often Does the Market Go Up?¶

https://finance.yahoo.com/quote/%5EGSPC/

Download the Russell 2000 (^RUT) - The Russell 2000 index is an index measuring the performance of approximately 2,000 smallest-cap American companies in the Russell 3000:

https://finance.yahoo.com/quote/%5ERUT/history?p=^RUT

In [180]:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import io, base64, os, json, re
import pandas as pd
import numpy as np
import datetime
import warnings
warnings.filterwarnings('ignore')


In [182]:
sp500_df = pd.read_csv('^GSPC.csv')
sp500_df['Date'] = pd.to_datetime(sp500_df['Date'])
sp500_df.columns = ['Date', 'SP500_close']
sp500_df.tail()

Out[182]:
Date SP500_close
17463 2019-05-30 2788.860107
17464 2019-05-31 2752.060059
17465 2019-06-03 2744.449951
17466 2019-06-04 2803.270020
17467 2019-06-05 2826.149902
In [183]:
sp500_df.head()

Out[183]:
Date SP500_close
0 1950-01-03 16.66
1 1950-01-04 16.85
2 1950-01-05 16.93
3 1950-01-06 16.98
4 1950-01-09 17.08
In [184]:
rus2000_df = pd.read_csv('^RUT.csv')
rus2000_df['Date'] = pd.to_datetime(rus2000_df['Date'])
rus2000_df.columns = ['Date', 'RUS2000_close']
rus2000_df.tail()

Out[184]:
Date RUS2000_close
7993 2019-05-30 1485.530029
7994 2019-05-31 1465.489990
7995 2019-06-03 1469.979980
7996 2019-06-04 1508.560059
7997 2019-06-05 1506.790039
In [185]:
rus2000_df.head()

Out[185]:
Date RUS2000_close
0 1987-09-10 168.970001
1 1987-09-11 170.539993
2 1987-09-14 170.429993
3 1987-09-15 169.199997
4 1987-09-16 168.919998

## Simple Visualization using the Adjusted Close¶

In [186]:
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(sp500_df['Date'], sp500_df['SP500_close'], color='blue')
plt.title('S&P 500')
plt.grid()
plt.show()

In [187]:
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(rus2000_df['Date'], rus2000_df['RUS2000_close'], color='blue')
plt.title('Russell 2000')
plt.grid()


## Let's count up days¶

In [189]:

sp500_df['Previous_Close'] = sp500_df['SP500_close'].shift(1)

Out[189]:
Date SP500_close Previous_Close
0 1950-01-03 16.66 NaN
1 1950-01-04 16.85 16.66
2 1950-01-05 16.93 16.85
3 1950-01-06 16.98 16.93
4 1950-01-09 17.08 16.98
In [190]:
# drop NAs
sp500_df = sp500_df.dropna(axis=0)

Out[190]:
Date SP500_close Previous_Close
1 1950-01-04 16.850000 16.66
2 1950-01-05 16.930000 16.85
3 1950-01-06 16.980000 16.93
4 1950-01-09 17.080000 16.98
5 1950-01-10 17.030001 17.08
In [191]:
sp500_df['Price_Diff'] = sp500_df['SP500_close'] - sp500_df['Previous_Close']
sp500_df['Is_Up_Day'] = np.where(sp500_df['SP500_close'] > sp500_df['Previous_Close'], 1, 0)

Out[191]:
Date SP500_close Previous_Close Price_Diff Is_Up_Day
1 1950-01-04 16.850000 16.66 0.190000 1
2 1950-01-05 16.930000 16.85 0.080000 1
3 1950-01-06 16.980000 16.93 0.050000 1
4 1950-01-09 17.080000 16.98 0.100000 1
5 1950-01-10 17.030001 17.08 -0.049999 0
In [192]:
print('Up days:', np.sum(sp500_df['Is_Up_Day']), 'out of:', len(sp500_df))

Up days: 9253 out of: 17467

In [64]:
print(np.sum(sp500_df['Is_Up_Day']==1) / len(sp500_df))

0.5297417988206332

In [65]:
print(np.sum(sp500_df['Is_Up_Day']==0) / len(sp500_df))

0.4702582011793668


## Track up versus down dollar moves¶

In [193]:
np.sum(sp500_df[sp500_df['Is_Up_Day']== 1]['Price_Diff'])

Out[193]:
37646.364575
In [70]:
np.sum(sp500_df[sp500_df['Is_Up_Day']== 0]['Price_Diff'])

Out[70]:
-34836.874673
In [102]:
# let's check our math
37646.364575 -  34836.874673

Out[102]:
2809.489902000001

## Let's create our own simulation¶

### Casino Mathematics¶

https://gaming.unlv.edu/casinomath.html

In [214]:
from random import randint

up_down_days = []
for x in range(0,1000000):
up_down_days.append(1 if randint(1, 100) <= 52 else -1)

up_down_days[0:10]

Out[214]:
[-1, 1, 1, -1, -1, 1, -1, -1, 1, -1]
In [215]:
np.sum(up_down_days)

Out[215]:
39840
In [216]:
np.array(up_down_days).cumsum()

Out[216]:
array([   -1,     0,     1, ..., 39838, 39839, 39840])
In [217]:
plt.plot(np.array(up_down_days).cumsum() )

Out[217]:
[<matplotlib.lines.Line2D at 0x13b47c710>]

### Show Notes

(pardon typos and formatting -
these are the notes I use to make the videos)