PyDecisionGraph 0.1.0__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of PyDecisionGraph might be problematic. Click here for more details.
- pydecisiongraph-0.1.2/PKG-INFO +149 -0
- pydecisiongraph-0.1.2/PyDecisionGraph.egg-info/PKG-INFO +149 -0
- pydecisiongraph-0.1.2/PyDecisionGraph.egg-info/SOURCES.txt +32 -0
- pydecisiongraph-0.1.2/PyDecisionGraph.egg-info/requires.txt +9 -0
- pydecisiongraph-0.1.2/PyDecisionGraph.egg-info/top_level.txt +1 -0
- pydecisiongraph-0.1.2/README.md +123 -0
- pydecisiongraph-0.1.2/decision_graph/__init__.py +16 -0
- pydecisiongraph-0.1.2/decision_graph/decision_tree/__init__.py +44 -0
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2/decision_graph}/decision_tree/abc.py +66 -18
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2/decision_graph}/decision_tree/collection.py +7 -13
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2/decision_graph}/decision_tree/node.py +25 -11
- pydecisiongraph-0.1.2/decision_graph/logic_group/__init__.py +22 -0
- pydecisiongraph-0.1.2/decision_graph/logic_group/base.py +72 -0
- pydecisiongraph-0.1.2/decision_graph/logic_group/pending_request.py +253 -0
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2}/pyproject.toml +15 -4
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2}/setup.cfg +8 -3
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2}/setup.py +12 -1
- pydecisiongraph-0.1.0/PKG-INFO +0 -21
- pydecisiongraph-0.1.0/PyDecisionGraph.egg-info/PKG-INFO +0 -21
- pydecisiongraph-0.1.0/PyDecisionGraph.egg-info/SOURCES.txt +0 -25
- pydecisiongraph-0.1.0/PyDecisionGraph.egg-info/top_level.txt +0 -1
- pydecisiongraph-0.1.0/README.md +0 -2
- pydecisiongraph-0.1.0/decision_tree/__init__.py +0 -36
- pydecisiongraph-0.1.0/decision_tree/logic_group.py +0 -307
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2}/LICENSE +0 -0
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2}/MANIFEST.in +0 -0
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2}/PyDecisionGraph.egg-info/dependency_links.txt +0 -0
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2}/PyDecisionGraph.egg-info/not-zip-safe +0 -0
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2/decision_graph}/decision_tree/exc.py +0 -0
- {pydecisiongraph-0.1.0 → pydecisiongraph-0.1.2/decision_graph}/decision_tree/expression.py +0 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: PyDecisionGraph
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: A rule-based decision tree implementation for Python
|
|
5
|
+
Home-page: https://github.com/BolunHan/PyDecisionGraph
|
|
6
|
+
Author: Han Bolun
|
|
7
|
+
Author-email: Han Bolun <Bolun.Han@outlook.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/BolunHan/PyDecisionGraph
|
|
10
|
+
Project-URL: Repository, https://github.com/BolunHan/PyDecisionGraph
|
|
11
|
+
Project-URL: Issues, https://github.com/BolunHan/PyDecisionGraph/issues
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Provides-Extra: docs
|
|
20
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
21
|
+
Requires-Dist: sphinx-rtd-theme; extra == "docs"
|
|
22
|
+
Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
|
|
23
|
+
Provides-Extra: visualization
|
|
24
|
+
Requires-Dist: pyvis; extra == "visualization"
|
|
25
|
+
Requires-Dist: networkx; extra == "visualization"
|
|
26
|
+
|
|
27
|
+
# PyDecisionGraph
|
|
28
|
+
|
|
29
|
+
`PyDecisionGraph` is an easy-to-use library to create custom decision trees, primarily designed for trading and financial decision-making. This package helps you build rule-based decision processes and visualize them effectively.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
To install the package, run:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install PyDecisionGraph
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
# Requirements
|
|
40
|
+
|
|
41
|
+
- Python 3.12 or higher
|
|
42
|
+
|
|
43
|
+
# Documentation
|
|
44
|
+
|
|
45
|
+
For detailed documentation, visit https://pydecisiongraph.readthedocs.io/.
|
|
46
|
+
|
|
47
|
+
# Quick Start
|
|
48
|
+
|
|
49
|
+
Here is a quick demo on how to use `PyDecisionGraph` for building a decision tree based on various conditions:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from decision_graph.decision_tree import LogicNode, LOGGER, AttrExpression, LongAction, ShortAction, NoAction, RootLogicNode, LogicMapping
|
|
53
|
+
|
|
54
|
+
# Mapping of attribute names to their values
|
|
55
|
+
LogicMapping.AttrExpression = AttrExpression
|
|
56
|
+
|
|
57
|
+
state = {
|
|
58
|
+
"exposure": 0, # Current exposure
|
|
59
|
+
"working_order": 0, # Current working order
|
|
60
|
+
"up_prob": 0.8, # Probability of price going up
|
|
61
|
+
"down_prob": 0.2, # Probability of price going down
|
|
62
|
+
"volatility": 0.24, # Current market volatility
|
|
63
|
+
"ttl": 15.3 # Time to live (TTL) of the decision tree
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
# Root of the logic tree
|
|
67
|
+
with RootLogicNode() as root:
|
|
68
|
+
# Define root logic mapping with state data
|
|
69
|
+
with LogicMapping(name='Root', data=state) as lg_root:
|
|
70
|
+
lg_root: LogicMapping
|
|
71
|
+
|
|
72
|
+
# Condition for zero exposure
|
|
73
|
+
with lg_root.exposure == 0:
|
|
74
|
+
root: LogicNode
|
|
75
|
+
with LogicMapping(name='check_open', data=state) as lg:
|
|
76
|
+
with lg.working_order != 0:
|
|
77
|
+
break_point = NoAction() # No action if there's a working order
|
|
78
|
+
lg.break_(scope=lg) # Exit the current scope
|
|
79
|
+
|
|
80
|
+
with lg.volatility > 0.25: # Check if volatility is high
|
|
81
|
+
with lg.down_prob > 0.1: # Action for down probability
|
|
82
|
+
LongAction()
|
|
83
|
+
|
|
84
|
+
with lg.up_prob < -0.1: # Action for up probability
|
|
85
|
+
ShortAction()
|
|
86
|
+
|
|
87
|
+
# Condition when TTL is greater than 30
|
|
88
|
+
with lg_root.ttl > 30:
|
|
89
|
+
with lg_root.working_order > 0:
|
|
90
|
+
ShortAction() # Action to short if working order exists
|
|
91
|
+
LongAction() # Always take long action
|
|
92
|
+
lg_root.break_(scope=lg_root) # Exit scope
|
|
93
|
+
|
|
94
|
+
# Closing logic based on exposure and probabilities
|
|
95
|
+
with LogicMapping(name='check_close', data=state) as lg:
|
|
96
|
+
with (lg.exposure > 0) & (lg.down_prob > 0.):
|
|
97
|
+
ShortAction() # Short action for positive exposure and down probability
|
|
98
|
+
|
|
99
|
+
with (lg.exposure < 0) & (lg.up_prob > 0.):
|
|
100
|
+
LongAction() # Long action for negative exposure and up probability
|
|
101
|
+
|
|
102
|
+
# Visualize the decision tree
|
|
103
|
+
root.to_html()
|
|
104
|
+
|
|
105
|
+
# Log the evaluation result
|
|
106
|
+
LOGGER.info(root())
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Explanation of the Script:
|
|
110
|
+
|
|
111
|
+
- LogicNode & LogicMapping:
|
|
112
|
+
- LogicNode: Represents a node in the decision tree where conditions are evaluated.
|
|
113
|
+
- LogicMapping: Associates logical conditions with the state (data) used in decision-making.
|
|
114
|
+
|
|
115
|
+
- State:
|
|
116
|
+
- A dictionary containing the variables used for decision-making, such as exposure, working_order, up_prob, etc.
|
|
117
|
+
|
|
118
|
+
- RootLogicNode:
|
|
119
|
+
- The entry point for the decision tree where all logical decisions are linked.
|
|
120
|
+
|
|
121
|
+
- Decision Conditions:
|
|
122
|
+
- Inside each with block, logical conditions are evaluated (e.g., lg.volatility > 0.25, lg.up_prob < -0.1) to determine which action to take.
|
|
123
|
+
- Actions like LongAction() or ShortAction() are taken based on the conditions.
|
|
124
|
+
|
|
125
|
+
- Action Handling:
|
|
126
|
+
- LongAction(), ShortAction(), and NoAction() represent different actions you can trigger in the decision tree based on the conditions.
|
|
127
|
+
|
|
128
|
+
- Logging:
|
|
129
|
+
- The result of the tree evaluation is logged using the LOGGER object, which outputs to the console.
|
|
130
|
+
|
|
131
|
+
- Visualization:
|
|
132
|
+
- root.to_html() generates an HTML representation of the decision tree for visualization.
|
|
133
|
+
|
|
134
|
+
# Features
|
|
135
|
+
|
|
136
|
+
- Easily define custom decision rules.
|
|
137
|
+
- Actionable outcomes like LongAction, ShortAction, and NoAction.
|
|
138
|
+
- Log outputs for debugging and tracking.
|
|
139
|
+
- Visualize decision paths through HTML export.
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
# Contributing
|
|
144
|
+
|
|
145
|
+
Feel free to fork, submit issues, and create pull requests. Contributions are always welcome!
|
|
146
|
+
|
|
147
|
+
# License
|
|
148
|
+
|
|
149
|
+
This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: PyDecisionGraph
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: A rule-based decision tree implementation for Python
|
|
5
|
+
Home-page: https://github.com/BolunHan/PyDecisionGraph
|
|
6
|
+
Author: Han Bolun
|
|
7
|
+
Author-email: Han Bolun <Bolun.Han@outlook.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/BolunHan/PyDecisionGraph
|
|
10
|
+
Project-URL: Repository, https://github.com/BolunHan/PyDecisionGraph
|
|
11
|
+
Project-URL: Issues, https://github.com/BolunHan/PyDecisionGraph/issues
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Provides-Extra: docs
|
|
20
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
21
|
+
Requires-Dist: sphinx-rtd-theme; extra == "docs"
|
|
22
|
+
Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
|
|
23
|
+
Provides-Extra: visualization
|
|
24
|
+
Requires-Dist: pyvis; extra == "visualization"
|
|
25
|
+
Requires-Dist: networkx; extra == "visualization"
|
|
26
|
+
|
|
27
|
+
# PyDecisionGraph
|
|
28
|
+
|
|
29
|
+
`PyDecisionGraph` is an easy-to-use library to create custom decision trees, primarily designed for trading and financial decision-making. This package helps you build rule-based decision processes and visualize them effectively.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
To install the package, run:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install PyDecisionGraph
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
# Requirements
|
|
40
|
+
|
|
41
|
+
- Python 3.12 or higher
|
|
42
|
+
|
|
43
|
+
# Documentation
|
|
44
|
+
|
|
45
|
+
For detailed documentation, visit https://pydecisiongraph.readthedocs.io/.
|
|
46
|
+
|
|
47
|
+
# Quick Start
|
|
48
|
+
|
|
49
|
+
Here is a quick demo on how to use `PyDecisionGraph` for building a decision tree based on various conditions:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from decision_graph.decision_tree import LogicNode, LOGGER, AttrExpression, LongAction, ShortAction, NoAction, RootLogicNode, LogicMapping
|
|
53
|
+
|
|
54
|
+
# Mapping of attribute names to their values
|
|
55
|
+
LogicMapping.AttrExpression = AttrExpression
|
|
56
|
+
|
|
57
|
+
state = {
|
|
58
|
+
"exposure": 0, # Current exposure
|
|
59
|
+
"working_order": 0, # Current working order
|
|
60
|
+
"up_prob": 0.8, # Probability of price going up
|
|
61
|
+
"down_prob": 0.2, # Probability of price going down
|
|
62
|
+
"volatility": 0.24, # Current market volatility
|
|
63
|
+
"ttl": 15.3 # Time to live (TTL) of the decision tree
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
# Root of the logic tree
|
|
67
|
+
with RootLogicNode() as root:
|
|
68
|
+
# Define root logic mapping with state data
|
|
69
|
+
with LogicMapping(name='Root', data=state) as lg_root:
|
|
70
|
+
lg_root: LogicMapping
|
|
71
|
+
|
|
72
|
+
# Condition for zero exposure
|
|
73
|
+
with lg_root.exposure == 0:
|
|
74
|
+
root: LogicNode
|
|
75
|
+
with LogicMapping(name='check_open', data=state) as lg:
|
|
76
|
+
with lg.working_order != 0:
|
|
77
|
+
break_point = NoAction() # No action if there's a working order
|
|
78
|
+
lg.break_(scope=lg) # Exit the current scope
|
|
79
|
+
|
|
80
|
+
with lg.volatility > 0.25: # Check if volatility is high
|
|
81
|
+
with lg.down_prob > 0.1: # Action for down probability
|
|
82
|
+
LongAction()
|
|
83
|
+
|
|
84
|
+
with lg.up_prob < -0.1: # Action for up probability
|
|
85
|
+
ShortAction()
|
|
86
|
+
|
|
87
|
+
# Condition when TTL is greater than 30
|
|
88
|
+
with lg_root.ttl > 30:
|
|
89
|
+
with lg_root.working_order > 0:
|
|
90
|
+
ShortAction() # Action to short if working order exists
|
|
91
|
+
LongAction() # Always take long action
|
|
92
|
+
lg_root.break_(scope=lg_root) # Exit scope
|
|
93
|
+
|
|
94
|
+
# Closing logic based on exposure and probabilities
|
|
95
|
+
with LogicMapping(name='check_close', data=state) as lg:
|
|
96
|
+
with (lg.exposure > 0) & (lg.down_prob > 0.):
|
|
97
|
+
ShortAction() # Short action for positive exposure and down probability
|
|
98
|
+
|
|
99
|
+
with (lg.exposure < 0) & (lg.up_prob > 0.):
|
|
100
|
+
LongAction() # Long action for negative exposure and up probability
|
|
101
|
+
|
|
102
|
+
# Visualize the decision tree
|
|
103
|
+
root.to_html()
|
|
104
|
+
|
|
105
|
+
# Log the evaluation result
|
|
106
|
+
LOGGER.info(root())
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Explanation of the Script:
|
|
110
|
+
|
|
111
|
+
- LogicNode & LogicMapping:
|
|
112
|
+
- LogicNode: Represents a node in the decision tree where conditions are evaluated.
|
|
113
|
+
- LogicMapping: Associates logical conditions with the state (data) used in decision-making.
|
|
114
|
+
|
|
115
|
+
- State:
|
|
116
|
+
- A dictionary containing the variables used for decision-making, such as exposure, working_order, up_prob, etc.
|
|
117
|
+
|
|
118
|
+
- RootLogicNode:
|
|
119
|
+
- The entry point for the decision tree where all logical decisions are linked.
|
|
120
|
+
|
|
121
|
+
- Decision Conditions:
|
|
122
|
+
- Inside each with block, logical conditions are evaluated (e.g., lg.volatility > 0.25, lg.up_prob < -0.1) to determine which action to take.
|
|
123
|
+
- Actions like LongAction() or ShortAction() are taken based on the conditions.
|
|
124
|
+
|
|
125
|
+
- Action Handling:
|
|
126
|
+
- LongAction(), ShortAction(), and NoAction() represent different actions you can trigger in the decision tree based on the conditions.
|
|
127
|
+
|
|
128
|
+
- Logging:
|
|
129
|
+
- The result of the tree evaluation is logged using the LOGGER object, which outputs to the console.
|
|
130
|
+
|
|
131
|
+
- Visualization:
|
|
132
|
+
- root.to_html() generates an HTML representation of the decision tree for visualization.
|
|
133
|
+
|
|
134
|
+
# Features
|
|
135
|
+
|
|
136
|
+
- Easily define custom decision rules.
|
|
137
|
+
- Actionable outcomes like LongAction, ShortAction, and NoAction.
|
|
138
|
+
- Log outputs for debugging and tracking.
|
|
139
|
+
- Visualize decision paths through HTML export.
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
# Contributing
|
|
144
|
+
|
|
145
|
+
Feel free to fork, submit issues, and create pull requests. Contributions are always welcome!
|
|
146
|
+
|
|
147
|
+
# License
|
|
148
|
+
|
|
149
|
+
This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.cfg
|
|
6
|
+
setup.py
|
|
7
|
+
./decision_graph/__init__.py
|
|
8
|
+
./decision_graph/decision_tree/__init__.py
|
|
9
|
+
./decision_graph/decision_tree/abc.py
|
|
10
|
+
./decision_graph/decision_tree/collection.py
|
|
11
|
+
./decision_graph/decision_tree/exc.py
|
|
12
|
+
./decision_graph/decision_tree/expression.py
|
|
13
|
+
./decision_graph/decision_tree/node.py
|
|
14
|
+
./decision_graph/logic_group/__init__.py
|
|
15
|
+
./decision_graph/logic_group/base.py
|
|
16
|
+
./decision_graph/logic_group/pending_request.py
|
|
17
|
+
PyDecisionGraph.egg-info/PKG-INFO
|
|
18
|
+
PyDecisionGraph.egg-info/SOURCES.txt
|
|
19
|
+
PyDecisionGraph.egg-info/dependency_links.txt
|
|
20
|
+
PyDecisionGraph.egg-info/not-zip-safe
|
|
21
|
+
PyDecisionGraph.egg-info/requires.txt
|
|
22
|
+
PyDecisionGraph.egg-info/top_level.txt
|
|
23
|
+
decision_graph/__init__.py
|
|
24
|
+
decision_graph/decision_tree/__init__.py
|
|
25
|
+
decision_graph/decision_tree/abc.py
|
|
26
|
+
decision_graph/decision_tree/collection.py
|
|
27
|
+
decision_graph/decision_tree/exc.py
|
|
28
|
+
decision_graph/decision_tree/expression.py
|
|
29
|
+
decision_graph/decision_tree/node.py
|
|
30
|
+
decision_graph/logic_group/__init__.py
|
|
31
|
+
decision_graph/logic_group/base.py
|
|
32
|
+
decision_graph/logic_group/pending_request.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
decision_graph
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# PyDecisionGraph
|
|
2
|
+
|
|
3
|
+
`PyDecisionGraph` is an easy-to-use library to create custom decision trees, primarily designed for trading and financial decision-making. This package helps you build rule-based decision processes and visualize them effectively.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To install the package, run:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install PyDecisionGraph
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
# Requirements
|
|
14
|
+
|
|
15
|
+
- Python 3.12 or higher
|
|
16
|
+
|
|
17
|
+
# Documentation
|
|
18
|
+
|
|
19
|
+
For detailed documentation, visit https://pydecisiongraph.readthedocs.io/.
|
|
20
|
+
|
|
21
|
+
# Quick Start
|
|
22
|
+
|
|
23
|
+
Here is a quick demo on how to use `PyDecisionGraph` for building a decision tree based on various conditions:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from decision_graph.decision_tree import LogicNode, LOGGER, AttrExpression, LongAction, ShortAction, NoAction, RootLogicNode, LogicMapping
|
|
27
|
+
|
|
28
|
+
# Mapping of attribute names to their values
|
|
29
|
+
LogicMapping.AttrExpression = AttrExpression
|
|
30
|
+
|
|
31
|
+
state = {
|
|
32
|
+
"exposure": 0, # Current exposure
|
|
33
|
+
"working_order": 0, # Current working order
|
|
34
|
+
"up_prob": 0.8, # Probability of price going up
|
|
35
|
+
"down_prob": 0.2, # Probability of price going down
|
|
36
|
+
"volatility": 0.24, # Current market volatility
|
|
37
|
+
"ttl": 15.3 # Time to live (TTL) of the decision tree
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
# Root of the logic tree
|
|
41
|
+
with RootLogicNode() as root:
|
|
42
|
+
# Define root logic mapping with state data
|
|
43
|
+
with LogicMapping(name='Root', data=state) as lg_root:
|
|
44
|
+
lg_root: LogicMapping
|
|
45
|
+
|
|
46
|
+
# Condition for zero exposure
|
|
47
|
+
with lg_root.exposure == 0:
|
|
48
|
+
root: LogicNode
|
|
49
|
+
with LogicMapping(name='check_open', data=state) as lg:
|
|
50
|
+
with lg.working_order != 0:
|
|
51
|
+
break_point = NoAction() # No action if there's a working order
|
|
52
|
+
lg.break_(scope=lg) # Exit the current scope
|
|
53
|
+
|
|
54
|
+
with lg.volatility > 0.25: # Check if volatility is high
|
|
55
|
+
with lg.down_prob > 0.1: # Action for down probability
|
|
56
|
+
LongAction()
|
|
57
|
+
|
|
58
|
+
with lg.up_prob < -0.1: # Action for up probability
|
|
59
|
+
ShortAction()
|
|
60
|
+
|
|
61
|
+
# Condition when TTL is greater than 30
|
|
62
|
+
with lg_root.ttl > 30:
|
|
63
|
+
with lg_root.working_order > 0:
|
|
64
|
+
ShortAction() # Action to short if working order exists
|
|
65
|
+
LongAction() # Always take long action
|
|
66
|
+
lg_root.break_(scope=lg_root) # Exit scope
|
|
67
|
+
|
|
68
|
+
# Closing logic based on exposure and probabilities
|
|
69
|
+
with LogicMapping(name='check_close', data=state) as lg:
|
|
70
|
+
with (lg.exposure > 0) & (lg.down_prob > 0.):
|
|
71
|
+
ShortAction() # Short action for positive exposure and down probability
|
|
72
|
+
|
|
73
|
+
with (lg.exposure < 0) & (lg.up_prob > 0.):
|
|
74
|
+
LongAction() # Long action for negative exposure and up probability
|
|
75
|
+
|
|
76
|
+
# Visualize the decision tree
|
|
77
|
+
root.to_html()
|
|
78
|
+
|
|
79
|
+
# Log the evaluation result
|
|
80
|
+
LOGGER.info(root())
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Explanation of the Script:
|
|
84
|
+
|
|
85
|
+
- LogicNode & LogicMapping:
|
|
86
|
+
- LogicNode: Represents a node in the decision tree where conditions are evaluated.
|
|
87
|
+
- LogicMapping: Associates logical conditions with the state (data) used in decision-making.
|
|
88
|
+
|
|
89
|
+
- State:
|
|
90
|
+
- A dictionary containing the variables used for decision-making, such as exposure, working_order, up_prob, etc.
|
|
91
|
+
|
|
92
|
+
- RootLogicNode:
|
|
93
|
+
- The entry point for the decision tree where all logical decisions are linked.
|
|
94
|
+
|
|
95
|
+
- Decision Conditions:
|
|
96
|
+
- Inside each with block, logical conditions are evaluated (e.g., lg.volatility > 0.25, lg.up_prob < -0.1) to determine which action to take.
|
|
97
|
+
- Actions like LongAction() or ShortAction() are taken based on the conditions.
|
|
98
|
+
|
|
99
|
+
- Action Handling:
|
|
100
|
+
- LongAction(), ShortAction(), and NoAction() represent different actions you can trigger in the decision tree based on the conditions.
|
|
101
|
+
|
|
102
|
+
- Logging:
|
|
103
|
+
- The result of the tree evaluation is logged using the LOGGER object, which outputs to the console.
|
|
104
|
+
|
|
105
|
+
- Visualization:
|
|
106
|
+
- root.to_html() generates an HTML representation of the decision tree for visualization.
|
|
107
|
+
|
|
108
|
+
# Features
|
|
109
|
+
|
|
110
|
+
- Easily define custom decision rules.
|
|
111
|
+
- Actionable outcomes like LongAction, ShortAction, and NoAction.
|
|
112
|
+
- Log outputs for debugging and tracking.
|
|
113
|
+
- Visualize decision paths through HTML export.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
# Contributing
|
|
118
|
+
|
|
119
|
+
Feel free to fork, submit issues, and create pull requests. Contributions are always welcome!
|
|
120
|
+
|
|
121
|
+
# License
|
|
122
|
+
|
|
123
|
+
This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
__version__ = "0.1.2"
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
LOGGER = logging.getLogger("DecisionGraph")
|
|
7
|
+
LOGGER.setLevel(logging.INFO)
|
|
8
|
+
|
|
9
|
+
if not LOGGER.hasHandlers():
|
|
10
|
+
ch = logging.StreamHandler(sys.stdout)
|
|
11
|
+
ch.setLevel(logging.INFO) # Set handler level
|
|
12
|
+
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
|
|
13
|
+
ch.setFormatter(formatter)
|
|
14
|
+
LOGGER.addHandler(ch)
|
|
15
|
+
|
|
16
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from .. import LOGGER
|
|
4
|
+
|
|
5
|
+
LOGGER = LOGGER.getChild("DecisionTree")
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
'LOGGER', 'set_logger', 'activate_expression_model', 'activate_node_model',
|
|
9
|
+
'NodeError', 'TooManyChildren', 'TooFewChildren', 'NodeNotFountError', 'NodeValueError', 'EdgeValueError', 'ResolutionError', 'ExpressFalse', 'ContextsNotFound',
|
|
10
|
+
'LGM', 'LogicGroup', 'SkipContextsBlock', 'LogicExpression', 'ExpressionCollection', 'LogicNode', 'ActionNode', 'ELSE_CONDITION',
|
|
11
|
+
'NoAction', 'LongAction', 'ShortAction', 'RootLogicNode', 'ContextLogicExpression', 'AttrExpression', 'MathExpression', 'ComparisonExpression', 'LogicalExpression',
|
|
12
|
+
'LogicMapping', 'LogicGenerator'
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
from .exc import *
|
|
16
|
+
from .abc import *
|
|
17
|
+
from .node import *
|
|
18
|
+
from .collection import *
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def set_logger(logger: logging.Logger):
|
|
22
|
+
global LOGGER
|
|
23
|
+
LOGGER = logger
|
|
24
|
+
|
|
25
|
+
abc.LOGGER = logger.getChild('abc')
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def activate_expression_model():
|
|
29
|
+
import importlib
|
|
30
|
+
importlib.import_module('decision_graph.decision_tree.expression')
|
|
31
|
+
importlib.reload(collection)
|
|
32
|
+
collection.LogicMapping.AttrExpression = AttrExpression
|
|
33
|
+
collection.LogicGenerator.AttrExpression = AttrExpression
|
|
34
|
+
# importlib.reload(logic_group)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def activate_node_model():
|
|
38
|
+
import importlib
|
|
39
|
+
|
|
40
|
+
importlib.import_module('decision_graph.decision_tree.node')
|
|
41
|
+
importlib.reload(collection)
|
|
42
|
+
collection.LogicMapping.AttrExpression = AttrExpression
|
|
43
|
+
collection.LogicGenerator.AttrExpression = AttrExpression
|
|
44
|
+
# importlib.reload(logic_group)
|