Exemplo n.º 1
0
def 每日浮动收益统计(symbol:str, Type:str):
    from data_reader import get_index_day, get_stock_day, get_index_future_day, get_comm_future_day
    import pymssql

    start_date = str(date_list.iloc[1])[:10]
    end_date = str(date_list.iloc[-1])[:10]

    print("Fetching data from SQL database...")
    if Type=='stock':
        close = get_stock_day(symbol, start_date, end_date, freq = '1D')
    elif Type=='index':
        close = get_index_day(symbol, start_date, end_date, freq = '1D')
    elif Type=='index_future':
        close = get_index_future_day(symbol, start_date, end_date, freq = '1D')
    elif Type=='comm_future':
        close = get_comm_future_day(symbol, start_date, end_date, freq = '1D')
    elif Type=='coin':
        conn = pymssql.connect(server='192.168.0.28', port=1433, user='******', password='******', database='rawdata',charset = 'GBK')
        SQL = "select * from coin.dbo.coin_1min where "+"date between "+"\'"+start_date+"\' and \'" + end_date+"\'"
        temp_data = pd.read_sql(SQL, conn)
        close = temp_data.set_index('date')
        close = close.resample('1D').last()
    elif Type[:3] == 'AIP':
        if Type[-5:] == 'stock':
            close = get_stock_day(symbol, start_date, end_date, freq = '1D')
        elif Type[-5:] == 'index':
            close = get_index_day(symbol, start_date, end_date, freq = '1D')
        else:raise TypeError('Type "{}" is NOT acceptable, please check your input.'.format(Type))
    else:
        raise TypeError('Type "{}" is NOT acceptable, please check your input.'.format(Type))

    close.index = close.index + pd.Timedelta(24, unit='h')
    record = []
    截止当日的累计盈利, comfirmed_profit = 0, 0
    for day in close.index:
        float_profit = 0
        for deal in deal_list:
            if deal.is_valid(day):
                float_profit += deal.cal_floating(close.loc[day]['sclose'])
                deal.floating_profit=deal.cal_floating(close.loc[day]['sclose'])
            elif day.date()==deal.end.date():
                #如果是当天结束的,当天确认收益
                deal.confirm_profit()
                comfirmed_profit+=deal.profit
                #deal_list.remove(deal)
        截止当日的累计盈利 = comfirmed_profit + float_profit
        #print(day, int(float_profit), int(comfirmed_profit), int(截止当日的累计盈利),sep='\t')
        record.append((day, float_profit, comfirmed_profit, 截止当日的累计盈利))
    ans=pd.DataFrame(record,columns=('date','floating_profit','comfirmed_profit','accumulated_profit'))
    ans=ans.set_index('date')
    if Type.endswith('future'):
        choice = input("You are using futures;\nDo you want to multiply amount by 200?\nInput 1 for YES, 0 for NO: ")
        if choice=='0': future_multiplier = 1
        else: future_multiplier = 200
        ans[['floating_profit','comfirmed_profit','accumulated_profit']]*=future_multiplier
    ans['当日盈亏']=ans.accumulated_profit.diff()
    return ans
Exemplo n.º 2
0
    def init_data(self, type):
        """
        Fetch Data
        ----------
        获取收盘数据
        
        Type 0 for "index"; 1 for "stock"

        """
        print("Fetching Historical Data...")
        if type == 1:
            price = get_muti_close_day(self.POOL,
                                       self.START_DATE,
                                       self.END_DATE,
                                       adjust=True)
        elif type == 0:
            price = {}
            for symbol in self.POOL:
                price[symbol] = get_index_day(symbol, self.START_DATE,
                                              self.END_DATE).sclose
            price = pd.DataFrame(price)
        price.fillna(method="ffill", inplace=True)
        print("Historical Data Loaded!")

        self.price = price
        del price
Exemplo n.º 3
0
 def 每日浮动收益统计(symbol='', Type='stock'):
     from data_reader import get_index_day, get_stock_day, get_index_future_day
     
     start_date = str(date_list[0])[:10]
     end_date = str(date_list[-1])[:10]
     # 如果未指定代码,则从ts原始文件中选取
     if symbol=='': symbol = pd.read_excel(f, sheet_name=-1,header=2).columns[1]
     if Type=='stock':
         close = get_stock_day(symbol, start_date, end_date, freq = '1D')
     elif Type=='index':
         close = get_index_day(symbol, start_date, end_date, freq = '1D')
     elif Type=='future':
         close = get_index_future_day(symbol, start_date, end_date, freq = '1D')
     close.index=close.index+pd.Timedelta(15,unit='h')
     record=[]
     截止当日的累计盈利=0
     comfirmed_profit=0
     for day in close.index:
         float_profit=0
         for deal in deal_list:
             if deal.is_valid(day):
                 float_profit += deal.cal_floating(close.loc[day]['close'])
                 deal.floating_profit=deal.cal_floating(close.loc[day]['close'])
             elif day.date()==deal.end.date():
                 #如果是当天结束的,当天确认收益
                 deal.confirm_profit()
                 comfirmed_profit+=deal.profit
                 #deal_list.remove(deal)
         截止当日的累计盈利 = comfirmed_profit + float_profit
         #print(day, int(float_profit), int(comfirmed_profit), int(截止当日的累计盈利),sep='\t')
         record.append((day, (float_profit), (comfirmed_profit), (截止当日的累计盈利)))
     ans=pd.DataFrame(record,columns=('date','floating_profit','comfirmed_profit','accumlated_profit'))
     ans=ans.set_index('date')
     ans['当日盈亏']=ans.accumlated_profit.diff()
     return ans
Exemplo n.º 4
0
def 每日浮动收益统计(symbol:str, Type:str):
    from data_reader import get_index_day, get_stock_day, get_index_future_day, get_comm_future_day
    
    start_date = str(date_list[0])[:10]
    end_date = str(date_list[-1])[:10]
    
    if Type=='stock':
        close = get_stock_day(symbol, start_date, end_date, freq = '1D')
    elif Type=='index':
        close = get_index_day(symbol, start_date, end_date, freq = '1D')
    elif Type=='index_future':
        close = get_index_future_day(symbol, start_date, end_date, freq = '1D')
    elif Type=='comm_future':
        close = get_comm_future_day(symbol, start_date, end_date, freq = '1D')
    else:
        print('Type is NOT acceptable, please check your input.')
        quit()

    close.index = close.index + pd.Timedelta(15, unit='h')
    record = []
    截止当日的累计盈利, comfirmed_profit = 0, 0
    for day in close.index:
        float_profit = 0
        for deal in deal_list:
            if deal.is_valid(day):
                float_profit += deal.cal_floating(close.loc[day]['sclose'])
                deal.floating_profit=deal.cal_floating(close.loc[day]['sclose'])
            elif day.date()==deal.end.date():
                #如果是当天结束的,当天确认收益
                deal.confirm_profit()
                comfirmed_profit+=deal.profit
                #deal_list.remove(deal)
        截止当日的累计盈利 = comfirmed_profit + float_profit
        #print(day, int(float_profit), int(comfirmed_profit), int(截止当日的累计盈利),sep='\t')
        record.append((day, float_profit, comfirmed_profit, 截止当日的累计盈利))
    ans=pd.DataFrame(record,columns=('date','floating_profit','comfirmed_profit','accumlated_profit'))
    ans=ans.set_index('date')
    if Type.endswith('future'):
        choice = input("You are using futures;\nDo you want to multiply amount by 200?\nInput 1 for YES, 0 for NO: ")
        if choice=='0': future_multiplier = 1
        else: future_multiplier = 200
        ans[['floating_profit','comfirmed_profit','accumlated_profit']]*=future_multiplier
    ans['当日盈亏']=ans.accumlated_profit.diff()
    return ans
Exemplo n.º 5
0
# pylint: disable=E1101,E1103
# pylint: disable=W0212,W0231,W0703,W0622
CAPITAL = 5e7
TODAY = datetime.date.today().strftime('%Y-%m-%d')

# 获取标的数据
underLying = 'hs300'  #zz500
if underLying == 'hs300':
    conn = pymssql.connect(server='192.168.0.28',
                           port=1433,
                           user='******',
                           password='******',
                           database='WIND')
    SQL = '''SELECT b.code FROM HS300COMPWEIGHT as b
    where b.[Date] BETWEEN '2018-07-01' and '2018-07-03' ORDER BY b.code'''
    hs300 = get_index_day('000300.SH', '2007-02-01', TODAY, '1D')
    hs300 = hs300.sclose
elif underLying == 'zz500':
    conn = pymssql.connect(server='192.168.0.28',
                           port=1433,
                           user='******',
                           password='******',
                           database='RawData')
    SQL = '''SELECT b.code FROM [中证500成分权重] as b
    where b.[Date] BETWEEN '2018-06-21' and '2018-07-03' '''
    zz500 = get_index_day('000905.SH', '2007-02-01', TODAY, '1D')
    zz500 = zz500.sclose
elif underLying == 'hsi':
    hsi = get_hk_index_day('HSI.HI', '2007-02-01', TODAY, '1D')
    hsi = hsi.sclose
elif underLying == 'zz800':
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import talib
from data_reader import get_muti_close_day, get_index_day, get_hk_index_day
import pymssql
# pylint: disable=E1101,E1103
# pylint: disable=W0212,W0231,W0703,W0622
# 获取标的数据
underLying = 'hs300'#zz500
if underLying == 'hs300':
    conn = pymssql.connect(server='192.168.0.28', port=1433, user='******', password='******', database='WIND')
    SQL = '''SELECT b.code FROM HS300COMPWEIGHT as b
    where b.[Date] BETWEEN '2018-07-01' and '2018-07-03' ORDER BY b.code'''
    hs300 = get_index_day('000300.SH', '2007-02-01', datetime.date.today().strftime('%Y-%m-%d'), '1D')
    hs300 = hs300.sclose
elif underLying == 'zz500':
    conn = pymssql.connect(server='192.168.0.28', port=1433, user='******', password='******', database='RawData')
    SQL = '''SELECT b.code FROM [中证500成分权重] as b
    where b.[Date] BETWEEN '2018-06-21' and '2018-07-03' '''
    zz500 = get_index_day('000905.SH', '2007-02-01', datetime.date.today().strftime('%Y-%m-%d'), '1D')
    zz500 = zz500.sclose
elif underLying == 'hsi':
    hsi = get_hk_index_day('HSI.HI', '2007-02-01', datetime.date.today().strftime('%Y-%m-%d'), '1D')
    hsi = hsi.sclose
elif underLying == 'zz800':
    conn1 = pymssql.connect(server='192.168.0.28', port=1433, user='******', password='******', database='WIND')
    SQL1 = '''SELECT b.code FROM HS300COMPWEIGHT as b where b.[Date] BETWEEN '2018-07-01' and '2018-07-03' ORDER BY b.code'''
    conn2 = pymssql.connect(server='192.168.0.28', port=1433, user='******', password='******', database='RawData')
    SQL2 = '''SELECT b.code FROM [中证500成分权重] as b where b.[Date] BETWEEN '2018-06-21' and '2018-07-03' '''
Exemplo n.º 7
0
Mlow = pd.DataFrame([row for row in M_low(data)],
                    columns=['date', '600000.SH']).set_index('date')
M = Mhigh - Mlow
Ret20 = Ret(data, 20)

#factors = pd.concat([Mhigh.set_index('date'), Mlow.set_index('date')],axis=1)

#%% 获取价格数据
price = get_muti_close_day(selection.columns,
                           '2009-03-31',
                           '2018-11-30',
                           freq='M',
                           adjust=-1)  # 回测时还是使用前复权价格
priceFill = price.fillna(method='ffill')
price_change = priceFill.diff()
hs300 = get_index_day('000300.SH', '2009-4-30', '2018-11-30', 'M').sclose
szzz = get_index_day('000001.SH', '2009-4-30', '2018-11-30', 'M').sclose
zz500 = get_index_day('000905.SH', '2009-4-30', '2018-11-30', 'M').sclose
IC = pd.read_hdf("monthlyData-over10B.h5", 'IC')
IF = pd.read_hdf("monthlyData-over10B.h5", 'IF')

#%% 股票选择
isBig = (mv.T > mv.T.quantile(0.8)).T
isSmall = (mv.T < mv.T.quantile(0.3)).T


def reindex_fill_gap(inputDataFrame, defaultIndex=price.index):
    return inputDataFrame.reindex(defaultIndex, method='ffill')


selection = reindex_fill_gap(selection)
YEAR = 2017
START = '%s-11-01' % (YEAR - 1)  #前三个月的数据需要用来计算RSI
TODAY = '%s-12-31' % YEAR  #datetime.date.today().strftime('%Y-%m-%d')

# 获取标的数据
underLying = 'hs300'  #zz500
if underLying == 'hs300':
    conn = pymssql.connect(server='10.0.0.51',
                           port=1433,
                           user='******',
                           password='******',
                           database='WIND')
    SQL = '''SELECT b.code FROM HS300COMPWEIGHT as b
    where b.[Date] BETWEEN '%s-07-01' and '%s-07-30' ORDER BY b.code''' % (
        YEAR, YEAR)
    hs300 = get_index_day('000300.SH', START, TODAY, '1D')
    hs300 = hs300[str(YEAR)].sclose

if underLying == 'hs300' or underLying == 'zz500':
    data = pd.read_sql(SQL, conn)
pool = list(data['code'])
del data


#
def 获取数据():
    START_DATE = START
    END_DATE = TODAY
    print('正在获取数据...自 {} 至 {}'.format(START_DATE, END_DATE))
    price = get_muti_close_day(pool,
                               START_DATE,
Exemplo n.º 9
0
    elif symbol == 'SH':
        symbol = '000001.SH'
    elif symbol == 'ZZ500':
        symbol = '000905.SH'
    elif symbol == 'CYB':
        symbol = '399006.SZ'
    elif symbol == "HS300":
        symbol = '399300.sz'
    elif symbol in ['HSI.HI', 'HSCEI.HI']:
        symbol = symbol.split('.')[0]
        temp = get_HKindex_min(symbol, start_date, end_date, freq='1W')
        close[index] = temp.sclose
        symbols.append(symbol)
        continue
    try:
        temp = get_index_day(symbol, start_date, end_date, freq='1W')
        close[index] = temp.sclose
        print(symbol, 'success!')
    except:
        print(symbol, "failed")
    symbols.append(symbol)
close = pd.DataFrame(close, index=pe.index)

#%% 计算移动平均线,相对PE
rpe = {}
mean = {}
for index in indexes:
    #计算移动平均线
    mean[index] = close[index].rolling(5).mean()
    #计算相对PE
    temp = pe[index] / pe[index][1]
Exemplo n.º 10
0
benchmark = pd.DataFrame(index=close_data.index)

benchmark = pd.concat(
    [
        benchmark,
        close_data["benchmark_close"],
        open_data["benchmark_open"],
        high_data["benchmark_high"],
        low_data["benchmark_low"],
        vol_data["benchmark_vol"],
    ],
    axis=1,
)

time_data = rd.get_index_day("000001.SH", "2010-01-01", "2019-01-01", "1D")


close_data = close_data / close_data.iloc[0]

stock_pool = close_data.columns[:-1]

TRAIN_SIZE = 1200
TIME_SERIES = 150
BATCH_SIZE = 200
TRAIN_DATE = "2015-01-01"
LAYER = 2
HOLDING = 1

holding_return = (close_data.shift(-HOLDING) - close_data) / close_data
holding_return = holding_return.dropna()
Exemplo n.º 11
0
START_DATE = "2008-01-01"
END_DATE = datetime.date.today().strftime('%Y-%m-%d')
INITIAL_CAPITAL = 1000
# CAPITAL = INITIAL_CAPITAL / 3
DURATION = 250
Profit_Ceiling = [0.4, 0.2]  #止盈线
Trailing_Percentage = 0.2  #优先止盈百分比

# %% 获取收盘数据
if TYPE == 1:
    price = get_muti_close_day(STOCK_POOL, START_DATE, END_DATE)
# df = get_index_day('600519.SH',START_DATE,END_DATE)
elif TYPE == 0:
    price = {}
    for symbol in STOCK_POOL:
        price[symbol] = get_index_day(symbol, START_DATE, END_DATE).sclose
    price = pd.DataFrame(price)
price.fillna(method="ffill", inplace=True)
print("Historical Price Loaded!")

# %% 技术指标
price.fillna(value=0, inplace=True)  # 把早年暂未上市的股票价格填充为0
price_diff = price.diff()
price_pct_chg = price.pct_change()

price_std = price.rolling(window=DURATION).std()  # 计算标准差
R = (price / price.shift(1)).apply(np.log)
sigma = R.rolling(window=DURATION).std()
mu = (price / price.shift(DURATION)).apply(np.log) + 0.5 * np.square(sigma)

# %% 策略部分 分配仓位
Exemplo n.º 12
0
def 每日浮动收益统计(symbol: str, Type: str):
    from data_reader import get_index_day, get_stock_day, get_index_future_day, get_comm_future_day
    import pymssql

    start_date = str(date_list.iloc[1] -
                     pd.Timedelta(30, 'd'))[:10]  # 早30天,为了使用RSI数据
    end_date = str(date_list.iloc[-1])[:10]
    print("[Time Range]", start_date, "~", end_date)

    print("Fetching data from SQL database...")
    if Type == 'stock':
        close = get_stock_day(symbol, start_date, end_date, freq='1D')
    elif Type == 'index':
        close = get_index_day(symbol, start_date, end_date, freq='1D')
    elif Type == 'index_future':
        close = get_index_future_day(symbol, start_date, end_date, freq='1D')
    elif Type == 'comm_future':
        close = get_comm_future_day(symbol, start_date, end_date, freq='1D')
    elif Type == 'coin':
        conn = pymssql.connect(server='192.168.0.28',
                               port=1433,
                               user='******',
                               password='******',
                               database='rawdata',
                               charset='GBK')
        SQL = "select * from coin.dbo.coin_1min where " + "date between " + "\'" + start_date + "\' and \'" + end_date + "\'"
        temp_data = pd.read_sql(SQL, conn)
        close = temp_data.set_index('date')
        close = close.resample('1D').last()
    elif Type == 'AIP':
        close = get_stock_day(symbol, start_date, end_date, freq='1D')
    else:
        print('Type is NOT acceptable, please check your input.')
        quit()
    close.index = close.index + pd.Timedelta(24, unit='h')
    print("SQL data loaded...")

    print("统计当日盈亏中...")
    t0 = time.time()
    record = []
    截止当日的累计盈利, comfirmed_profit = 0, 0
    for day in close.index[30:]:  #close数据提前了30天
        float_profit, 累计投入 = 0, 0
        for deal in deal_list:
            if (deal.start < day) and (deal.end > day):
                累计投入 += deal.investment
            if deal.is_valid(day):
                float_profit += deal.cal_floating(close.loc[day]['sclose'])
                deal.floating_profit = deal.cal_floating(
                    close.loc[day]['sclose'])
            elif day.date() == deal.end.date():
                #如果是当天结束的,当天确认收益
                deal.confirm_profit()
                comfirmed_profit += deal.profit
                #deal_list.remove(deal)
        截止当日的累计盈利 = comfirmed_profit + float_profit
        #print(day, int(float_profit), int(comfirmed_profit), int(截止当日的累计盈利),sep='\t')
        record.append((day, float_profit, comfirmed_profit, 截止当日的累计盈利, 累计投入))
    ans = pd.DataFrame(record,
                       columns=('date', 'floating_profit', 'comfirmed_profit',
                                'accumulated_profit',
                                'accumulated_investment'))
    ans = ans.set_index('date')
    if Type.endswith('future'):
        choice = input(
            "You are using futures;\nDo you want to multiply amount by 200?\nInput 1 for YES, 0 for NO: "
        )
        if choice == '0': future_multiplier = 1
        else: future_multiplier = 200
        ans[['floating_profit', 'comfirmed_profit',
             'accumulated_profit']] *= future_multiplier
    ans['当日盈亏'] = ans.accumulated_profit.diff()
    t1 = time.time()
    tpy = t1 - t0
    print('统计当日盈亏已完成,用时 %5.3f 秒' % tpy)
    return ans, close
Exemplo n.º 13
0
import matplotlib.pyplot as plt
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from data_reader import get_muti_close_day, get_index_day
TODAY = datetime.date.today().strftime('%Y-%m-%d')

#dataset_train = pd.read_csv('NSE-TATAGLOBAL.csv')
#training_set = dataset_train.iloc[:, 1:2].values
hs300 = get_index_day('000300.SH', '2007-02-01', TODAY, '1D')
training_set, testing_set = train_test_split(hs300, test_size=0.1, shuffle=False)

# 处理数据
training_set = training_set.sclose.values
training_set = training_set.reshape(-1, 1) 
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)

#%% 数据集构造
X_train = []
y_train = []
step = 60
for i in range(step, len(training_set)):
   X_train.append(training_set_scaled[i-step:i, 0])
   y_train.append(training_set_scaled[i, 0])