Skip to content

Commit 259dbc8

Browse files
shinny-mayanqiongshinny-chengzhi
authored andcommitted
ta: 使用 np.full_like 填充 nan
1 parent 251235e commit 259dbc8

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

tqsdk/ta.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,11 +2540,11 @@ def BS_VALUE(df, quote=None, r=0.025, v=None):
25402540
[..., 3036.698780158862, 2393.333388624822, 2872.607833620801]
25412541
"""
25422542
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"])
25442544
if v is None:
25452545
v = tqsdk.tafunc._get_volatility(df["close"], df["duration"], quote.trading_time, float('nan'))
25462546
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"])
25482548
o = 1 if quote.option_class == "CALL" else -1
25492549
t = tqsdk.tafunc._get_t_series(df["datetime"], df["duration"], quote)
25502550
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):
25832583
print(list(greeks["rho"]))
25842584
25852585
"""
2586+
new_df = pd.DataFrame()
25862587
if not (quote and quote.ins_class.endswith("OPTION") and quote.instrument_id == df["symbol"][0]
25872588
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)
26012606
return new_df
26022607

26032608

@@ -2626,18 +2631,20 @@ def OPTION_VALUE(df, quote=None):
26262631
print(list(values["time"]))
26272632
api.close()
26282633
"""
2634+
new_df = pd.DataFrame()
26292635
if not (quote and quote.ins_class.endswith("OPTION") and quote.instrument_id == df["symbol"][0]
26302636
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"])
26372644
return new_df
26382645

26392646

2640-
def OPTION_IMPV(df, quote=None, r=0.025, init_v=None):
2647+
def OPTION_IMPV(df, quote=None, r=0.025):
26412648
"""
26422649
计算期权隐含波动率
26432650
@@ -2648,8 +2655,6 @@ def OPTION_IMPV(df, quote=None, r=0.025, init_v=None):
26482655
26492656
r (float): 无风险利率
26502657
2651-
init_v (float): 初始对波动率的估计
2652-
26532658
Returns:
26542659
pandas.DataFrame: 返回的 DataFrame 包含 1 列, 是 "impv", 与参数 df 行数相同
26552660
@@ -2666,9 +2671,8 @@ def OPTION_IMPV(df, quote=None, r=0.025, init_v=None):
26662671
"""
26672672
if not (quote and quote.ins_class.endswith("OPTION") and quote.instrument_id == df["symbol"][0]
26682673
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)
26722676
o = 1 if quote.option_class == "CALL" else -1
26732677
t = tqsdk.tafunc._get_t_series(df["datetime"], df["duration"], quote) # 到期时间
26742678
return pd.DataFrame(

0 commit comments

Comments
 (0)