import pandas as pd
import numpy as np
from ipy_table import make_table,apply_theme,set_cell_style,set_column_style,set_row_style
from IPython.display import clear_output, display
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
%precision 4
TaiOpt=pd.read_csv("TaiOpt.csv")#,skiprows = [1])
TaiOpt=TaiOpt.iloc[0:15,:]
#TaiOptPut=pd.DataFrame()
#TaiOptCall=pd.DataFrame()
TaiTotal=pd.DataFrame()
def updown(x):
if float(x)>0:
x='△'+(x)
elif float(x)<0:
x='▽'+str(abs(float(x)) )
return x
TaiTotal['買權 履約價']=TaiOpt.iloc[1:14,7]
TaiTotal['時間']=TaiOpt.iloc[1:14,6].astype(str).str[:5]
TaiTotal['成交價']=TaiOpt.iloc[1:14,2]
TaiTotal['買價']=TaiOpt.iloc[1:14,2]
TaiTotal['賣價']=TaiOpt.iloc[1:14,1]
TaiTotal['漲跌']=TaiOpt.iloc[1:14,3].apply(lambda x:updown(x))
TaiTotal['總量']=TaiOpt.iloc[1:14,5]
TaiTotal['賣權 履約價']=TaiOpt.iloc[1:14,7]
TaiTotal['時間_']=TaiOpt.iloc[1:14,14].astype(str).str[:5]
TaiTotal['成交價_']=TaiOpt.iloc[1:14,10]
TaiTotal['買價_']=TaiOpt.iloc[1:14,8]
TaiTotal['賣價_']=TaiOpt.iloc[1:14,9]
TaiTotal['漲跌_']=TaiOpt.iloc[1:14,11].apply(lambda x:updown(x))
TaiTotal['總量_']=TaiOpt.iloc[1:14,13]
display(TaiTotal)
r=0.01844
def arb(df_,S1,S2):
df=df_.copy()
df['cp']=df['買價'].astype(float)-df['賣價'].astype('float')
df['b']=S1-df['買權 履約價'].astype(float)*(1+r)**(-1/12)
df['arbitrage']=(df['b']-df['cp']).map('{:,.2f}'.format)
#df['arbitrage']=df['買權 履約價'].apply(lambda K:arbitrage(C,P,S,float(K),r))
df['cp1']=df['買價_'].astype(float)-df['賣價_'].astype('float')
df['b1']=S2-df['賣權 履約價'].astype(float)*(1+r)**(-1/12)
df['arbitrage1']=(df['b1']-df['cp1']).map('{:,.2f}'.format)
print('買權 履約價',' Arbitrage',' 賣權 履約價',' Arbitrage')
for i in range(len(df)):
print('{:11} {:>9} {:>11} {:>9}'.format(df.iloc[i,0],df.iloc[i,-4],df.iloc[i,0],df.iloc[i,-1]))
#print(df[['買權 履約價','arbitrage']])
@interact
def show_arbitrage(Pull=(9900, 12000, 100),Call=(9900, 12000, 100)):
return arb(TaiTotal,Pull,Call)
def arb_future(df_,S1,S2):
df=df_.copy()
df['cp']=df['買價'].astype(float)-df['賣價'].astype('float')
df['b']=S1-df['買權 履約價'].astype(float)*(1+r)**(1/12)
df['arbitrage']=(df['b']-df['cp']).map('{:,.2f}'.format)
#df['arbitrage']=df['買權 履約價'].apply(lambda K:arbitrage(C,P,S,float(K),r))
df['cp1']=df['買價_'].astype(float)-df['賣價_'].astype('float')
df['b1']=S2-df['賣權 履約價'].astype(float)*(1+r)**(1/12)
df['arbitrage1']=(df['b1']-df['cp1']).map('{:,.2f}'.format)
print('買權 履約價',' Arbitrage',' 賣權 履約價',' Arbitrage')
for i in range(len(df)):
print('{:11} {:>9} {:>11} {:>9}'.format(df.iloc[i,0],df.iloc[i,-4],df.iloc[i,0],df.iloc[i,-1]))
#print(df[['買權 履約價','arbitrage']])
@interact
def show_arbitrage_future(Pull=(9900, 12000, 100),Call=(9900, 12000, 100)):
return arb_future(TaiTotal,Pull,Call)