@@ -2540,11 +2540,11 @@ def BS_VALUE(df, quote=None, r=0.025, v=None):
2540
2540
[..., 3036.698780158862, 2393.333388624822, 2872.607833620801]
2541
2541
"""
2542
2542
if not (quote and quote .ins_class .endswith ("OPTION" ) and quote .underlying_symbol == df ["symbol" ][0 ]):
2543
- return pd .DataFrame (df . where (df ["close" ] < 0 ), columns = ["bs_price" ])
2543
+ return pd .DataFrame (np . full_like (df ["close" ], float ( 'nan' ) ), columns = ["bs_price" ])
2544
2544
if v is None :
2545
2545
v = tqsdk .tafunc ._get_volatility (df ["close" ], df ["duration" ], quote .trading_time , float ('nan' ))
2546
2546
if math .isnan (v ):
2547
- return pd .DataFrame (df . where (df ["close" ] < 0 ), columns = ["bs_price" ])
2547
+ return pd .DataFrame (np . full_like (df ["close" ], float ( 'nan' ) ), columns = ["bs_price" ])
2548
2548
o = 1 if quote .option_class == "CALL" else - 1
2549
2549
t = tqsdk .tafunc ._get_t_series (df ["datetime" ], df ["duration" ], quote )
2550
2550
return pd .DataFrame (data = list (tqsdk .tafunc .get_bs_price (df ["close" ], quote .strike_price , r , v , t , o )),
@@ -2583,21 +2583,26 @@ def OPTION_GREEKS(df, quote=None, r=0.025, v=None):
2583
2583
print(list(greeks["rho"]))
2584
2584
2585
2585
"""
2586
+ new_df = pd .DataFrame ()
2586
2587
if not (quote and quote .ins_class .endswith ("OPTION" ) and quote .instrument_id == df ["symbol" ][0 ]
2587
2588
and quote .underlying_symbol == df ["symbol1" ][0 ]):
2588
- return pd .DataFrame (df .where (df ["close1" ] < 0 ), columns = ["delta" , "theta" , "gamma" , "vega" , "rho" ])
2589
- o = 1 if quote .option_class == "CALL" else - 1
2590
- t = tqsdk .tafunc ._get_t_series (df ["datetime" ], df ["duration" ], quote ) # 到期时间
2591
- if v is None :
2592
- his_v = tqsdk .tafunc ._get_volatility (df ["close1" ], df ["duration" ], quote .trading_time , 0.3 )
2593
- v = tqsdk .tafunc .get_impv (df ["close1" ], df ["close" ], quote .strike_price , r , his_v , t , o )
2594
- d1 = tqsdk .tafunc ._get_d1 (df ["close1" ], quote .strike_price , r , v , t )
2595
- new_df = pd .DataFrame ()
2596
- new_df ["delta" ] = tqsdk .tafunc .get_delta (df ["close1" ], quote .strike_price , r , v , t , o , d1 )
2597
- new_df ["theta" ] = tqsdk .tafunc .get_theta (df ["close1" ], quote .strike_price , r , v , t , o , d1 )
2598
- new_df ["gamma" ] = tqsdk .tafunc .get_gamma (df ["close1" ], quote .strike_price , r , v , t , d1 )
2599
- new_df ["vega" ] = tqsdk .tafunc .get_vega (df ["close1" ], quote .strike_price , r , v , t , d1 )
2600
- new_df ["rho" ] = tqsdk .tafunc .get_rho (df ["close1" ], quote .strike_price , r , v , t , o , d1 )
2589
+ new_df ["delta" ] = pd .Series (np .full_like (df ["close1" ], float ('nan' )))
2590
+ new_df ["theta" ] = pd .Series (np .full_like (df ["close1" ], float ('nan' )))
2591
+ new_df ["gamma" ] = pd .Series (np .full_like (df ["close1" ], float ('nan' )))
2592
+ new_df ["vega" ] = pd .Series (np .full_like (df ["close1" ], float ('nan' )))
2593
+ new_df ["rho" ] = pd .Series (np .full_like (df ["close1" ], float ('nan' )))
2594
+ else :
2595
+ o = 1 if quote .option_class == "CALL" else - 1
2596
+ t = tqsdk .tafunc ._get_t_series (df ["datetime" ], df ["duration" ], quote ) # 到期时间
2597
+ if v is None :
2598
+ his_v = tqsdk .tafunc ._get_volatility (df ["close1" ], df ["duration" ], quote .trading_time , 0.3 )
2599
+ v = tqsdk .tafunc .get_impv (df ["close1" ], df ["close" ], quote .strike_price , r , his_v , t , o )
2600
+ d1 = tqsdk .tafunc ._get_d1 (df ["close1" ], quote .strike_price , r , v , t )
2601
+ new_df ["delta" ] = tqsdk .tafunc .get_delta (df ["close1" ], quote .strike_price , r , v , t , o , d1 )
2602
+ new_df ["theta" ] = tqsdk .tafunc .get_theta (df ["close1" ], quote .strike_price , r , v , t , o , d1 )
2603
+ new_df ["gamma" ] = tqsdk .tafunc .get_gamma (df ["close1" ], quote .strike_price , r , v , t , d1 )
2604
+ new_df ["vega" ] = tqsdk .tafunc .get_vega (df ["close1" ], quote .strike_price , r , v , t , d1 )
2605
+ new_df ["rho" ] = tqsdk .tafunc .get_rho (df ["close1" ], quote .strike_price , r , v , t , o , d1 )
2601
2606
return new_df
2602
2607
2603
2608
@@ -2626,18 +2631,20 @@ def OPTION_VALUE(df, quote=None):
2626
2631
print(list(values["time"]))
2627
2632
api.close()
2628
2633
"""
2634
+ new_df = pd .DataFrame ()
2629
2635
if not (quote and quote .ins_class .endswith ("OPTION" ) and quote .instrument_id == df ["symbol" ][0 ]
2630
2636
and quote .underlying_symbol == df ["symbol1" ][0 ]):
2631
- return pd .DataFrame (df .where (df ["close1" ] < 0 ), columns = ["intrins" , "time" ])
2632
- o = 1 if quote .option_class == "CALL" else - 1
2633
- new_df = pd .DataFrame ()
2634
- intrins = o * (df ["close1" ] - quote .strike_price )
2635
- new_df ["intrins" ] = pd .Series (np .where (intrins > 0.0 , intrins , 0.0 ))
2636
- new_df ["time" ] = pd .Series (df ["close" ] - new_df ["intrins" ])
2637
+ new_df ["intrins" ] = pd .Series (np .full_like (df ["close1" ], float ('nan' )))
2638
+ new_df ["time" ] = pd .Series (np .full_like (df ["close1" ], float ('nan' )))
2639
+ else :
2640
+ o = 1 if quote .option_class == "CALL" else - 1
2641
+ intrins = o * (df ["close1" ] - quote .strike_price )
2642
+ new_df ["intrins" ] = pd .Series (np .where (intrins > 0.0 , intrins , 0.0 ))
2643
+ new_df ["time" ] = pd .Series (df ["close" ] - new_df ["intrins" ])
2637
2644
return new_df
2638
2645
2639
2646
2640
- def OPTION_IMPV (df , quote = None , r = 0.025 , init_v = None ):
2647
+ def OPTION_IMPV (df , quote = None , r = 0.025 ):
2641
2648
"""
2642
2649
计算期权隐含波动率
2643
2650
@@ -2648,8 +2655,6 @@ def OPTION_IMPV(df, quote=None, r=0.025, init_v=None):
2648
2655
2649
2656
r (float): 无风险利率
2650
2657
2651
- init_v (float): 初始对波动率的估计
2652
-
2653
2658
Returns:
2654
2659
pandas.DataFrame: 返回的 DataFrame 包含 1 列, 是 "impv", 与参数 df 行数相同
2655
2660
@@ -2666,9 +2671,8 @@ def OPTION_IMPV(df, quote=None, r=0.025, init_v=None):
2666
2671
"""
2667
2672
if not (quote and quote .ins_class .endswith ("OPTION" ) and quote .instrument_id == df ["symbol" ][0 ]
2668
2673
and quote .underlying_symbol == df ["symbol1" ][0 ]):
2669
- return pd .DataFrame (df .where (df ["close1" ] < 0 ), columns = ["impv" ])
2670
- if init_v is None :
2671
- init_v = tqsdk .tafunc ._get_volatility (df ["close1" ], df ["duration" ], quote .trading_time , 0.3 )
2674
+ return pd .DataFrame (np .full_like (df ["close1" ], float ('nan' )), columns = ["impv" ])
2675
+ init_v = tqsdk .tafunc ._get_volatility (df ["close1" ], df ["duration" ], quote .trading_time , 0.3 )
2672
2676
o = 1 if quote .option_class == "CALL" else - 1
2673
2677
t = tqsdk .tafunc ._get_t_series (df ["datetime" ], df ["duration" ], quote ) # 到期时间
2674
2678
return pd .DataFrame (
0 commit comments