Skip to content

Commit

Permalink
Refactor engine for simplicity and code readability (#15)
Browse files Browse the repository at this point in the history
- Remove protected attributes
- Move plotting to separate file (separate concerns)
- Create private methods for each of the `type`s, and small refactors on the logic of those methods
  • Loading branch information
marc-at-brightnight authored Mar 24, 2024
1 parent fd7a4c1 commit 2379a4a
Show file tree
Hide file tree
Showing 11 changed files with 613 additions and 601 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 1.1.0 (2024-03-24)

- Refactor the engine's `run` method for readability.
- Accept dictionary of inputs to `StratgyEngine` init.

## 1.0.1 (2024-03-18)

- Refactor __holidays__.py to a utils function using the `holiday` library
Expand Down
20 changes: 4 additions & 16 deletions examples/black_scholes_calculator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-15T17:36:52.330797Z",
"start_time": "2024-03-15T17:36:52.121865Z"
"end_time": "2024-03-15T21:15:37.010803Z",
"start_time": "2024-03-15T21:15:36.450216Z"
}
},
"outputs": [
{
"ename": "ImportError",
"evalue": "cannot import name 'get_bs_info' from 'optionlab' (/Users/mnhmbp/PycharmProjects/optionlab/optionlab/__init__.py)",
"output_type": "error",
"traceback": [
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[0;31mImportError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[0;32mIn[6], line 3\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m__future__\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m print_function\n\u001B[1;32m 2\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m__future__\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m division\n\u001B[0;32m----> 3\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01moptionlab\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m VERSION, get_bs_info\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01msys\u001B[39;00m\n",
"\u001B[0;31mImportError\u001B[0m: cannot import name 'get_bs_info' from 'optionlab' (/Users/mnhmbp/PycharmProjects/optionlab/optionlab/__init__.py)"
]
}
],
"outputs": [],
"source": [
"from __future__ import print_function\n",
"from __future__ import division\n",
Expand Down
30 changes: 15 additions & 15 deletions examples/naked_call.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-15T17:50:56.431755Z",
"start_time": "2024-03-15T17:50:55.507652Z"
"end_time": "2024-03-15T21:16:28.614458Z",
"start_time": "2024-03-15T21:16:27.865860Z"
}
},
"outputs": [],
Expand All @@ -31,7 +31,7 @@
"import datetime as dt\n",
"import sys\n",
"\n",
"from optionlab import VERSION, StrategyEngine, Inputs\n",
"from optionlab import VERSION, StrategyEngine, Inputs, plot_pl\n",
"\n",
"%matplotlib inline"
]
Expand Down Expand Up @@ -64,11 +64,11 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-13T02:27:33.629312Z",
"start_time": "2024-03-13T02:27:33.621905Z"
"end_time": "2024-03-15T21:16:37.874205Z",
"start_time": "2024-03-15T21:16:37.868325Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -100,20 +100,20 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-13T02:27:36.676391Z",
"start_time": "2024-03-13T02:27:36.670314Z"
"end_time": "2024-03-15T21:16:40.959949Z",
"start_time": "2024-03-15T21:16:40.942429Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.94 ms, sys: 1.22 ms, total: 3.16 ms\n",
"Wall time: 2.36 ms\n"
"CPU times: user 2.23 ms, sys: 1.28 ms, total: 3.51 ms\n",
"Wall time: 2.57 ms\n"
]
}
],
Expand All @@ -131,11 +131,11 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-13T02:27:38.761229Z",
"start_time": "2024-03-13T02:27:38.445697Z"
"end_time": "2024-03-15T21:16:42.811428Z",
"start_time": "2024-03-15T21:16:42.639286Z"
}
},
"outputs": [
Expand All @@ -158,7 +158,7 @@
}
],
"source": [
"st.plot_pl()"
"plot_pl(st)"
]
},
{
Expand Down
7 changes: 6 additions & 1 deletion optionlab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typing


VERSION = "1.0.0"
VERSION = "1.1.0"


if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -36,6 +36,7 @@
get_theta,
)
from .engine import StrategyEngine
from .plot import plot_pl
from .support import (
get_pl_profile,
get_pl_profile_stock,
Expand Down Expand Up @@ -84,6 +85,8 @@
"get_delta",
"get_gamma",
"get_theta",
# plot
"plot_pl",
)

# A mapping of {<member name>: (package, <module name>)} defining dynamic imports
Expand Down Expand Up @@ -124,6 +127,8 @@
"get_delta": (__package__, ".black_scholes"),
"get_gamma": (__package__, ".black_scholes"),
"get_theta": (__package__, ".black_scholes"),
# plot
"plot_pl": (__package__, ".plot"),
}


Expand Down
Loading

0 comments on commit 2379a4a

Please sign in to comment.