forked from philippe-ostiguy/PyBacktesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_op.py
96 lines (82 loc) · 3.89 KB
/
math_op.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/local/bin/env python3.7
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# The MIT License (MIT)
# Copyright (c) 2020 Philippe Ostiguy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
###############################################################################
"""
Module support for mathematical operations
"""
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
class MathOp():
"""
Class to provide mathematical operation support
"""
@classmethod
def __init__(cls,series,default_col):
cls.series = series
cls.default_col=default_col
@classmethod
def local_extremum(cls,start_point,end_point,window = 6,min_= 'min',max_='max',index_ = 'index'):
""" Function to find local extremum (min and max) on a Dataframe.
It checks if the values for a number of points on each side (`window`) are greater or lesser and then
determine the local extremum.
Parameters
----------
start_point : int
the first data (index) to check in the Dataframe
end_point : int
the last data (index) to check in the Dataframe
window : int
the number of the data the method check before and after to determine the local extremum. Default is 6.
Recommended values are between 5 and 7.
min_ : str
Name given to min data column
max_ : str
Name given to max data column
Return
------
DataFrame list : Return a pandas dataframe `cls.series` with the none empty min or max value
(if both are empty, nothing is returned. If one of
them has a value, return the local min or max with index no)
"""
cls.series=cls.series.loc[start_point:end_point,cls.default_col]
cls.series=pd.DataFrame({cls.default_col: cls.series})
cls.series[min_] = cls.series.iloc[argrelextrema(cls.series.values, np.less_equal,
order=window)[0]][cls.default_col]
cls.series[max_] = cls.series.iloc[argrelextrema(cls.series.values, np.greater_equal,
order=window)[0]][cls.default_col]
cls.series[index_] = cls.series.index
# Plot results - to get ride when the project is done. Only as a guideline at the moment
"""
plt.scatter(cls.series.index, cls.series[min_], c='r')
plt.scatter(cls.series.index, cls.series[max_], c='g')
plt.plot(cls.series.index, cls.series[cls.default_col])
plt.ion()
plt.show()
"""
#Filter nan value for min or max out
cls.series=cls.series.loc[(cls.series[min_].isna())==False | (cls.series[max_].isna() == False)]
return cls.series