<commit_before>from .basestrategy import BaseStrategy, MissingSettingsException
from pprint import pprint


class CoreExchangeRateTracker(BaseStrategy):
    """
    Configuration:

    ::

        bots["CERTracker"] = {"bot" : CoreExchangeRateTracker,
                              # markets to serve (as to have BTS as base)
                              "markets" : ["MKR : BTS"],
                              # target premium over relative asset (below)
                              "target_premium_percentage" : 2.0,
                              # relative to "highest_bid", "last", "price24h", "midprice"
                              "target_relative_to" : "highest_bid",
                              # thresholds in percent (CER will be
                              # updated if not between upper/lower)
                              "upper_bound_threshold" : 15,
                              "lower_bound_threshold" : 4,
                              # Skip blocks (not smaller than 1)
                              # Only check every x blocks
                              "skip_blocks" : 20 * 5,
                              # Force CER to be smaller than highest bid!
                              "force_lower_than_higest_bid" : True,
                              }
    """


    block_counter = 0

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def init(self):
        #: Verify that the markets are against the core asset
        sym = self.dex.core_asset["symbol"]
        for m in self.settings["markets"]:
            if sym != m.split(self.dex.market_separator)[1]:
                raise Exception(
                    "Base needs to be core asset %s" % sym
                )
        self.quote_asset = m.split(self.dex.market_separator)[0]

    def update_asset_cer(self, asset_name, new_cer):
        asset = self.dex.rpc.get_asset(asset_name)
        options = asset["options"]
        core_asset = self.dex.core_asset

        base_amount = int(10 ** asset["precision"])
        quote_amount = int(new_cer * 10 ** core_asset["precision"])

        options["core_exchange_rate"] = {
           "base": {
               "amount": base_amount,
               "asset_id": asset["id"]},
           "quote": {
               "amount": quote_amount,
               "asset_id": "1.3.0"}
        }
        pprint(self.dex.rpc.update_asset(asset["symbol"], None, options, True))

    def update_cer(self, market):
        asset = self.quote_asset
        print("Updating CER for %s" % asset)

        ticker = self.dex.returnTicker()[market]
        premium = self.settings["target_premium_percentage"] / 100.0

        if self.settings["target_relative_to"] == "price24h":
            new_cer = ticker["price24h"] * (1.0 - premium)
        elif self.settings["target_relative_to"] == "midprice":
            new_cer = (ticker["lowestAsk"] + ticker["highestBid"]) / 2.0 * (1.0 - premium)
        elif self.settings["target_relative_to"] == "last":
            new_cer = ticker["last"] * (1.0 - premium)
        elif self.settings["target_relative_to"] == "highest_bid":
            new_cer = ticker["highestBid"] * (1.0 - premium)
        else:
            raise MissingSettingsException()

        if (self.settings["force_lower_than_higest_bid"] and
                new_cer > ticker["highestBid"]):
            new_cer = ticker["highestBid"]

        self.update_asset_cer(asset, new_cer)

    def tick(self):
        self.block_counter += 1
        if (self.block_counter % self.settings["skip_blocks"]) == 0:
            ticker = self.dex.returnTicker()
            for m in ticker:
                print("Checking CER for %s" % self.quote_asset)
                cer = ticker[m]["core_exchange_rate"]
                price24h = ticker[m]["price24h"]
                highest_bid = ticker[m]["highestBid"]
                midprice = (ticker[m]["highestBid"] + ticker[m]["lowestAsk"]) / 2.0<commit_msg>[CER Strategy] Run tick() on single-shot once so that the script can be run in cronjobs<commit_after>from .basestrategy import BaseStrategy, MissingSettingsException
from pprint import pprint


class CoreExchangeRateTracker(BaseStrategy):
    """
    Configuration:

    ::

        bots["CERTracker"] = {"bot" : CoreExchangeRateTracker,
                              # markets to serve (as to have BTS as base)
                              "markets" : ["MKR : BTS"],
                              # target premium over relative asset (below)
                              "target_premium_percentage" : 2.0,
                              # relative to "highest_bid", "last", "price24h", "midprice"
                              "target_relative_to" : "highest_bid",
                              # thresholds in percent (CER will be
                              # updated if not between upper/lower)
                              "upper_bound_threshold" : 15,
                              "lower_bound_threshold" : 4,
                              # Skip blocks (not smaller than 1)
                              # Only check every x blocks
                              "skip_blocks" : 20 * 5,
                              # Force CER to be smaller than highest bid!
                              "force_lower_than_higest_bid" : True,
                              }
    """


    block_counter = 0

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def init(self):
        #: Verify that the markets are against the core asset
        sym = self.dex.core_asset["symbol"]
        for m in self.settings["markets"]:
            if sym != m.split(self.dex.market_separator)[1]:
                raise Exception(
                    "Base needs to be core asset %s" % sym
                )
        self.quote_asset = m.split(self.dex.market_separator)[0]

        """ After startup, execute one tick()
        """
        self.tick()

    def update_asset_cer(self, asset_name, new_cer):
        asset = self.dex.rpc.get_asset(asset_name)
        options = asset["options"]
        core_asset = self.dex.core_asset

        base_amount = int(10 ** asset["precision"])
        quote_amount = int(new_cer * 10 ** core_asset["precision"])

        options["core_exchange_rate"] = {
           "base": {
               "amount": base_amount,
               "asset_id": asset["id"]},
           "quote": {
               "amount": quote_amount,
               "asset_id": "1.3.0"}
        }
        pprint(self.dex.rpc.update_asset(asset["symbol"], None, options, True))

    def update_cer(self, market):
        asset = self.quote_asset
        print("Updating CER of %s" % asset)

        ticker = self.dex.returnTicker()[market]
        premium = self.settings["target_premium_percentage"] / 100.0

        if self.settings["target_relative_to"] == "price24h":
            new_cer = ticker["price24h"] * (1.0 - premium)
        elif self.settings["target_relative_to"] == "midprice":
            new_cer = (ticker["lowestAsk"] + ticker["highestBid"]) / 2.0 * (1.0 - premium)
        elif self.settings["target_relative_to"] == "last":
            new_cer = ticker["last"] * (1.0 - premium)
        elif self.settings["target_relative_to"] == "highest_bid":
            new_cer = ticker["highestBid"] * (1.0 - premium)
        else:
            raise MissingSettingsException()

        if (self.settings["force_lower_than_higest_bid"] and
                new_cer > ticker["highestBid"]):
            new_cer = ticker["highestBid"]

        self.update_asset_cer(asset, new_cer)

    def tick(self):
        self.block_counter += 1
        if (self.block_counter % self.settings["skip_blocks"]) == 0:
            ticker = self.dex.returnTicker()
            for m in ticker:
                print("Checking CER of %s" % self.quote_asset)
                cer = ticker[m]["core_exchange_rate"]
                price24h = ticker[m]["price24h"]
                highest_bid = ticker[m]["highestBid"]
                midprice = (ticker[m]["highestBid"] + ticker[m]["lowestAsk"]) / 2.0
{'fraction_non_alphanumeric': 0.11548458967694021, 'fraction_numerical': 0.011635103354375542, 'mean_word_length': 1.7141417534430634, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2265610', 'n_tokens_mistral': 2398, 'n_tokens_neox': 2220, 'n_words': 500}