
    ~gL                         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mZ d dlZ ej                   e      Z G d	 d
      Zy)    )Jobbing)Strategy)BrokerAccount)getUniqueStructgetItemByKey)	Strategis)HistoricalData)RedisDBNc                   F    e Zd ZdZd
ddZd
dZd ZddZd
dZd
dZ	d	 Z
y)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.
    Nc                      || _         || _        y)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   s      SC:\Janisar\Projects\MarketYanta\Yantra\yantra\trading\Strategies\strategyManager.py__init__zStrategyManager.__init__   s    
 		    c                 @   g }g }t         j                  j                         }| j                  r|j	                  | j                        }|D ]N  }t        j                  | j                  |j                  |      }|s1|j                  s>|j                  |       P |D ])  }|j                  D ]  }||vs|j                  |        + |r,|| _        |}g }|D ]  }|d   |k(  s|j                  |        || _        || _
        t        | j                  d      }	|S )zP
        getting all strategy objects and relavant setups as dictionary
        )r   exchangeT)r   objectsallr   filterr   getStrategyInstancer   isActiveappendActiveScriptsr   ActiveStrategiesr
   )
r   r   lstActiveStrategieslstActiveScripts
strategiesstratstrategyscriptlstTempredisDBs
             r   getActiveStrategiesz#StrategyManager.getActiveStrategies    s   
 !&&**,
99#**$))*<JE33DIIuzz8TH$$'..x8	   ,H"00!11$++F3 1 ,
 $DM&G!!*%1$++F3 " !4-$))T* #"r   c           
         t         j                  d| j                   d| j                   d       | j                  }t        |      }| j                  D ]U  }|j                  D ]D  }| j                  j                  |      }|j                  |j                  t        |d|             F W t         j                  d| j                   d| j                   d       y)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   ActiveAccountsbrokerAccountgetBrokerObjectsubscribeWebSocketsrequireOrderCallbackr   )r   lstTicks	lstUniquer#   r*   	brokerObjs         r   r1   z#StrategyManager.subscribeWebSocketsH   s     	5dii[DII;VWXY((#H-	--H%44	 ..>>yI	--h.K.K\ZcdoqzM{| 5 .
 	3DII;k$))TUVWr   c                 L   g }g }| j                  |       t        | j                        }|j                          || _        | j
                  D ]:  }|j                          |j                         }|D ]  }|j                  |        < || _	        |r| j                          y y N)r'   r   r   Connectr/   r   initiategetScriptForTickCallbackr   r-   r1   )	r   r   	subscriber3   lstTickForCallback	brokerAccr#   ticksticks	            r   r9   zStrategyManager.initiateY   s      *!$)),	& --H557E%  .
 !)$$& r   c                 ^    | j                   D ]  }|j                  | j                  |         y r7   )r   startr/   r   r   r#   s      r   rA   zStrategyManager.starto   s(    
 --HNN4--x8 .r   c                 ^    | j                   D ]  }|j                  | j                  |         y r7   )r   resyncStrategyValuesr/   rB   s      r   rD   z$StrategyManager.resyncStrategyValuesw   s(    --H))$*<*<hG .r   c           	      `    t        | j                  |      }|j                  ||||d||      }	|	S )Nr   )r	   r/   getHistoricCandleData)
r   r*   exchSegsymboltokenfromdatetodateintervalhisthistDatas
             r   getHistoricalDataz!StrategyManager.getHistoricalData|   s8    d00)<--guhPQS[\bcr   r7   )returnN)NT)__name__
__module____qualname____doc__r   r'   r1   r9   rA   rD   rO    r   r   r   r      s-    &#PX"',9H
r   r   )trading.Strategies.jobbingr   trading.Strategies.strategyr   -trading.Entities.BrokerAccounts.brokerAccountr   utils.viewsr   r   trading.modelsr   &trading.Entities.Report.historicalDatar	   trading.Redis.redisdbr
   logging	getLoggerrQ   r+   r   rU   r   r   <module>r_      s=    . 0 G 5 $ A ) 			8	$l lr   