当前位置:网站首页>Common skills for quantitative investment - drawing 3: drawing the golden section line
Common skills for quantitative investment - drawing 3: drawing the golden section line
2022-06-13 00:54:00 【Your name is Yu yuezheng】
Quantitative investment common skills —— Drawing 3
Preface
Previous articles have described how to Draw a line chart of the closing price 、 Candlelight 、 Draw a moving average based on the closing price , Next, how to draw the golden section line
You can pay attention to our Tiktok :“ Financial observation ”(JRGC8888) Learn more about
The golden section
The golden section line is also known to us 0.382,0.618 Split line
Visually 0.382 And visual 0.618
The calculation method is as follows :
sp382 = (max - min) * 0.382 + min
sp618 = (max - min) * 0.618 + min
Statistically 0.382 And statistical 0.618
We need to use scipy Under the Treasury stats Class scoreatpercentile() Method , Use pip You can install scipy
pip install scipy
How to use it is as follows
- stats.scoreatpercentile(arr, per)
- arr For input data , It can be a list or DataFrame
- per The percentage you need to get , The value involved in the calculation is per/100, so 0.628 You should enter per=62.8
sp382_stats = stats.scoreatpercentile(data, 38.2)
sp618_stats = stats.scoreatpercentile(data, 61.8)
Delimit the golden section line
The golden section line delimits two areas 0.382 Split line and two 0.618 The range between , So we use the following function to calculate the range
above618 = np.maximum(sp618, sp618_stats) # From vision 0.618 And statistics 0.618 Filter for larger values in
below618 = np.minimum(sp618, sp618_stats) # From vision 0.618 And statistics 0.618 Filter smaller values in
above382 = np.maximum(sp382, sp382_stats) # From vision 0.382 And statistics 0.382 Filter for larger values in
below382 = np.minimum(sp382, sp382_stats) # From vision 0.382 And statistics 0.382 Filter smaller values in
Draw the golden section
The data we use is through abupy Library imported tsla Historical data of , Combined with the introduction of Draw a candle Methods , The code for drawing the golden section line is as follows
import abupy
import numpy as np
import matplotlib.pyplot as plt
import mpl_finance as mpf
from scipy import stats
from matplotlib.dates import date2num
from abupy import ABuSymbolPd, pd_rolling_mean, nd
# ———————————————————— #
# ———— Default parameter settings ———— #
# ———————————————————— #
__colorup__ = "red"
__colordown__ = "green"
abupy.env.enable_example_env_ipython() # Using sandbox data , The goal is the same data environment as in the book , If not used, an error will be reported
tsla_df = ABuSymbolPd.make_kl_df('usTSLA', n_folds=2) # Fixed import tsla Market data of
tsla_df = tsla_df[:50] # Before selection 50 individual , Too much data makes it difficult to observe
print(tsla_df[:10])
# ———————————————————— #
def plot_ochl(data_df=tsla_df, axs=None, show=False):
''' Draw a candle :param data_df: Input data , By default tsla Historical market data of , The input data type currently only supports DataFrame type :param axs: Whether to draw on the subgraph :param show: Whether to display the image :return: '''
drawer = plt if axs is None else axs
fig, ax = drawer.subplots(figsize=(14, 7))
qutotes = []
for index, (d, o, c, h, l) in enumerate(
zip(data_df.index, data_df.open, data_df.close,
data_df.high, data_df.low)):
d = date2num(d) # The date of the candle chart should be used matplotlib.finance.date2num To a unique numeric value
val = (d, o, c, h, l) # date , The opening quotation , The close , The highest , The lowest composition tuple object val
qutotes.append(val) # Add val Join in qutotes
# Use mpf.candlestick_ochl Drawing candles ,ochl representative :open,close,high,low
mpf.candlestick_ochl(ax, qutotes, width=0.6, colorup=__colorup__, colordown=__colordown__)
ax.autoscale_view()
ax.xaxis_date()
if show:
plt.show()
def plot_goldline(data_df=tsla_df, axs=None, show=False):
''' Draw the golden section :param data_df: Input data , By default tsla Historical market data of , The input data type currently only supports DataFrame type :param axs: Whether to draw on the subgraph :param show: Whether to display the image :return: '''
cs_max = data_df.close.max() # The maximum value in the closing price series
cs_min = data_df.close.min() # The lowest value in the closing price series
# Visually 0.382 And visual 0.618
sp382 = (cs_max - cs_min) * 0.382 + cs_min
sp618 = (cs_max - cs_min) * 0.618 + cs_min
# Statistically 0.382 And statistical 0.618
sp382_stats = stats.scoreatpercentile(data_df.close, 38.2)
sp618_stats = stats.scoreatpercentile(data_df.close, 61.8)
above618 = np.maximum(sp618, sp618_stats) # From vision 0.618 And statistics 0.618 Filter for larger values in
below618 = np.minimum(sp618, sp618_stats) # From vision 0.618 And statistics 0.618 Filter smaller values in
above382 = np.maximum(sp382, sp382_stats) # From vision 0.382 And statistics 0.382 Filter for larger values in
below382 = np.minimum(sp382, sp382_stats) # From vision 0.382 And statistics 0.382 Filter smaller values in
plt.axhline(sp382, c='r') # Horizontal line vision 0.382
plt.axhline(sp382_stats, c='m') # Horizontal line statistics 0.382
plt.axhline(sp618, c='g') # Horizontal line vision 0.618
plt.axhline(sp618_stats, c='k') # Horizontal line statistics 0.618
plt.fill_between(data_df.index, above618, below618, alpha=0.5, color="r") # fill 0.618 red
plt.fill_between(data_df.index, above382, below382, alpha=0.5, color="g") # fill 0.382 green
plt.legend(['close', 'sp382', 'sp382_stats', 'sp618', 'sp618_stats'], loc='best') # Label the names according to the drawing order
if show:
plt.show()
if __name__ == '__main__':
plot_ochl() # Draw a candle , Will create a new canvas
plot_goldline(show=True) # Draw the golden section
The output image is as follows :
Welcome to pay attention to us
Our tiktok : Financial observation (JRGC8888)
边栏推荐
- Paper reading and sharing
- Kotlin 协程,job的生命周期
- 3623. Merge two ordered arrays
- How to solve the duplication problem when MySQL inserts data in batches?
- pytorch和tensorflow有什么区别?
- 深度学习训练多少轮?迭代多少次?
- [imx6ull] video monitoring project (USB camera +ffmepeg)
- 什么是 Meebits?一个简短的解释
- Androi天气
- How to handle different types of data
猜你喜欢

gpu加速pytorch能用吗?

人神共愤,唐山“群殴女性事件”细节...
![[JS component] customize the right-click menu](/img/a3/4555619db17e4c398e72c7d6b12f5d.jpg)
[JS component] customize the right-click menu

Hard (magnetic) disk (II)

MySQL locates the position of the character in the string String substitution

Influence of higher order poles on waveform

市值破万亿,连续三个月销量破10万,比亚迪会成为最强国产品牌?

.net core 抛异常对性能影响的求证之路

Canvas game lower level 100

Kotlin coroutine withcontext switch thread
随机推荐
通过抓包下载钉钉直播回放
高阶极点对于波形的影响
.net core 抛异常对性能影响的求证之路
[JS component] customize the right-click menu
Static analysis of malicious code
pytorch是什么?解释pytorch的基本概念
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
Aof persistence
In / out / inout details of MySQL stored procedures
ImportError: cannot import name ' get_ ora_ doc' from partially initialized module
The grass is bearing seeds
How to solve the duplication problem when MySQL inserts data in batches?
Arduino controls tb6600 driver +42 stepper motor
Win10 home vs pro vs enterprise vs enterprise LTSC
[server data recovery] successful cases of data loss recovery during data migration between storage servers
Kotlin coroutine suspend function suspend keyword
Mysql database password modification
What is dummy change?
杂记:intel11代和12代移动版支持原生Thunderbolt4接口,桌面版不支持
gpu加速pytorch能用吗?