def signal(gpdm, gpmc, start, m1, m, n, in_threshold, de_threshold): tdxday = Tdxday(gpdm) ohlc = tdxday.get_qfqdata(start=start) if ohlc.empty: return None ohlc.ta.doublebottom(append=True, m1=m1, m=m, n1=n1, n=n, in_threshold=in_threshold, de_threshold=de_threshold) sc = get_gpfl(gpdm)[0].upper() gpdm = f'{gpdm}.{sc}' gpmc1 = get_gpmc_fromdb(gpdm) ohlc = pd.merge(ohlc, gpmc1, on='date', how='outer') ohlc = ohlc.sort_index() ohlc['gpmc'].fillna(method='ffill', inplace=True) # 下句对于新股,数据库中还没有日期对应名称的情况进行处理 ohlc.loc[ohlc['gpmc'].isnull(), 'gpmc'] = gpmc ohlc = ohlc.assign(date=ohlc.index) start1 = '20180101' ohlc = ohlc.loc[(ohlc['date'] >= start1)] # ohlc1 = ohlc.loc[(ohlc['double_bott'] == 1)] # ohlc = selefirstsignal(ohlc) # if ohlc1.empty: # return None ohlc = ohlc.assign(gpdm=tdxday.gpdm) ohlc = ohlc.assign(ssdate=tdxday.ssdate) df = dfsortcolumns(ohlc, subset='gpdm,gpmc,ssdate,date,close') # 注意:df.iloc[-1]返回的是pandas.core.series.Series # 下句-1后面有冒号,返回的是pandas.core.frame.DataFrame。“:”不能少, # return df.iloc[-1:] return df
def signal(gpdm, start, m1, m, n, in_threshold, de_threshold): tdxday = Tdxday(gpdm) ohlc = tdxday.get_qfqdata(start=start) if ohlc.empty: return None ohlc.ta.doublebottom(append=True, m1=m1, m=m, n1=n1, n=n, in_threshold=in_threshold, de_threshold=de_threshold) try: # 新股由于交易天数少,无法计算,会出现没有返回 # double_bott,double_bott1列的情况 # signals = ohlc.loc[(ohlc['double_bott'] == 1)] # signals = ohlc.loc[(ohlc['double_bott1'] == 1)] signals = ohlc.loc[(ohlc['double_bott2'] == 1)] except: return None if not signals.empty: signals = signals.assign(date=signals.index) signals = signals.assign(gpdm=tdxday.gpdm) signals = signals.assign(gpmc=tdxday.gpmc) return dfsortcolumns(signals, subset='gpdm,gpmc,date') return None
def signal(gpdm): tdxday = Tdxday(gpdm) start = '20170101' ohlc = tdxday.get_qfqdata(start=start) if ohlc.empty: return None ohlc.ta.bottompattern(append=True) signals = ohlc.loc[(ohlc['BOTTOM_REVERSAL'] == 1)] if not signals.empty: signals['date'] = signals.index signals['gpdm'] = tdxday.gpdm signals['gpmc'] = tdxday.gpmc return signals[['date', 'gpdm', 'gpmc']] return None
columns.append(df.columns[match[0]]) return df[columns] if __name__ == '__main__': import sys filename = '603093' tdxday = Tdxday(filename) # stock = tdxday.get_data_yahoo(start='20180101') # stock = tdxday.get_qfqdata(start='20180101', otype='ohlc') # stock = tdxday.get_data_backtrader(start='20180101') # scdm = stock.scdm # stock.to_csv(f"{scdm}{filename}.csv") stock = tdxday.get_qfqdata(start='20130101') stock.ta.doublebottom(append=True, in_threshold=0.3, de_threshold=-0.2) stock.to_csv('tmp1.csv') sys.exit() stock.ta.alligator(append=True) stock.ta.macd(append=True) stock.ta.fractals(append=True) stock.ta.bottomreversal(append=True) scdm = stock.scdm stock.to_csv(f"{scdm}{filename}.csv") start = '20180501' end = '20181231' start, end = nd_today(180) stock = add_datenum(stock, start=start, end=end) lines = stock[[ 'ALLIGATOR_JAW_13_8', 'ALLIGATOR_TEETH_8_5', 'ALLIGATOR_LIPS_5_3'
for f_int, f_ext in zip(int_mask, ext_mask): obj = dataobj.get(f_ext) if isinstance(obj, pd.Series): df[f_int] = obj else: df[f_int] = None if any([isinstance(x, pd.Series) for x in list(df.values())]): return pd.DataFrame(df) return None if __name__ == '__main__': filename = '002456' tdxday = Tdxday(filename) ohlc = tdxday.get_qfqdata(start='20170101') sys.exit() # pybacktest.Backtest通过从python locals()提取交易原始数据 # pandas DataFrame的名称可以命名为'ohlc', 'bars', 'ohlcv' # 数据格式遵循Amibroker的命名约定,date(index),O(open),H(high), # L(low),C(close) # 信号Series的名称buy,sell,cover(Boy to Cover),short(Sell Short) ohlc = tdxday.get_data_pybacktest(start='20110101') ohlc.ta.fractals(high='H', low='L', open='O', close='C', append=True) ohlc.ta.bottomreversal(high='H', low='L', open='O', close='C', append=True) ohlc.ta.topreversal(high='H', low='L', open='O', close='C', append=True) scdm = ohlc.scdm ohlc.to_csv(f"{scdm}{filename}.csv") # buy买入,sell卖出,Buy to Cover 空单补回,Sell Short 卖空 # 由于融资融券风险较大,在此不做融券策略,也就是不配置cover和short
email: [email protected] QQ: 592440193 """ from stock_pandas.tdx.tdxdayread import Tdxday from stock_pandas.tdx.tdxcfg import Tdx from stock_pandas.tdx.class_func import * import pandas_ta as ta if __name__ == '__main__': filename = '600138' tdxday = Tdxday(filename) df2 = tdxday.get_qfqdata(start='20190101') df2.ta.bottomreversal(append=True) # df2.to_csv('tmp1.csv') # df2.ta.alligator(append=True) # df2 = df2.assign(bias = 100 * (df2['close'] - df2['ALLIGATOR_TEETH_8_5']) / df2['ALLIGATOR_TEETH_8_5']) # df2.ta.fractals(append=True) # df2=df2.assign(A=(df2['bias']>15),B=(df2['bias']<-15)) #if __name__ == '__main__': # tdxday = Tdxday(r'D:\tdx\sz002294.day') # df = tdxday.get_data() # # df2 = get_data(r'D:\tdx\szlday\sz002294.day') # # df3 = get_data(r'D:\tdx\sz002294.day')
QQ: 592440193 """ import pandas as pd import matplotlib.pyplot as plt import numpy as np from collections import OrderedDict from stock_pandas.tdx.tdxdayread import Tdxday from stock_pandas.tdx.tdxcfg import Tdx from stock_pandas.tdx.class_func import * import pandas_ta as ta filename = '600138' tdxday = Tdxday(filename) df = tdxday.get_qfqdata('20150101') rng_date = df.index df = df.reset_index() #重新按行号建立索引 def tick_category(frequency, step): ''' 刻度分类:根据频率和频数获取刻度的时间轴 frequency 可取值: 'year','month','day','hour','minute','second' ''' if frequency == 'year': df['frequency'] = rng_date.strftime('%Y') elif frequency == 'month': df['frequency'] = rng_date.strftime('%Y-%m')
z = (peaks[p] / troughs[t] - 1) * 100 if (zf is None) or zf < z: zf = z tt = t pp = p return tt.strftime('%Y-%m-%d'), pp.strftime('%Y-%m-%d'), round(zf, 2) if __name__ == "__main__": gpdm = '002451' tdxday = Tdxday(gpdm) start = '20180101' end = '20191231' df = tdxday.get_qfqdata(start=start, end=end) ds = round(df['close'], 4) sys.exit() # 波峰:大于前一,同时大于后一或等于后一且大于后二 p = (ds.diff(1) > 0) & ((ds.diff(-1) > 0) | ((ds.diff(-1) == 0) & (ds.diff(-2) > 0))) peaks = ds.loc[p] # 波谷:小于前一,同时小于后一或等于后一且小于后二 t = (ds.diff(1) < 0) & ((ds.diff(-1) < 0) | ((ds.diff(-1) == 0) & (ds.diff(-2) < 0))) troughs = ds.loc[t] # 起始点 start = pd.Series([ds[0]], index=[ds.index[0]]) end = pd.Series([ds[-1]], index=[ds.index[-1]]) # 最先是波峰,则将起点加入波谷 if peaks.index[0] < troughs.index[0]: # 最先是峰,将起点加入谷
for n in maxset: df1 = hhv(ds, n) df = df.join(df1) for n in minset: df1 = llv(ds, n) df = df.join(df1) # df = df.drop(columns=[name]) return df maxmin.__doc__ = \ ''' 思路:假定股价经过较长时间的盘整,近期拉升,最近回调。通过查找n天内和n1天内的高点 如果两个高点重合,即确定该高点为近期高点(位置idxmax)。再从idxmax往前查找m天内和 m1天内点近远两个低点(idxmin,idxmin1), ''' if __name__ == '__main__': from stock_pandas.tdx.tdxdayread import Tdxday from stock_pandas.tdx.class_func import * gpdm = '600734' start = '20180101' tdxday = Tdxday(gpdm) ohlc = tdxday.get_qfqdata(start=start) close = ohlc.close df = maxmin(ohlc.close)
# -*- coding: utf-8 -*- """ Created on 2019-10-06 20:41:34 author: huangyunbin email: [email protected] QQ: 592440193 """ import pandas as pd import matplotlib.pyplot as plt import numpy as np from collections import OrderedDict from stock_pandas.tdx.tdxdayread import Tdxday from stock_pandas.tdx.tdxcfg import Tdx from stock_pandas.tdx.class_func import * import pandas_ta as ta filename = '600138' tdxday = Tdxday(filename) df = tdxday.get_qfqdata() rng_date = df.index df = df.reset_index()