正向合約&反向合約倉位變化計算

beijingAviator發表於2020-10-18
import pandas as pd

p = 10000
slr = -0.005 # stop loss 
tpr = 0.01   # take profit
side = -1    # long (1) or short (-1)

rows = []
# Inverse contract
sl = p/(1-side*slr)
tp = p/(1-side*tpr)
rows += [{'sl': '{:.2f}'.format(sl),
          'p0': '{:.2f}'.format(p),
          'tp': '{:.2f}'.format(tp),
          'name': 'inverse'
          }]

# Normal contract
sl = p*(1+side*slr)
tp = p*(1+side*tpr)
rows += [{'sl': '{:.2f}'.format(sl),
          'p0': '{:.2f}'.format(p),
          'tp': '{:.2f}'.format(tp),
          'name': 'normal'
          }]

df = pd.DataFrame.from_records( rows )
df.set_index('name',inplace=True)
print(df)

相關文章