-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/liquidity info #281
Feat/liquidity info #281
Changes from 22 commits
864024d
55c6c2a
9dda954
14bf941
b90b1ec
4f2e08c
681ab65
06d858a
7292294
791b42b
359ab23
bfddaea
46f1909
6fe22a0
df312c5
4ccf048
5254878
c9f26e5
3af8042
d0a19d4
0f8ca2e
3a0ebc2
a8d5407
f7efe70
bf8cb69
ac069f8
4e817ec
a27eb32
e9f7644
aed9822
caad3c9
0c6e99b
785f887
adf8cde
db96372
eb36e9f
a23e667
e073601
bede5d5
4f37b32
736a9a2
83dbcb0
bc90d8c
8cfa1ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
import csv | ||
import json | ||
from math import prod | ||
from typing import Any, Dict, Generator, Optional, Tuple, Union | ||
from typing import Any, Dict, Generator, Optional, Tuple, Union, List | ||
|
||
from packages.valory.skills.decision_maker_abci.behaviours.base import ( | ||
DecisionMakerBaseBehaviour, | ||
|
@@ -272,6 +272,99 @@ def _calc_binary_shares( | |
|
||
return num_shares, available_shares | ||
|
||
def _get_mocked_bet(self) -> Bet: | ||
"""Function to prepare the mocked bet based on liquidity info at the shared state""" | ||
liquidity_amounts = self.shared_state.liquidity_amounts | ||
liquidity_prices = self.shared_state.liquidity_prices | ||
markets = list(liquidity_amounts.keys()) | ||
if self.shared_state.mock_data is not None: | ||
question_id = self.shared_state.mock_data.id | ||
else: | ||
raise ValueError("No mocked data information") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is duplicated below, you may create a property for safely accessing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the BenchmarkingMockData there is no benchmarking mode so I guess this use-case is already covered in the code even before. No extra checks needed here. |
||
|
||
# check if the question is at the dictionary | ||
outcome_token_amounts = self.benchmarking_mode.outcome_token_amounts | ||
outcome_token_prices = self.benchmarking_mode.outcome_token_marginal_prices | ||
if question_id in markets: # read the previous information | ||
outcome_token_amounts = liquidity_amounts[question_id] | ||
outcome_token_prices = liquidity_prices[question_id] | ||
else: # initializing liquidity info | ||
liquidity_amounts[question_id] = outcome_token_amounts | ||
liquidity_prices[question_id] = outcome_token_prices | ||
|
||
self.context.logger.info(f"outcome token amounts: {outcome_token_amounts}") | ||
self.context.logger.info(f"outcome token prices: {outcome_token_prices}") | ||
|
||
mocked_bet = Bet( | ||
id="", | ||
market="", | ||
title="", | ||
collateralToken="", | ||
creator="", | ||
fee=self.benchmarking_mode.pool_fee, | ||
openingTimestamp=0, | ||
outcomeSlotCount=2, | ||
outcomeTokenAmounts=outcome_token_amounts, | ||
outcomeTokenMarginalPrices=outcome_token_prices, | ||
outcomes=["Yes", "No"], | ||
scaledLiquidityMeasure=10, | ||
) | ||
return mocked_bet | ||
|
||
def _update_liquidity_info( | ||
self, bet_amount: float, vote: int | ||
) -> Tuple[List[int], List[int]]: | ||
"""Function to update the liquidity information after placing a bet for a market | ||
and to return the old and new prices""" | ||
liquidity_amounts = self.shared_state.liquidity_amounts | ||
liquidity_prices = self.shared_state.liquidity_prices | ||
markets = list(liquidity_amounts.keys()) | ||
if self.shared_state.mock_data is not None: | ||
question_id = self.shared_state.mock_data.id | ||
else: | ||
raise ValueError("No mocked data information") | ||
if question_id not in markets: | ||
raise ValueError( | ||
f"The market id {question_id} is not at the shared state dictionary" | ||
) | ||
|
||
old_liquidity_amounts = liquidity_amounts[question_id] | ||
selected_type_tokens_in_pool = old_liquidity_amounts[vote] | ||
opposite_vote = vote ^ 1 | ||
other_tokens_in_pool = old_liquidity_amounts[opposite_vote] | ||
self.context.logger.info(f"Voting for option = {vote}") | ||
if vote == 0: | ||
old_L0, old_L1 = selected_type_tokens_in_pool, other_tokens_in_pool | ||
new_L0, new_L1 = old_L0 + bet_amount, old_L0 * old_L1 / ( | ||
old_L0 + bet_amount | ||
) | ||
else: | ||
old_L0, old_L1 = other_tokens_in_pool, selected_type_tokens_in_pool | ||
new_L0, new_L1 = ( | ||
old_L0 * old_L1 / (old_L1 + bet_amount), | ||
old_L1 + bet_amount, | ||
) | ||
# new liquidity prices computed from the new amounts | ||
new_p0 = new_L0 / (new_L0 + new_L1) | ||
new_p1 = new_L1 / (new_L0 + new_L1) | ||
liquidity_prices[question_id] = [new_p0, new_p1] | ||
self.context.logger.info( | ||
f"updating liquidity prices for question: {question_id}" | ||
) | ||
self.shared_state.liquidity_prices = liquidity_prices | ||
|
||
# updating liquidity amounts | ||
new_amounts = [int(new_L0), int(new_L1)] | ||
liquidity_amounts[question_id] = new_amounts | ||
old_amounts = [old_L0, old_L1] | ||
self.context.logger.info( | ||
f"updating liquidity amounts for question: {question_id}" | ||
) | ||
self.context.logger.info(f"New amounts={new_amounts}") | ||
self.shared_state.liquidity_amounts = liquidity_amounts | ||
|
||
return old_amounts, new_amounts | ||
|
||
def _is_profitable( | ||
self, | ||
vote: int, | ||
|
@@ -281,23 +374,11 @@ def _is_profitable( | |
confidence: float, | ||
) -> Generator[None, None, Tuple[bool, int]]: | ||
"""Whether the decision is profitable or not.""" | ||
|
||
bet = ( | ||
self.sampled_bet | ||
if not self.benchmarking_mode.enabled | ||
else Bet( | ||
id="", | ||
market="", | ||
title="", | ||
collateralToken="", | ||
creator="", | ||
fee=self.benchmarking_mode.pool_fee, | ||
openingTimestamp=0, | ||
outcomeSlotCount=2, | ||
outcomeTokenAmounts=self.benchmarking_mode.outcome_token_amounts, | ||
outcomeTokenMarginalPrices=self.benchmarking_mode.outcome_token_marginal_prices, | ||
outcomes=["Yes", "No"], | ||
scaledLiquidityMeasure=10, | ||
) | ||
else self._get_mocked_bet() | ||
) | ||
selected_type_tokens_in_pool, other_tokens_in_pool = self._get_bet_sample_info( | ||
bet, vote | ||
|
@@ -348,7 +429,12 @@ def _is_profitable( | |
|
||
if self.benchmarking_mode.enabled: | ||
if is_profitable: | ||
self._write_benchmark_results(p_yes, p_no, confidence, bet_amount) | ||
old_amounts, new_amounts = self._update_liquidity_info( | ||
net_bet_amount, vote | ||
) | ||
self._write_benchmark_results( | ||
p_yes, p_no, confidence, bet_amount, old_amounts, new_amounts | ||
) | ||
else: | ||
self._write_benchmark_results(p_yes, p_no, confidence) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you are using liquidity_info on line 663-666. If this is None, shouldnt we handle this case differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected