-
Notifications
You must be signed in to change notification settings - Fork 662
add ability to shift moving average on plots #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
don't know why the tests are failing. The image comparison works locally. Is there a setting I can change to be more like the CI environment? |
Ted,
I believe the image test is failing because it is a new test and the reference image is not yet in the production repository. As long as the new test is the only one failing, and you say it is succeeding locally then all is good. The other check that is failing is: I would like to understand the choice of the dict keyword --Daniel |
I don't know how I feel about that. I didn't have the resolve image in place and had a failure and it was a different error message. I put the resolve image in to get it to pass locally, and it is included in the PR.
Done.
Changed to period. I didn't have a reason for the name scale other than that I couldn't think of a better one. I don't like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ted,
I have some concerns about the _mav_validator()
code. nothing a show-stopper; I could accept the PR as is, but I think I will jot my thoughts down here and ask for your feedback:
- There is some repetitive code in that many of the same checks are done for
period
, and forshift
, and for the case ofisinstance(mav_value,(tuple,list,int))
- There is an inconsistency in that
period
andshift
do not limit number of moving averages, but the plainmav
version does. I am inclined to either limit, or not limit, all cases. Admittedly a maximum of 7 moving averages was an arbitrary decision on my part, but to date no one has requested more. On the other hand, removing a limit may simplify the code: maybe let the user shoot themselves in the foot trying to plot dozens or even hundreds of moving averages. - It makes sense that
period
must be > 1, but it may be reasonable to have ashift
== 1.
With the above in mind, I took a shot at rewriting _mav_validator(mav_value)
. Let me know what you think:
def _mav_validator(mav_value):
'''
Value for mav (moving average) keyword may be:
scalar int greater than 1, or tuple of ints, or list of ints (each greater than 1)
or a dict of `period` and `shift` each of which may be:
scalar int, or tuple of ints, or list of ints. `period` must be greater than 1
while `shift` must be greater than 0.
'''
def _valid_mav(value,minimum=2):
if not isinstance(value,(tuple,list,int)):
return False
if isinstance(value,int):
return isinstance(value,int) and value >= minimum
# Must be a tuple or list here:
for num in value:
if not isinstance(num,int) and num >= minimum:
return False
return True
if not isinstance(mav_value,(tuple,list,int,dict)):
return False
if not isinstance(mav_value,dict):
return _valid_mav(mav_value)
else: #isinstance(mav_value,dict)
if 'period' not in mav_value: return False
period = mav_value['period']
if not _valid_mav(period): return False
if 'shift' not in mav_value: return True
shift = mav_value['shift']
if not _valid_mav(shift,1): return False
if isinstance(period,int) and isinstance(shift,int): return True
if isinstance(period,(tuple,list)) and isinstance(shift,(tuple,list)):
if len(period) != len(shift): return False
return True
return False
I like it, but you may want to do a 0 or negative shift as well. I changed your code slightly to only do the |
@deltreey I'm going to merge this PR shortly. Please consider making a small .ipynb file for the examples directory, to show a few use cases of doing All the best. Thanks. --Daniel |
For techniques such as the alligator, it is useful to be able to shift your moving averages. This PR creates the ability to make the
mav
parameter into a dict with 2 keys,scale
is the original mav values (tuple, int, or list)shift
is another set of values (tuple, int, or list) that must match the length ofscale
and shifts the moving average according to the valueNote: this PR does not change the way the
mav
parameter worked already. It only enhances it by adding this additional optionI created a test as well.