49.1-Point-And-Figure-Chart
Sat 17 May 2025
# Created: 20250104
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml312-2024; pyv: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:27:36) [GCC 11.2.0]'
print(pyu.ps2("yfinance pandas matplotlib"))
yfinance==0.2.51
pandas==2.2.3
matplotlib==3.9.3
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
# Step 1: Download data
symbol = "^GSPC"
data = yf.download(symbol, start="2020-01-01", end="2023-12-31")
# Step 2: Define parameters
box_size = 50 # Set the box size
reversal_amount = 3 # Reversal occurs after 3 box sizes
# Step 3: Prepare data for Point & Figure Chart
pf_data = data[['Close']].copy()
pf_data['Change'] = pf_data['Close'].diff()
# Calculate uptrend and downtrend based on box size and reversal amount
trend = []
current_trend = None
current_price = pf_data['Close'].iloc[0]
for price in pf_data['Close']:
if current_trend is None:
current_trend = 'X' if price > current_price else 'O'
elif current_trend == 'X' and price < current_price - (box_size * reversal_amount):
current_trend = 'O'
elif current_trend == 'O' and price > current_price + (box_size * reversal_amount):
current_trend = 'X'
current_price = price
trend.append(current_trend)
pf_data['Trend'] = trend
# Step 4: Plot Point & Figure Chart
plt.figure(figsize=(14, 7))
plt.scatter(pf_data.index, pf_data['Close'], c=pf_data['Trend'].map({'X': 'green', 'O': 'red'}), label='Point & Figure')
plt.title(f'{symbol} Point & Figure Chart (Box Size: {box_size}, Reversal: {reversal_amount})')
plt.xlabel('Date')
plt.ylabel('Price')
plt.grid(True)
plt.tight_layout()
plt.show()
[*********************100%***********************] 1 of 1 completed
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 23
21 for price in pf_data['Close']:
22 if current_trend is None:
---> 23 current_trend = 'X' if price > current_price else 'O'
24 elif current_trend == 'X' and price < current_price - (box_size * reversal_amount):
25 current_trend = 'O'
File ~/miniconda3/envs/ml312-2024/lib/python3.12/site-packages/pandas/core/ops/common.py:76, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
72 return NotImplemented
74 other = item_from_zerodim(other)
---> 76 return method(self, other)
File ~/miniconda3/envs/ml312-2024/lib/python3.12/site-packages/pandas/core/arraylike.py:48, in OpsMixin.__lt__(self, other)
46 @unpack_zerodim_and_defer("__lt__")
47 def __lt__(self, other):
---> 48 return self._cmp_method(other, operator.lt)
File ~/miniconda3/envs/ml312-2024/lib/python3.12/site-packages/pandas/core/series.py:6119, in Series._cmp_method(self, other, op)
6116 lvalues = self._values
6117 rvalues = extract_array(other, extract_numpy=True, extract_range=True)
-> 6119 res_values = ops.comparison_op(lvalues, rvalues, op)
6121 return self._construct_result(res_values, name=res_name)
File ~/miniconda3/envs/ml312-2024/lib/python3.12/site-packages/pandas/core/ops/array_ops.py:341, in comparison_op(left, right, op)
337 res_values = np.zeros(lvalues.shape, dtype=bool)
339 elif is_numeric_v_string_like(lvalues, rvalues):
340 # GH#36377 going through the numexpr path would incorrectly raise
--> 341 return invalid_comparison(lvalues, rvalues, op)
343 elif lvalues.dtype == object or isinstance(rvalues, str):
344 res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)
File ~/miniconda3/envs/ml312-2024/lib/python3.12/site-packages/pandas/core/ops/invalid.py:40, in invalid_comparison(left, right, op)
38 else:
39 typ = type(right).__name__
---> 40 raise TypeError(f"Invalid comparison between dtype={left.dtype} and {typ}")
41 return res_values
TypeError: Invalid comparison between dtype=float64 and str
def show_graph(symbol):
pass
show_graph("AMZN")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[5], line 1
----> 1 show_graph("AMZN")
NameError: name 'show_graph' is not defined
Score: 5
Category: stockmarket