Пример #1
0
def get_barwidth(self, X, width=None):
    # The Xaxis could be dates and so on, so we want to calculate
    # the with of this bastard independently of that

    if (len(X.shape)):
        X = X.flatten()
    print(X.shape)
    print("X axis type: ", type(X[0]).__name__)
    if (type(width) == type(None)):
        width = 1
#        print width
    if (type(X[0]).__name__ == "Timestamp"
        ):  #  or (type(X[0]).__name__ == "datetime64" )
        width_size = min(bMl.diff(X)[1:])
        width_size = (width_size.total_seconds()) / (24.0 * 60 * 60)
    else:

        width_size = min(bMl.diff(X)[1:])  # (X[1] - X[0])


#        print type(X[0,0])
#        print X.shape
#        width_size = min(bMa.diff(X, cval = 10000))
#        width_size = (width_size.total_seconds())/ (24.0*60*60)
    width = width_size * width
    #    print type(X[0])
    width = float(width)
    print("width is: ", width)
    return width
Пример #2
0
def get_RSI(prices, n=14):
    """
    The relative strength index (RSI) is a momentum indicator that 
    compares the magnitude of recent gains and losses 
    over a specified time period to measure 
    speed and change of price movements of a security. 
    It is primarily used to attempt to identify overbought 
    or oversold conditions in the trading of an asset.
    """
    
    # First we calculate the increases to then separate them into
    # increasing instances and decreasing instances
    deltas = bMa.diff(prices, lag = 1)
    
    # For the first N samples we will not be able to do it properli

    ## What we do is a rolling mean of the positive and the negative

    dUp, dDown = deltas.copy(), deltas.copy()
    ## Vector of positive and negative
    dUp[dUp < 0] = 0
    dDown[dDown > 0] = 0
    
    # Calculate the rolling mean, the Window !!
    # Calculates the average dUp and dDown in time
    RolUp = pd.rolling_mean(dUp, n)
    RolDown = np.abs(pd.rolling_mean(dDown, n))

#    print RolUp[0:30]
    # To avoid division by 0
    RS = RolUp / (RolDown +0.0000001)
    RSI = 100. - 100. / (1. + RS)
    return RSI
Пример #3
0
 def get_Residuals_LinearModel(X_data, price, lag = 20):
     # This functions gets the residuals when we train a linear model with 
     # the X_data into predictiong the price return with a given lag.
 
     ## Prediction of the thingies
     Y_data = bMA.diff(price, lag = lag, cval = np.NaN)
     Y_data = bMA.shift(Y_data, lag = -lag, cval = np.NaN)
 
     ## Eliminate the Nans !! Even if they appear in just one dim
     mask_X = np.sum(np.isnan(X_data), axis = 1) == 0
     mask_Y = np.isnan(Y_data) == 0
     mask = mask_X & mask_Y[:,0]
     
     # Create linear regression object
     regr = linear_model.LinearRegression()
     # Train the model using the training sets
     regr.fit(X_data[mask,:], Y_data[mask,:])
     
     #    coeffs = np.array([regr.intercept_, regr.coef_])[0]
     coeffs = np.append(regr.intercept_, regr.coef_)
     params = np.array(coeffs)
 
     residual = regr.residues_
     residual = regr.score(X_data[mask,:], Y_data[mask,:])
     return residual
Пример #4
0
    def get_Residuals_LinearModel(X_data, price, lag=20):
        # This functions gets the residuals when we train a linear model with
        # the X_data into predictiong the price return with a given lag.

        ## Prediction of the thingies
        Y_data = bMA.diff(price, lag=lag, cval=np.NaN)
        Y_data = bMA.shift(Y_data, lag=-lag, cval=np.NaN)

        ## Eliminate the Nans !! Even if they appear in just one dim
        mask_X = np.sum(np.isnan(X_data), axis=1) == 0
        mask_Y = np.isnan(Y_data) == 0
        mask = mask_X & mask_Y[:, 0]

        # Create linear regression object
        regr = linear_model.LinearRegression()
        # Train the model using the training sets
        regr.fit(X_data[mask, :], Y_data[mask, :])

        #    coeffs = np.array([regr.intercept_, regr.coef_])[0]
        coeffs = np.append(regr.intercept_, regr.coef_)
        params = np.array(coeffs)

        residual = regr.residues_
        residual = regr.score(X_data[mask, :], Y_data[mask, :])
        return residual
Пример #5
0
def get_momentum (time_series, N = 1):
    """ 
    The Momentum Technical Indicator measures the amount that a 
    security's price has changed over a given time span. 
    It is used with the closed price
     There are several variations of the momentum indicator,
     but whichever version is used, the momentum (M) is a
     comparison between the current closing price (CP) a closing price 
     "n" periods ago (CPn). The "n" is determined by you. 
     In the attached chart, Momentum is set to "10," so the indicator 
     is comparing the current price to the price 10 time instances ago 
     (because it is a 1-minute chart).
    """
#    diff = bMa.diff(time_series, lag = 1, n = 2)
#    diff = bMa.diff([1,2,3,4,5,6,7,8], lag = 1, n = 2)
#    print diff[0:10,:]
#    print time_series[0:10,:]

    momentum = bMa.diff(time_series, lag = N, n = 2)
    
    return momentum
def get_barwidth(self, X, width):
    # The Xaxis could be dates and so on, so we want to calculate
    # the with of this bastard independently of that

    if (width < 0):
        width = 1


#        print width
    if (type(X[0]).__name__ == "Timestamp"):
        width_size = min(ul.diff(X))

        width_size = (width_size.total_seconds()) / (24.0 * 60 * 60)
    else:
        width_size = (X[1] - X[0])
        width_size = min(bMa.diff(X, cval=10000))

    width = width_size * width
    #    print type(X[0])
    width = float(width)

    return width
if (preprocessing_data):
    timeData = tut.preprocess_data(timeData, sdate, edate)
    ## Get the valid trading days sorted
    days_keys, day_dict = timeData.get_indexDictByDay()
    Ndays = len(days_keys)

    timeData_daily = tut.get_daily_timedata(timeData, symbols[0])
    H, L, O, C, V = np.array(
        timeData_daily.TD[["High", "Low", "Open", "Close", "Volume"]][:]).T

if (extract_features):
    """
    Create the target for the regressing and classifier systems
    """
    Target = bMl.diff(C).flatten()  # Continuous target .Target[0] = NaN
    Target_bin = np.zeros(Target.shape)  # Binarized target
    Target_bin[np.where(Target >= 0)] = 1
    data_df = None

    ## Create Pandas Data Frame for the information of the ML problem
    data_df = pd.DataFrame({
        'Time': days_keys,
        'Target_clas': Target_bin,
        'Target_reg': Target
    })
    data_df.set_index('Time', inplace=True)

    ## Every feature that is computed for each day has to be lagged before introducing
    ## it into the dataframe so that the input features do not contain information
    ## from their target day
Пример #8
0
    yerr = ypred - y  # Difference at each point, between the predicted and the real.
    for i in range (Ns):
        yerr_i = yerr[[i],:]
        logpred += np.log(np.linalg.det(SigmaYYpredList[i]))
        logpred += (yerr_i.T).dot(np.linalg.inv(SigmaYYpredList[i])).dot(yerr_i)
        
    logpred = -logpred/2
    logpred -= Ns/np.log(np.pi * 2)
    return logpred


if (KM == 1):
    # Kalman Filter !
    timeSeries = timeData.get_timeSeries(["Close"]);
    returns = timeData.get_timeSeriesReturn()  # Variable to estimate. Nsig x Ndim
    returns  = bMA.diff(timeSeries, cval = 0.000001)
    
    dates = timeData.get_dates()
    dates = ul.fnp(ul.datesToNumbers(dates))
    dates = ul.fnp(range(dates.size))
        
    ############################ Create the data #######################################
    Matrix_tr = np.concatenate([timeSeries,returns], axis = 1)  # Nsam * 4
    Ns,Nd = Matrix_tr.shape
    ########################### AR matrix ###############################
    # We assume as simple relationship
    # P_t = P_(t-1) + vP_(t-1)  -> The dinamics of the next price
    # vxt = vx_(t-1)            -> The dinamics of the next return
    A = np.array([[1,0.99],
                  [0,1]])
    ######################### C Matrix ###################################
# We get now the moving averages
indx = 1
lag = 35

## MACD Parameters !!
n_fast = 12
n_slow = 26
n_smooth = 9
EMAfast = Cartera.EMA(n=n_fast)
EMAslow = Cartera.EMA(n=n_slow)
MACD = EMAfast - EMAslow
MACD = pd.ewma(MACD, span=n_smooth, min_periods=n_smooth - 1)

# Smoothin of velocity, we want to enter when it is falling
MACD_vel = bMA.diff(MACD, n=1)
nsmooth_vel = 4
MACD_vel = indl.get_SMA(MACD_vel, L=nsmooth_vel)

## RSI Parameter !
RSI = Cartera.RSI(n=14)
RSI_vel = bMA.diff(RSI, n=1)
#RSI_vel = indl.get_SMA(RSI_vel, L = nsmooth_vel)

## ATR Parameter !
ATR = Cartera.ATR(n=14)
ATR_vel = bMA.diff(ATR, n=1)
RSI_vel = indl.get_SMA(ATR_vel, L=nsmooth_vel)

###########################################################
################# PREPARE THE DATA ########################
Пример #10
0
    yerr = ypred - y  # Difference at each point, between the predicted and the real.
    for i in range (Ns):
        yerr_i = yerr[[i],:]
        logpred += np.log(np.linalg.det(SigmaYYpredList[i]))
        logpred += (yerr_i.T).dot(np.linalg.inv(SigmaYYpredList[i])).dot(yerr_i)
        
    logpred = -logpred/2
    logpred -= Ns/np.log(np.pi * 2)
    return logpred


if (KM == 1):
    # Kalman Filter !
    timeSeries = timeData.get_timeSeries(["Close"]);
    returns = timeData.get_timeSeriesReturn()  # Variable to estimate. Nsig x Ndim
    returns  = bMA.diff(timeSeries, cval = 0.000001)
    
    dates = timeData.get_dates()
    dates = ul.fnp(ul.datesToNumbers(dates))
    dates = ul.fnp(range(dates.size))
        
    ############################ KALMAN FILTER !! #######################################

    Matrix_tr = np.concatenate([timeSeries,returns], axis = 1)  # Nsam * 4
    
    Ns,Nd = Matrix_tr.shape
    Nout = 1 # Number of output
    # System of equations A
    #   Xt = A*Xt-1 + B*Ut + SystemNoise  
    #   Yt = Cx_k  + MeasurementNoise
    # In the normal formulation Xt is a column vector !!
Пример #11
0
    timeData.set_csv(storage_folder,file_name)  # Load the data into the model

if (preprocessing_data):
    timeData = tut.preprocess_data(timeData, sdate,edate)
    ## Get the valid trading days sorted
    days_keys, day_dict = timeData.get_indexDictByDay()
    Ndays = len(days_keys)

    timeData_daily = tut.get_daily_timedata(timeData, symbols[0])
    H,L,O,C,V = np.array(timeData_daily.TD[["High","Low","Open","Close","Volume"]][:]).T
    
"""
Create the target for the classifier:
"""

Target = bMl.diff(C).flatten()  # Continuous target Target[0] = NaN
Target_bin = np.zeros(Target.shape) # Binarized
Target_bin[np.where(Target >=0)] = 1

## Create Pandas Data Frame for the information of the ML problem

data_df = pd.DataFrame({'Time': days_keys, 'Target_clas': Target_bin,  'Target_reg': Target})  #  'Current_diff': Target} 
data_df.set_index('Time',inplace = True)

"""
#########################################################
CREATE WINDOWED VECTOR OF FEATURES !!
#########################################################
"""

Nlast_Close = 2  # The last Diff in Close and increase
prices = Cartera.get_timeSeries()
dates = Cartera.get_dates(period, Cartera.symbol_names[0])

# We get now the moving averages
indx = 1
lag = 35

## MACD Parameters !!
n_fast = 12; n_slow = 26; n_smooth = 9
EMAfast = Cartera.EMA(n = n_fast)
EMAslow = Cartera.EMA(n = n_slow)
MACD = EMAfast - EMAslow
MACD = pd.ewma(MACD, span = n_smooth, min_periods = n_smooth - 1)

# Smoothin of velocity, we want to enter when it is falling
MACD_vel = bMA.diff(MACD,n = 1)
nsmooth_vel = 4
MACD_vel = indl.get_SMA(MACD_vel, L = nsmooth_vel)

## RSI Parameter !
RSI = Cartera.RSI(n = 14)
RSI_vel = bMA.diff(RSI,n = 1)
#RSI_vel = indl.get_SMA(RSI_vel, L = nsmooth_vel)

## ATR Parameter !
ATR = Cartera.ATR(n = 14)
ATR_vel = bMA.diff(ATR,n = 1)
RSI_vel = indl.get_SMA(ATR_vel, L = nsmooth_vel)

###########################################################
################# PREPARE THE DATA ########################