
    /!g                     f    d dl mZmZmZ d dlmZ deeef   deeef   fdZd Zd Zd Z	d	 Z
d
 Zy)    )UnionDictList)ObjectIddatareturnc           	         t        | t              rH| j                         D ci c],  \  }}|t        |t              rt	        |      n
t        |      . c}}S t        | t              r| D cg c]  }t        |       c}S | S c c}}w c c}w )z8Convert MongoDB ObjectId to string in nested structures.)
isinstancedictitemsr   strconvert_objectid_ohlclist)r   kvitems       6/var/www/html/trade_iq/helper_functions/ohlc_report.pyr   r      s|    $ JJL*Aq Z843q6:OPQ:RR * 	*	D$	8<=%d+==K	* >s   1B-B
c           	         dg i}t        |j                               }| dddd|D ci c]  }|||   
 c}}|d   j                  |       |rT||d      }| d|  ddd|D ci c]"  }||dk(  rdnt        ||   |z
  |z  dz  d	      $ c}}|d   j                  |       t	        | ||      }|d   j                  |       |S c c}w c c}w )
z/Helper function to process summary calculationssummary_totalTOTAL typesymbolqty	avg_pricer   z% TOTAL CHANGE in -d      )sortedkeysappendroundcalculate_day_changesextend)	report_typetotal_pricesr   sorted_datesdatetotal_entryfirst_totalchange_entryday_changess	            r   process_summary_calculationsr.      s*   $b)M,++-.L 	
 1=
=4d##
=K /"))+6 "<?3*;-8	

 )  [A-cT*[8KG#MPQRS

 	o&--l; (\<PK/"))+61 >s   C$'Cc                    i }g }| d|  ddd}t        |      D ]8  \  }}|dk(  rd||<   n ||dz
     }t        ||   ||   z
  d      ||<   ||   ||<   : |j                  |       | d|  dddt        |      D ci c]9  \  }}||dk(  s|||dz
        dk(  rdnt        ||   |||dz
        z  d	z  d      ; c}}}	|j                  |	       |S c c}}w )
z(Helper function to calculate day changeszDAY CHANGE in r   r   r   r      r   zDAY CHANGE % in r   )	enumerater#   r"   )
r&   r(   r'   r-   day_change_entriesday_change_entryir)   	prev_dateday_change_percent_entrys
             r   r$   r$   2   sP   K ";-0	 \* 346 #K$QqS)I %l4&8<	;R&RTU VK!,T!23 ./ $[M2	
  %\2
 4 a<QqS0A#Ba#G#;t$|L14E'FFLqQR

  67
s   =>Cc           	          ddiddiddiddiddiddiddidd	id
}|j                         D ]  \  }}|D ]  }	 t        ||||          | d   j                  |       y# t        $ r Y 6w xY w)z5Helper function to process metrics for summary reportr   zHigh - Openz
Open - Lowz
High - LowzOpen - ClosezHigh - Open %zOpen - Low %zHigh - Low %zOpen - Close %)high_minus_openopen_minus_lowhigh_minus_lowopen_minus_closehigh_minus_open_propen_minus_low_prhigh_minus_low_propen_minus_close_prsummary_reportN)r   calculate_metric_valueKeyErrorupdate)results_by_typer(   metricsmetricresultr)   s         r   process_metricsrH   Y   s     %m4#\2#\2%~6'9&7&7 (*:;	G "--/   	D&vtV_M	 $%,,W5  s   A	A+*A+c                    | j                  d      rO| j                  dd      }t        |||      }|d   dk(  rd||<   yt        |d   |d   z
  |d   z  dz  d      ||<   yt        | ||      }t        |d   |d   z
  d      ||<   y)	al  
    Helper function to calculate various metric values for the summary report

    Args:
        metric (str): The type of metric to calculate
        date (str): The date for which to calculate the metric
        result (dict): Dictionary to store the calculated result
        results_by_type (dict): The full results dictionary containing all report types
    _prr   r0   r   r   r   r   N)endswithreplaceget_metric_valuesr#   )rF   r)   rG   rD   base_metricvaluess         r   rA   rA   o   s     unnUB/";oF!9>F4L &)fQi"76!9!Ds!JANF4L #64AVAY2A6t    c                     | dk(  r|d   d   |   |d   d   |   fS | dk(  r|d   d   |   |d   d   |   fS | dk(  r|d   d   |   |d   d   |   fS | dk(  r|d   d   |   |d   d	   |   fS t        d
|        )a_  
    Helper function to get the appropriate values for metric calculation

    Args:
        metric (str): The type of metric
        date (str): The date for which to get values
        results_by_type (dict): The full results dictionary containing all report types

    Returns:
        tuple: A pair of values needed for the metric calculation
    r8   r@   highopenr9   lowr:   r;   closezUnknown metric type: )
ValueError)rF   r)   rD   s      r   rM   rM      s    "",-f5d;,-f5d;
 	
 
#	#,-f5d;,-e4T:
 	
 
#	#,-f5d;,-e4T:
 	
 
%	%,-f5d;,-g6t<
 	

 09::rP   N)typingr   r   r   bsonr   r   r.   r$   rH   rA   rM    rP   r   <module>rZ      sN    $ $ dDj 1 eD$J6G #J%N6,70!;rP   