diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..7152b80 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.md LICENSE \ No newline at end of file diff --git a/README.md b/README.md index 26e2a1a..4d298a5 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,15 @@ # opstrat - opstrat is a Python library for visualizing options. -## Version -0.0.1 +## Latest Version +0.0.12 ## Installation -Use the package manager [pip](https://pip.pypa.io/en/stable/) to install foobar. +Use the package manager [pip](https://pip.pypa.io/en/stable/) to install opstrat. ```bash -pip install opstrat==0.0.2 +pip install opstrat ``` ## Usage @@ -26,5 +25,7 @@ Pull requests are welcome. For major changes, please open an issue first to disc Please make sure to update tests as appropriate. -## License -[MIT](https://choosealicense.com/licenses/mit/) \ No newline at end of file +## Content License +[MIT](https://choosealicense.com/licenses/mit/) + + diff --git a/opstrat/basic_multi.py b/opstrat/basic_multi.py new file mode 100644 index 0000000..18f7716 --- /dev/null +++ b/opstrat/basic_multi.py @@ -0,0 +1,100 @@ +#basic_multi +import numpy as np +import matplotlib.pyplot as plt +import seaborn as sns + +abb={'c': 'Call', + 'p': 'Put', + 'b': 'Long', + 's': 'Short'} + +def option_plotter(spot_range=20, spot=100, + op_list=[{'op_type':'c','strike':110,'tr_type':'s','op_pr':2}, + {'op_type':'p','strike':95,'tr_type':'s','op_pr':6}]): + """ + Plots a basic option payoff diagram for a multiple options and resultant payoff diagram + + Parameters + ---------- + spot: int, float, default: 100 + Spot Price + + spot_range: int, float, optional, default: 20 + Range of spot variation in percentage + + op_list: list of dictionary + + Each dictionary must contiain following keys + 'strike': int, float, default: 720 + Strike Price + 'tr_type': kind {'b', 's'} default:'b' + Transaction Type>> 'b': long, 's': short + 'op_pr': int, float, default: 10 + Option Price + 'op_type': kind {'c','p'}, default:'c' + Opion type>> 'c': call option, 'p':put option + + Example + ------- + op1={'op_type':'c','strike':110,'tr_type':'s','op_pr':2} + op2={'op_type':'p','strike':95,'tr_type':'s','op_pr':6} + + from opstrat.basic_multi import option_plotter + option_plotter(spot_range=20, spot=100, op_list=[op1,op2]) + + #Plots option payoff diagrams for each op1 and op2 and combined payoff + + """ + x=spot*np.arange(100-spot_range,101+spot_range,0.01)/100 + + def check_optype(op_type): + if (op_type not in ['p','c']): + raise ValueError("Input 'p' for put and 'c' for call!") + def check_trtype(tr_type): + if (tr_type not in ['b','s']): + raise ValueError("Input 'b' for Buy and 's' for Sell!") + + def payoff_calculator(op_type, strike, op_pr, tr_type): + y=[] + if op_type=='c': + for i in range(len(x)): + y.append(max((x[i]-strike-op_pr),-op_pr)) + else: + for i in range(len(x)): + y.append(max(strike-x[i]-op_pr,-op_pr)) + + if tr_type=='s': + y=-np.array(y) + return y + + y_list=[] + for op in op_list: + op_type=str.lower(op['op_type']) + tr_type=str.lower(op['tr_type']) + check_optype(op_type) + check_trtype(tr_type) + + strike=op['strike'] + op_pr=op['op_pr'] + y_list.append(payoff_calculator(op_type, strike, op_pr, tr_type)) + + + def plotter(): + y=0 + plt.figure(figsize=(10,6)) + for i in range (len(op_list)): + label=str(abb[op_list[i]['tr_type']])+' '+str(abb[op_list[i]['op_type']])+' ST: '+str(op_list[i]['strike']) + sns.lineplot(x=x, y=y_list[i], label=label, alpha=0.5) + y+=np.array(y_list[i]) + + sns.lineplot(x=x, y=y, label='combined', alpha=1, color='k') + plt.axhline(color='k', linestyle='--') + plt.axvline(x=spot, color='r', linestyle='--', label='spot price') + plt.legend() + plt.legend(loc='upper right') + title="Multiple Options Plotter" + plt.title(title) + plt.tight_layout() + plt.show() + + plotter() \ No newline at end of file diff --git a/opstrat/basic.py b/opstrat/basic_single.py similarity index 64% rename from opstrat/basic.py rename to opstrat/basic_single.py index 36cfe24..0c4adc5 100644 --- a/opstrat/basic.py +++ b/opstrat/basic_single.py @@ -11,6 +11,35 @@ 's': 'Short'} def option_plotter(op_type='c',spot=725, spot_range=5,strike=720,tr_type='b',op_pr=10): + """ + Plots a basic option payoff diagram for a single option + Parameters + ---------- + op_type: kind {'c','p'}, default:'c' + Opion type>> 'c': call option, 'p':put option + + spot: int, float, default: 725 + Spot Price + + spot_range: int, float, optional, default: 5 + Range of spot variation in percentage + + strike: int, float, default: 720 + Strike Price + + tr_type: kind {'b', 's'} default:'b' + Transaction Type>> 'b': long, 's': short + + op_pr: int, float, default: 10 + Option Price + + Example + ------- + from opstrat.basic_single import option_plotter + option_plotter(op_type='p', spot_range=20, spot=1000, strike=950) + #Plots option payoff diagram for put option with spot price=1000, strike price=950, range=+/-20% + + """ op_type=str.lower(op_type) diff --git a/setup.py b/setup.py index c0366b4..0d0c14c 100644 --- a/setup.py +++ b/setup.py @@ -1,15 +1,36 @@ -from setuptools import setup, find_packages -import codecs +from codecs import open import os +from setuptools import setup, find_packages +import pypandoc +''' here = os.path.abspath(os.path.dirname(__file__)) -with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh: - long_description = "\n" + fh.read() +with open(os.path.join(here, 'README.md'), encoding='utf-8') as f: + long_description = f.read() +''' +try: + long_description = pypandoc.convert('README.md', 'rst') + long_description = long_description.replace("\r","") # Do not forget this line +except OSError: + print("Pandoc not found. Long_description conversion failure.") + import io + # pandoc is not installed, fallback to using raw contents + with io.open('README.md', encoding="utf-8") as f: + long_description = f.read() + + +''' +try: + import pypandoc + long_description = pypandoc.convert('README.md', 'rst') +except(IOError, ImportError): + long_description = open('README.md').read() +''' -VERSION = '0.0.1' +VERSION = '0.0.12' DESCRIPTION = 'Option stategy visualizer' -LONG_DESCRIPTION = 'Option strategy visualizer' +LONG_DESCRIPTION = DESCRIPTION URL = 'https://github.com/abhijith-git/opstrat' # Setting up @@ -19,12 +40,21 @@ author="Abhijith Chandradas", author_email="", description=DESCRIPTION, - long_description_content_type="text/markdown", long_description=long_description, + long_description_content_type='text/markdown', url=URL, + license='MIT', packages=find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]), - install_requires=['matplotlib', 'pandas', 'numpy'], - keywords=['python', 'video', 'stream', 'video stream', 'camera stream', 'sockets'], + install_requires=['matplotlib', + 'pandas', + 'numpy', + 'seaborn'], + keywords=['python', + 'options', + 'finance', + 'opstrat', + 'data visualization', + 'stock market'], classifiers=[ "Development Status :: 1 - Planning", "Intended Audience :: Developers", @@ -33,4 +63,8 @@ "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows", ] -) \ No newline at end of file +) + +#Display README.md in PYPI +#https://stackoverflow.com/questions/26737222/how-to-make-pypi-description-markdown-work +