forked from ANGELTALAVERA/Finance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_highs_new_lows.py
109 lines (89 loc) · 3.31 KB
/
new_highs_new_lows.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Import dependencies
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf
import datetime as dt
yf.pdr_override()
# input
symbol = "SPY"
start = dt.date.today() - dt.timedelta(days=365 * 7)
end = dt.date.today()
# Read data
df = yf.download(symbol, start, end)
new_high = df["Adj Close"].rolling(52).max() # 52-week lows
new_low = df["Adj Close"].rolling(52).min() # 52-week highs
print("Yesterday's Value:", df["Adj Close"][-2]) # Yesterday's Value
print("Current Value:", df["Adj Close"][-1]) # Current's Value
new_high = new_high.dropna()
new_low = new_low.dropna()
# Record_High_Percent = (new_high /(new_high + new_low)) * 100
# nhnl = new_high - new_low
# 1. Cumulative New High/Low Line
# Today's Value = Yesterday's Value + (Today's New Highs - Today's New Lows)
df["CNHL"] = df["Adj Close"][1] + (new_high - new_low)
# 2. New-High Minus New-Low Oscillator
# Oscillator = Today\'s New Highs – Today\'s New Lows
df["Oscillator"] = new_high - new_low
# 3. New High/Low Ratio
# Ratio = Today\'s New Highs / Today\'s New Lows
df["Ratio"] = new_high / new_low
# 4. Percentage of New-High to New High + New Low
# % New Highs = Today\'s New Highs / (Today\'s New Highs + Today\'s New Lows)
# % New Lows = Today\'s New Lows / (Today\'s New Highs + Today\'s New Lows)
df["NH"] = new_high / (new_high + new_low)
df["NL"] = new_high / (new_high + new_low)
# 5. Percentage of New Highs to Total Market
# % New Highs = Today\'s New Highs / Total # of Listed Stocks in Given Market
# % New Lows = Today\'s New Lows / Total # of Listed Stocks in Given Market
df["NHTM"] = new_high / 5 # Number of stocks
df["NLTM"] = new_low / 5 # Number of stocks
df = df.dropna()
fig = plt.figure(figsize=(14, 14))
ax1 = plt.subplot(3, 1, 1)
ax1.plot(df["Adj Close"])
ax1.set_title("Stock " + symbol + " Closing Price")
ax1.set_ylabel("Price")
ax2 = plt.subplot(3, 1, 2)
ax2.plot(df["CNHL"], label="Cumulative New High/Low Line")
# ax2.axhline(y=0, color='red')
ax2.set_ylabel("Cumulative New High/Low Line")
ax2.grid()
ax3 = plt.subplot(3, 1, 3)
ax3.plot(df["Oscillator"], label="Oscillator")
# ax3.axhline(y=50, color='red')
ax3.set_ylabel("Oscillator")
ax3.set_xlabel("Date")
ax3.grid()
plt.show()
# ## Candlestick with New Highs/New Lows
from matplotlib import dates as mdates
df["VolumePositive"] = df["Open"] < df["Adj Close"]
df = df.dropna()
df = df.reset_index()
df["Date"] = mdates.date2num(df["Date"].tolist())
from mplfinance.original_flavor import candlestick_ohlc
fig = plt.figure(figsize=(16, 8))
ax1 = plt.subplot(3, 1, 1)
candlestick_ohlc(ax1, df.values, width=0.5, colorup="g", colordown="r", alpha=1.0)
ax1.xaxis_date()
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%d-%m-%Y"))
ax1v = ax1.twinx()
colors = df.VolumePositive.map({True: "g", False: "r"})
ax1v.bar(df.Date, df["Volume"], color=colors, alpha=0.4)
ax1v.axes.yaxis.set_ticklabels([])
ax1v.set_ylim(0, 3 * df.Volume.max())
ax1.set_title("Stock " + symbol + " Closing Price")
ax1.set_ylabel("Price")
ax2 = plt.subplot(3, 1, 2)
ax2.plot(df["CNHL"], label="Cumulative New High/Low Line")
# ax2.axhline(y=0, color='red')
ax2.grid()
ax2.set_ylabel("Cumulative New High/Low Line")
ax3 = plt.subplot(3, 1, 3)
ax3.plot(df["Oscillator"], label="Oscillator")
# ax3.axhline(y=50, color='red')
ax3.grid()
ax3.set_ylabel("Oscillator")
ax3.set_xlabel("Date")
plt.show()