Beispiel #1
0
    def train_model() -> None:
        target_scaler = create_target_scaler()
        preprocess_pipeline = create_preprocess_pipeline()
        input_data = InfluxdbQuery(
            dict(fields='open,high,low,close,volume',
                 result_limit=100000)).query()
        input_data = input_data.sort_values(by='time',
                                            ascending=True).set_index('time')
        input_data_features = extract_features(input_data,
                                               lookahead=MLTools.lookahead)
        input_data_features = input_data_features.pipe(
            lambda df: df[~pd.isna(df['target'])])
        dataframe_x, dataframe_y = split_dataframe_on_columns(
            input_data_features, ['target'])
        X_train, X_val, y_train, y_val = train_test_split(dataframe_x.values,
                                                          dataframe_y.values,
                                                          test_size=0.03,
                                                          random_state=42,
                                                          shuffle=False)

        X_train = preprocess_pipeline.fit_transform(X_train)
        X_val = preprocess_pipeline.transform(X_val)

        y_train = target_scaler.fit_transform(y_train)
        y_val = target_scaler.transform(y_val)

        X_train, y_train = transform_rnn_sequences(X_train,
                                                   y_train,
                                                   lookback=MLTools.lookback,
                                                   lookahead=MLTools.lookahead)
        X_val, y_val = transform_rnn_sequences(X_val,
                                               y_val,
                                               lookback=MLTools.lookback,
                                               lookahead=MLTools.lookahead)

        joblib.dump(preprocess_pipeline, MLTools.preprocess_pipeline_path)
        joblib.dump(target_scaler, MLTools.target_scaler_path)

        rnn_model = build_model(input_shape=X_train.shape[1:],
                                output_neurons=y_train.shape[1])
        rnn_model.fit(X_train,
                      y_train,
                      validation_data=(X_val, y_val),
                      epochs=30,
                      batch_size=32,
                      callbacks=[
                          EarlyStopping(restore_best_weights=True,
                                        patience=2,
                                        monitor='val_loss')
                      ])

        save_model(rnn_model, MLTools.rnn_model_path)
Beispiel #2
0
 def extract(self) -> ndarray:
     if self.preprocess_pipeline is None:
         return
     input_data = InfluxdbQuery(
         dict(fields='open,high,low,close,volume',
              result_limit=MLTools.lookback * 2)).query()
     input_data = input_data.sort_values(by='time',
                                         ascending=True).set_index('time')
     input_data_features = extract_features(input_data)
     dataframe_x, dataframe_y = split_dataframe_on_columns(
         input_data_features, ['target'])
     X = self.preprocess_pipeline.transform(dataframe_x.values)
     X_rnn, _ = transform_rnn_sequences(X,
                                        dataframe_y.values,
                                        lookback=MLTools.lookback)
     self.last_time_point = input_data.index.values[-1]
     self.last_close_value = input_data["close"].values[-1]
     return X_rnn[-1:]
Beispiel #3
0
def timeseries():
    result_df = InfluxdbQuery(request.args).query()
    return result_df.to_json(orient='split', index=False)
Beispiel #4
0
 def test_query(self, mock_create):
     mock_create.return_value.query = lambda x: {'ohlcv': self.input_data}
     influxapi = InfluxdbQuery()
     res = influxapi.query()
     self.assertEqual(res.columns.tolist(), ["time", "a", "b"])
     self.assertEqual(res.shape[0], 2)
Beispiel #5
0
 def test_query_empty_res(self, mock_create):
     mock_create.return_value.query = lambda x: {}
     influxapi = InfluxdbQuery()
     res = influxapi.query()
     self.assertEqual(res.shape, (0, 0))
Beispiel #6
0
 def test_transform_dataframe(self):
     influxapi = InfluxdbQuery()
     output_data = influxapi._transform_dataframe(self.input_data)
     self.assertEqual(set(output_data.columns.tolist()), set(['time', 'a', 'b']))
     self.assertEqual(len(self.input_data), len(output_data))
Beispiel #7
0
 def test_init(self):
     influxapi = InfluxdbQuery()
     self.assertEqual(influxapi.request_parameters, {})
     self.assertEqual(influxapi.measurement, 'ohlcv')
     self.assertEqual(influxapi.fields, '*')
     self.assertEqual(influxapi.asset, 'btc')