This project is designed to analyze stock data, identify golden cross patterns, MACD data, K line type, etc.
Further checks are made to filter stocks in KDJ daily golden cross and KDJ daily death to operation 1.1 and operation 2 for final manual check.
The analysis is outputed in output folder. Ecah folder includes:
An Excel file:
- Stock code
- Stock name
- Dates of golden crosses in the past week
- Total number of stocks with golden crosses
And the related png files:
- each picture is grouped by at most 50 stock codes
- for user to load into their stock softwares
The project is implemented in Python and utilizes libraries including akshare, pandas, matplot and openpyxl.
Lib yfinance is no longer used due to its unstability.
- Golden Cross Detection: Identify daily and weekly golden cross patterns in stock data.
- MACD Monthly Analysis: Filter stocks that experienced a MACD monthly golden cross in current month and its DIF is above 0.
- Excel Report Generation: Output the results to an Excel file with a summary of the total number of stocks meeting the criteria.
- Error Handling for Data Retrieval: Automatically retries data download if an error occurs.
- Batch handling: Take 100 stock code as a batch. Once one batch finishes, it will be printed on screen.
- Garbage Collection: Within every batch, memory used will be printed on screeen.
Ensure you have the following installed:
- Python 3.8+
- Required libraries:
matplotaksharepandasnumpy
You can install the required libraries using:
pip install matplot
pip install akshare
pip install pandas- Input Files:
- Every required file is stored in
txt_libfolder.- Yet, the stock name is not updated and some may even have been delisted. Please check out and you could update that by editing the stock_name.txt.
- Every required file is stored in
The analysis script can be scheduled to run daily at 8:00 AM (e.g., using Windows Task Scheduler). The main script to execute is kdj_整合.ipynb or its converted Python script.
-
Convert the Jupyter Notebook to a Python script if necessary:
jupyter nbconvert --to script kdj_整合.ipynb jupyter nbconvert --to script kdj_分类筛选.ipynb
Or, you could use the export function in Vscode to get a py file from
kdj_整合.ipynb. -
Execute the script.
- The script generates an
outputfolder:- KDJ日金
- KDJ日死
- 操作1.1
- 操作2
- KDJ8
- death_output
- golden_output
- month_macd
-
日金叉
-
周金叉
-
今日股价上涨
-
日死叉
-
周金叉
-
今日股价上涨
- 今日金叉
- 今日死叉
-
今日金叉
-
MACD
-
DIF刚刚>0 == DIF 今日大于0, 昨日小于0
-
上零线
-
今日不能碰上轨
-
KDJ日金(今日数据)条件也要满足
-
-
-
KDJ日金(今日数据)
-
abs((今日最低价 - 中轨) / (上轨 - 中轨)) < 12%
- abs((max(开盘价,收盘价) - 上轨) / (上轨 - 中轨)) > 50%
or
abs((今日最低价 - 下轨) / (中轨 - 下轨)) <12%
- abs((max(开盘价,收盘价) - 中轨) / (中轨 - 下轨)) > 50%
-
周kdj金叉2周内
-
DIF > 0
-
最高价 < 上轨
-
- KDJ日金(昨日数据)
-
J线拐头
今日J值小于昨日J值
今日J值大于今日K值
-
-
KDJ日死(昨日数据)
-
布林线
上次碰轨事件——碰上轨
今日碰下轨
-
接近金叉
昨日D线K线差值 大于 今日D线K线差值
DIF > 0
MACD > 0
-
-
KDJ日死(今日数据)
-
阴线
今日是第二根阴线
日MACD > 0
'第三日'创最近十日新高
最近两天不能碰中轨
-
-
KDJ日金(昨日数据)
-
周死叉
-
macd < 0
-
当日处于死叉阶段
-
-
KDJ日死(昨日数据)
-
周死叉
-
当日处于金叉阶段
-
- 最近30天内会分红
- 周KDJ金叉
-
KDJ class
-
calculate_kdj(df: DataFrame, n=9)- Calculates the daily KDJ.
- window = n = 9 is set according to the normal calculation
-
find_kdj_golden_cross- Calculate golden cross, death cross and rise/ fall.
-
get_recent_golden_cross_dates(stock_code)- Retrieves golden cross dates of a given stock and the date is within this week.
-
-
MACD class
calculate_macd(df: DataFrame, short=12, long=26, signal=9)- Calculates the daily MACD.
- short=12, long=26, signal=9 are all set according to the normal calculation
-
Bol class
calculate_bollinger_bands- calculate the exacat upper, mid and lower Bollinger band
- the lower band is ¥0.03 upper due to special needs
-
Mix class
- functions within this class are mixture usage of upper classes, which are highly customized
get_k_line_type- determine today's K line type (negative/ positive)
- Data Retrieval: Implements retry logic for downloading stock data using
akshareto handle occasionalJSONDecodeErroror other network issues. - Fallback Values: Ensures the analysis continues even if some stocks have incomplete data.
To schedule the script to run every weekday at 8:00 AM:
- Open Windows Task Scheduler.
- Create a new task:
- Set the trigger to daily at 4:00 PM.
- Set the action to run the Python script (
kdj_金叉+死叉_ak.py). - Ensure the correct Python interpreter is used.
- Save and enable the task.
This project is licensed under the MIT License. See LICENSE for details.