From 583f5438d30834cdb47e491dfba19ca5ef903310 Mon Sep 17 00:00:00 2001 From: ben adelman <109058028+bena777@users.noreply.github.com> Date: Wed, 18 Jan 2023 19:24:35 -0500 Subject: [PATCH 1/2] displays max profit and loss for single plotter + changed font --- opstrat/basic_single.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/opstrat/basic_single.py b/opstrat/basic_single.py index 97067a9..fd113b7 100644 --- a/opstrat/basic_single.py +++ b/opstrat/basic_single.py @@ -56,20 +56,30 @@ def single_plotter(op_type='c',spot=100, spot_range=10,strike=102,tr_type='b',op def payoff_calculator(): x=spot*np.arange(100-spot_range,101+spot_range,0.01)/100 - y=[] if str.lower(op_type)=='c': for i in range(len(x)): y.append(max((x[i]-strike-op_pr),-op_pr)) + if tr_type=='s': + max_loss='∞' + max_gain=op_pr + else: + max_loss=op_pr + max_gain='∞' else: for i in range(len(x)): y.append(max(strike-x[i]-op_pr,-op_pr)) - + if tr_type=='s': + max_loss='∞' + max_gain=op_pr + else: + max_loss=op_pr + max_gain='∞' if str.lower(tr_type)=='s': y=-np.array(y) - return x,y + return x,y,max_gain,max_loss - x,y=payoff_calculator() + x,y,max_gain,max_loss=payoff_calculator() y0=np.zeros_like(x) def plotter(x,y): @@ -77,13 +87,20 @@ def plotter(x,y): sns.lineplot(x=x, y=y) plt.axhline(color='k', linestyle='--') plt.axvline(x=spot, color='r', linestyle='--') + font = {'family': 'serif', + 'color': 'purple', + 'weight': 'bold', + 'size':16, + } + plt.text(s="Max Gain= $"+str(max_gain),x=min(x)-1,y=max(y),fontdict=font) + plt.text(s="Max Loss= $"+str(max_loss),x=min(x)-1,y=max(y)-1,fontdict=font) title=str(abb[op_type])+' '+str(abb[tr_type])+'\n St price :'+str(strike) plt.fill_between(x, y, 0, alpha=0.2, where=y>y0, facecolor='green', interpolate=True) plt.fill_between(x, y, 0, alpha=0.2, where=y Date: Wed, 18 Jan 2023 19:38:53 -0500 Subject: [PATCH 2/2] returns max loss, gain, and breakeven for single_plotter --- opstrat/basic_single.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/opstrat/basic_single.py b/opstrat/basic_single.py index fd113b7..78d3564 100644 --- a/opstrat/basic_single.py +++ b/opstrat/basic_single.py @@ -58,6 +58,7 @@ def payoff_calculator(): x=spot*np.arange(100-spot_range,101+spot_range,0.01)/100 y=[] if str.lower(op_type)=='c': + breakeven = (strike * 100)+op_pr for i in range(len(x)): y.append(max((x[i]-strike-op_pr),-op_pr)) if tr_type=='s': @@ -67,6 +68,7 @@ def payoff_calculator(): max_loss=op_pr max_gain='∞' else: + breakeven = (strike*100)-op_pr for i in range(len(x)): y.append(max(strike-x[i]-op_pr,-op_pr)) if tr_type=='s': @@ -77,9 +79,9 @@ def payoff_calculator(): max_gain='∞' if str.lower(tr_type)=='s': y=-np.array(y) - return x,y,max_gain,max_loss + return x,y,max_gain,max_loss,breakeven - x,y,max_gain,max_loss=payoff_calculator() + x,y,max_gain,max_loss,breakeven=payoff_calculator() y0=np.zeros_like(x) def plotter(x,y): @@ -87,20 +89,14 @@ def plotter(x,y): sns.lineplot(x=x, y=y) plt.axhline(color='k', linestyle='--') plt.axvline(x=spot, color='r', linestyle='--') - font = {'family': 'serif', - 'color': 'purple', - 'weight': 'bold', - 'size':16, - } - plt.text(s="Max Gain= $"+str(max_gain),x=min(x)-1,y=max(y),fontdict=font) - plt.text(s="Max Loss= $"+str(max_loss),x=min(x)-1,y=max(y)-1,fontdict=font) title=str(abb[op_type])+' '+str(abb[tr_type])+'\n St price :'+str(strike) plt.fill_between(x, y, 0, alpha=0.2, where=y>y0, facecolor='green', interpolate=True) plt.fill_between(x, y, 0, alpha=0.2, where=y