Esempio n. 1
0
def calculate_value_deltas(present: PriceSnapshot,
                           history: PriceSnapshot) -> Dict[str, float]:
    result = {}
    for currency in present.currencies:
        price_delta = present.get_unit_value(
            currency) - history.get_unit_value(currency)
        result[currency] = price_delta

    return result
Esempio n. 2
0
    def get_history(self, seconds_back: float) -> PriceSnapshot:
        if seconds_back < 0 and isinstance(self._connector, PeekConnector):
            raise AssertionError(
                "Can request only history (not future) for PeekConnector")

        history = PriceSnapshot(self, self._connector,
                                self.current_time - seconds_back)
        return history
Esempio n. 3
0
def future_value(fund: Fund, target_currency: str, present: PriceSnapshot,
                 future):
    converted_fund = present.after_conversion(fund, target_currency)
    #if converted_fund.currency != target_currency:
    #    converted_fund = Fund(converted_fund.amount * 0.99, converted_fund.currency)

    value = future.get_value(converted_fund)
    #print(f"{converted_fund} value estimation: {value}")

    return value
Esempio n. 4
0
    def _calculate_future_unit_values(self, present: PriceSnapshot):
        # we will look same time to the past as we need to predict to the future
        past = present.get_snapshot(
            seconds_back=self.prediction_lookahead_seconds)

        result = {}
        for currency in present.non_target_currencies:
            # calculate predictions for all relevant currencies
            current_value = present.get_unit_value(currency)
            past_value = past.get_unit_value(currency)
            delta = current_value - past_value

            # use linear interpolation for predictions
            predicted_value = current_value + delta * self._delta_scale

            result[
                currency] = predicted_value  # store the prediction for further use

        return result
Esempio n. 5
0
    def _calculate_future_unit_values(self, present: PriceSnapshot):
        history = present.get_snapshot(seconds_back=self.window_steps *
                                       self.sample_period)

        result = {}
        for currency in present.non_target_currencies:
            # get input data for the network - sample single window
            samples = history.get_unit_value_samples(currency,
                                                     self.sample_period)
            model_value = self._get_prediction(samples)
            current_value = present.get_unit_value(currency)
            final_prediction = (
                1.0 - self.model_strength
            ) * current_value + self.model_strength * model_value
            if model_value < current_value:
                final_prediction = model_value  # don't underestimate low trends

            result[
                currency] = final_prediction  # store the prediction for further use

        return result
Esempio n. 6
0
    def _get_normalized_windows(self, currency,
                                training_snapshot: PriceSnapshot):
        value_samples = training_snapshot.get_unit_value_samples(
            currency, self.sample_period)
        input_windows = []
        targets = []

        lookahead = int(self.prediction_lookahead_seconds / self.sample_period)

        for i in range(self.window_steps, len(value_samples) - lookahead):
            target = value_samples[i + lookahead]
            window = (value_samples[i - self.window_steps:i])
            normalized_window, normalized_target = self._normalize_data(
                window, target)

            input_windows.append(normalized_window)
            targets.append(normalized_target)

        return input_windows, targets
Esempio n. 7
0
    def _run_training(self, snapshot: PriceSnapshot):
        training_data_length = 10000.0  # seconds into history that will be used for generating training data
        self.window_steps = 100  # how large window will be fed as the input
        self.sample_period = 5.0  # how long apart the window samples will be
        self.model_strength = 0.9  # inhibits strength of model predictions by keeping them close to current values
        self.target_factor = 1000  # is used for scaling output - this gives better loss readings

        training_start_snapshot = snapshot.get_snapshot(
            seconds_back=training_data_length)

        inputs = []
        targets = []
        for currency in training_start_snapshot.non_target_currencies:
            # collect history windows for all currencies
            cis, cts = self._get_normalized_windows(currency,
                                                    training_start_snapshot)
            inputs.extend(cis)
            targets.extend(cts)

        # train model on pair window -> target (where target is the value after the predicted period)
        self._model = self._train_model(inputs, targets)