Do your own quantitative trading software (27) Xiaobai quantitative actual combat 1-transformation of weapons and displays

Do your own quantitative trading software (27) Xiaobai Quantitative Practical Battle 1-Transformation of Weapons and Display

The long and short sides of the stock market are like "Wulin", in which there are many "masters" and "masters" playing games with each other. When the comprehension and skill are equal, one must rely on “super weapons” that cut iron and mud, such as the Xuanyuan sword and dragon sword. With this "super weapon", one can exert one's abilities exponentially and be able to "smile proud of martial arts."

Many investors, after honed in the stock market, always sum up some of their own analytical techniques, and even create new analytical techniques, but it is very inconvenient to use these techniques to select stocks by themselves, such as 3 minutes for each stock analysis , It takes 50 hours to finish reading 1000 stocks. When a good stock is found, the time has been missed. If you can use the computer to pick stocks, you can seize the opportunity in time.
Professional investment analysis platforms such as Feihu Trader, Big Wisdom LEVEL-2, Tongdaxin, Flushing, etc., provide a complete set of analysis method design, testing, evaluation, and optimization platforms. Users can base their experience on stock trading or various new ideas and methods in the field of securities analysis. Design a variety of formula systems by yourself to create a secret weapon for the stock market.

However, these stock analysis tools do not have money management, position management, package stock backtesting analysis, and arbitrage analysis backtesting tools, and finally automatic ordering. Therefore, Python supports deep learning, crawlers, etc., and is the preferred language for quantitative analysis. It is the quantitative module of the Python platform.
The author published a book "Cheats for Watching Disks" (https://item.jd.com/10469068.html) in 2008. This book mainly introduces the design of stock software indicators and the development of indicator formulas in C++. Readers of this book, in addition to understanding the author’s investment analysis philosophy, will also get information on C++ development indicator formulas and demonstration programs, and can obtain a set of C++ development indicators (https://mp.weixin.qq.com/s/c9J -d30haP5nonU3bjiwSg), this set of indicators supports software such as analysts, big wisdom, and flying fox traders.
The series of articles at the beginning of this article teach you from creating self-compiled formulas to realizing backtesting and automatic trading codes for stocks, futures, foreign exchange, and virtual currencies. It is not difficult for readers to design self-edited indicators based on their own investment experience and investment philosophy to create their own "Dragon Sword".

We take Tongdaxin software "Guangfa Securities Financial Terminal" as an example to introduce the process of creating our own weapons, and the use of Xiaobai quantitative platform modules to achieve backtesting and fully automated transactions.
There are thousands of indicators in stock software. If they can make money with this, many investors will not make money. In fact, for decades, investors have always made two losses. Ta-lib integrates common stock indicator functions, so it is difficult to make money if it depends on Ta-lib as a quantitative trading tool.
Xiaobai Quantitative can imitate the index formulas of stock software such as Tongdaxin, Great Wisdom, and Flying Fox Trader, which can realize the analysis and back-testing of the index compiled by users, and realize automatic trading. By purchasing the book "Building a Quantitative Investment System with Zero Base-Using Python as a Tool" (https://item.jd.com/61567375505.html), you can get all the source code of Xiaobai Quantitative Second Generation.
Friends who have read the author’s "Cheats for Watching Disks" all know that the macro aspects of securities investment analysis include fundamentals, trends, strengths, energy, public mentality, economic and political environment, etc., and micro aspects include the center of gravity, compression, and Resonance, speed, direction and many other aspects. We cannot introduce them one by one, but can only make a simple example. Let readers understand the entire process of quantitative investment.

First of all, to find a test tool, we choose Tongdaxin software "Guangfa Securities Financial Terminal". Use this software to design a prototype of self-compiled formulas. After porting this self-compiled formula to Python, graphical display, backtesting, and finally fully automatic trading are realized.
Among the commonly used indicators, the author's favorite is the Bollinger Bands BOLL indicator. The original indicator code is as follows:

N:=26;
P:=2;
MID :  MA(CLOSE,N);
UPPER: MID + P*STD(CLOSE,N);
LOWER: MID - P*STD(CLOSE,N);

BOLL main chart indicators
Judging from the BOLL indicator in the above figure, we are not easy to analyze, at least one thing is in line with the author's point of view, compression and diffusion. Compression is storing energy for skyrocketing and plummeting.
We add 3 more lines to this indicator, and the indicator can provide complete trading tips. Look at the following code:

N:=24;
M:=2;
X:=25;
MID :  MA(CLOSE,N);
UPPER: MID + 2*STD(CLOSE,N),LINETHICK2;
LOWER: MID - 2*STD(CLOSE,N),LINETHICK2;
M1:(UPPER-MID)/2+MID;
M2:(MID-LOWER)/2+LOWER;
MM1:=EMA((OPEN+CLOSE*2+LOW+HIGH)/5,M);
MM2:=EMA(IF(MM1>=MID,MM1*(1+X/1000),MM1*(1-X/1000)),3);
MM:MM2,COLORYELLOW,LINETHICK3;

Insert picture description here
The above picture shows the content of Tongdaxin software indicators.
Insert picture description here
The above picture shows the indicator display of Tongdaxin software.
We can clearly see that the mid-line operation: mm thick yellow line, upper rail LOWER line to buy; mm thick yellow line, lower top rail UPPER line to sell.
Short-term operation: buy on each track and sell on the next track.
Is there an obvious trading reminder? Of course, the inaccuracy of the standard is not a problem we want to discuss. This is a simple example. You can change to the index you think is accurate.
Different periods or different varieties need to adjust the parameters N, M, X. With the advantage of Tongdaxin software, we can adjust the parameters and change the graphic effect.
At this point, our weapon design has come to an end.
Next we will port to the Python platform. Of course, the premise is that Xiaobai quantifies the second-generation financial module.

# -*- 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'

global CLOSE,LOW,HIGH,OPEN,VOL

#小白换档买卖线
def XBHDMMX(N=24,M=2,X=25):
    MID =MA(CLOSE,N)
    UPPER=MID + 2*STD(CLOSE,N)
    LOWER= MID - 2*STD(CLOSE,N)
    M1=(UPPER-MID)/2+MID
    M2=(MID-LOWER)/2+LOWER
    MM1=EMA((OPEN+CLOSE*2+LOW+HIGH)/5,M);
    MM2=EMA(IF(MM1>=MID,MM1*(1+X/1000),MM1*(1-X/1000)),3)
    MM=MM2
    return MID,UPPER,LOWER,M1,M2,MM

#首先要对数据预处理
#获取数据
htdx.TdxInit(ip='183.60.224.178',port=7709)
code='600080'
df = htdx. get_security_bars(nCategory=4,nMarket = 0,code=code)

#对数据做小白量化各式转换
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']


#调用自定义指标
MID,UPPER,LOWER,M1,M2,MM=XBHDMMX()

#把指标值添加到mydf数据表中
mydf['MID']=MID
mydf['UPPER']=UPPER
mydf['LOWER']=LOWER
mydf['M1']=M1
mydf['M2']=M2
mydf['MM']=MM


#数据裁减
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=0)
mydf['MID'].plot.line(legend=True)
mydf['UPPER'].plot.line(legend=True,linewidth=3)
mydf['LOWER'].plot.line(legend=True,linewidth=3)
mydf['M1'].plot.line(legend=True)
mydf['M2'].plot.line(legend=True)
mydf['MM'].plot.line(legend=True,linewidth=4)

#绘制副图指标
ax2=plt.subplot(212)
mydf['MID'].plot.line(legend=True)
mydf['UPPER'].plot.line(legend=True,linewidth=3)
mydf['LOWER'].plot.line(legend=True,linewidth=3)
mydf['M1'].plot.line(legend=True)
mydf['M2'].plot.line(legend=True)
mydf['MM'].plot.line(legend=True,linewidth=4)


plt.show()


The results of the program are as follows:
Insert picture description here

Below we show it on MT5.

# -*- 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 MetaTrader5 as mt5
import HP_mt5 as hmt5   #小白量化MT5接口
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'

code="BTCUSD"  #品种比特币


global CLOSE,LOW,HIGH,OPEN,VOL

#小白换档买卖线
def XBHDMMX(N=24,M=2,X=25):
    MID =MA(CLOSE,N)
    UPPER=MID + 2*STD(CLOSE,N)
    LOWER= MID - 2*STD(CLOSE,N)
    M1=(UPPER-MID)/2+MID
    M2=(MID-LOWER)/2+LOWER
    MM1=EMA((OPEN+CLOSE*2+LOW+HIGH)/5,M);
    MM2=EMA(IF(MM1>=MID,MM1*(1+X/1000),MM1*(1-X/1000)),3)
    MM=MM2
    return MID,UPPER,LOWER,M1,M2,MM

#首先要对数据预处理
#获取数据
#初始化小白mt5库
hmt5.init()    

#登陆MT5帐号
用户名=''
密码=''
服务器=''
hmt5.login(login=用户名, server=服务器,password=密码)

#获取行情数据
rates= mt5.copy_rates_from_pos(code, mt5.TIMEFRAME_D1, 0, 500)

#转化为小白量化数据格式
df=hmt5.tohpdata(rates)
df['time']=[x[11:19] for x in df.time.astype(str)]


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


#调用自定义指标
MID,UPPER,LOWER,M1,M2,MM=XBHDMMX()

#把指标值添加到mydf数据表中
mydf['MID']=MID
mydf['UPPER']=UPPER
mydf['LOWER']=LOWER
mydf['M1']=M1
mydf['M2']=M2
mydf['MM']=MM


#数据裁减
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=0)
mydf['MID'].plot.line(legend=True)
mydf['UPPER'].plot.line(legend=True,linewidth=3)
mydf['LOWER'].plot.line(legend=True,linewidth=3)
mydf['M1'].plot.line(legend=True)
mydf['M2'].plot.line(legend=True)
mydf['MM'].plot.line(legend=True,linewidth=4)

#绘制副图指标
ax2=plt.subplot(212)
mydf['MID'].plot.line(legend=True)
mydf['UPPER'].plot.line(legend=True,linewidth=3)
mydf['LOWER'].plot.line(legend=True,linewidth=3)
mydf['M1'].plot.line(legend=True)
mydf['M2'].plot.line(legend=True)
mydf['MM'].plot.line(legend=True,linewidth=4)

plt.show()

The results of the program are as follows:
Insert picture description here
The Bitcoin market of MT5 is displayed above. Xiaobai quantization can also be used for any products such as futures and foreign exchange.
The BOLL indicator is a very interesting indicator. The self-compiled BOLL indicator solves the problems of bargain hunting, price prediction, buying and selling signals, and trend judgment. In the future, my article or video will introduce you to these analysis techniques.
In the following article, we will use this self-compiled indicator to use the Xiaobai quantification system to realize the backtesting of stocks and foreign exchange.

Guess you like

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