|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# @Date : 2021/12/2 |
| 4 | +# @Author : sukai |
| 5 | +MODEL_REPR = '{}({})' |
| 6 | + |
| 7 | + |
| 8 | +class PortfolioAccount: |
| 9 | + """ |
| 10 | + prime/paper account assets |
| 11 | + """ |
| 12 | + def __init__(self, account, update_timestamp=None): |
| 13 | + """ |
| 14 | + :param account: |
| 15 | + :param update_timestamp: asset update timestamp in milliseconds. |
| 16 | + """ |
| 17 | + self.account = account |
| 18 | + self.update_timestamp = update_timestamp |
| 19 | + self._segments = dict() |
| 20 | + |
| 21 | + @property |
| 22 | + def segments(self): |
| 23 | + """account information by contract type |
| 24 | + :return: dict with two keys, 'S' for stocks, 'C' for commodity futures; |
| 25 | + """ |
| 26 | + return self._segments |
| 27 | + |
| 28 | + def add_segment(self, segment): |
| 29 | + if segment.category not in self._segments: |
| 30 | + self._segments[segment.category] = segment |
| 31 | + return segment |
| 32 | + else: |
| 33 | + return self._segments.get(segment.category) |
| 34 | + |
| 35 | + def __repr__(self): |
| 36 | + d = {'account': self.account, 'update_timestamp': self.update_timestamp, 'segments': self.segments} |
| 37 | + return MODEL_REPR.format(self.__class__.__name__, d) |
| 38 | + |
| 39 | + |
| 40 | +class Segment: |
| 41 | + def __init__(self): |
| 42 | + self.currency = None |
| 43 | + self.capability = None |
| 44 | + self.category = None |
| 45 | + self.cash_balance = float('inf') |
| 46 | + self.cash_available_for_trade = float('inf') |
| 47 | + self.cash_available_for_withdrawal = float('inf') |
| 48 | + self.gross_position_value = float('inf') |
| 49 | + self.equity_with_loan = float('inf') |
| 50 | + self.net_liquidation = float('inf') |
| 51 | + self.init_margin = float('inf') |
| 52 | + self.maintain_margin = float('inf') |
| 53 | + self.overnight_margin = float('inf') |
| 54 | + self.unrealized_pl = float('inf') |
| 55 | + self.realized_pl = float('inf') |
| 56 | + self.excess_liquidation = float('inf') |
| 57 | + self.overnight_liquidation = float('inf') |
| 58 | + self.buying_power = float('inf') |
| 59 | + self.leverage = float('inf') |
| 60 | + self._currency_assets = dict() |
| 61 | + |
| 62 | + @property |
| 63 | + def currency_assets(self): |
| 64 | + return self._currency_assets |
| 65 | + |
| 66 | + def add_currency_asset(self, asset): |
| 67 | + if asset.currency not in self._currency_assets: |
| 68 | + self._currency_assets[asset.currency] = asset |
| 69 | + return asset |
| 70 | + else: |
| 71 | + return self._currency_assets.get(asset.currency) |
| 72 | + |
| 73 | + def __repr__(self): |
| 74 | + d = {k: v for k, v in self.__dict__.items() if not k.startswith('_')} |
| 75 | + d['currency_assets'] = self.currency_assets |
| 76 | + return MODEL_REPR.format(self.__class__.__name__, d) |
| 77 | + |
| 78 | + |
| 79 | +class CurrencyAsset: |
| 80 | + def __init__(self): |
| 81 | + self.currency = None |
| 82 | + self.cash_balance = float('inf') |
| 83 | + self.cash_available_for_trade = float('inf') |
| 84 | + self.gross_position_value = float('inf') |
| 85 | + self.stock_market_value = float('inf') |
| 86 | + self.futures_market_value = float('inf') |
| 87 | + self.option_market_value = float('inf') |
| 88 | + self.unrealized_pl = float('inf') |
| 89 | + self.realized_pl = float('inf') |
| 90 | + |
| 91 | + @staticmethod |
| 92 | + def from_dict(d): |
| 93 | + currency_asset = CurrencyAsset() |
| 94 | + currency_asset.__dict__.update(d) |
| 95 | + return currency_asset |
| 96 | + |
| 97 | + def __repr__(self): |
| 98 | + return MODEL_REPR.format(self.__class__.__name__, self.__dict__) |
0 commit comments