Coder Social home page Coder Social logo

boostarcher / support_resistance_line Goto Github PK

View Code? Open in Web Editor NEW

This project forked from westxu/support_resistance_line

0.0 0.0 0.0 1.81 MB

A well-tuned algorithm to generate & draw support/resistance line on time series. 根据时间序列自动生成支撑线压力线

License: MIT License

Python 100.00%

support_resistance_line's Introduction

Support Resistance Line

pypi

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()

example

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

Demo

support_resistance_line's People

Contributors

westxu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.