定义计算KDJ指标的函数
def calculate_kdj(data, n=19, m1=3, m2=3):
# 设置窗口周期
window = 9
code Copy code
# 计算Low_min和High_max
data['Low_min'] = data['Low'].rolling(window=window, min_periods=1).min()
data['High_max'] = data['High'].rolling(window=window, min_periods=1).max()
# 计算RSV
data['RSV'] = (data['Close'] - data['Low_min']) / (data['High_max'] - data['Low_min']) * 100
# 初始化K和D
data['K'] = 50
data['D'] = 50
for i in range(1, len(data)):
data.loc[i, 'K'] = 2 / 3 * data.loc[i - 1, 'K'] + 1 / 3 * data.loc[i, 'RSV']
data.loc[i, 'D'] = 2 / 3 * data.loc[i - 1, 'D'] + 1 / 3 * data.loc[i, 'K']
# 计算J
data['J'] = 3 * data['K'] - 2 * data['D']
# 打印结果
print(data[['Datetime', 'K', 'D', 'J']])
kdj_k = data['K']
kdj_d = data['D']
kdj_j = data['J']
return kdj_k, kdj_d, kdj_j
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 406, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in call
return await self.app(scope, receive, send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/applications.py", line 1054, in call
await super().call (scope, receive, send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/applications.py", line 113, in call
await self.middleware_stack(scope, receive, send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 187, in call
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 165, in call
await self.app(scope, receive, _send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 62, in call
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/_exception_handler.py", line 62, in wrapped_app
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/_exception_handler.py", line 51, in wrapped_app
await app(scope, receive, sender)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 715, in
await self.middleware_stack(scope, receive, send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 735, in app
await route.handle(scope, receive, send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 288, in handle
await self.app(scope, receive, send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 76, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/_exception_handler.py", line 62, in wrapped_app
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/_exception_handler.py", line 51, in wrapped_app
await app(scope, receive, sender)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 73, in app
response = await f(request)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/routing.py", line 301, in app
raw_response = await run_endpoint_function(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/routing.py", line 212, in run_endpoint_function
return await dependant.call(**values)
File "/Users/weene/develop/Python/smalltools/stock_alarm.py", line 298, in plot_stock_chart
img_base64 = plot_candlestick_with_fibonacci(data, input_date)
File "/Users/weene/develop/Python/smalltools/stock_alarm.py", line 247, in plot_candlestick_with_fibonacci
kdj_k, kdj_d, kdj_j = calculate_kdj(data)
File "/Users/weene/develop/Python/smalltools/stock_alarm.py", line 56, in calculate_kdj
data.loc[i, 'K'] = 2 / 3 * data.loc[i - 1, 'K'] + 1 / 3 * data.loc[i, 'RSV']
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexing.py", line 1183, in
return self.obj._get_value(*key, takeable=self._takeable)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py", line 4221, in _get_value
row = self.index.get_loc(index)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexes/datetimes.py", line 627, in get_loc
raise KeyError(key)
KeyError: 0