U
    -Phm                     @   sp   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlZeeZG dd	 d	ZdS )
    )Jobbing)Strategy)BrokerAccount)getUniqueStructgetItemByKey)	Strategis)HistoricalDataNc                   @   sX   e Zd ZdZdddddZdddZdd	 ZdddZdddZdddZ	dd Z
dS )StrategyManagerz
    Strategy Manager is an overall central component that manages all the strategy for a user
    it contains all the objects relevant objeccts viz. all strategy objects with setup, all borker account and broker conneciton to perform its task.
    N)returnc                 C   s   || _ || _dS )z
        initialize manager with optional strategy code. 
        strategy coe is used to instantiate object for specific strategy and perform operations without affecting other strategy in place.
        N)Usercode)selfuserr    r   M/home/worker/MarketYantra/Yantra/yantra/trading/Strategies/strategyManager.py__init__   s    zStrategyManager.__init__c           	      C   s   g }g }t j }| jr&|j| jd}|D ]*}t| j|j|}|r*|jr*|	| q*|D ]"}|j
D ]}||krd|	| qdqZ|r|| _|}g }|D ]}|d |kr|	| q|| _|| _
|S )zP
        getting all strategy objects and relavant setups as dictionary
        )r   exchange)r   objectsallr   filterr   ZgetStrategyInstancer   isActiveappendZActiveScriptsr   ActiveStrategies)	r   r   ZlstActiveStrategiesZlstActiveScriptsZ
strategiesZstratstrategyscriptZlstTempr   r   r   getActiveStrategies"   s0    

z#StrategyManager.getActiveStrategiesc              	   C   s   t d| j d| j d | j}t|}| jD ]2}|jD ]&}| j	|}|
|jt|d| q:q0t d| j d| j d dS )a8  
        subscribe to the websocket for getting messages for Order update or/and price changes
        this will subscribe to the sockets depending on the respective strategies settings. 
        The sockets read the input and publish it over the redis pubsub. user broker object QueueId to subscribe to
        zstart: Websocket subscription z strategy []	accountIdzEnd: Websocket subscription N)loggerdebugr   r   TicksForCallbackr   r   ZActiveAccountsbrokerAccountgetBrokerObjectsubscribeWebSocketsZrequireOrderCallbackr   )r   lstTicksZ	lstUniquer   r   Z	brokerObjr   r   r   r#   J   s    

z#StrategyManager.subscribeWebSocketsTc           
   
   C   s   zg }g }|  | t| j}|  || _| jD ]8}|  | }t	d|  |D ]}|
| qZq2|| _|r~|   W n6 tk
r }	 zt	dt|	  W 5 d }	~	X Y nX d S )NzTicks zinitiate Error )r   r   r   Connectr!   r   initiateZgetScriptForTickCallbackr   r   r   r    r#   	Exceptionstr)
r   r   	subscriber$   ZlstTickForCallbackZ	brokerAccr   ticksticker   r   r   r&   [   s$    


zStrategyManager.initiatec                 C   s   | j D ]}|| j| qd S N)r   startr!   r   r   r   r   r   r   r.   u   s    
zStrategyManager.startc                 C   s   | j D ]}|| j| qd S r-   )r   resyncStrategyValuesr!   r/   r   r   r   r0   }   s    
z$StrategyManager.resyncStrategyValuesc           
   	   C   s&   t | j|}|||||d||}	|	S )Nr   )r   r!   ZgetHistoricCandleData)
r   r   exchSegsymboltokenfromdatetodateintervalZhistZhistDatar   r   r   getHistoricalData   s    z!StrategyManager.getHistoricalData)N)N)NT)N)N)__name__
__module____qualname____doc__r   r   r#   r&   r.   r0   r7   r   r   r   r   r	      s   
(


r	   )trading.Strategies.jobbingr   Ztrading.Strategies.strategyr   -trading.Entities.BrokerAccounts.brokerAccountr   Zutils.viewsr   r   trading.modelsr   Z&trading.Entities.Report.historicalDatar   logging	getLoggerr8   r   r	   r   r   r   r   <module>   s   
