forked from dannasman/point_and_figure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_and_figure.py
191 lines (161 loc) · 7 KB
/
point_and_figure.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import yfinance as yf
import pandas as pd
import math
class PointAndFigure:
def __init__(self, step, ticker, startDate, endDate=None):
self.step = step
self.ticker = ticker
self.startDate = startDate
self.endDate = None
self.instrument = yf.Ticker(ticker)
self.months = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C']
def floor_to_nearest(self, price):
return round(math.floor(price / self.step) * self.step, 2)
def get_month_index(self, date):
month = str(date).split("-")[1]
match month:
case '01':
return 0
case '02':
return 1
case '03':
return 2
case '04':
return 3
case '05':
return 4
case '06':
return 5
case '07':
return 6
case '08':
return 7
case '09':
return 8
case '10':
return 9
case '11':
return 10
case '12':
return 11
def chart(self):
hist = self.instrument.history(start=self.startDate, end=self.endDate)
df = pd.DataFrame({'Date':hist['Close'].index, 'Close': hist['Close'].values})
close_prices = df['Close']
dates = df['Date']
# Getting the index of the last row
last_close_index = df.index[-1]
last_close = df['Close'].index[-1]
current = self.floor_to_nearest(close_prices[0])
min_price = self.floor_to_nearest(min(close_prices))
max_price = self.floor_to_nearest(max(close_prices))
height = round((max_price-min_price)/self.step)+1
width = 100
grid = [[' ' for _ in range(width)] for _ in range(height)]
#set starting position to price of the start date
row = round((max_price-current)/self.step)
col = 0
#these values are later used when chart is printed
startRow = row
b = current
th = 3 #threshold for changing trend direction
monthIndex = self.get_month_index(dates[0]) #get starting month
grid[row][col] = self.months[monthIndex] #set the mark of starting position to starting month
trend = 0 #trend is zero at the beginning
#calculate chart for rest of the prices
dateIndex = 1
record_count = 0 # used to count to the last_close_index
latest_close = False
newMonth = False
for close_price in close_prices[1:]:
oldMonthIndex = monthIndex
monthIndex = self.get_month_index(dates[dateIndex])
if record_count == last_close_index - 1:
latest_close = True
if oldMonthIndex != monthIndex:
newMonth = True
price_rounded = self.floor_to_nearest(close_price)
if trend == 1:
if price_rounded > current:
for i in range(0, round((price_rounded-current)/self.step)):
if newMonth:
grid[row-i-1][col] = self.months[monthIndex]
newMonth = False
else:
grid[row-i-1][col] = 'X'
row -= round((price_rounded-current)/self.step)
current = price_rounded
elif price_rounded <= current - th*self.step:
col += 1
for i in range(0, round((current-price_rounded)/self.step)):
if newMonth:
grid[row+i+1][col] = self.months[monthIndex]
newMonth = False
else:
grid[row+i+1][col] = 'O'
row -= round((price_rounded-current)/self.step)
current = price_rounded
trend = -1
elif trend == -1:
if price_rounded < current:
for i in range(0, round((current-price_rounded)/self.step)):
if newMonth:
grid[row+i+1][col] = self.months[monthIndex]
newMonth = False
else:
grid[row+i+1][col] = 'O'
row -= round((price_rounded-current)/self.step)
current = price_rounded
elif price_rounded >= current + th*self.step:
col += 1
for i in range(0, round((price_rounded-current)/self.step)):
if newMonth:
grid[row-i-1][col] = self.months[monthIndex]
newMonth = False
else:
grid[row-i-1][col] = 'X'
row -= round((price_rounded-current)/self.step)
current = price_rounded
trend = 1
else:
if price_rounded >= current + th*self.step:
for i in range(0, round((price_rounded-current)/self.step)):
if newMonth:
grid[row-i-1][col] = self.months[monthIndex]
newMonth = False
else:
grid[row-i-1][col] = 'X'
row -= round((price_rounded-current)/self.step)
current = price_rounded
trend = 1
elif price_rounded <= current - th*self.step:
col += 1
for i in range(0, round((current-price_rounded)/self.step)):
if newMonth:
grid[row+i+1][col] = self.months[monthIndex]
newMonth = False
else:
grid[row+i+1][col] = 'O'
row -= round((price_rounded-current)/self.step)
current = price_rounded
trend = -1
# Mark the latest closing price on the grid
if latest_close:
grid[row][col] = '$$'
record_count += 1
dateIndex += 1
# Change the Colors of the X's and O's and print the chart
chart = ""
for i in range(height):
chart += "{:>4.2f} ".format((startRow - i) * self.step + b)
for j in range(width):
if grid[i][j] == 'O':
chart += "\033[31mO\033[0m" # Red 'O'
elif grid[i][j] == 'X':
chart += "\033[32mX\033[0m" # Green 'X'
else:
chart += grid[i][j]
# Add space between columns (adjust the number of spaces as needed)
chart += " " * 1 # Adding 1 space between columns
chart += "\n"
return chart