Skip to content

Commit

Permalink
0.0.12 update basic_multi
Browse files Browse the repository at this point in the history
  • Loading branch information
hashABCD committed Mar 18, 2021
1 parent e7824bb commit 2810f79
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 17 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.md LICENSE
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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/)
## Content License
[MIT](https://choosealicense.com/licenses/mit/)


100 changes: 100 additions & 0 deletions opstrat/basic_multi.py
Original file line number Diff line number Diff line change
@@ -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()
29 changes: 29 additions & 0 deletions opstrat/basic.py → opstrat/basic_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 44 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,12 +40,21 @@
author="Abhijith Chandradas",
author_email="<[email protected]>",
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",
Expand All @@ -33,4 +63,8 @@
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
]
)
)

#Display README.md in PYPI
#https://stackoverflow.com/questions/26737222/how-to-make-pypi-description-markdown-work

0 comments on commit 2810f79

Please sign in to comment.