def tur10_dataPre(qx,xnam0,ksgn0):
    ''' 策略.数据预处理函数 说明
    
    Args:
        qx (zwQuantX): zwQuantX数据包 
        xnam0 (str):函数标签
        ksgn0 (str): 价格列名称,一般是'adj close'
        '''

    zwx.sta_dataPre0xtim(qx,xnam0);
    #----对各只股票数据,进行预处理,提高后期运算速度
    ksgn,qx.priceCalc=ksgn0,ksgn0;  #'adj close';
    for xcod in zw.stkLibCode:
        d20=zw.stkLib[xcod];
        
        #  计算交易价格kprice和策略分析采用的价格dprice,kprice一般采用次日的开盘价
        #d20['dprice']=d20['open']*d20[ksgn]/d20['close']
        d20['dprice']=d20['close']
        #d20['kprice']=d20['dprice'].shift(-1)
        d20['kprice']=d20['dprice']
        #
        d=qx.staVars[0];d20=zwta.MA(d20,d,ksgn);
        d=qx.staVars[1];d20=zwta.MA(d20,d,ksgn);
        #
        zw.stkLib[xcod]=d20;
        if qx.debugMod>0:
            print(d20.tail())    
            #---
            fss='tmp\\'+qx.prjName+'_'+xcod+'.csv'
            d20.to_csv(fss)   
示例#2
0
def SMA_dataPre(qx, xnam0, ksgn0):
    ''' 簡單均線策略資料預處理函數
    
    Args:
        qx (zwQuantX): zwQuantX數據包 
        xnam0 (str):函數標籤
        ksgn0 (str): 價格列名稱,一般是'adj close'
            
    :ivar xcod (int): 股票代碼
    '''

    zwx.sta_dataPre0xtim(qx, xnam0)
    #----對各檔股票資料,進行預處理,提高後期運算速度
    ksgn, qx.priceCalc = ksgn0, ksgn0
    #'adj close';
    for xcod in zw.stkLibCode:
        d20 = zw.stkLib[xcod]

        #  計算交易價格kprice和策略分析採用的價格dprice,kprice一般採用次日的開盤價
        d20['dprice'] = d20['open'] * d20[ksgn] / d20['close']
        d20['kprice'] = d20['dprice'].shift(-1)
        #d20['kprice']=d20['dprice']
        #
        d = qx.staVars[0]
        d20 = zwta.MA(d20, d, ksgn)
        d = qx.staVars[1]
        d20 = zwta.MA(d20, d, ksgn)
        #
        zw.stkLib[xcod] = d20
        if qx.debugMod > 0:
            print(d20.tail())
            #---
            fss = 'tmp\\' + qx.prjName + '_' + xcod + '.csv'
            d20.to_csv(fss)
示例#3
0
def SMA_dataPre(qx, xnam0, ksgn0):
    ''' 简单均线策略数据预处理函数
    
    Args:
        qx (zwQuantX): zwQuantX数据包 
        xnam0 (str):函数标签
        ksgn0 (str): 价格列名称,一般是'adj close'
            
    :ivar xcod (int): 股票代码
    '''

    zwx.sta_dataPre0xtim(qx, xnam0)
    #----对各只股票数据,进行预处理,提高后期运算速度
    ksgn, qx.priceCalc = ksgn0, ksgn0
    #'adj close';
    for xcod in zw.stkLibCode:
        d20 = zw.stkLib[xcod]

        #  计算交易价格kprice和策略分析采用的价格dprice,kprice一般采用次日的开盘价
        d20['dprice'] = d20['open'] * d20[ksgn] / d20['close']
        d20['kprice'] = d20['dprice'].shift(-1)
        #d20['kprice']=d20['dprice']
        #
        d = qx.staVars[0]
        d20 = zwta.MA(d20, d, ksgn)
        d = qx.staVars[1]
        d20 = zwta.MA(d20, d, ksgn)
        #
        zw.stkLib[xcod] = d20
        if qx.debugMod > 0:
            print(d20.tail())
            #---
            fss = 'tmp\\' + qx.prjName + '_' + xcod + '.csv'
            d20.to_csv(fss)
示例#4
0
def tim0Trade_dataPre(qx, xnam0):
    # 设置当前策略的变量参数,此处是5、30日的MA日均线数据
    qx.staName = xnam0
    qx.rfRate = 0.05;  # 无风险年收益,一般为0.05(5%),计算夏普指数等需要
    # qx.stkNum9=20000;   #每手交易,最多20000股
    #
    # 按指定的时间周期,裁剪数据源
    # xtim0=parse('9999-01-01');xtim9=parse('1000-01-01');
    # xtim0=xtim0.strftime('%Y-%m-%d');xtim9=xtim9.strftime('%Y-%m-%d')
    # qx.qxTimSet(xtim0,xtim9)
    # zwx.stkLibSet8XTim(qx.xtim0,qx.xtim9);#    print('zw.stkLibCode',zw.stkLibCode)
    # ============
    # ---设置qxUsr用户数据
    qx.qxUsr = zwx.qxObjSet(qx.xtim0, 0, qx.money, 0);
    # ----对各只股票数据,进行预处理,提高后期运算速度
    ksgn = 'adj close';
    for xcod in zw.stkLibCode:
        d20 = zw.stkLib[xcod];
        #  计算交易价格kprice和策略分析采用的价格dprice,kprice一般采用次日的开盘价
        # d20['dprice']=d20['open']*d20[ksgn]/d20['close']
        d20['dprice'] = d20[ksgn]
        # d20['kprice']=d20['dprice'].shift(-1)
        d20['kprice'] = d20['dprice']
        #
        d = qx.staVars[0];
        d20 = zwta.MA(d20, d, ksgn);
        d = qx.staVars[1];
        d20 = zwta.MA(d20, d, ksgn);
        #
        zw.stkLib[xcod] = d20;
        print(d20.tail())
        # ---
        fss = 'tmp\\' + qx.prjName + '_' + xcod + '.csv'
        d20.to_csv(fss)
def CMA_dataPre(qx,xnam0,ksgn0):
    ''' 均线交叉策略数据预处理函数
    
    Args:
        qx (zwQuantX): zwQuantX数据包 
        xnam0 (str):函数标签
        ksgn0 (str): 价格列名称,一般是'adj close'
        '''

    zwx.sta_dataPre0xtim(qx,xnam0);
    #----对各只股票数据,进行预处理,提高后期运算速度
    ksgn,qx.priceCalc=ksgn0,ksgn0;  #'adj close';
    for xcod in zw.stkLibCode:
        d20=zw.stkLib[xcod];
        #---------------dprice,kprice
        #d20['dprice']=d20['open']*d20['adj close']/d20['close']
        d20['dprice']=d20[ksgn]
        d20['kprice']=d20['dprice']
        #d20['kprice']=d20['dprice'].shift(-1)
        #
        d=qx.staVars[0];d20=zwta.MA(d20,d,ksgn);k0ma='ma_%d' %qx.staVars[0]
        #d=qx.staVars[1];d20=zwta.MA(d20,d,ksgn);k1ma='ma_%d' %qx.staVars[1]
        #
        #d20['ma1n']=d20[k0ma].shift(1)
        d20['ma2n']=d20[k0ma].shift(2)
        
        #d20['dp1n']=d20['dprice'].shift(1)
        d20['dp2n']=d20['dprice'].shift(2)
        #---
        d20=np.round(d20,3);
        zw.stkLib[xcod]=d20;
        if qx.debugMod>0:
            print(d20.tail())
            fss='tmp\\'+qx.prjName+'_'+xcod+'.csv'
            d20.to_csv(fss)
def tim0Trade_dataPre(qx,xnam0,ksgn0):    
    zwx.sta_dataPre0xtim(qx,xnam0);
    #-------
    ksgn,qx.priceCalc=ksgn0,ksgn0;  #'adj close';
    for xcod in zw.stkLibCode:
        d20=zw.stkLib[xcod];
        #  计算交易价格kprice和策略分析采用的价格dprice,kprice一般采用次日的开盘价
        #d20['dprice']=d20['open']*d20[ksgn]/d20['close']
        d20['dprice']=d20[ksgn]
        #d20['kprice']=d20['dprice'].shift(-1)
        d20['kprice']=d20['dprice']
        #
        d=qx.staVars[0];d20=zwta.MA(d20,d,ksgn);
        d=qx.staVars[1];d20=zwta.MA(d20,d,ksgn);
        #
        zw.stkLib[xcod]=d20;
        if qx.debugMod>0:
            print(d20.tail())    
            fss='tmp\\'+qx.prjName+'_'+xcod+'.csv'
            d20.to_csv(fss)        
示例#7
0
def CMA_dataPre(qx, xnam0, ksgn0):
    ''' 均線交叉策略資料預處理函數
    
    Args:
        qx (zwQuantX): zwQuantX數據包 
        xnam0 (str):函數標籤
        ksgn0 (str): 價格列名稱,一般是'adj close'
        '''

    zwx.sta_dataPre0xtim(qx, xnam0)
    #----對各檔股票資料,進行預處理,提高後期運算速度
    ksgn, qx.priceCalc = ksgn0, ksgn0
    #'adj close';
    for xcod in zw.stkLibCode:
        d20 = zw.stkLib[xcod]
        #---------------dprice,kprice
        #d20['dprice']=d20['open']*d20['adj close']/d20['close']
        d20['dprice'] = d20[ksgn]
        d20['kprice'] = d20['dprice']
        #d20['kprice']=d20['dprice'].shift(-1)
        #
        d = qx.staVars[0]
        d20 = zwta.MA(d20, d, ksgn)
        k0ma = 'ma_%d' % qx.staVars[0]
        #d=qx.staVars[1];d20=zwta.MA(d20,d,ksgn);k1ma='ma_%d' %qx.staVars[1]
        #
        #d20['ma1n']=d20[k0ma].shift(1)
        d20['ma2n'] = d20[k0ma].shift(2)

        #d20['dp1n']=d20['dprice'].shift(1)
        d20['dp2n'] = d20['dprice'].shift(2)
        #---
        d20 = np.round(d20, 3)
        zw.stkLib[xcod] = d20
        if qx.debugMod > 0:
            print(d20.tail())
            fss = 'tmp\\' + qx.prjName + '_' + xcod + '.csv'
            d20.to_csv(fss)
示例#8
0
# -*- coding: utf-8 -*-

import numpy as np
import scipy as sp

import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import zw_talib as ta
#

#----------code.init
mpl.style.use('seaborn-whitegrid')

#============main

fss = "dat\\AAPL-201x.csv"
df = pd.read_csv(fss, encoding='gbk')
#df=ta.MA(df,5);
df = ta.MA(df, 10, 'Close')
df = ta.MA(df, 30, 'Close')
df = ta.MA(df, 50, 'Close')
print(df.tail())

df['Close'].plot(figsize=(15, 5))
#df['ma_5'].plot();
df['ma_10'].plot()
df['ma_30'].plot()
df['ma_50'].plot()
plt.legend(ncol=5)