Beispiel #1
0
def stocks_corr_analyzation(days, stock, *compare_stocks):
    ''' analyze the pct_change corr between stocks '''
    compare_stock_data = {}
    for s in compare_stocks:
        try:
            compare_stock_data[s.name] = get_recent_data(s.code,
                                                         s.market_code,
                                                         days,
                                                         update=False)
        except Exception as e:
            print_err(e)

    df_compare_stocks_pct = DataFrame(
        {stock: df['Adj Close']
         for stock, df in compare_stock_data.items()}).pct_change()

    if stock is not None:
        if stock in compare_stocks:
            ser_stock_pct = df_compare_stocks_pct[stock.name]
            df_compare_stocks_pct.drop(columns=stock.name, inplace=True)
        else:
            df = get_recent_data(stock.code,
                                 stock.market_code,
                                 days,
                                 update=False)
            ser_stock_pct = df['Adj Close'].pct_change()

        ser = df_compare_stocks_pct.corrwith(ser_stock_pct)
        # 筛选出0.5以上的,并格式化
        ser = ser[ser >= 0.5].map(lambda x: '{:.2f}'.format(x))
        ser.name = '涨跌相关性'
        return ser.to_frame()
    else:
        df = df_compare_stocks_pct.corr()
        return df.applymap(lambda x: '{:.2f}'.format(x))
Beispiel #2
0
def start_scrapy():
    count = 0
    stocks = Stock.objects.filter()[count:]
    print('[+] Total {} stocks '.format(stocks.count()))
    for s in stocks:
        try:
            count += 1
            print('[+] Scraping the {} item'.format(count))
            print('[+] stock_code: {}, market_code: {}'.format(
                s.code, s.market_code))
            get_recent_data(s.code, s.market_code, 400)
            print('[+] scrapy success.')
            print('[+] sleep a random time...' + '\n')
            time.sleep(random.random())
        except Exception as e:
            print('[-] Error happened.')
            print_err(e)
Beispiel #3
0
def beta(stock_code, index_code, market_code):
    ''' 贝塔系数 '''

    stock365 = get_recent_data(stock_code, market_code, 365, update=False)
    if stock365.shape[0] < 200:
        raise DataMissingError

    index365 = get_recent_data(index_code, market_code, 365, update=False)
    if index365.shape[0] < 200:
        raise DataMissingError

    s200 = stock365[:200]
    i200 = index365[:200]
    ser_s_pct = s200['Adj Close'].pct_change()
    ser_i_pct = i200['Adj Close'].pct_change()

    # 标准差
    s_std = ser_s_pct.std()
    i_std = ser_i_pct.std()
    #  相关系统
    corr = ser_s_pct.corr(ser_i_pct)
    # 贝塔
    b = corr * (s_std / i_std)
    return '{:.2f}'.format(b)
Beispiel #4
0
def volume_mean(stock_code, market_code):
    '''计算最近90,30,7交易日的平均成交量'''
    _df365 = get_recent_data(stock_code, market_code, 365, update=False)
    if _df365.shape[0] < 90:
        raise DataMissingError

    df90 = _df365[:90]
    df30 = _df365[:30]
    df7 = _df365[:7]
    # 平均成交量
    v_mean_30 = df30['Volume'].mean()
    v_mean_90 = df90['Volume'].mean()
    v_mean_7 = df7['Volume'].mean()

    row_v_mean = Series([v_mean_90, v_mean_30,
                         v_mean_7]).map(lambda x: '{:.2f}'.format(x / 1000000))

    return DataFrame(columns=['90日', '30日', '7日'],
                     index=['平均成交量/万手'],
                     data=[row_v_mean.values])
Beispiel #5
0
 def recent_data(self, days):
     return sdm.get_recent_data(self.code, self.market_code, days)
Beispiel #6
0
        return self.col.delete_many(kwargs or {})

    def create_index(self, col_name, unique):
        return self.col.create_index([(col_name, pymongo.DESCENDING)],
                                     unique=unique)

    def is_index_exist(self, col_name):
        return '{}_1'.format(col_name) in self.col.index_information()

    def drop_index(self, col_name):
        return self.col.drop_index('{}_1'.format(col_name))


if __name__ == '__main__':
    # set up django env
    import os
    import sys
    import django

    pathname = os.path.dirname(os.path.abspath(__file__))
    sys.path.insert(0, pathname)
    sys.path.insert(0, os.path.abspath(os.path.join(pathname, '../..')))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "a_stock.settings")
    django.setup()

    import stock_analyze.helper.stock_data_manager as sd

    df = sd.get_recent_data('000001', 1, 90)
    sc = get_stock_collection(sd.get_stock_request_code('000001', 1))
    sc.insert_dataframe(df)