
    P#j                        d Z ddlmZ ddlZddlZddlZddlZddlmZ h dZdd
Z	ddZ
edk    r ej         e
                       dS dS )a  Position size calculator for US stocks/ETFs in KRW accounts.

Optional live mode uses yfinance if installed. Otherwise provide --price-usd
and --usdkrw manually. Every output includes the FX source and timestamp so
trade sizing is never based on a stale hidden assumption.
    )annotationsN)datetime>   SHPSQQLDSSOFNGDFNGUSOXLSOXSSPXLSPXSSQQQTECLTECSTQQQUPROtickerstrreturn&tuple[float | None, float | None, str]c                V   	 dd l }n# t          $ r Y dS w xY w	 |                    |           }t          |dd           }d }|r*|                    d          p|                    d          }|sP|                    dd          }|j        s2t          |d	                                         j	        d
                   }|                    d          }t          |dd           }d }|r*|                    d          p|                    d          }|sP|                    dd          }	|	j        s2t          |	d	                                         j	        d
                   }|rt          |          nd |rt          |          nd dfS # t          $ r}
d d d|
 fcY d }
~
S d }
~
ww xY w)Nr   )NNzyfinance not installed	fast_info
last_priceregular_market_price1d1m)periodintervalClosezKRW=Xzyfinance: ticker + KRW=Xzyfinance error: )
yfinance	ExceptionTickergetattrgethistoryemptyfloatdropnailoc)r   yfstockinfopricehist	fx_tickerfx_infousdkrwfx_histexcs              #outputs/position_size_calculator.pyfetch_yfinancer7      s   4 4 4 433344		&!!uk400 	OHH\**Ndhh7M.N.NE 	?==t=<<D: ?d7m22449"=>>IIg&&	)[$77 	V[[..U'++>T2U2UF 	C''td'CCG= Cww/6688=bABB$.uU|||$0QfTSmmm 4 4 4T3c3333333334s*    
E2F 
F(F#F(#F(intc                     t          j        d          } |                     dd           |                     dddgd	
           |                     dt          d	           |                     dt                     |                     dt                     |                     dd           |                     dd           |                     dt          d           |                     ddd           |                     ddd           |                                 }|j                                        }|j        dk    r|t          v rt          d| d           dS |j
        }|j        }|j        }|j        }|j        r't          |          \  }}}	|p|}|p|}|r|	n|}|r|	n|}|r|st          d            d!S t!          j                                        d"#          }
d!|j        d$z  z   }||z  |z  }t)          j        |j        |z            }||z  |z  }|j        ||z  z
  }|
|j        |||
||
t/          |d%          t/          |d%          t/          |j                  t/          |j        d%          |t/          |          t/          |          d&}|j        r&t          t1          j        |d'd(                     d)S t          d*|d+                     t          d,|d-                     t          d.|d                     t          d/|d0                     t          d1|d2                     t          d3|d4         d5           t          d6|d7         d5           t          d8|d9         d:           t          d;|d<         d=           t          d>|d?         d@           t          dA|dB         d:           t          dC|dD         d:           d)S )ENz/Calculate US stock share count from KRW budget.)descriptionr   zTicker, e.g. SOXL, SOXS, AMD)helpz	--accountmainsubT)choicesrequiredz--budget-krw)typer?   z--price-usd)r@   z--usdkrwz--price-sourcemanual)defaultz--fx-sourcez--fee-buffer-pctg333333?)r@   rB   z--live
store_truez"Try yfinance for price and USD/KRW)actionr;   z--jsonzPrint machine-readable JSONzNOT ELIGIBLE: z6 is leveraged/inverse and sub account cannot trade it.   zZNEEDS LIVE PRICE/FX: provide --price-usd and --usdkrw, or install yfinance and use --live.   seconds)timespecd      )	timestampaccountr   price_sourceprice_timestamp	fx_sourcefx_timestamp	price_usdr3   
budget_krwfee_buffer_pctcalculated_sharesapprox_exposure_krwfee_buffered_leftover_krwF)ensure_asciiindentr   ztimestamp: rK   z	account: rL   zticker: zprice_source: rM   zfx_source: rO   zprice_usd: rQ   z,.4fzusdkrw: r3   zbudget_krw: rR   z,.0fzfee_buffer_pct: rS   z.3fzcalculated_shares: rT   ,zapprox_exposure_krw: rU   zfee_buffered_leftover_krw: rV   )argparseArgumentParseradd_argumentr)   
parse_argsr   upperrL   LEVERAGED_OR_INVERSEprintrQ   r3   rM   rO   liver7   r   now	isoformatrS   mathfloorrR   roundjsondumps)parserargsr   r/   r3   rM   rO   
live_pricelive_fxlive_sourcerK   fee_multiplierper_share_krwsharesexposureleftoverresults                    r6   r<   r<   8   s   $1bcccF
'EFFF
fe_tLLL
UTBBB
E222

///
((;;;
x888
*EEE
<`aaa
<YZZZD[  F|u+?!?!?]v]]]^^^qNE[F$LIy :+9&+A+A(
G[#e"F&0B{{l#*9KK		  jkkkq(()(<<I$-34NFN^3MZ-788F~&H&="89H <$$!5!__""DO,, 3Q77#$X%*8__ F" y djeA>>>???q	
-{+
-
-...	
)fY'
)
)***	
'VH%
'
'(((	
36.1
3
3444	
-{+
-
-...	
2{+
2
2
2333	
,VH%
,
,
,---	
4-
4
4
4555	
;V$45
;
;
;<<<	
?': ;
?
?
?@@@	
F&)>"?
F
F
FGGG	
R/J(K
R
R
RSSS1    __main__)r   r   r   r   )r   r8   )__doc__
__future__r   rZ   rd   rg   sysr   r_   r7   r<   __name__exit rt   r6   <module>r|      s     # " " " " "    



         4 4 4 4@K K K K\ zCHTTVV rt   