def test_save_and_load_portfolio_storage_in_influxdb( influx_database: InfluxDBClient): storage = PortfolioSnapshotInnoDbStorage(influx_database) storage.save( PortfolioSnapshot(DUMMY_TIME, DUMMY_MARKET, DUMMY_ORDER_ID, STRATEGY_RUN_ID, [ Balance(DUMMY_MARKET, 'USD', Decimal('500')), Balance(DUMMY_MARKET, 'BTC', Decimal('0.5')), ])) result = storage.get_for_order(DUMMY_ORDER_ID) assert isinstance(result, PortfolioSnapshot) assert DUMMY_ORDER_ID == result.order_id assert 2 == len(result.balances) assert 'USD' in list(map(lambda balance: balance.currency, result.balances)) assert 'BTC' in list(map(lambda balance: balance.currency, result.balances)) assert Decimal('500') in list( map(lambda balance: balance.available_amount, result.balances)) assert Decimal('0.5') in list( map(lambda balance: balance.available_amount, result.balances)) snapshots = storage.get_for_strategy_run(STRATEGY_RUN_ID) assert isinstance(snapshots[str(DUMMY_ORDER_ID)], PortfolioSnapshot)
def get_balance(self, currency: str) -> Balance: currency = self._convert_currency_code_to_bittrex_format(currency) result = self._client_v2.get_balance(currency) self._validate_result(result) return Balance(self.name, currency, Decimal(result['result']['Available']))
def get_balances(self) -> List[Balance]: result = self._client_v2.get_balances() self._validate_result(result) return list( map( lambda data: Balance( self.name, self._normalize_currency_code(data['Balance']['Currency']), Decimal(data['Balance']['Available'])), result['result']))
def test_serialize_portfolio_snapshot(): snapshot = PortfolioSnapshot( DUMMY_TIME, DUMMY_MARKET, DUMMY_ORDER_ID, STRATEGY_RUN_ID, [ Balance(DUMMY_MARKET, 'USD', Decimal('500')), Balance(DUMMY_MARKET, 'BTC', Decimal('0.5')), ]) assert { 'balances': [{ 'available_amount': '500', 'currency': 'USD' }, { 'available_amount': '0.5', 'currency': 'BTC' }], 'market_name': 'dummy_market', 'order_id': '16fd2706-8baf-433b-82eb-8c7fada847db', 'strategy_run_id': '99fd2706-8baf-433b-82eb-8c7fada847ff', 'time': '2017-01-02T03:04:05+00:00' } == serialize_portfolio_snapshot(snapshot)
def _create_platform_snapshot_from_serialized( row: Dict) -> PortfolioSnapshot: balances: List[Balance] = [] market_name = row['market_name'] for key, value in row.items(): if value is None: value = '0' if key not in [ 'order_id', 'time', 'strategy_run_id', 'market_name' ]: balances.append(Balance(market_name, key, Decimal(value))) time = dateutil.parser.parse( row['time']).replace(tzinfo=datetime.timezone.utc) return PortfolioSnapshot(time, market_name, UUID(row['order_id']), UUID(row['strategy_run_id']), balances)
def get_balances(self) -> List[Balance]: result = [] for currency, available_amount in self._balances.items(): result.append(Balance(self.name, currency, available_amount)) return result
def get_balance(self, currency: str) -> Balance: if currency not in self._balances: self._balances[currency] = Decimal('0') return Balance(self._name, currency, self._balances[currency])