A well-tuned algorithm to generate & draw support/resistance line on time series. 根据时间序列自动生成支撑线压力线
Project description
Support Resistance Line
Installation
You can install from pypi using pip install support_resistance_line, or install from source using python3 setup.py install
How to Use
One-Liner
SupportResistanceLine(pd.Series(some_data)).plot_both()
Runnable Example
import numpy as np
import pandas as pd
from support_resistance_line import SupportResistanceLine
# Generate a random series
sr = pd.Series(np.random.random(size=250))
# ... moving avg to make it more trending
sr = sr.rolling(50).mean().dropna()
# Init. Index will be ignored
srl = SupportResistanceLine(sr, kind='support')
# Plot the best 3 support lines
srl.plot_top_lines()
# Plot the best 3 resistance lines
srl.twin.plot_top_lines()
# Plot the best support & resistance lines
srl.plot_both()
# View the logic steps if you want
srl.plot_steps()
Incentive
支撑线(support line)又称为抵抗线。当股价跌到某个价位附近时,股价停止下跌,甚至有可能还有回升,这是因为多方在此买入造成的。支撑线起阻止股价继续下跌的作用。这个起着阻止股价继续下跌或暂时阻止股价继续下跌的位置就是支撑线所在的位置。
压力线(resistance line)又称为阻力线。当股价上涨到某价位附近时,股价会停止上涨,甚至回落,这是因为空方在此抛出造成的。压力线起阻止股价继续上升的作用。这个起着阻止或暂时阻止股价继续上升的位置就是压力线所在的位置。
因为支撑线压力线的定义不够精确,有较强的主观差异,所以不同的人在同一条时间序列数据上可能会作出完全不同的支撑线压力线。
这里我们采用精确的数学算法来绘制支撑线压力线。
这套算法主要有四个步骤:多项式拟合、寻找极值、聚类分析、图像绘制。
Logic
1.多项式拟合
这里以30分之一序列长度作为窗口的移动平均线作为参考线,从项数取40分之一时间序列长度开始迭代,整体误差在移动平均线的0.6倍以内时停止迭代。
2.遍历多项式曲线取极值
不管画支撑线还是压力线都同时计算极大值和极小值。极值的计算采用一阶导数取零点、二阶导数区分的方式。以支撑线为例,遍历极小值,以任意一个极小值两边的极大值之内的区间,对原序列取最小值,作为支撑点。
3.邻近聚类
对极值聚类,把相距非常近的极值剔除。
4.找线
从最后一个支撑点出发,和其之前的所有支撑点连线。对斜率用Kmeans聚类,每类取最下方一条线,该类点个数为,该类所有点到该直线的平均距离为
,该类所有点的横坐标标准差为
,平均值为
,评分公式为
最终线为所有聚类的线评分最小的。 对序列后四分之一的所有支撑点遍历以上过程,寻找最好的线。
Demo
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file support_resistance_line-0.0.7.tar.gz.
File metadata
- Download URL: support_resistance_line-0.0.7.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abe09b70415bebf5d6277106feba437934ab4a89575cfe06ecf0a864162382b7
|
|
| MD5 |
9bceb2ce8399d630e8b8fdd583132132
|
|
| BLAKE2b-256 |
0ea422d8de545100fbc3116f2a5aa218d592b36a86629b19d6d739a2975397fc
|
File details
Details for the file support_resistance_line-0.0.7-py3-none-any.whl.
File metadata
- Download URL: support_resistance_line-0.0.7-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c49d5f207977ab1886e9bdfd33b59da6a31de2fec6c3306e9e1dee0192492a0b
|
|
| MD5 |
240ee36cd768eade99cd6003e876320e
|
|
| BLAKE2b-256 |
e8a1d41336cac5a562b6c8de85bdf006e6706ffe47572fd59a3fb23166bf12ee
|