예제 #1
0
def priceChange_random(stock, time, volume, proportion=1000):
    """
    Updates the stockPool and hurstPool as price change
    Random +/- increment to Hurst
    """
    global stockPool, hurstPool

    increment = float(
        np.random.choice(np.linspace(-volume / proportion, volume / proportion,
                                     7),
                         size=1))

    if increment != 0:
        # print(volume)
        # print(increment)
        h0 = hurstPool[stock][time]
        numberNewPrices = len(stockPool[stock][time + 1:])
        p0 = (stockPool[stock][time])

        h1 = h0 + increment
        if h1 < 0.45:
            h1 = 0.45
        elif h1 > 0.75:
            h1 = 0.75

        hurstPool[stock, time + 1:] = (h1)
        fbmNew = fbm((h1), 2**14, 2**14)
        fbmNew = abs(fbmNew[:numberNewPrices] + float(p0))
        print('stock ', stock, ' original H', h0, ' to ', (h1))
        stockPool[stock][time + 1:] = np.asarray(fbmNew)
예제 #2
0
def generate_1d_fbms(N=10000,n=256,reCalc=False):
    #reCalc = True
    #reCalc = False
    if os.path.exists('data1d.bin') and not reCalc:
        print 'loading data from file'
        print 'disabling gc'
        gc.disable()
        Xtrain, Xtest, Ytrain, Ytest = pickle.load(open('data1d.bin','rb'),pickle.HIGHEST_PROTOCOL)
        gc.enable()
    else:
        print 'generating data and saving'
        np.random.seed(0)
        X = []
        H = np.linspace(0.05,0.8,N)# [0.5]*N
        for i in H:
            fbmr,fgnr,times= fbm(n-1,i,L=1)
            fbmr = fbmr-np.min(fbmr)
            fbmr = fbmr/np.max(fbmr)
            X.append(fbmr)
        X0=np.array(X)
        H = np.array(H)
        Y = H
        X=np.expand_dims(X0,2) # to make input of size (n,1) instead of just (n)
        Xtrain, Xtest, Ytrain, Ytest = train_test_split(X0,Y,test_size=0.2,random_state=0)

        pickle.dump([Xtrain,Xtest,Ytrain,Ytest],open('data1d.bin','w'))
예제 #3
0
def priceChange_updown(stock, time, volume, proportion=10000):
    """
    Updates the stockPool and hurstPool as price change
    Sell => decrease Hurst (more volatile)
    Buy => increase Hurst (less volatile)
    """
    global stockPool, hurstPool

    increment = volume / proportion
    h0 = hurstPool[stock][time]
    numberNewPrices = len(stockPool[stock][time:])
    p0 = stockPool[stock][time]

    if volume < 0:
        h1 = h0 - increment
        if h1 < 0.4:
            h1 = 0.4
        hurstPool[stock][time:] = h1
    else:
        h1 = h0 + increment
        if h1 > 0.8:
            h1 = 0.8
        hurstPool[stock][time:] = h1

    fbmNew = fbm(h1, 2**14, 2**14)
    fbmNew = abs(fbmNew[:numberNewPrices] + p0)
    print('stock ', stock, ' original H', h0, ' to ', h1)
    stockPool[stock][time:] = fbmNew
## ################################################
## generate fbms
reCalc = True
#reCalc = False
n = 256
N = 10000
if os.path.exists('data.bin') and not reCalc:
    print 'loading data from file'
    Xtrain, Xtest, Ytrain, Ytest = pickle.load(open('data.bin', 'r'))
else:
    print 'generating data and saving'
    np.random.seed(0)
    X = []
    H = np.linspace(0.05, 0.8, N)  # [0.5]*N
    for i in H:
        fbmr, fgnr, times = fbm(n - 1, i, L=1)
        fbmr = fbmr - np.min(fbmr)
        fbmr = fbmr / np.max(fbmr)
        X.append(fbmr)
    X0 = np.array(X)
    H = np.array(H)
    Y = H
    X = np.expand_dims(X0,
                       2)  # to make input of size (n,1) instead of just (n)
    Xtrain, Xtest, Ytrain, Ytest = train_test_split(X0,
                                                    Y,
                                                    test_size=0.2,
                                                    random_state=0)

    pickle.dump([Xtrain, Xtest, Ytrain, Ytest], open('data.bin', 'w'))