From b9eeabb6694a64236cb6c836f560ed7b42086035 Mon Sep 17 00:00:00 2001 From: sb Date: Mon, 5 Feb 2024 16:40:58 -0500 Subject: [PATCH] adding ranges tracking to markets, and reporting to sim_stats --- sharkfin/markets/__init__.py | 3 +++ sharkfin/markets/ammps.py | 11 ++++++++++- sharkfin/simulation.py | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sharkfin/markets/__init__.py b/sharkfin/markets/__init__.py index 24c81a2..0ba55a0 100644 --- a/sharkfin/markets/__init__.py +++ b/sharkfin/markets/__init__.py @@ -168,6 +168,7 @@ class MockMarket(AbstractMarket): prices = None dividends = None + ranges = None dividend_growth_rate = None dividend_shock_std = None @@ -191,6 +192,7 @@ def __init__( self.prices = [self.default_sim_price] self.dividends = [self.default_sim_price / self.price_to_dividend_ratio] + self.ranges = [] self.rng = rng if rng is not None else np.random.default_rng() @@ -213,6 +215,7 @@ def run_market(self, seed=0, buy_sell=(0,0), run_args = None): print('dividend: ' + str(new_dividend)) self.dividends.append(new_dividend) + self.ranges.append(new_price / 10) # entirely arbitrary value for now. return new_price, new_dividend diff --git a/sharkfin/markets/ammps.py b/sharkfin/markets/ammps.py index 8c86f40..bd49aae 100644 --- a/sharkfin/markets/ammps.py +++ b/sharkfin/markets/ammps.py @@ -25,6 +25,7 @@ class ClientRPCMarket(AbstractMarket): rng = None macro_price_field = None + range_field = None def __init__(self, seed=None, @@ -34,7 +35,8 @@ def __init__(self, dividend_std = 0.011988, price_to_dividend_ratio = 60 / 0.05, rng = None, - macro_price_field = None + macro_price_field = None, + range_field = None ): # discounted future value, divided by days per quarter @@ -51,6 +53,7 @@ def __init__(self, self.latest_price = None self.prices = [self.default_sim_price] self.dividends = [self.default_sim_price / self.price_to_dividend_ratio] + self.ranges = [] self.rpc_queue_name = queue_name self.rpc_host_name = host @@ -59,6 +62,10 @@ def __init__(self, if macro_price_field is not None \ else "ClosingPrice" + self.range_field = range_field \ + if range_field is not None \ + else "DailyRange" + self.init_rpc() def _get_rpc_market_host(self): @@ -142,6 +149,7 @@ def run_market(self, buy_sell=(0, 0), run_args = None): self.latest_price = np.nan self.prices.append(np.nan) + self.ranges.append(np.nan) raise MarketFailureError(f"AMMPS Market Failure: {self.response['MarketState']}") @@ -149,6 +157,7 @@ def run_market(self, buy_sell=(0, 0), run_args = None): self.latest_price = self.response[ self.macro_price_field ] self.prices.append(float(self.response[ self.macro_price_field ])) + self.ranges.append(float(self.response[self.range_field])) return self.latest_price, new_dividend diff --git a/sharkfin/simulation.py b/sharkfin/simulation.py index 81dbdf2..ddff5e4 100644 --- a/sharkfin/simulation.py +++ b/sharkfin/simulation.py @@ -700,6 +700,16 @@ def class_stat_column_to_dict(clabel): }).dropna() sim_stats["price_dividend_pearsonr"] = pd_df.corr()['p']['d'] + + sim_stats["ranges_mean"] = mean(self.market.ranges) + sim_stats["ranges_stdev"] = stdev(self.market.ranges) + sim_stats["ranges_min"] = min(self.market.ranges) + sim_stats["ranges_max"] = max(self.market.ranges) + + ranges_zscore = stats.zscore(np.array(self.market.ranges)) + + sim_stats["ranges_zs_max"] = ranges_zscore.max() + return sim_stats