2
2
import importlib
3
3
import os
4
4
import sys
5
+ import types
6
+
5
7
from os .path import abspath , join , exists , basename , splitext
6
8
from glob import glob
7
- import types
9
+
8
10
import pandas_ta
9
- import pandas as pd
10
11
from pandas_ta import AnalysisIndicators
11
12
12
13
@@ -86,7 +87,7 @@ def import_dir(path, verbose=True):
86
87
return
87
88
88
89
# list the contents of the directory
89
- dirs = glob (abspath (join (path , '*' )))
90
+ dirs = glob (abspath (join (path , "*" )))
90
91
91
92
# traverse full directory, importing all modules found there
92
93
for d in dirs :
@@ -99,7 +100,7 @@ def import_dir(path, verbose=True):
99
100
continue
100
101
101
102
# for each module found in that category (directory)...
102
- for module in glob (abspath (join (path , dirname , ' *.py' ))):
103
+ for module in glob (abspath (join (path , dirname , " *.py" ))):
103
104
module_name = splitext (basename (module ))[0 ]
104
105
105
106
# ensure that the supplied path is included in our python path
@@ -111,13 +112,13 @@ def import_dir(path, verbose=True):
111
112
112
113
# figure out which of the modules functions to bind to pandas_ta
113
114
fcn_callable = module_functions .get (module_name , None )
114
- fcn_method_callable = module_functions .get (module_name + " _method" , None )
115
+ fcn_method_callable = module_functions .get (f" { module_name } _method" , None )
115
116
116
117
if fcn_callable == None :
117
118
print (f"[X] Unable to find a function named '{ module_name } ' in the module '{ module_name } .py'." )
118
119
continue
119
120
if fcn_method_callable == None :
120
- missing_method = module_name + " _method"
121
+ missing_method = f" { module_name } _method"
121
122
print (f"[X] Unable to find a method function named '{ missing_method } ' in the module '{ module_name } .py'." )
122
123
continue
123
124
@@ -145,7 +146,7 @@ def import_dir(path, verbose=True):
145
146
146
147
If you at some late point would like to push them into the pandas_ta library
147
148
you can do so very easily by following the step by step instruction here
148
- https://github.com/twopirllc/pandas-ta/issues/264 .
149
+ https://github.com/twopirllc/pandas-ta/issues/355 .
149
150
150
151
A brief example of usage:
151
152
@@ -160,8 +161,8 @@ def import_dir(path, verbose=True):
160
161
>>> import os
161
162
>>> from os.path import abspath, join, expanduser
162
163
>>> from pandas_ta.custom import create_dir, import_dir
163
- >>> my_dir = abspath(join(expanduser("~"), "my_indicators"))
164
- >>> create_dir(my_dir )
164
+ >>> ta_dir = abspath(join(expanduser("~"), "my_indicators"))
165
+ >>> create_dir(ta_dir )
165
166
166
167
3. You can now create your own custom indicator e.g. by copying existing
167
168
ones from pandas_ta core module and modifying them.
@@ -194,14 +195,14 @@ def import_dir(path, verbose=True):
194
195
4. We can now dynamically load all our custom indicators located in our
195
196
designated indicators directory like this:
196
197
197
- >>> import_dir(my_dir )
198
+ >>> import_dir(ta_dir )
198
199
199
200
If your custom indicator(s) loaded succesfully then it should behave exactly
200
201
like all other native indicators in pandas_ta, including help functions.
201
202
"""
202
203
203
204
204
- def load_indicator_module (module_name ):
205
+ def load_indicator_module (name ):
205
206
"""
206
207
Helper function to (re)load an indicator module.
207
208
@@ -215,9 +216,9 @@ def load_indicator_module(module_name):
215
216
"""
216
217
# load module
217
218
try :
218
- module = importlib .import_module (module_name )
219
+ module = importlib .import_module (name )
219
220
except Exception as ex :
220
- print (f"[X] An error occurred when attempting to load module { module_name } : { ex } " )
221
+ print (f"[X] An error occurred when attempting to load module { name } : { ex } " )
221
222
sys .exit (1 )
222
223
223
224
# reload to refresh previously loaded module
0 commit comments