Exemplo n.º 1
0
def sample_signal(signal_class: Type[TradingSignal],
                  time_series: TIME_SERIES,
                  parameter_ranges: Tuple[Tuple[float, float], ...],
                  no_samples: int,
                  plot: bool = False) -> Collection[SAMPLE]:

    sequence = list(time_series)

    def series_eval(parameter: float) -> float:
        signal = signal_class(round(parameter))
        return evaluate_signal(signal, sequence)

    optimizer = StatefulOptimizer(series_eval, parameter_ranges)
    samples = set()

    for i in range(no_samples):
        if Timer.time_passed(2000):
            print("Iteration {:d}/{:d}".format(i, no_samples))
        each_sample = optimizer.next()
        samples.add(each_sample)
        if plot:
            pyplot.plot(each_sample[0], [each_sample[1]], "x")

    print("Best parameters: {:s} (value: {:.4f})".format(
        str(optimizer.best_parameters), optimizer.best_value))
    if plot:
        axes = sorted(samples, key=lambda _x: _x[0])
        pyplot.plot(*zip(*axes))
        pyplot.show()

    return samples
Exemplo n.º 2
0
    def run(self):
        while True:
            try:
                time, rates = self._get_rates()
            except StopIteration as e:
                print(e)
                break

            try:
                portfolio = self._get_portfolio()
                base_values = {
                    _k: _v * rates[_k]
                    for _k, _v in portfolio.items()
                }
                self.__log_portfolio(time, base_values)
            except Exception as e:
                print(e)
                continue

            if self.iteration == 0:
                distribution = TradingBot.__balanced_portfolio(portfolio)

            else:
                try:
                    signal_inputs = self._get_signals_input()
                except Exception as e:
                    print(e)
                    continue

                self._train_signals(signal_inputs, rates)

                signals = self.__get_signals(signal_inputs)
                distribution = self.__signal_portfolio(portfolio, signals,
                                                       rates)

            total_base_value = TradingBot._get_total_base_value(
                portfolio, rates)
            target_portfolio = TradingBot._distribute(total_base_value,
                                                      distribution, rates)
            target_portfolio = {
                _k: self.risk * _v + (1. - self.risk) * portfolio[_k]
                for _k, _v in target_portfolio.items()
            }
            transactions = self.__get_transactions(portfolio, target_portfolio,
                                                   rates)

            for source_value, source_asset, target_asset in transactions:
                try:
                    #if portfolio[source_asset] < source_value:
                    #    raise ValueError()
                    self.__transfer(source_value, source_asset, target_asset)
                except ValueError as e:
                    raise e

            if Timer.time_passed(2000):
                print("{:s}: total value is {:s} {:.5f}".format(
                    time.strftime("%Y-%m-%d %H:%M"), self.base_asset,
                    total_base_value))
            self._wait()
            self.iteration += 1
def make_path(matrix: Sequence[Sequence[float]]) -> Sequence[int]:
    len_path = len(matrix)

    path = []
    i = len_path - 1
    while i >= 0:
        amounts = matrix[i]
        asset_last, _ = max_index(amounts)
        path.insert(0, asset_last)
        i -= 1
        if Timer.time_passed(2000):
            print(
                f"finished {(len_path - i) * 100. / len_path:5.2f}% of making path..."
            )

    return path
Exemplo n.º 4
0
def time_stamp_test():
    with open("../../configs/time_series.json", mode="r") as file:
        config = json.load(file)

    start_ts = 1501113780
    #start_ts = 1521113780
    # end_ts = 1502508240
    end_ts = 1532508240

    data_dir = config["data_dir"]
    file_names = ["BNTETH.csv", "SNTETH.csv", "QTUMETH.csv", "EOSETH.csv"]
    # file_names = os.listdir(data_dir)[-10:]

    delta = 60

    time_axes = tuple([] for _ in file_names)
    value_axes = tuple([] for _ in file_names)
    for _i, each_file in enumerate(file_names):
        file_path = data_dir + each_file
        last_t = -1
        for t, v in equisample(series_generator(file_path,
                                                start_timestamp=start_ts,
                                                end_timestamp=end_ts),
                               target_delta=delta):
            if 0 < last_t:
                if not t - last_t == delta:
                    print("skip")
            last_t = t
            time_axes[_i].append(t)
            value_axes[_i].append(v)
            if Timer.time_passed(2000):
                print("{:5.2f}% of file {:d}/{:d}".format(
                    100. * (t - start_ts) / (end_ts - start_ts), _i + 1,
                    len(file_names)))

    for _i, (each_time, each_value) in enumerate(zip(time_axes, value_axes)):
        pyplot.plot(each_time, each_value, label=file_names[_i])

    pyplot.legend()
    pyplot.show()

    for _i, each_axis in enumerate(time_axes):
        for _j in range(_i + 1, len(time_axes)):
            print(
                "{:s} and {:s}: {:s}".format(file_names[_i], file_names[_j],
                                             str(each_axis == time_axes[_j])))
Exemplo n.º 5
0
def store_matrix(path_file: str, matrix: Iterable[Tuple[Sequence[int],
                                                        Tuple[int, float]]],
                 no_datapoints: int):
    t_last = 0
    with open(path_file, mode="a") as file:
        for t, (snapshot, (best, roi)) in enumerate(matrix):
            line = "\t".join(f"{a:d}"
                             for a in snapshot) + f", {best:d}: {roi:.8f}\n"
            file.write(line)

            if Timer.time_passed(2000):
                speed_per_sec = (t - t_last) // 2
                secs_remaining = (no_datapoints - t) // speed_per_sec
                minutes_remaining = secs_remaining // 60

                print(
                    f"finished {100. * t / no_datapoints:5.2f}% of saving matrix ({t:d} of {no_datapoints:d} total). {minutes_remaining:d} minutes remaining..."
                )
                t_last = t
Exemplo n.º 6
0
    def _post_process(self):
        growths = self.state_experiment["growths"]
        if any(0. >= x for x in growths):
            print(f"erroneous growths: {str(growths):s}. skipping...")
            return

        rates = self.state_experiment["rates"]
        if any(0. >= x for x in rates):
            print(f"erroneous rates: {str(rates):s}. skipping...")
            return

        value_market_last = self.state_experiment["value_market_last"]
        portfolios = self.state_experiment["portfolios"]
        value_assets = tuple(
            tuple(max(r * a, 0.) for r, a in zip(rates, each_portfolio))
            for each_portfolio in portfolios)
        value_portfolios_last = self.state_experiment["value_portfolios_last"]

        growth_market = self.__get_growth_market(growths)
        value_market = value_market_last * growth_market
        value_portfolios = tuple(
            self.__evaluate(i) for i in range(len(self.applications)))
        for each_value in value_portfolios:
            assert 0. < each_value

        growth_portfolios = self.__get_growth_portfolios(
            portfolios, value_portfolios_last, rates)
        self.__add_to_graph(growth_market, value_market, growth_portfolios,
                            value_portfolios, value_assets)

        if Timer.time_passed(2000):
            for each_application in self.applications:
                if not isinstance(each_application, TraderApproximation):
                    continue
                each_approximation = each_application.approximation
                if isinstance(each_approximation, ApproximationSemioticModel):
                    print(
                        f"{each_approximation.index_classifier_current:>3d}: {str(each_approximation.get_structure()):s}"
                    )

        self.state_experiment["value_market_last"] = value_market
        self.state_experiment["value_portfolios_last"] = value_portfolios
Exemplo n.º 7
0
def generate_path_from_file(path_file: str) -> Sequence[int]:
    path = []
    asset_last = -1
    #with open(path_file, mode="r") as file:

    size_total = os.path.getsize(path_file)
    size_read_last = -1
    size_read = 0
    for i, line in enumerate(read_reverse_order(path_file)):
        #for i, line in enumerate(base.readline(file, reverse=True)):
        stripped = line.strip()
        if len(stripped) < 1:
            continue
        snapshot_str, rest_str = stripped.split(", ")
        snapshot_split = snapshot_str.split("\t")
        rest = rest_str.split(": ")

        if i < 1:
            asset_last = int(rest[0])
            path.append(asset_last)

        asset_last = int(snapshot_split[asset_last])
        path.insert(0, asset_last)

        size_read += len(line)

        if Timer.time_passed(2000):
            if size_read_last < 0:
                min_str = "??"

            else:
                speed = (size_read - size_read_last) // 2
                seconds_remaining = (size_total - size_read) // speed
                min_str = f"{seconds_remaining // 60:d}"

            print(
                f"finished reading {100. * size_read / size_total:5.2f}% percent of path. {min_str:s} minutes remaining..."
            )
            size_read_last = size_read

    return path
Exemplo n.º 8
0
def write_examples_path(interval_minutes: int, pairs: Sequence[Tuple[str,
                                                                     str]],
                        path_investment: Sequence[int], file_path: str,
                        header: Sequence[str], time_range: Tuple[int, int]):
    len_path = len(path_investment)
    print(f"length path: {len_path:d}")

    generate_stats = rates_binance_generator(
        pairs=pairs,
        timestamp_range=time_range,
        header=header,
        interval_minutes=interval_minutes,
        directory_data=PATH_DIRECTORY_DATA)
    header_combined = tuple(combine_assets_header(pairs, header))

    print("writing examples...")
    names_pairs = tuple(f"{x[0]:s}-{x[1]}" for x in pairs)
    with open(file_path, mode="a") as file:
        file.write("\t".join(header_combined + ("target", )) + "\n")

        last_i = -1
        for i, (snapshot,
                target) in enumerate(zip(generate_stats, path_investment)):
            line = tuple(
                convert_to_string(snapshot[column])
                for column in header_combined) + (names_pairs[target], )
            file.write("\t".join(line) + "\n")

            if Timer.time_passed(2000):
                if last_i < 0:
                    min_str = "??"
                else:
                    speed = (i - last_i) // 2
                    seconds_remaining = (len_path - i) // speed
                    min_str = f"{seconds_remaining // 60:d}"

                print(
                    f"finished {i * 100. / len_path:5.2f}% of writing examples. {min_str:s} minutes remaining..."
                )
                last_i = i
Exemplo n.º 9
0
def generate_path(
    matrix: Sequence[Tuple[Sequence[int], Tuple[int,
                                                float]]]) -> Sequence[int]:
    path = []
    asset_last = -1

    len_matrix = len(matrix)

    for t in range(len_matrix - 1, -1, -1):
        assets_from, (asset_max, _) = matrix[t]
        if t >= len_matrix - 1:
            asset_last = asset_max
            path.append(asset_last)

        asset_last = assets_from[asset_last]
        path.insert(0, asset_last)

        if Timer.time_passed(2000):
            print(
                f"finished reading {100. * (t - len_matrix) / len_matrix:5.2f}% percent of path..."
            )

    return path