def get_price(self):
        price = get_stock_price(self.stock)

        # today, yesterday, current, high, low
        today = float(price[1])
        yesterday = float(price[2])
        current = float(price[3])
        high = float(price[4])
        low = float(price[5])
        turnover = int(float(price[9])/100000000.0)
        name = price[0].split('"')[-1]

        r = str(rate(current, yesterday))[:4]

        if not self.last_price:
            self.forward = '--'
        elif current > self.last_price:
            self.forward = '/\\'
        elif current < self.last_price:
            self.forward = '\/'
        else:
            pass

        self.last_price = current
        return u'{} {} ({}% {}) {} {}'.format(self.name, self.stock, r, current, self.forward, turnover)
Exemple #2
0
def get_priceMom(rebalDate, codes):

    past = ut.get_recentBday(rebalDate - datetime.timedelta(365))
    recent = ut.get_recentBday(rebalDate - datetime.timedelta(30))
    rebalDate = ut.get_recentBday(rebalDate)
    prices = ut.get_stock_price(codes, past, rebalDate )
    mom_12M = (prices.loc[rebalDate, :] / prices.loc[past, :]) - 1
    mom_1M = (prices.loc[rebalDate, :] / prices.loc[recent, :]) - 1
    momData = (mom_12M - mom_1M).dropna()
    return momData   
    def get_price(self):
        price = get_stock_price(self.stock)

        # today, yesterday, current, high, low
        today = float(price[1])
        yesterday = float(price[2])
        current = float(price[3])
        high = float(price[4])
        low = float(price[5])
        volume = float(price[8])
        name = price[0].split('"')[-1]

        if today == '0.00':
            return u'{}(0.00)'.format(self.stock)

        r = rate(current, yesterday)

        if not self.available and r < 9.9:
            self.reopen = True

        if r > 9.9:
            self.available = False
        else:
            self.available = True

        if len(self.five_price) == 5:
            self.five_price.pop(0)
        self.five_price.append(r)

        if len(self.five_price) > 1:
            forward = self.five_price[-1] - self.five_price[-2]
        else:
            forward = '0.00'

        r = str(r)[:4]
        forward = str(forward)[:4]

        buy = int(price[10]) + int(price[12]) + int(price[14]) + int(price[16]) + int(price[18])
        sell = int(price[20]) + int(price[22]) + int(price[24]) + int(price[26]) + int(price[28])

        if sell == 0:
            bs = float(buy) / float(1)
            if volume > 0:
                buy_rate = buy /volume
        else:
            bs = float(buy) / float(sell)

        if bs > 1:
            bs = str(bs)[:4]
            return u'{}({} {})({})'.format(self.stock, r, forward, bs)
        else:
            return u''
def get_priceMom(codes, rebalDate):
    
    yearAgo = rebalDate - datetime.timedelta(days=365)
    monthAgo = rebalDate - datetime.timedelta(days=30)
    
    yearAgo = util.get_recentBday(yearAgo)
    monthAgo = util.get_recentBday(monthAgo)
    rebalDate = util.get_recentBday(rebalDate)
    
    prices = util.get_stock_price(codes, yearAgo, rebalDate)
    
    mom_12M = (prices.loc[rebalDate, :] / prices.loc[yearAgo, :]) - 1
    mom_1M = (prices.loc[rebalDate, :] / prices.loc[monthAgo, :]) - 1
    
    momData = (mom_12M - mom_1M).dropna()
    
    return momData                    
Exemple #5
0
def find():
    count = 0
    rank = []
    for stock in get_all_stock():
        price = get_stock_price(stock)

        # today, yesterday, current, high, low
        today = float(price[1])
        yesterday = float(price[2])
        current = float(price[3])
        high = float(price[4])
        low = float(price[5])
        volume = float(price[8])
        name = price[0].split('"')[-1]

        if today == '0.00':
            continue

        r = rate(current, yesterday)

        buy = int(price[10]) + int(price[12]) + int(price[14]) + int(price[16]) + int(price[18])
        sell = int(price[20]) + int(price[22]) + int(price[24]) + int(price[26]) + int(price[28])

        if sell == 0:
            bs = float(buy) / float(1)
        else:
            bs = float(buy) / float(sell)

        if volume > 0:
            buy_rate = buy / volume

        ###############

        if r > 9.9:
            count += 1
        else:
            if name.find(u'中') == 0:
                print u'{} {} {}'.format(stock, name, r)

    print count
def get_backtest_history(dollar_inv, rebalData, roundup=False, tradeCost=0.01):

    dollar_ongoing = dollar_inv
    basket_history = {}
    rebal_date = list(set(rebalData.date))
    rebal_date.sort()
    tradeCost_history = {}
    turnover_history = {}
    weight_history = {}
    inv_money_list = [dollar_inv]
    inv_money_date = [rebal_date[0]]

    for i in tqdm(range(len(rebal_date))):

        if i < len(rebal_date) - 1:

            rebalDate = rebal_date[i]  # 리밸런싱 시점
            stock_list_i = rebalData[rebalData.date ==
                                     rebal_date[i]].code  # 기간별 종목 리스트
            w_ = rebalData[rebalData.date == rebal_date[i]].weight
            weightData = pd.concat([stock_list_i, w_],
                                   axis=1).set_index('code')

            if i == 0:
                weightData['money'] = weightData[
                    'weight'] * dollar_inv  # 1기인 경우 초기 투자금으로 시작
            else:
                weightData['money'] = weightData[
                    'weight'] * dollar_ongoing  # 1기 이후는 투자금의 매 기간별 마지막 시점에서의 금액 재투자

            stock_price_ = util.get_stock_price(
                stock_list_i, util.get_recentBday(rebalDate),
                util.get_recentBday(
                    rebalDate)).transpose()  # 해당 일자의 바스켓의 주가 추출

            weightData = pd.merge(
                weightData,
                stock_price_,
                how='inner',
                left_on=weightData.index,
                right_on=stock_price_.index).set_index('key_0')

            weightData.columns = ['weight', 'money', 'price']
            weightData['n_stocks'] = weightData['money'] / weightData['price']

            if roundup == True:  # 초기 투입자금으로 투자가능한 종목별 주식수 (정수로 내림)
                weightData.n_stocks = np.floor(weightData.n_stocks)
            else:
                weightData.n_stocks = weightData.n_stocks

            basket_price = util.get_basket_history(
                weightData.index.values, weightData.n_stocks.values,
                util.get_recentBday(rebal_date[i]),
                util.get_recentBday(rebal_date[i + 1]))

            dollar_ongoing = basket_price.iloc[-1, :].values[
                0]  # 투자금의 매 기간별 마지막 시점에서의 포트의 가치(금액)

            # 투자기간 마지막 시점 (다음리밸) 에서의 가격
            last_price = util.get_stock_price(
                weightData.index.values,
                util.get_recentBday(rebal_date[i + 1]),
                util.get_recentBday(rebal_date[i + 1])).transpose()

            weightData = pd.merge(weightData,
                                  last_price,
                                  how='inner',
                                  left_on=weightData.index,
                                  right_on=last_price.index).set_index('key_0')

            weightData.columns = [
                'weight', 'money', 'price', 'n_stocks', 'last_price'
            ]

            weightData['last_weight'] = (
                weightData.last_price * weightData.n_stocks) / (
                    weightData.last_price *
                    weightData.n_stocks).sum()  # 투자기간의 마지막 시점에서의 비중
            new_weight = rebalData[rebalData.date == rebal_date[
                i + 1]].set_index('code')['weight']  # 새로운 비중
            new_weight = pd.Series(new_weight, name='new_weight')
            weightData = pd.concat([weightData, new_weight], axis=1)
            weightData['new_weight'] = weightData['new_weight'].fillna(0)
            weightData['weight'] = weightData['weight'].fillna(0)

            tradingCost = np.abs(weightData.new_weight - weightData.last_weight
                                 ).sum() * tradeCost * dollar_ongoing

            dollar_ongoing -= tradingCost
            basket_history[rebalDate] = basket_price
            inv_money_list += list(basket_price.values.squeeze()[1:-1])
            inv_money_list += [dollar_ongoing]
            inv_money_date += list(basket_price.index[1:])
            tradeCost_history[''.join([
                x for x in str(rebal_date[i + 1])[:10] if x != '-'
            ])] = tradingCost
            turnover_history[''.join([
                x for x in str(rebal_date[i + 1])[:10] if x != '-'
            ])] = tradingCost / dollar_ongoing
            weight_history[''.join([
                x for x in str(rebal_date[i + 1])[:10] if x != '-'
            ])] = weightData

        elif i == len(rebal_date) - 1:

            rebalDate = rebal_date[i]  # 리밸런싱 시점
            #print(rebalDate)
            stock_list_i = rebalData[rebalData.date ==
                                     rebal_date[i]].code  # 기간별 종목 리스트
            lastDay = datetime.datetime.today() - datetime.timedelta(2)
            w_ = rebalData[rebalData.date == rebal_date[i]].weight
            weightData = pd.concat([stock_list_i, w_],
                                   axis=1).set_index('code')

            if i == 0:
                weightData['money'] = weightData[
                    'weight'] * dollar_inv  # 1기인 경우 초기 투자금으로 시작
            else:
                weightData['money'] = weightData[
                    'weight'] * dollar_ongoing  # 1기 이후는 투자금의 매 기간별 마지막 시점에서의 금액 재투자

            stock_price_ = util.get_stock_price(
                stock_list_i, util.get_recentBday(rebalDate),
                util.get_recentBday(
                    rebalDate)).transpose()  # 해당 일자의 바스켓의 주가 추출

            weightData = pd.merge(
                weightData,
                stock_price_,
                how='inner',
                left_on=weightData.index,
                right_on=stock_price_.index).set_index('key_0')

            weightData.columns = ['weight', 'money', 'price']
            weightData['n_stocks'] = weightData['money'] / weightData['price']

            if roundup == True:  # 초기 투입자금으로 투자가능한 종목별 주식수 (정수로 내림)
                weightData.n_stocks = np.floor(weightData.n_stocks)
            else:
                weightData.n_stocks = weightData.n_stocks

            basket_price = util.get_basket_history(
                weightData.index.values, weightData.n_stocks.values,
                util.get_recentBday(rebal_date[i]),
                util.get_recentBday(lastDay))

            dollar_ongoing = basket_price.iloc[-1, :].values[
                0]  # 투자금의 매 기간별 마지막 시점에서의 포트의 가치(금액)
            #print(inv_money_list)

            # 투자기간 마지막 시점 (다음리밸) 에서의 가격
            last_price = util.get_stock_price(
                weightData.index.values, util.get_recentBday(lastDay),
                util.get_recentBday(lastDay)).transpose()

            weightData = pd.merge(weightData,
                                  last_price,
                                  how='inner',
                                  left_on=weightData.index,
                                  right_on=last_price.index).set_index('key_0')

            weightData.columns = [
                'weight', 'money', 'price', 'n_stocks', 'last_price'
            ]
            weightData['last_weight'] = (
                weightData.last_price * weightData.n_stocks) / (
                    weightData.last_price *
                    weightData.n_stocks).sum()  # 투자기간의 마지막 시점에서의 비중
            weightData['new_weight'] = 0
            tradingCost = 0
            dollar_ongoing -= tradingCost
            basket_history[rebalDate] = basket_price
            inv_money_list += list(basket_price.values.squeeze()[1:-1])
            inv_money_list += [dollar_ongoing]
            inv_money_date += list(basket_price.index[1:])
            tradeCost_history[''.join(
                [x for x in str(lastDay)[:10] if x != '-'])] = tradingCost
            turnover_history[''.join([
                x for x in str(lastDay)[:10] if x != '-'
            ])] = tradingCost / dollar_ongoing
            weight_history[''.join([x for x in str(lastDay)[:10]
                                    if x != '-'])] = weightData

    inv_money_history = pd.DataFrame(inv_money_list, index=inv_money_date)

    return inv_money_history, tradeCost_history, turnover_history, weight_history
    pd.read_excel('info.xlsx', sheet_name='market_M'))
sector = util.data_cleansing_ts(
    pd.read_excel('info.xlsx', sheet_name='sector_M'))
risk_1 = util.data_cleansing_ts(
    pd.read_excel('info.xlsx', sheet_name='risk_1_M'))
risk_2 = util.data_cleansing_ts(
    pd.read_excel('info.xlsx', sheet_name='risk_2_M'))
inK200 = util.data_cleansing_ts(
    pd.read_excel('info.xlsx', sheet_name='inK200_M'))
inKQ150 = util.data_cleansing_ts(
    pd.read_excel('info.xlsx', sheet_name='inKQ150_M'))

# 종목 및 BM 가격 데이터
allFirm = market.columns.values
rPrice_firm = util.get_stock_price(
    allFirm,
    pd.to_datetime(market.index.values[0]) - datetime.timedelta(days=365),
    market.index.values[-1])
#rPrice_bm = util.get_index_price(['I.001', 'I.101'], market.index.values[0], market.index.values[-1])
return_20D = rPrice_firm.pct_change(20)

##############################################################################
# 1. 투자 유니버스 구성
##############################################################################


def getUniverse(marketInfo,
                volInfo,
                riskInfo_1,
                riskInfo_2,
                rebalDate_,
                tradeVolLimit=10):
__author__ = 'yu'

from pymongo import MongoClient
from util import get_all_stock, get_stock_price, boll_daily

c = MongoClient('121.199.5.143').stock

for stock in get_all_stock():
    print stock

    price = get_stock_price(stock)

    # today, yesterday, current, high, low
    today = float(price[1])
    yesterday = float(price[2])
    current = float(price[3])
    high = float(price[4])
    low = float(price[5])
    name = price[0].split('"')[-1]
    date = price[30]
    volume = price[8]

    c.history.update({
        'date': date,
        'stock': stock
    }, {
        '$set': {
            'date': date,
            'open': today,
            'high': high,
            'low': low,
Exemple #9
0
mpl.style.use('bmh')
import pandas_datareader.data as web
import matplotlib.pylab as plt
from datetime import datetime
import statsmodels.api as sm
from pykalman import KalmanFilter
from math import sqrt

path = 'C:/Woojin/###. Git/Project_Q/III. Factor Exposed Pairs Trading'
os.chdir(path)

#import backtest_pipeline_ver2 as bt
import util

codes = pd.read_excel('firmCode.xlsx')['Code'].values
price = util.get_stock_price(codes, pd.to_datetime('2010-01-01'),
                             pd.to_datetime('2019-06-30'))
size = util.data_cleansing(pd.read_excel('firmSize.xlsx'))


#NOTE CRITICAL LEVEL HAS BEEN SET TO 5% FOR COINTEGRATION TEST
def find_cointegrated_pairs(dataframe, critial_level=0.05):
    n = dataframe.shape[1]  # the length of dateframe
    pvalue_matrix = np.ones((n, n))  # initialize the matrix of p
    keys = dataframe.columns  # get the column names
    pairs = []  # initilize the list for cointegration
    for i in range(n):
        for j in range(i + 1, n):  # for j bigger than i
            stock1 = dataframe[keys[i]]  # obtain the price of "stock1"
            stock2 = dataframe[keys[j]]  # obtain the price of "stock2"
            result = sm.tsa.stattools.coint(stock1,
                                            stock2)  # get conintegration