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
'%.4f'
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)
買權 履約價 時間 成交價 買價 賣價 漲跌 總量 賣權 履約價 時間_ 成交價_ 買價_ 賣價_ 漲跌_ 總量_
1 9900 10:45 725 725 755 △25 16 9900 13:44 16.5 16.5 17.5 △2 2094
2 10000 13:04 595 595 670 ▽15.0 7 10000 13:43 22.5 21.5 22 △3.5 2138
3 10100 12:17 515 515 520 0 15 10100 13:42 30 28.5 30 △5 2052
4 10200 13:38 421 421 428 ▽8.0 10 10200 13:44 40.5 40.5 41 △4.5 5065
5 10300 13:39 339 339 343 ▽3.0 894 10300 13:44 56 55 56 △5 5273
6 10400 13:43 262 262 265 0 1022 10400 13:44 78 77 78 △7 3636
7 10500 13:44 193 193 194 ▽2.0 2351 10500 13:44 108 107 108 △7 5123
8 10600 13:44 133 133 135 ▽2.0 3092 10600 13:44 149 147 150 △9 3486
9 10700 13:44 88 88 89 0 4187 10700 13:44 203 201 205 △8 2529
10 10800 13:44 55 55 55 △2 8020 10800 13:44 267 266 271 △4 979
11 10900 13:44 33 33 33 △2 7049 10900 13:34 343 345 349 △2 481
12 11000 13:44 19.5 19.5 20 △1 5793 11000 13:11 450 432 436 △23 64
13 11100 13:43 12.5 12.5 13 △2.5 13481 11100 13:01 535 520 535 △10 2
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)

Future Arbitrage

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)