Skip to content

Commit

Permalink
black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswinder9051998 committed Oct 31, 2021
1 parent 9c7eea3 commit 5a10c35
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@


class AbsolutePriceOscillator:
"""
"""
Provided dataframe must be in ascending order.
"""

def __init__(
self,
fast_period: int = 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@


class AverageDirectionalMovementIndex:
def __init__(self,
directional_movement_lookback_period: int = 4,
def __init__(
self,
directional_movement_lookback_period: int = 4,
directional_movement_min_periods: int = None,
directional_movement_smoothing_period: int = 14,
directional_movement_smoothing_min_periods: int = 0,
Expand All @@ -29,7 +30,8 @@ def __init__(self,
true_range_lookback: int = 4,
average_true_range_span: int = 6,
true_range_min_periods: int = None,
average_true_range_periods: int = 1):
average_true_range_periods: int = 1,
):
"""
Parameters
----------
Expand All @@ -54,9 +56,7 @@ def __init__(self,
average_true_range_periods : int, optional
Minimum number of observations in window required to have a value for average true range calculation , by default 1
"""
self.directional_movement_lookback_period = (
directional_movement_lookback_period
)
self.directional_movement_lookback_period = directional_movement_lookback_period
self.directional_movement_min_periods = directional_movement_min_periods
self.directional_movement_smoothing_period = (
directional_movement_smoothing_period
Expand All @@ -78,13 +78,13 @@ def __init__(self,
self.average_true_range_periods = average_true_range_periods

def _plus_dm(self, x, look_back_period):
look_back_period=int(look_back_period/2)
look_back_period = int(look_back_period / 2)
return np.max(x.iloc[look_back_period:]) - np.max(
x.iloc[0 : look_back_period - 1]
)

def _minus_dm(self, x, look_back_period):
look_back_period=int(look_back_period/2)
look_back_period = int(look_back_period / 2)
return np.min(x.iloc[0 : look_back_period - 1]) - np.min(
x.iloc[look_back_period:]
)
Expand Down Expand Up @@ -136,8 +136,6 @@ def fit(
operation="mean",
)



plus_dma = self._plus_dma_object._template_feature_calculation(
function_name="plus_dma",
win_function=_identity_window,
Expand Down Expand Up @@ -191,7 +189,11 @@ def fit(
dataframe=dataframe,
first_fit=first_fit,
)
average_true_range=average_true_range.to_frame() if isinstance(average_true_range,pd.Series) else average_true_range
average_true_range = (
average_true_range.to_frame()
if isinstance(average_true_range, pd.Series)
else average_true_range
)

plus_directional_index = 100 * (smoothed_plus_dma.div(average_true_range))
minus_directional_index = 100 * (smoothed_minus_dma.div(average_true_range))
Expand All @@ -206,5 +208,3 @@ def fit(
first_fit=first_fit,
)
return adx


24 changes: 13 additions & 11 deletions NitroFE/time_based_features/indicator_features/_AverageTrueRange.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
import pandas as pd
from typing import Union, Callable

from NitroFE.time_based_features.weighted_window_features.weighted_windows import _equal_window, _identity_window
from NitroFE.time_based_features.weighted_window_features.weighted_windows import (
_equal_window,
_identity_window,
)
from NitroFE.time_based_features.weighted_window_features.weighted_window_features import (
weighted_window_features,
)


class AverageTrueRange:
def __init__(self,
def __init__(
self,
true_range_lookback: int = 4,
average_true_range_span: int = 6,
true_range_min_periods: int = None,
average_true_range_periods: int = 1,
return_true_range:bool=False):
return_true_range: bool = False,
):
"""
Parameters
----------
Expand All @@ -28,13 +34,13 @@ def __init__(self,
return_true_range : bool, optional
If true, True range is returned instead of Average True range
"""

self.true_range_lookback = true_range_lookback
self.average_true_range_span = average_true_range_span

self.true_range_min_periods = true_range_min_periods
self.average_true_range_periods = average_true_range_periods
self.return_true_range=return_true_range
self.return_true_range = return_true_range

def true_range(self, x):
return np.max(
Expand All @@ -45,11 +51,7 @@ def true_range(self, x):
]
)

def fit(
self,
dataframe: Union[pd.DataFrame, pd.Series],
first_fit: bool = True
):
def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True):
"""
For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False
Expand Down Expand Up @@ -96,4 +98,4 @@ def fit(
operation_args=(),
)

return average_true_range
return average_true_range
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@


class ElasticSeriesWeightedAverage:
def __init__(self,
weight_sum_lookback: int = 4):
def __init__(self, weight_sum_lookback: int = 4):
"""
Parameters
----------
Expand All @@ -25,7 +24,8 @@ def fit(
self,
dataframe: Union[pd.DataFrame, pd.Series],
dataframe_for_weight: Union[pd.DataFrame, pd.Series],
first_fit: bool = True):
first_fit: bool = True,
):
"""
elastic_series_weighted_average
This feature is an implementation of elastic volume weighted moving average
Expand Down Expand Up @@ -55,7 +55,6 @@ def fit(
if first_fit:
self._object = weighted_window_features()


rolling_sum = self._object._template_feature_calculation(
function_name="_lag_object",
win_function=_identity_window,
Expand All @@ -70,7 +69,7 @@ def fit(

if isinstance(dataframe, pd.Series):
dataframe = dataframe.to_frame()

eswa = pd.DataFrame(np.zeros(dataframe.shape), columns=dataframe.columns)
print(dataframe.shape)
print(eswa)
Expand All @@ -95,8 +94,7 @@ def fit(
+ ((r1[1] - previous_eswa) * (r3[1] / r4[1])).fillna(0)
).values[0]


res=eswa.iloc[1:][ll]

res = eswa.iloc[1:][ll]

self.values_from_last_run = eswa.iloc[-1:]
return res
return res
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ExponentialMovingFeature,
)


class KeltnerChannel:
def __init__(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@


class MovingAverageConvergenceDivergence:
"""
"""
Provided dataframe must be in ascending order.
"""

def __init__(
self,
fast_period: int = 26,
Expand All @@ -32,7 +33,7 @@ def __init__(
return_histogram=False,
):
"""
Parameters
----------
fast_period : int, optional
Expand Down Expand Up @@ -86,7 +87,7 @@ def fit(
For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False
Returns --> Smoothed signal line , macd histogram
Parameters
----------
dataframe : Union[pd.DataFrame, pd.Series]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@


class PercentageValueOscillator:
"""
"""
Provided dataframe must be in ascending order.
"""

def __init__(
self,
fast_period: int = 4,
slow_period: int = 8,
smoothing_period:int = 9,
smoothing_period: int = 9,
fast_operation: str = "mean",
slow_operation: str = "mean",
initialize_using_operation: bool = False,
Expand Down Expand Up @@ -52,7 +53,7 @@ def __init__(
"""
self.span_fast = fast_period
self.span_slow = slow_period
self.span_smoothing=smoothing_period
self.span_smoothing = smoothing_period

self.min_periods = min_periods

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import numpy as np
import pandas as pd
from typing import Union
Expand All @@ -10,13 +8,18 @@
_equal_window,
_identity_window,
)
from NitroFE.time_based_features.moving_average_features.moving_average_features import ExponentialMovingFeature,HullMovingFeature,\
KaufmanAdaptiveMovingAverage,FractalAdaptiveMovingAverage,TripleExponentialMovingFeature,SmoothedMovingAverage
from NitroFE.time_based_features.moving_average_features.moving_average_features import (
ExponentialMovingFeature,
HullMovingFeature,
KaufmanAdaptiveMovingAverage,
FractalAdaptiveMovingAverage,
TripleExponentialMovingFeature,
SmoothedMovingAverage,
)


class RelativeStrengthIndex:
def __init__(self,
lookback_period:int=8):
def __init__(self, lookback_period: int = 8):
"""
Parameters
----------
Expand All @@ -25,19 +28,16 @@ def __init__(self,
"""
self.lookback_period = lookback_period

def _diff_pos(self,x):
diff_val=x.iloc[-1]-x.iloc[-2]
return diff_val if diff_val>0 else 0
def _diff_pos(self, x):
diff_val = x.iloc[-1] - x.iloc[-2]
return diff_val if diff_val > 0 else 0

def _diff_neg(self,x):
diff_val=x.iloc[-1]-x.iloc[-2]
res=diff_val if diff_val<0 else 0
def _diff_neg(self, x):
diff_val = x.iloc[-1] - x.iloc[-2]
res = diff_val if diff_val < 0 else 0
return -res

def fit(
self,
dataframe: Union[pd.DataFrame, pd.Series],
first_fit: bool = True):
def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True):
"""
Provided dataframe must be in ascending order.
Expand All @@ -59,10 +59,13 @@ def fit(
self._up_object = weighted_window_features()
self._down_object = weighted_window_features()

self._up_smoothed=SmoothedMovingAverage(lookback_period=self.lookback_period)
self._down_smoothed=SmoothedMovingAverage(lookback_period=self.lookback_period)
self._up_smoothed = SmoothedMovingAverage(
lookback_period=self.lookback_period
)
self._down_smoothed = SmoothedMovingAverage(
lookback_period=self.lookback_period
)


if isinstance(dataframe, pd.Series):
dataframe = dataframe.to_frame()

Expand All @@ -71,7 +74,7 @@ def fit(
win_function=_identity_window,
first_fit=first_fit,
dataframe=dataframe,
window=2 ,
window=2,
min_periods=None,
symmetric=None,
operation=self._diff_pos,
Expand All @@ -83,15 +86,19 @@ def fit(
win_function=_identity_window,
first_fit=first_fit,
dataframe=dataframe,
window=2 ,
window=2,
min_periods=None,
symmetric=None,
operation=self._diff_neg,
operation_args=(),
)

smoothed_up_value=self._up_smoothed.fit(dataframe=up_value,first_fit=first_fit)
smoothed_down_value=self._down_smoothed.fit(dataframe=down_value,first_fit=first_fit)
smoothed_up_value = self._up_smoothed.fit(
dataframe=up_value, first_fit=first_fit
)
smoothed_down_value = self._down_smoothed.fit(
dataframe=down_value, first_fit=first_fit
)

rsi = 100 -100/(1+(smoothed_up_value/smoothed_down_value))
return rsi
rsi = 100 - 100 / (1 + (smoothed_up_value / smoothed_down_value))
return rsi
Loading

0 comments on commit 5a10c35

Please sign in to comment.