Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2846 views
Kernel: Python 3
import numpy as np import matplotlib.pyplot as plt %matplotlib inline
def call_payoff(K, S): return np.maximum(S-K, 0) def put_payoff(K, S): return np.maximum(K-S, 0) def profit(pos, payoff, price): return pos*payoff - pos*price;
lw = 2.5 # line width fs = 12 # fontsize picWidth=12; picHeight=8;
pos_long = 1; pos_short = -1;
K = 100 x = np.linspace(0, 2*K, num=100) callPrice = 10 putPrice = 10 plt.figure(figsize=(picWidth, picHeight)) plt.subplot(2, 2, 1) plt.plot(x, pos_long * call_payoff(K, x), 'r--', linewidth=lw) plt.plot(x, profit(pos_long, call_payoff(K, x), callPrice), 'r', linewidth=lw) plt.axhline(0, color='k') plt.title('long call', fontsize=fs) plt.ylabel('profit / payoff', fontsize=fs) plt.grid(True) plt.subplot(2, 2, 2) plt.plot(x, pos_long * put_payoff(K, x), 'b--', linewidth=lw) plt.plot(x, profit(pos_long, put_payoff(K, x), putPrice), 'b', linewidth=lw) plt.axhline(0, color='k') plt.title('long put', fontsize=fs) plt.grid(True) plt.subplot(2, 2, 3) plt.plot(x, pos_short * call_payoff(K, x), 'r--', linewidth=lw) plt.plot(x, profit( pos_short, call_payoff(K, x), callPrice), 'r', linewidth=lw) plt.axhline(0, color='k') plt.title('short call', fontsize=fs) plt.xlabel('spot', fontsize=fs) plt.ylabel('profit / payoff', fontsize=fs) plt.grid(True) plt.subplot(2, 2, 4) plt.plot(x, pos_short * put_payoff(K, x), 'b--', linewidth=lw) plt.plot(x, profit(pos_short, put_payoff(K, x), putPrice), 'b', linewidth=lw) plt.axhline(0, color='k') plt.title('short put', fontsize=fs) plt.xlabel('spot', fontsize=fs) plt.grid(True) plt.show()
Image in a Jupyter notebook
K=30 callPrice = 2.5 putPrice = 2 xmin = K-10; xmax = K+10 x = np.linspace(xmin, xmax, num=100) plt.figure(figsize=(picWidth, picHeight)) print("Position price: %s"%(callPrice + putPrice)) y1 = profit(pos_long, call_payoff(K, x), callPrice) y2 = profit(pos_long, put_payoff(K, x), putPrice) plt.plot(x, y1, 'r--', linewidth=lw) plt.plot(x, y2, 'b--', linewidth=lw) plt.plot(x, y1 + y2, 'r', linewidth=lw) plt.axhline(0, color='k') axes = plt.gca() axes.set_xlim([xmin, xmax]) plt.title('Straddle', fontsize=fs+3) plt.ylabel('profit', fontsize=fs) plt.grid(True)
Position price: 4.5
Image in a Jupyter notebook
K1 = 28 K2 = 32 callPrice2 = 1.6 putPrice1 = 1.3 xmin = K-10; xmax = K+10 x = np.linspace(xmin, xmax, num=100) plt.figure(figsize=(picWidth, picHeight)) print("Position price: %s"%(putPrice1 + callPrice2)) y1 = profit(pos_long, put_payoff(K1, x), putPrice1) y2 = profit(pos_long, call_payoff(K2, x), callPrice2) plt.plot(x, y1, 'r--', linewidth=lw) plt.plot(x, y2, 'b--', linewidth=lw) plt.plot(x, y1 + y2, 'r', linewidth=lw) plt.axhline(0, color='k') axes = plt.gca() axes.set_xlim([xmin, xmax]) plt.title('Strangle', fontsize=fs+3) plt.ylabel('profit', fontsize=fs) plt.grid(True)
Position price: 2.9000000000000004
Image in a Jupyter notebook
K1 = 28 K2 = 32 putPrice1 = 1.2 callPrice2 = 1.5 xmin = K1-5; xmax = K2+5 x = np.linspace(xmin, xmax, num=100) plt.figure(figsize=(picWidth, picHeight)) print("Position price: %s"%(-putPrice1 + callPrice2)) y1 = profit(pos_short, put_payoff(K1, x), putPrice1) y2 = profit(pos_long, call_payoff(K2, x), callPrice2) plt.plot(x, y1, 'r--', linewidth=lw) plt.plot(x, y2, 'r--', linewidth=lw) plt.plot(x, y1 + y2, 'r', linewidth=lw) plt.axhline(0, color='k') axes = plt.gca() axes.set_xlim([xmin, xmax]) #axes.set_ylim([-5, 10]) plt.title('Risk Reversal', fontsize=fs+3) plt.ylabel('profit', fontsize=fs) plt.grid(True)
Position price: 0.30000000000000004
Image in a Jupyter notebook
K1 = 28 K = 30 K2 = 32 callPrice1 = 1.6 callPrice = 2.4 callPrice2 = 3.5 xmin = K-5; xmax = K+5 x = np.linspace(xmin, xmax, num=100) plt.figure(figsize=(picWidth, picHeight)) print("Position price: %s"%(callPrice1 -2*callPrice + callPrice2)) y1 = profit(pos_long, call_payoff(K1, x), callPrice1) y = 2 * profit(pos_short, call_payoff(K, x), callPrice) y2 = profit(pos_long, call_payoff(K2, x), callPrice2) plt.plot(x, y1, 'r--', linewidth=lw) plt.plot(x, y, 'b--', linewidth=lw) plt.plot(x, y2, 'r--', linewidth=lw) plt.plot(x, y1 + y + y2, 'r', linewidth=lw) plt.axhline(0, color='k') axes = plt.gca() axes.set_xlim([xmin, xmax]) axes.set_ylim([-5, 5]) plt.title('Butterfly', fontsize=fs+3) plt.ylabel('profit', fontsize=fs) plt.grid(True)
Position price: 0.30000000000000027
Image in a Jupyter notebook
plt.figure(figsize=(picWidth, picHeight)) plt.plot(x, y1 + y + y2, 'r', linewidth=lw) plt.axhline(0, color='k') axes = plt.gca() axes.set_xlim([xmin, xmax]) axes.set_ylim([-5, 5]) plt.title('Butterfly', fontsize=fs+3) plt.ylabel('profit', fontsize=fs) plt.grid(True)
Image in a Jupyter notebook
K1 = 30 K2 = 35 callPrice1 = 2.5 callPrice2 = 1 xmin = K1-5; xmax = K2+5 x = np.linspace(xmin, xmax, num=100) plt.figure(figsize=(picWidth, picHeight)) print("Position price: %s"%(callPrice1-callPrice2)) y1 = profit(pos_long, call_payoff(K1, x), callPrice1) y2 = profit(pos_short, call_payoff(K2, x), callPrice2) plt.plot(x, y1, 'r--', linewidth=lw) plt.plot(x, y2, 'r--', linewidth=lw) plt.plot(x, y1 + y2, 'r', linewidth=lw) plt.axhline(0, color='k') axes = plt.gca() axes.set_xlim([xmin, xmax]) #axes.set_ylim([-5, 10]) plt.title('Strike Spread (call spread, bull spread)', fontsize=fs+3) plt.ylabel('profit', fontsize=fs) plt.grid(True)
Position price: 1.5
Image in a Jupyter notebook
K1 = 30 K2 = 35 K3 = 25 callPrice1 = 2.5 callPrice2 = 1 putPrice = 1.5 xmin = K3-5; xmax = K2+5 x = np.linspace(xmin, xmax, num=100) plt.figure(figsize=(picWidth, picHeight)) print("Position price: %s"%(callPrice1-callPrice2-putPrice)) y1 = profit(pos_long, call_payoff(K1, x), callPrice1) y2 = profit(pos_short, call_payoff(K2, x), callPrice2) y3 = profit(pos_short, put_payoff(K3, x), putPrice) plt.plot(x, y1, 'r--', linewidth=lw) plt.plot(x, y2, 'r--', linewidth=lw) plt.plot(x, y3, 'b--', linewidth=lw) plt.plot(x, y1 + y2 + y3, 'r', linewidth=lw) plt.axhline(0, color='k') axes = plt.gca() axes.set_xlim([xmin, xmax]) #axes.set_ylim([-5, 10]) plt.title('Seagull', fontsize=fs+3) plt.ylabel('profit', fontsize=fs) plt.grid(True)
Position price: 0.0
Image in a Jupyter notebook