B
     9-eæ
  ã               @   s0   d dl mZ G dd„ deƒZG dd„ deƒZdS )é    )ÚDecimalc               @   s,   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdS )	ÚPositionRecordz9
    This class is to match the callback of position
    c             C   s   || _ || _|| _|| _dS )z²
        !!!! str_security does not have primaryExchange and exchange !!!!
        positions are aggregations of a few position that may be traded at different exchange.
        N)Ústr_securityÚamountÚ
cost_basisÚcontract)ÚselfZ+str_security_no_exchange_no_primaryExchanger   r   r   © r	   ú±G:\My Drive\STUDY\EPAT\09 TBP - Trading & Back-testing Platforms\TBP04 - Backtesting & Live Trading\IB IBridgePy API\02 Python 3.7 IBridgePy_Win_Anaconda37_64\models\Position.pyÚ__init__   s    zPositionRecord.__init__c             C   s   d| j | j| jf S )Nz%{security=%s amount=%s cost_basis=%s})r   r   r   )r   r	   r	   r
   Ú__str__   s    zPositionRecord.__str__c             C   s   | j S )N)r   )r   r	   r	   r
   Úprice   s    zPositionRecord.priceN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úpropertyr   r	   r	   r	   r
   r      s   
r   c               @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚKeyedPositionRecordszª
    Only store positions when amount > 0
    Delete positions when amount == 0
    keyed by str_security_no_exchange_no_primaryExchange, value=this::PositionRecord

    c             C   s   i | _ || _d S )N)Ú_keyedPositionRecordsÚ_log)r   Úlogr	   r	   r
   r   "   s    zKeyedPositionRecords.__init__c             C   s@   t | jƒdkrdS d}x$| jD ]}|d|| j| f 7 }qW |S )Nr   zEmpty keyedPositionRecordszPrint KeyedPositionRecords
z%s:%s
)Úlenr   )r   ÚansÚkeyr	   r	   r
   r   &   s    zKeyedPositionRecords.__str__c             C   s2   |j dkr"|j| jkr.| j|j= n|| j|j< d S )Nr   )r   r   r   )r   ÚpositionRecordr	   r	   r
   Úupdate.   s    
zKeyedPositionRecords.updatec             C   s2   |  ¡ }|| jkr| j| S t|tdƒdd ƒS d S )Nr   g        )Ú
full_printr   r   r   )r   Úsecurityr   r	   r	   r
   ÚgetPositionRecord7   s    

z&KeyedPositionRecords.getPositionRecordc             C   s   t | jƒdkS )Nr   )r   r   )r   r	   r	   r
   Úhold_any_positionA   s    z&KeyedPositionRecords.hold_any_positionc             C   s   | j S )zT
        :return: dictionary, keyed by str_security, value = PositionRecord
        )r   )r   r	   r	   r
   Úget_all_positionsD   s    z&KeyedPositionRecords.get_all_positionsc             C   s
   i | _ d S )N)r   )r   r	   r	   r
   Údelete_every_positionJ   s    z*KeyedPositionRecords.delete_every_positionN)r   r   r   r   r   r   r   r   r   r    r!   r	   r	   r	   r
   r      s   	
r   N)Údecimalr   Úobjectr   r   r	   r	   r	   r
   Ú<module>   s   