Exemple #1
0
def grid_search():
    tickers = ['AAPL']
    features = ['macd_9_18']
    ta = TradingAlgorithm(tickers,
                          features,
                          type='svm',
                          data_lookback_period=15,
                          label_lower_threshold=-0.15,
                          label_upper_threshold=0.015,
                          label_period=10,
                          data_splits=[0.8, 0.2],
                          cnn_split=0,
                          verbose=True)
    ta.grid_search(tickers[0], verbose=True)
Exemple #2
0
def loadTradingAlgorithm(tradingAlgorithmId):

    tradingAlgResult = db_wrapper.getTradingAlgorithm(tradingAlgorithmId)

    tickers = tradingAlgResult[1].split(',')
    features = tradingAlgResult[2].split(',')
    length = int(tradingAlgResult[3])
    upper_threshold = float(tradingAlgResult[4])
    lower_threshold = float(tradingAlgResult[5])
    period = int(tradingAlgResult[6])
    modelCollectionIds = tradingAlgResult[7].split(',')
    votingType = tradingAlgResult[8]
    mcs = []
    for modelCollectionId in modelCollectionIds:
        mc = loadModelCollection(modelCollectionId)

        mcs.append(mc)
    model_mc = mcs[0]
    features = model_mc.features
    t = model_mc.type

    ta = TradingAlgorithm(tickers=tickers,
                          features=features,
                          type=t,
                          data_lookback_period=length,
                          label_upper_threshold=upper_threshold,
                          label_lower_threshold=lower_threshold,
                          label_period=period,
                          voting_type=votingType,
                          models=mcs)

    return ta
Exemple #3
0
def test_conf_matrix_model_coll():
    # one cm for each model
    tickers = ['AAPL']
    features = ['macd_9_18', 'closes']
    ta = TradingAlgorithm(tickers,
                          features,
                          type='svm',
                          data_lookback_period=10,
                          label_lower_threshold=-0.15,
                          label_upper_threshold=0.015,
                          label_period=10,
                          data_splits=[0.8, 0.2],
                          cnn_split=0,
                          verbose=True,
                          model_params={
                              'gamma': 10,
                              "C": 1
                          })

    ta.plot_model_cm(tickers[0])
Exemple #4
0
def run_test_one():
    tickers = ['AAPL']
    features = ['macd_9_18', 'closes', 'prings', 'kst', 'kstTrix', 'rsi']
    ta = TradingAlgorithm(tickers,
                          features,
                          type='svm',
                          data_lookback_period=10,
                          label_lower_threshold=-0.15,
                          label_upper_threshold=0.035,
                          label_period=10,
                          voting_type='Penrose',
                          model_params={
                              'gamma': 100,
                              "C": 1
                          },
                          data_splits=[0.8, 0.2],
                          cnn_split=0,
                          verbose=True,
                          test_mode=True)
    print(ta.models[0].accuracy)
    ta.generate_conf_matricies(datetime.datetime(2018, 1, 1),
                               datetime.datetime(2019, 1, 1))
Exemple #5
0
def hyper_param_haul(features_list,ticker_list, model_params):
    '''
    Function for testing hyperparamters gamma, C for SVMs
    
    Inputs:
        ticker_list {list of lists} -- tickers to test for each feature set, each set of params
        feautures_list {list of lists} -- features to each test for each set of params
        model_params {list of dictionaries} -- combinations of gamma, C to test
    
    -- writes results to csv for analysis, csv's in backend/hyperparam_tests--

    '''
    acc_lst =[]
    cm_lst = []
    file = open('hyperparam_tst.txt', 'w')
    writer = csv.writer(file)
    writer.writerow(["tickers", "features", "params", "acc"])
    for ii in range(len(ticker_list)):
        tickers = ticker_list[ii]
        for jj in range(len(features_list)):
            features = features_list[jj]
            for kk in range(len(model_params)):
                params = model_params[kk]
                ta = TradingAlgorithm(tickers, features, type = 'svm', data_lookback_period = 10, label_lower_threshold = -0.15, label_upper_threshold = 0.035, label_period = 10, data_splits = [0.8, 0.2], cnn_split=0, verbose=True, model_params = params)
                cm = ta.plot_model_cm(tickers[0])
                cm_lst += cm
                acc = ta.models[0].accuracy
                acc_lst.append(acc)   
                writer.writerow([tickers, features, params, acc])
                writer.writerow(['confusion matrices for features of earlier models trained on: ' + str(tickers[0])+ ' and ' + str(features)])
                for ii in range(len(cm_lst)):
                    for jj in range(len(cm_lst[ii])):
                        file.write(str(cm_lst[ii][jj]))
                        file.write("\n")

    file.close()

    return max(acc_lst)
Exemple #6
0
def build_confusion_matrix():
    # one cm for whole alg
    tickers = ['AAPL']
    features = ['macd_9_18', 'closes', 'prings', 'kst', 'kstTrix', 'rsi']
    ta = TradingAlgorithm(tickers,
                          features,
                          type='svm',
                          data_lookback_period=10,
                          label_lower_threshold=-0.15,
                          label_upper_threshold=0.035,
                          label_period=10,
                          voting_type='accuracy',
                          model_params={
                              'gamma': 100,
                              "C": 1
                          },
                          data_splits=[0.8, 0.2],
                          cnn_split=0,
                          verbose=True,
                          test_mode=True)
    print(ta.models[0].accuracy)
    ## At this point, the model is trained for everything.
    ta.generate_conf_matricies(datetime.datetime(2018, 1, 1),
                               datetime.datetime(2020, 3, 15))
def build_example_model():
    tickers = ['AAPL', 'IBM']

    features = [
        'closes',
    ]

    look_back = 10
    lower_threshold = -0.15
    upper_threshold = 0.03
    label_period = 10

    ta = TradingAlgorithm(tickers,
                          features,
                          type='svm',
                          data_lookback_period=look_back,
                          label_lower_threshold=lower_threshold,
                          label_upper_threshold=upper_threshold,
                          label_period=label_period)
    print("Trading Alg Build")
    createTradingAlgorithm(ta)