通达信filter过滤函数X函数在python如何实现?

#导入数据
from tdx_data import tdx_data
import random
data=tdx_data()
#连接通达信
#连接通达信
data.connect()
data.get_security_daily_data()def next_connect(self):def next_connect(self):
'''
如果默认连接不成功使用,一般不用
'''
try:
print('通达信数据连接不成功,随机选择连接')
n=len(self.name_list)
#随机选择
random_name=self.name_list[random.randint(0,n-1)]
random_port=self.port_list[random.randint(0,n-1)]
random_ip=self.ip_list[random.randint(0,n-1)]
self.api.connect(ip=random_ip,port=random_port)
except:
random_name=self.name_list[random.randint(0,n-1)]
random_port=self.port_list[random.randint(0,n-1)]
random_ip=self.ip_list[random.randint(0,n-1)]
self.api.connect(ip=random_ip,port=random_port)
#选择数据类型函数#调整股票代码函数
选择数据类型函数
def select_data_type(self,stock='600031'):
'''
选择数据类型
'''
if stock[:3] in ['110','113','123','127','128','111','118']:
return 'bond'
elif stock[:3] in ['510','511','512','513','514','515','516','517','518','588','159','501']:
return 'fund'
else:
return 'stock'
调整市场函数
#调整股票代码函数
def adjust_stock(self,stock='600031.SH'):
'''
调整代码
'''
if stock[-2:]=='SH' or stock[-2:]=='SZ' or stock[-2:]=='sh' or stock[-2:]=='sz':
stock=stock.upper()
else:
if stock[:3] in ['600','601','603','688','510','511',
'512','513','515','113','110','118','501'] or stock[:2] in ['11']:
stock=stock+'.SH'
else:
stock=stock+'.SZ'
return stock
#格式化代码函数#格式化代码函数
def rename_stock_type_1(self,stock='600031'):
'''
将股票类型格式化
stock股票代码
1上海
0深圳
'''
if stock[:3] in ['600','601','603','688','510','511',
'512','513','515','113','110','118','501'] or stock[:2] in ['11']:
marker=1
else:
marker=0
return marker,stock
#获取分钟数据#获取分钟数据
'''
获取分钟数据
n数据类型
0 5分钟K线
1 15分钟K线
2 30分钟K线
3 1小时K线
4 日K线
5 周线
6 月线
7 1分钟
8 1分钟K线
9 年线
10 季线
marker市场0深圳1上海
stock股票代码
start开始位置
count返回的数据长度
'''
df3=data.get_security_minute_data(stock='600031',count=800,n=4)
df3#指数分析数据
#指数分析数据
'''
获取指数数据
0 5分钟K线
1 15分钟K线
2 30分钟K线
3 1小时K线
4 日K线
5 周线
6 月线
7 1分钟
8 1分钟K线
9 年线
10 季线
index_code指数代码
marker市场类型0深圳,1上海
'''
df4=data.get_index_minute_data(index_code='000001',count=800,n=4)
dfimport pytdx
from pytdx.config import hosts
from pytdx.hq import TdxHq_API
import pandas as pd
from pytdx.reader import base_reader
from pytdx.params import TDXParams
import random
import requests
import json
import random
#通达信数据
class tdx_data(object):
def __init__(self):
'''
当默认的连接不上,随机选择一个连接
到连接成功
self.df=pd.DataFrame(hosts.hq_hosts)
self.df.columns=['名称','ip','port']
self.ip_list=self.df['ip'].tolist()
self.port_list=self.df['port'].tolist()
self.name_list=self.df['名称'].tolist()
self.n=random.randint(0,len(self.name_list))
self.random_ip=self.ip_list[self.n]
self.random_port=int(self.port_list[self.n])
self.random_name=self.name_list[self.n]
'''
self.df=pd.DataFrame(hosts.hq_hosts)
self.df.columns=['name','ip','port']
self.name_list=self.df['name'].tolist()
self.ip_list=self.df['ip'].tolist()
self.port_list=self.df['port'].tolist()
self.name='招商证券深圳行情'
self.ip='119.147.212.81'
self.port=7709
self.eorr=0
#self.tdx=TdxHq_API()
self.api=TdxHq_API()
def connect(self):
'''
连接服务器端口
'''
try:
print('通达信数据连接成功')
self.api.connect(ip=self.ip,port=self.port)
return self.api
except:
self.next_connect()
def all_tdx_data(self):
self.api=TdxHq_API()
self.api=self.api.connect(ip=self.ip,port=self.port)
return self.api
def next_connect(self):
'''
如果默认连接不成功使用,一般不用
'''
try:
print('通达信数据连接不成功,随机选择连接')
n=len(self.name_list)
#随机选择
random_name=self.name_list[random.randint(0,n-1)]
random_port=self.port_list[random.randint(0,n-1)]
random_ip=self.ip_list[random.randint(0,n-1)]
self.api.connect(ip=random_ip,port=random_port)
except:
random_name=self.name_list[random.randint(0,n-1)]
random_port=self.port_list[random.randint(0,n-1)]
random_ip=self.ip_list[random.randint(0,n-1)]
self.api.connect(ip=random_ip,port=random_port)
def select_data_type(self,stock='600031'):
'''
选择数据类型
'''
if stock[:3] in ['110','113','123','127','128','111','118']:
return 'bond'
elif stock[:3] in ['510','511','512','513','514','515','516','517','518','588','159','501']:
return 'fund'
else:
return 'stock'
def adjust_stock(self,stock='600031.SH'):
'''
调整代码
'''
if stock[-2:]=='SH' or stock[-2:]=='SZ' or stock[-2:]=='sh' or stock[-2:]=='sz':
stock=stock.upper()
else:
if stock[:3] in ['600','601','603','688','510','511',
'512','513','515','113','110','118','501'] or stock[:2] in ['11']:
stock=stock+'.SH'
else:
stock=stock+'.SZ'
return stock
def rename_stock_type_1(self,stock='600031'):
'''
将股票类型格式化
stock证券代码
1上海
0深圳
'''
if stock[:3] in ['600','601','603','688','510','511',
'512','513','515','113','110','118','501'] or stock[:2] in ['11']:
marker=1
else:
marker=0
return marker,stock
def rename_stock_type(self,stock='600031'):
'''
将股票类型格式化
stock证券代码
1上海
0深圳
'''
if stock[:3] in ['600','601','603','688','510','511',
'512','513','515','113','110','118','501'] or stock[:2] in ['11']:
marker=1
else:
marker=0
result=[(marker,stock)]
return result
def marker_type(self,stock='600031'):
'''
判断市场类型
'''
if stock[:3] in ['600','601','603','688','510','511',
'512','513','515','113','110','118','501'] or stock[:2] in ['11']:
marker=1
else:
marker=0
return marker
def get_security_quotes_none(self,stock='600031'):
'''
获取股票行情数据一只
stock证券代码
[('market', 0),
('code', '000001'),
('active1', 2864),
('price', 9.19),
('last_close', 9.25),
('open', 9.23),
('high', 9.27),
('low', 9.16),
('reversed_bytes0', bytearray(b'\xbd\xc9\xec\x0c')),
('reversed_bytes1', -919),
('vol', 428899),
('cur_vol', 30),
('amount', 395218880.0),
('s_vol', 284703),
('b_vol', 144196),
('reversed_bytes2', 1),
('reversed_bytes3', 698),
('bid1', 9.18),
('ask1', 9.19),
('bid_vol1', 1078),
('ask_vol1', 5236),
('bid2', 9.17),
('ask2', 9.2),
('bid_vol2', 8591),
('ask_vol2', 3027),
('bid3', 9.16),
('ask3', 9.21),
('bid_vol3', 12638),
('ask_vol3', 3557),
('bid4', 9.15),
('ask4', 9.22),
('bid_vol4', 13234),
('ask_vol4', 2615),
('bid5', 9.14),
('ask5', 9.23),
('bid_vol5', 5377),
('ask_vol5', 6033),
('reversed_bytes4', 5768),
('reversed_bytes5', 1),
('reversed_bytes6', 16),
('reversed_bytes7', 83),
('reversed_bytes8', 20),
('reversed_bytes9', 0),
('active2', 2864)])]
'''
i=0
while i!=1:
try:
stock=stock[:6]
stock=self.rename_stock_type(stock=stock)
df=self.api.get_security_quotes(stock)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_security_quotes_more(self,stock_list=['600031','000001']):
'''
同时获取多只股票行情数据
code_list股票列表
[('market', 0),
('code', '000001'),
('active1', 2864),
('price', 9.19),
('last_close', 9.25),
('open', 9.23),
('high', 9.27),
('low', 9.16),
('reversed_bytes0', bytearray(b'\xbd\xc9\xec\x0c')),
('reversed_bytes1', -919),
('vol', 428899),
('cur_vol', 30),
('amount', 395218880.0),
('s_vol', 284703),
('b_vol', 144196),
('reversed_bytes2', 1),
('reversed_bytes3', 698),
('bid1', 9.18),
('ask1', 9.19),
('bid_vol1', 1078),
('ask_vol1', 5236),
('bid2', 9.17),
('ask2', 9.2),
('bid_vol2', 8591),
('ask_vol2', 3027),
('bid3', 9.16),
('ask3', 9.21),
('bid_vol3', 12638),
('ask_vol3', 3557),
('bid4', 9.15),
('ask4', 9.22),
('bid_vol4', 13234),
('ask_vol4', 2615),
('bid5', 9.14),
('ask5', 9.23),
('bid_vol5', 5377),
('ask_vol5', 6033),
('reversed_bytes4', 5768),
('reversed_bytes5', 1),
('reversed_bytes6', 16),
('reversed_bytes7', 83),
('reversed_bytes8', 20),
('reversed_bytes9', 0),
('active2', 2864)])]
'''
code_list=stock_list
stock_list=[]
for i in code_list:
stock=self.rename_stock_type(i)
stock_list.append(stock[0])
df=self.api.get_security_quotes(all_stock=stock_list)
result=self.api.to_df(df)
return result
def get_security_minute_data(self,n=0,stock='600031',start=0,count=800):
'''
获取分钟数据
n数据类型
0 5分钟K线
1 15分钟K线
2 30分钟K线
3 1小时K线
4 日K线
7 1分钟
8 1分钟K线
marker市场0深圳1上海
stock证券代码
start开始位置
count返回的数据长度
'''
i=0
while i!=1:
try:
stock=stock[:6]
marker=self.marker_type(stock=stock)
df=self.api.get_security_bars(category=n,market=marker,code=stock,start=start,count=count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_security_week_data(self,stock='600031',start=0,count=100):
'''
获取股票周线数据
stock证券代码
count返回长度
'''
i=0
while i!=1:
try:
stock=stock[:6]
n=self.marker_type(stock=stock)
df=self.api.get_security_bars(5,n,stock,start,count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_security_moth_data(self,stock='600031',start=0,count=100):
'''
获取股票月线数据
stock证券代码
count返回长度
'''
i=0
while i!=1:
try:
stock=stock[:6]
n=self.marker_type(stock=stock)
df=self.api.get_security_bars(6,n,stock,start,count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_security_daily_data(self,stock='600031',start=0,count=100):
'''
获取股票日线数据
stock证券代码
count返回长度
'''
i=0
while i!=1:
try:
stock=stock[:6]
n=self.marker_type(stock=stock)
df=self.api.get_security_bars(9,n,stock,start,count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_security_quarter_data(self,stock='600031',start=0,count=100):
'''
获取股票季线数据
stock证券代码
count返回长度
'''
i=0
while i!=1:
try:
stock=stock[:6]
n=self.marker_type(stock=stock)
df=self.api.get_security_bars(10,n,stock,start,count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_security_year_data(self,stock='600031',start=0,count=100):
'''
获取股票年线数据
注意count返回的长度
stock证券代码
count返回长度
'''
i=0
while i!=1:
try:
stock=stock[:6]
n=self.marker_type(stock=stock)
df=self.api.get_security_bars(11,n,stock,start,count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_index_minute_data(self,n=7,marker=1,index_code='000001',start=0,count=100):
'''
获取指数数据
n数据类型 0 5分钟K线
1 15分钟K线
2 30分钟K线
3 1小时K线
4 日K线
7 1分钟
8 1分钟K线
index_code指数代码
marker市场类型0深圳,1上海
'''
i=0
while i!=1:
try:
df=self.api.get_index_bars(category=n,market=marker,code=index_code,start=start,count=count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_index_week_data(self,marker=1,index_code='000001',start=0,count=100):
'''
获取指数周线数据
index_code指数代码
count返回长度
'''
i=0
while i!=1:
try:
df=self.api.get_security_bars(5,market=marker,code=index_code,start=start,count=count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_index_moth_data(self,marker=1,index_code='000001',start=0,count=100):
'''
获取指数月线数据
index_code指数代码
count返回长度
'''
i=0
while i!=1:
try:
df=self.api.get_security_bars(6,market=marker,code=index_code,start=start,count=count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_index_daily_data(self,marker=1,index_code='000001',start=0,count=100):
'''
获取指数月线数据
index_code指数代码
count返回长度
'''
i=0
while i!=1:
try:
df=self.api.get_security_bars(4,market=marker,code=index_code,start=start,count=count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_index_quarter_data(self,marker=1,index_code='000001',start=0,count=100):
'''
获取指数季度线数据
index_code指数代码
count返回长度
'''
i=0
while i!=1:
try:
df=self.api.get_security_bars(10,market=marker,code=index_code,start=start,count=count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_index_year_data(self,marker=1,index_code='000001',start=0,count=100):
'''
获取指数周线数据
index_code指数代码
count返回长度
'''
i=0
while i!=1:
try:
df=self.api.get_security_bars(11,market=marker,code=index_code,start=start,count=count)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_minute_time_data(self,stock='600031'):
'''
查询分时行情
stock证券代码
'''
i=0
while i!=1:
try:
stock=stock[:6]
n=self.marker_type(stock=stock)
df=self.api.get_minute_time_data(market=n,code=stock)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def marker_params(self,stock='600031'):
if stock[:3] in ['600','601','603','688','510','511',
'512','513','515','113','110','118','501'] or stock[:2] in ['11']:
marker=1
else:
marker=0
return marker
def get_history_minute_time_data(self,stock='600031',date='20220920'):
'''
查询历史分时行情
stock证券代码
date交易日
'''
i=0
while i!=1:
try:
stock=stock[:6]
market=self.marker_params(stock)
df=self.api.get_history_minute_time_data(market=market,code=stock,date=date)
result=self.api.to_df(df)
i=1
return result
except:
self.connect()
def get_trader_data(self,stock='600031',start=0,count=10000):
'''
查询分笔成交
stock证券代码
start开始位置
count结束位置
'''
i=0
while i!=1:
try:
stock=stock[:6]
market=self.marker_params(stock=stock)
df=self.api.get_transaction_data(market=market,code=stock,start=start,count=count)
result=self.api.to_df(df)
i=1
return result
except:
self.connect()
def get_history_trader_data(self,stock='600031',start=0,count=100,date='20220920'):
'''
查询流逝分笔成交
stock证券代码
start开始位置
count结束位置
date时间
'''
i=0
while i!=1:
try:
stock=stock[:6]
marker=self.marker_params(stock=stock)
df=self.api.get_history_transaction_data(market=marker,code=stock,start=start,count=count,date=date)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_company_info_category(self,stock='600031'):
'''
查询公司信息目录
stock证券代码
'''
i=0
while i!=1:
try:
stock=stock[:6]
marker=self.marker_params(stock=stock)
df=self.api.get_company_info_category(market=marker,code=stock)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_xdxr_info(self,stock='600031'):
'''
读取除权除息信息
stock证券代码
'''
i=0
while i!=1:
try:
stock=stock[:6]
marker=self.marker_params(stock=stock)
df=self.api.get_xdxr_info(marker=marker,code=stock)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_finance_info(self,stock='600031'):
'''
读取财务信息
stock证券代码
'''
i=0
while i!=1:
try:
stock=stock[:6]
marker=self.marker_params(stock=stock)
df=self.api.get_finance_info(marker=marker,code=stock)
result=self.api.to_df(df)
i=1
return result
except:
self.next_connect()
def get_stock_hist_data_em(self,stock='600031',start_date='20210101',end_date='20500101',data_type='D'):
'''
获取股票数据
start_date=''默认上市时间
- ``1`` : 分钟
- ``5`` : 5 分钟
- ``15`` : 15 分钟
- ``30`` : 30 分钟
- ``60`` : 60 分钟
- ``101`` : 日
- ``102`` : 周
- ``103`` : 月
fq=0股票除权
fq=1前复权
fq=2后复权
'''
data_dict = {'1': '1', '5': '5', '15': '15', '30': '30', '60': '60', 'D': '101', 'W': '102', 'M': '103'}
klt=data_dict[data_type]
if stock[0] =='6':
stock = '1.' +stock
else:
stock = '0.' + stock
url = 'http://push2his.eastmoney.com/api/qt/stock/kline/get?'
params = {
'fields1': 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13',
'fields2': 'f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61',
'beg': start_date,
'end': end_date,
'ut': 'fa5fd1943c7b386f172d6893dbfba10b',
'rtntype':end_date,
'secid': stock,
'klt':klt,
'fqt': '0',
'cb': 'jsonp1668432946680'
}
res = requests.get(url=url, params=params)
text = res.text[19:len(res.text) - 2]
json_text = json.loads(text)
try:
df = pd.DataFrame(json_text['data']['klines'])
df.columns = ['数据']
data_list = []
for i in df['数据']:
data_list.append(i.split(','))
data = pd.DataFrame(data_list)
columns = ['date', 'open', 'close', 'high', 'low', 'volume', '成交额', '振幅', '涨跌幅', '涨跌额', '换手率']
data.columns = columns
for m in columns[1:]:
data[m] = pd.to_numeric(data[m])
data.sort_index(ascending=False,ignore_index=True,inplace=True)
return data
except:
pass
if __name__=='__main__':
data=tdx_data()
data.connect()
print(data.get_history_minute_time_data())}
在股票市场交易的动态环境中,技术和金融的融合催生了分析市场趋势和预测未来价格走势的先进方法。本文将使用Python进行股票模式识别。from collections import defaultdict
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import argrelextrema
from statsmodels.nonparametric.kernel_regression import KernelReg
from yahoofinancials import YahooFinancials上面的库中,有几个要点需要介绍:collections.defaultdict:当缺少键时,返回默认值。使用它可以有效地存储和组织数据,比如键反映日期或资产符号等可识别的度量,值表示相应的变量。argrelextrema函数是SciPy库中的一个函数,用于进行科学计算和技术计算。它有助于识别价格数据中的局部最大值和最小值,指示价格数据中的潜在转折点或支撑位和阻力位。statsmodels.nonparametric.kernel_regression.KernelReg:这个来自statmodels的子模块提供了非参数核回归功能。交易可以使用这种方法来拟合价格数据的平滑曲线,以确定趋势,无需假设曲线具有特定的参数形式。YahooFinancials:该模块从雅虎财经获取财务数据。我们可以访问大量的财务数据,包括股票价格,财务报表和其他市场数据,用于分析和决定如何处理投资组合。start_date = '2017-01-01'
end_date = '2017-12-31'
stock_code = 'FB' # e.g. AMZN, GOOG, FB, NVDA我们获取的股票数据是在2017-01-01至2017-12-31期间。作为stock_code变量,Facebook, Inc.被设置为FB,即股票的代码。在指定的日期范围内,交易算法将执行该股票代码的数据分析、交易信号或实际交易等操作。此代码的目的是为交易算法建立基本参数:目标时间框架和交易的特定股票。变量最终将在代码中用于获取历史数据、执行财务分析和回溯测试交易策略。对于任何专注于股票市场的交易系统,这些参数是评估历史表现和执行实时交易的关键输入。def preprocess_data(start_date, end_date, stock_code):
stock_data = YahooFinancials(stock_code).get_historical_price_data(start_date, end_date, 'daily')
price_data = stock_data[stock_code]['prices']
columns = ['formatted_date', 'open', 'high', 'low', 'close', 'adjclose', 'volume']
new_columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
df = pd.DataFrame(data=price_data)[columns] # order dataframe columns
df = df.rename(index=str, columns=dict(zip(columns, new_columns))) # rename dataframe columns
return df, df['Close'], df['Date']preprocess_data有三个参数:start_date、end_date和stock_code,它们指定时间范围和股票类型。此函数的主要目标是从Financials检索给定股票的指定日期范围内的历史股票价格。获取包括全面的金融信息,包括每日股票价格、开盘价、最高价和最低价,以及调整后的收盘价。获得数据后,将其组织到pandas DataFrame中,通过重命名列,可以实现更好的可读性和与通用财务数据标准的一致性。该函数返回处理后的DataFrame以及两个Series一维数组,其中包括收盘价和收盘价发生的日期。df, prices, dates = preprocess_data(start_date, end_date, stock_code)
prices.index = np.linspace(1, len(prices), len(prices))
dates.index = np.linspace(1, len(dates), len(dates))我们为两组数据(价格和日期)设置索引。然后就是对价格的分析和局部最大值和最小值的识别,这对交易者来说是非常宝贵的。代码采用了一个核心回归模型,消除价格的周期性波动,从而更容易发现重要的趋势。# https://onlinelibrary.wiley.com/doi/full/10.1111/0022-1082.00265
# reference: https://www.quantopian.com/posts/an-empirical-algorithmic-evaluation-of-technical-analysis
def find_max_min(prices):
model = KernelReg(prices.values, prices.index.values, var_type='c', bw='cv_ls')
smooth_prices = pd.Series(data=model.fit([prices.index.values])[0], index=prices.index) # index also from 1
# use the minima and maxima from the smoothed timeseries
# to identify true local minima and maxima in the original timeseres
# by taking the maximum/minimum price within a t-1, t+1 window in the smoothed timeseries
smooth_prices_max_indices = argrelextrema(smooth_prices.values, np.greater)[0]
smooth_prices_min_indices = argrelextrema(smooth_prices.values, np.less)[0]
price_max_indices = []
for i in smooth_prices_max_indices:
if 1 < i < len(prices)-1:
price_max_indices.append(prices.iloc[i-2:i+2].idxmax())
price_min_indices = []
for i in smooth_prices_min_indices:
if 1 < i < len(prices)-1:
price_min_indices.append(prices.iloc[i-2:i+2].idxmin())
price_max = prices.loc[price_max_indices]
price_min = prices.loc[price_min_indices]
max_min = pd.concat([price_max, price_min]).sort_index()
max_min = max_min[~max_min.duplicated()] # deduplicate points that are both maximum and minimum
max_min
return smooth_prices, smooth_prices_max_indices, smooth_prices_min_indices, \
price_max_indices, price_min_indices, max_min使用一种算法来识别基于平滑价格数据的价格曲线改变方向的点,代码在这个平滑的时间序列中搜索相对最大值和最小值。代码试图在平滑数据中找到这些极值后,将这些极值映射回原始的非平滑价格数据。它通过检查平滑数据中每个极值点周围的小窗口来实现这一点,并确定该窗口内的价格最高或最低-这些是真正的局部最大值和最小值。在平滑和窗口化处理完成之后,代码将这些点组织到一个内聚输出中,删除可能同时存在于最大值和最小值的任何重复点。可以使用这个结果来确定交易的进入和退出点。除了在代码中使用外,该代码还可以用于更大的策略中,根据这些发现触发买入或卖出信号。smooth_prices, smooth_prices_max_indices, smooth_prices_min_indices, \
price_max_indices, price_min_indices, max_min = find_max_min(pricessmooth_prices包含平滑版本的价格数据,可以消除噪音,使趋势更容易识别。有各种各样的技术可用于平滑,包括移动平均线和其他算法。变量smooth_prices_max_indices和smooth_prices_min_indices可能表示平滑价格指数在局部最大值和最小值列表中的位置。当价格达到这些水平时,在价格反转之前识别潜在的买入或卖出信号是至关重要的。与前面的变量一样,price_max_indices和price_min_indices是从原始的、未平滑的价格中计算出来的。max_min可能是一个数组或列表,其中包含有关已识别的最大值和最小值的信息,可能结合平滑和非平滑数据,用于根据本地价格极值确定是否进入或退出头寸。可以分析金融价格数据,识别峰值和低谷,并准备数据用于算法交易。作为更大的技术分析系统的一部分,它可以用于基于历史价格模式的自动交易活动。下面我们看看上面代码计算得到的结果:fig, ax = plt.subplots(figsize=(20,10), dpi=200)
ax.plot(dates, prices, label='Prices')
ax.plot(dates, smooth_prices, label='Smoothed Prices', linestyle='dashed')
ax.set_xticks(np.arange(0, len(dates), 30))
smooth_prices_max = smooth_prices.loc[smooth_prices_max_indices]
smooth_prices_min = smooth_prices.loc[smooth_prices_min_indices]
price_max = prices.loc[price_max_indices]
price_min = prices.loc[price_min_indices]
ax.scatter(dates.loc[smooth_prices_max.index], smooth_prices_max.values, s=20, color='red', label='Smoothed Prices Maxima')
ax.scatter(dates.loc[smooth_prices_min.index], smooth_prices_min.values, s=20, color='purple', label='Smoothed Prices Minima')
ax.scatter(dates.loc[price_max.index], price_max.values, s=50, color='green', label='Prices Maxima')
ax.scatter(dates.loc[price_min.index], price_min.values, s=50, color='blue', label='Prices Minima')
ax.legend(loc='upper left')
ax.grid()代码绘制了具有不同线条风格的实际价格和平滑价格。该图还显示了实际和平滑价格数据中局部最大值和最小值的位置,可能识别交易进入和退出信号。为了区分最大值和最小值,使用较大的符号和不同的颜色。时间轴每隔一段时间显示在x轴上,以使其更清晰。图表的图例解释了情节元素,网格有助于分析价格随时间的变化,这些都是在绘图中必不可少的工作。下面一个函数是Plot_window,它生成一个折线图,显示实际价格和平滑价格随时间的变化。平滑可能有助于识别趋势并过滤掉噪声。在这张图上可以区分出几个关键点。颜色和大小用于识别实际和平滑价格曲线的局部最大值和最小高点和低点。交易策略通常关注这些关键点,因为它们可能预示着趋势的逆转或继续。def plot_window(dates, prices, smooth_prices,
smooth_prices_max_indices, smooth_prices_min_indices,
price_max_indices, price_min_indices,
start, end, ax=None):
if ax is None: fig, ax = plt.subplots(figsize=(20,10), dpi=200)
ax.plot(dates.loc[start:end], prices.loc[start:end], label='Prices')
ax.plot(dates.loc[start:end], smooth_prices.loc[start:end], label='Smoothed Prices', linestyle='dashed')
ax.set_xticks(np.linspace(0, len(dates.loc[start:end]), 10))
ax.tick_params(axis='x', rotation=45)
smooth_prices_max = smooth_prices.loc[smooth_prices_max_indices].loc[start:end]
smooth_prices_min = smooth_prices.loc[smooth_prices_min_indices].loc[start:end]
price_max = prices.loc[price_max_indices].loc[start:end]
price_min = prices.loc[price_min_indices].loc[start:end]
ax.scatter(dates.loc[smooth_prices_max.index], smooth_prices_max.values, s=20, color='red', label='Smoothed Prices Maxima')
ax.scatter(dates.loc[smooth_prices_min.index], smooth_prices_min.values, s=20, color='purple', label='Smoothed Prices Minima')
ax.scatter(dates.loc[price_max.index], price_max.values, s=50, color='green', label='Prices Maxima')
ax.scatter(dates.loc[price_min.index], price_min.values, s=50, color='blue', label='Prices Minima')
ax.legend(fontsize='small')
ax.grid()可以在较大的数据集中指定一个从开始到结束的时间窗口,这样可以查看数据的子集。为了清晰起见,在x轴上显示日期的同时还显示了一个图例和一个网格。plot_window(dates, prices, smooth_prices,
smooth_prices_max_indices, smooth_prices_min_indices,
price_max_indices, price_min_indices,
start=18, end=34, ax=None)下面我们可以寻找一些简单的模式:def find_patterns(max_min):
patterns = defaultdict(list)
for i in range(5, len(max_min)):
window = max_min.iloc[i-5:i]
# pattern must play out in less than 36 days
if window.index[-1] - window.index[0] > 35:
continue
# Using the notation from the paper to avoid mistakes
e1, e2, e3, e4, e5 = window.iloc[:5]
rtop_g1 = np.mean([e1, e3, e5])
rtop_g2 = np.mean([e2, e4])
# Head and Shoulders
if (e1 > e2) and (e3 > e1) and (e3 > e5) and \
(abs(e1 - e5) <= 0.03*np.mean([e1,e5])) and \
(abs(e2 - e4) <= 0.03*np.mean([e1,e5])):
patterns['HS'].append((window.index[0], window.index[-1]))
# Inverse Head and Shoulders
elif (e1 < e2) and (e3 < e1) and (e3 < e5) and \
(abs(e1 - e5) <= 0.03*np.mean([e1,e5])) and \
(abs(e2 - e4) <= 0.03*np.mean([e1,e5])):
patterns['IHS'].append((window.index[0], window.index[-1]))
# Broadening Top
elif (e1 > e2) and (e1 < e3) and (e3 < e5) and (e2 > e4):
patterns['BTOP'].append((window.index[0], window.index[-1]))
# Broadening Bottom
elif (e1 < e2) and (e1 > e3) and (e3 > e5) and (e2 < e4):
patterns['BBOT'].append((window.index[0], window.index[-1]))
# Triangle Top
elif (e1 > e2) and (e1 > e3) and (e3 > e5) and (e2 < e4):
patterns['TTOP'].append((window.index[0], window.index[-1]))
# Triangle Bottom
elif (e1 < e2) and (e1 < e3) and (e3 < e5) and (e2 > e4):
patterns['TBOT'].append((window.index[0], window.index[-1]))
# Rectangle Top
elif (e1 > e2) and (abs(e1-rtop_g1)/rtop_g1 < 0.0075) and \
(abs(e3-rtop_g1)/rtop_g1 < 0.0075) and (abs(e5-rtop_g1)/rtop_g1 < 0.0075) and \
(abs(e2-rtop_g2)/rtop_g2 < 0.0075) and (abs(e4-rtop_g2)/rtop_g2 < 0.0075) and \
(min(e1, e3, e5) > max(e2, e4)):
patterns['RTOP'].append((window.index[0], window.index[-1]))
# Rectangle Bottom
elif (e1 < e2) and (abs(e1-rtop_g1)/rtop_g1 < 0.0075) and \
(abs(e3-rtop_g1)/rtop_g1 < 0.0075) and (abs(e5-rtop_g1)/rtop_g1 < 0.0075) and \
(abs(e2-rtop_g2)/rtop_g2 < 0.0075) and (abs(e4-rtop_g2)/rtop_g2 < 0.0075) and \
(max(e1, e3, e5) > min(e2, e4)):
patterns['RBOT'].append((window.index[0], window.index[-1]))
return patterns迭代DataFrame中的条目,同时考虑5个数据点。确定每个5点窗口的模式是否在36天内发生。如果没有,则进入到下一个窗口。我们这里有几种类型的技术分析图表模式:Head and Shoulders(头肩顶):这是一种反转图表模式,通常表示股价在涨势中即将反转。它包括一个中间峰(头)和两个较低的峰(肩),形成一个上升趋势的结束信号。Inverse Head and Shoulders(倒头肩底):与头肩顶相反,这是一种底部反转图表模式。它包括一个中间洼地(倒头)和两个较低的洼地(倒肩),形成一个下降趋势的结束信号。Broadening Top(扩顶形态):这是一种表示不稳定市场的图表模式,由两个趋势线分散开来形成。它可能表示市场波动性增加,预示价格的不确定性。Broadening Bottom(扩底形态):与扩顶形态相反,这是一种表示不稳定市场的图表模式,由两个趋势线逐渐汇聚。它可能表示市场波动性增加,预示价格的不确定性。Triangle Top(三角形顶部):这是一种形成在上升趋势中的图表模式,由两个趋势线收敛形成三角形。它可能表示价格即将下降。Triangle Bottom(三角形底部):与三角形顶部相反,这是一种形成在下降趋势中的图表模式,由两个趋势线收敛形成三角形。它可能表示价格即将上升。Rectangle Top(矩形顶部):这是一种在上升趋势中形成的图表模式,由水平线形成一个矩形。它表示市场可能经历一段横盘整理,价格可能会下跌。Rectangle Bottom(矩形底部):与矩形顶部相反,这是一种在下降趋势中形成的图表模式,由水平线形成一个矩形。它表示市场可能经历一段横盘整理,价格可能会上升。上面的这些模式是根据这些局部最大值和最小值的相对位置和值来识别的,检测到的每个模式都存储在一个字典中,模式名称作为键,窗口的开始和结束索引作为值。这些索引元组存储在每个模式末尾的字典中。这样的代码在算法交易中很有用,当它自动检测与某些市场行为相关的历史模式时,允许交易者根据这些模式的存在做出明智的决策。patterns = find_patterns(max_min)
patterns上面这些专有名字可能不太容易理解,所以我们可以使用代码把它们进行可视化def visualize_patterns(dates, prices, smooth_prices,
smooth_prices_max_indices, smooth_prices_min_indices,
price_max_indices, price_min_indices,
patterns, shorthand_fullname_dict):
for name, end_day_nums in patterns.items():
print('Pattern Identified: {} \nNumber of Observations: {}'.format(shorthand_fullname_dict[name], len(end_day_nums)))
rows = int(np.ceil(len(end_day_nums)/2))
fig, axes = plt.subplots(rows, 2, figsize=(20,5*rows), dpi=200)
fig.subplots_adjust(hspace=0.5)
axes = axes.flatten()
i = 0
for start_date, end_date in end_day_nums:
plot_window(dates, prices, smooth_prices,
smooth_prices_max_indices, smooth_prices_min_indices,
price_max_indices, price_min_indices,
start=start_date-1, end=end_date+1, ax=axes[i])
i += 1
plt.show()
visualize_patterns(dates, prices, smooth_prices,
smooth_prices_max_indices, smooth_prices_min_indices,
price_max_indices, price_min_indices,
patterns, shorthand_fullname_dict)可以看到各种模式是不一样的,这对我们这些刚入门金融行业的人来说更容易理解。通过图形化分析股票价格走势和算法识别模式的工具,可以有助于我们更直观地理解市场行为随着时间的推移,这对算法交易至关重要。作者:Nickolas Discolll}

我要回帖

更多关于 通达信filter过滤函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信