Browse Source

feat: sma & ema

pull/15/head
QuentinN42 8 months ago
parent
commit
a610c8770d
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 28
      auto_trading/indicators/ema.py
  2. 28
      auto_trading/indicators/sma.py

28
auto_trading/indicators/ema.py

@ -0,0 +1,28 @@
"""EMA indicator."""
import pandas as pd # type: ignore
from ..interfaces import Indicator
class EMA(Indicator):
"""
https://stackoverflow.com/questions/48613151/simple-python-pandas-ema-ewma
"""
def __init__(self, nb_values: int):
"""Save the value."""
super().__init__()
self.nb_values = nb_values
def __call__(self, data: pd.DataFrame) -> pd.Series:
"""Return a dataframe of valuation of each stock from the input data.
Args:
data (DataFrame): Time-Stock valuated candlestick data.
For each time and each stock give (high, low, open, close).
Returns:
DataFrame: Stock valuated float.
For each stock give -1 if realy bad and +1 if realy good.
"""
return data.ewm(self.nb_values).mean().close.loc[data.index[-1][0]]

28
auto_trading/indicators/sma.py

@ -0,0 +1,28 @@
"""EMA indicator."""
import pandas as pd # type: ignore
from ..interfaces import Indicator
class SMA(Indicator):
"""
https://stackoverflow.com/questions/48613151/simple-python-pandas-ema-ewma
"""
def __init__(self, nb_values: int):
"""Save the value."""
super().__init__()
self.nb_values = nb_values
def __call__(self, data: pd.DataFrame) -> pd.Series:
"""Return a dataframe of valuation of each stock from the input data.
Args:
data (DataFrame): Time-Stock valuated candlestick data.
For each time and each stock give (high, low, open, close).
Returns:
DataFrame: Stock valuated float.
For each stock give -1 if realy bad and +1 if realy good.
"""
return data.rolling(self.nb_values).mean().close.loc[data.index[-1][0]]
Loading…
Cancel
Save