def test_inverse_price(): GIVEN("a price") buy_price = 2 WHEN("we calculate the inverse") sell_price = calc_inverse_price(buy_price) THEN("the correct price is returned") assert sell_price == 2 GIVEN("another valid price") buy_price = 1.5 WHEN("we calculate the sell price") sell_price = calc_inverse_price(buy_price) THEN("the correct price is returned") assert sell_price == 1 / (1 - (1 / buy_price)) GIVEN("a string price") buy_price = "a price" WHEN("we calculate the sell price") sell_price = calc_inverse_price(buy_price) THEN("the sell price is shown to be not a number") assert is_not_a_number(sell_price) GIVEN("a negative price") buy_price = -2 WHEN("we calculate the sell price") sell_price = calc_inverse_price(buy_price) THEN("the sell price is shown to be not a number") assert is_not_a_number(sell_price) GIVEN("a price between 0 and 1") buy_price = 0.99 WHEN("we calculate the sell price") sell_price = calc_inverse_price(buy_price) THEN("the sell price is shown to be not a number") assert is_not_a_number(sell_price)
def __calc_ex_average_lay_price(self): total_lay_price = sum( calc_sell_liability(price=trade.get("price"), size=trade.get("size")) * calc_inverse_price(trade.get("price")) for trade in self.__traded_volume) ex_average_lay_price = (total_lay_price / self.__ex_lay_size if self.__ex_lay_size else not_a_number()) return ex_average_lay_price
def __calc_ex_average_lay_price(self, dict): trades = dict.get("ex").get("trd") valid_trades = self.__get_valid_entries(trades) liabilities = self.__calc_lay_liabilities(trades) total_liability = sum(liabilities) return ( sum( calc_inverse_price(buy_price) * liability for buy_price, liability in zip(valid_trades.keys(), liabilities) ) / total_liability if total_liability else not_a_number() )
def __construct_data(self): data = {"id": self.__id} if "removal_date" in self.__required_variables: data["removal_date"] = self.__get_removal_date() if "sp_back_price" in self.__required_variables: data["sp_back_price"] = self.__sp_back_price if "sp_lay_price" in self.__required_variables: data["sp_lay_price"] = calc_inverse_price(self.__sp_back_price) if "sp_back_size" in self.__required_variables: data["sp_back_size"] = self.__calc_sp_back_size() if "sp_lay_size" in self.__required_variables: data["sp_lay_size"] = self.__calc_sp_lay_size() if "ex_back_size" in self.__required_variables: data["ex_back_size"] = self.__ex_back_size if "ex_lay_size" in self.__required_variables: data["ex_lay_size"] = self.__ex_lay_size if "ex_average_back_price" in self.__required_variables: data["ex_average_back_price"] = self.__calc_ex_average_back_price() if "ex_average_lay_price" in self.__required_variables: data["ex_average_lay_price"] = self.__calc_ex_average_lay_price() if "ex_offered_back_price" in self.__required_variables: data[ "ex_offered_back_price"] = self.__get_ex_ex_offered_back_price( ) if "ex_offered_lay_price" in self.__required_variables: data["ex_offered_lay_price"] = self.__get_ex_ex_offered_lay_price() return data
def __calc_sp_lay_price(self, dict): sp_back_price = self.__get_sp_back_price(dict) return calc_inverse_price(sp_back_price)