Do your own quantitative trading software (29) Xiaobai quantitative actual combat 3-cross-cycle resonance

Do your own quantitative trading software (29) Xiaobai Quantitative Practice 3-Inter-cycle Resonance
  In the First World War, a group of German soldiers stepped neatly through a bridge, and the bridge collapsed. As far as the load capacity of the bridge is concerned, it is far greater than the weight of this team of German soldiers. However, because the soldiers adjusted their steps and rhythms uniformly, the bridge collapsed under the effect of this uniform force. This is the effect of resonance.
  Resonance is a term used in physics with very high frequency. Resonance is also called "resonance" in acoustics. It refers to the phenomenon that objects make sound due to resonance. For example, if two tuning forks with the same frequency are close together, when one vibrates and makes sound, the other also makes sound. In electricity, the resonance phenomenon of an oscillator circuit is called "resonance."
  Resonance is not only used in physics at a very high frequency, but resonance phenomenon can also be said to be one of the most common and frequent natural phenomena in the universe. It can even be said that there is no world without resonance.
  Resonance is a universal law of the movement of all matter in the universe. Humans and other living things are also matter in the universe. Of course, resonance is also common in these beings. In addition to the natural frequencies of breathing, heartbeat, blood circulation, and speech, the brain waves generated by the human brain during thinking activities also have resonance.
  The resonance phenomenon in the natural world also has resonance in the stock market.
  Looking back at historical trends, it can be found that stock trends often fluctuate. Once started from a low position, an upward breakthrough occurs, and the stock price is like a wild horse running upward; and once a downward breakthrough occurs from a high position, the stock price is like a river that bursts. . This is the reflection of resonance in the stock market.
  Common demand can generate momentum, and once this momentum occurs, the upward and downward power is enormous. It can trigger people's emotions and operational behaviors, resulting in a one-sided situation. When it goes up, people are in high spirits and swarms into the market; when it goes down, everyone panics, and the stock price plummets, just like the end of the world, Gann called it a price collapse.
  Many of Hepu’s indicators are so easy to use that they completely rely on the design indicators of resonance. A few simple lines contain the essence of different indicators. Therefore, for more than ten years, there have always been people who like to crack Hepo's formula and explore its principles.
  The simplest resonance application is the cross-cycle application. Let's take an example to make a cross-cycle indicator on Tongdaxin software.
  For example, the daily KDJ quotes the weekly KDJ custom indicator. The content of the indicator is as follows:

N:=9;
M1:=3;
M2:=3;
RSV:=(CLOSE-LLV(LOW,N))/(HHV(HIGH,N)-LLV(LOW,N))*100;
K:SMA(RSV,M1,1);
D:=SMA(K,M2,1);
J:3*K-2*D;

{
    
    下面是周KDJ指标的引用。}
ZK:"KDJ.K#WEEK"(N,M1,M2),LINETHICK3;
ZD:"KDJ.D#WEEK"(N,M1,M2),LINETHICK3;
ZJ:="KDJ.J#WEEK"(N,M1,M2);

Insert picture description here
The indicator display results are as follows.
Insert picture description here

Let's see how to implement cross-period calculations on Python.
For example, the 1-minute line refers to the 5-minute line or indicator.
1. First process the 5-minute indicator calculation, for example, read 100 sets of data mydf5 to
calculate the indicator to obtain 5-minute kdj data. k5, d5, j5
2. Read 500 sets of data
for 1 minute. Mydf performs index calculation
3. Put the 5-minute index values ​​k5, d5, j5, and the data to 5 times to become 500 sets of data.
4. Change k5, d5, J5 is merged into the mydf table
5. You can handle the calculations you want later

Our following program gives the Python sequence of daily KDJ reference to weekly KDJ.

# -*- coding: utf-8 -*-
# 小白量化跨周期自编指标计算
'''
独狼荷蒲qq:2886002
通通小白python量化群:524949939
tkinter,pyqt,gui,Python交流2:517029284
微信公众号:独狼股票分析
'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import HP_global as g  #小白量化全局变量库
from HP_formula import *
import HP_tdx as htdx
import HP_plt as hplt   #小白量化指标绘图模块
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
#白底色
g.ubg='w'
g.ufg='b'
g.utg='b'
g.uvg='#1E90FF'

#列表扩展
def Listexpand(List,n):
    Lista=[]
    for x in List:
        for i in range(n):
            Lista.append(x)
    return Lista


global CLOSE,LOW,HIGH,OPEN,VOL

def KDJ(N=9, M1=3, M2=3):
    """
    KDJ 随机指标
    """
    RSV = (CLOSE - LLV(LOW, N)) / (HHV(HIGH, N) - LLV(LOW, N)) * 100
    K = EMA(RSV, (M1 * 2 - 1))
    D = EMA(K, (M2 * 2 - 1))
    J = K * 3 - D * 2

    return K, D, J

#首先要对数据预处理
#获取数据
htdx.TdxInit(ip='183.60.224.178',port=7709)
code='600436'

#nCategory -> K 线种类 
#0 5 分钟K 线 
#1 15 分钟K 线 
#2 30 分钟K 线 
#3 1 小时K 线 
#4 日K 线 
#5 周K 线 
#6 月K 线 
#7 1 分钟 
#8 1 分钟K 线 
#9 日K 线 
#10 季K 线 
#11 年K 线 

#获取周线数据
df = htdx. get_security_bars(nCategory=5,nMarket = 0,code=code,nStart=0, nCount=100)

#对数据做小白量化各式转换
mydf=df.copy()
CLOSE=mydf['close']
LOW=mydf['low']
HIGH=mydf['high']
OPEN=mydf['open']
VOL=mydf['volume']
C=mydf['close']
L=mydf['low']
H=mydf['high']
O=mydf['open']
V=mydf['volume']

k5,d5,j5=KDJ()   #周线指标计算



#获取日线数据
df = htdx. get_security_bars(nCategory=4,nMarket = 0,code=code,nStart=0, nCount=500)

#对数据做小白量化各式转换
mydf=df.copy()
CLOSE=mydf['close']
LOW=mydf['low']
HIGH=mydf['high']
OPEN=mydf['open']
VOL=mydf['volume']
C=mydf['close']
L=mydf['low']
H=mydf['high']
O=mydf['open']
V=mydf['volume']

k,d,j=KDJ()   #日线指标计算


#把指标值添加到mydf数据表中
mydf['k']=k
mydf['d']=d
mydf['j']=j

#把周线数据放大5,保存到日线数据表中
mydf['k5']=pd.Series(Listexpand(list(k5),5))
mydf['d5']=pd.Series(Listexpand(list(d5),5))
mydf['j5']=pd.Series(Listexpand(list(j5),5))

#输入水平线20,50,80
mydf['z20']=20
mydf['z50']=50
mydf['z80']=80


#数据裁减
m=1
mydf=mydf.tail(150*m).head(150).copy()

#绘制图形
plt.figure(1,figsize=(16,12), dpi=80)

#绘制主图指标
ax1=plt.subplot(211)
hplt.ax_K(ax1,mydf,t=code,n=6)

#绘制副图指标
ax2=plt.subplot(212)
mydf['k'].plot.line(legend=True,linewidth=3)
#mydf['d'].plot.line(legend=True,linewidth=3)
mydf['j'].plot.line(legend=True,linewidth=3)
mydf['k5'].plot.line(legend=True,color='blue')
mydf['d5'].plot.line(legend=True,)
#mydf['j5'].plot.line(legend=True)
mydf['z20'].plot.line(legend=False,color='red')
mydf['z50'].plot.line(legend=False,color='red')
mydf['z80'].plot.line(legend=False,color='red')

plt.show()

The results of the program operation are as follows:
Insert picture description here
This article introduces the combined display problem of different cycle indicator calculations. Of course, we can also perform cross-cycle calculations. From the above figure, we clearly find that the daily k value is below the value of 40 and passes through the weekly KDJ k5 value. It is a good mid-line buying point. How do we describe our buying point indicator?

#小白量化仿通达信指标计算
mydf['buy']=IF(k<40,CROSS(k,k5),0)

If the value of mydf.buy is 1, it means there is a buying point, otherwise the value is 0. How to use this signal for backtesting and automatic trading will be introduced later.
  The technical knowledge introduction of this article is over.

If you want to learn indicator writing, including C++ writing great wisdom indicators, please refer to the book "Cheats for Watching Disks".
  If you want to learn quantitative software development or automated trading, refer to the book "Building a Quantitative Investment System with Zero Foundation-Using Python as a Tool".
  My readers said that my blog has obviously reduced the technical difficulty of quantitative development. Do quantitative traders ask for cabbage prices?

Please continue to pay attention to my blog, my progress is your progress!

Guess you like

Origin blog.csdn.net/hepu8/article/details/112001158