Loading ...

干货-选币系统-选币因子

Author: 混沌量化-蔯葰葳, Date: 2022-02-12 11:13:58
Tags:


import numpy as np
from scipy.stats import norm
from sklearn import preprocessing
import json

#计算波动率因子值
def GetAtrFactorService(records):
    atrlength = 14
    atrs = TA.ATR(records, atrlength)
    acs = sorted(range(len(atrs)), key=lambda k: atrs[k])
    ac = acs[-1]
    arr_mean = np.mean(acs)
    arr_std = np.std(acs,ddof=1)
    p = norm.cdf(x=ac, loc=arr_mean, scale = arr_std)
    #P值越大,波动性越大
    atrFactor = _N(p,3)
    return atrFactor 

#计算机构的行为痕迹因子值        
def GetITFactorService(records):
    #本福特定律分布频数
    PN = [301, 176, 125, 97, 79, 67, 58, 51, 46]
    FN = [0, 0, 0, 0, 0, 0, 0, 0, 0]
    for i in range(len(records)):
        valume = records[i]['Volume']*10000
        strValume = str(valume)
        num = strValume[0]
        for j in range(len(FN)):
            key = j + 1
            if int(num) == key:
                FN[j] += 1
    if sum(FN) == 0:
        FN = PN
    X = 0
    for i in range(len(PN)):
        X += (FN[i] - PN[i])**2
    ITFactor = X
    #X 的数值越大,则成交量数据与本福特理想分布的偏离越大,机构的行为痕迹也越大
    return ITFactor

#计算价格因子值
def GetPriceFactorService(records):
    record = records[-1]
    price = record["Close"]
    PFactor = 1/price 
    #PFactor数值越大,则价格越小 
    return PFactor 

#标准化处理   --计算出来的因子值因为数量级不同,要进行标准化处理。暂不处理空值、异常值
def StandardizedService(factor):
    # 标准化处理
    factorArray = np.asarray(factor)
    factorArray = preprocessing.scale(factorArray)
    factor = factorArray.tolist()

ext.GetAtrFactorService = GetArtFactorService 
ext.GetITFactorService = GetITFactorService 
ext.GetPriceFactorService = GetPriceFactorService 
ext.StandardizedService = StandardizedService

More