12 changed files with 209 additions and 50 deletions
@ -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.close.unstack().ewm(self.nb_values).mean().loc[data.index[-1][0]] |
@ -0,0 +1,55 @@ |
|||
"""SMA indicator, not ready yet.""" |
|||
from calendar import c |
|||
from re import sub |
|||
import pandas as pd # type: ignore |
|||
|
|||
from ..interfaces import Indicator |
|||
|
|||
|
|||
class SMA(Indicator): |
|||
"""Replay the value.""" |
|||
|
|||
def __init__(self, windowSize: int): |
|||
"""Save the value.""" |
|||
super().__init__() |
|||
self.windowSize = windowSize |
|||
|
|||
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. |
|||
""" |
|||
|
|||
# only use date as index => actions become columns |
|||
data2 = data.unstack() |
|||
|
|||
# select high prices for each action |
|||
data3 = data2["close"] |
|||
|
|||
# lastND.rolling(window=2).mean() |
|||
sma = data3.rolling(window=self.windowSize).mean() |
|||
|
|||
res = {} |
|||
for column in sma.columns: |
|||
# pour chaque type d'action |
|||
# print(column) |
|||
if len(sma.index) > 1: |
|||
# print(last2D[column].get(last2D.index[-1]), last2D[column].get(last2D.index[-2])) |
|||
|
|||
ultieme = sma[column].get(sma.index[-1]) |
|||
penultieme = sma[column].get(sma.index[-2]) |
|||
|
|||
if ultieme / penultieme > 1.05: |
|||
res[column] = 1 |
|||
elif ultieme / penultieme < 0.95: |
|||
res[column] = -1 |
|||
else: |
|||
res[column] = 0 |
|||
else: |
|||
res[column] = 1 |
|||
|
|||
return pd.Series(res) |
@ -0,0 +1,39 @@ |
|||
"""Realy basic tests.""" |
|||
import pytest |
|||
from datetime import datetime |
|||
from pandas import DataFrame, Series # type: ignore |
|||
|
|||
from auto_trading.indicators.ema import EMA |
|||
|
|||
|
|||
def date(j: int) -> datetime: |
|||
return datetime.strptime(f"2015-03-{j}", "%Y-%m-%d") |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"nb_values, data, expected", |
|||
[ |
|||
( |
|||
10, |
|||
DataFrame( |
|||
{ |
|||
"close": { |
|||
(date(i), st): 10 if i == 10 and st == "GOOG" else 0 |
|||
for i in range(1, 11) |
|||
for st in ("GOOG", "GOOGL") |
|||
} |
|||
} |
|||
), |
|||
{"GOOG": 1.48, "GOOGL": 0.0}, |
|||
), |
|||
], |
|||
) |
|||
def test_ema(nb_values: int, data: DataFrame, expected: Series) -> None: |
|||
"""Test the ema indicator.""" |
|||
ema = EMA(nb_values) |
|||
res = ema(data).to_dict() |
|||
|
|||
# round all values |
|||
res = {k: round(v, 3) for k, v in res.items()} |
|||
|
|||
assert res == expected |
@ -0,0 +1,34 @@ |
|||
"""Realy basic tests.""" |
|||
import pytest |
|||
from datetime import datetime |
|||
from pandas import DataFrame, Series # type: ignore |
|||
|
|||
from auto_trading.indicators.sma import SMA |
|||
|
|||
|
|||
def date(j: int) -> datetime: |
|||
return datetime.strptime(f"2015-03-{j}", "%Y-%m-%d") |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"nb_values, data, expected", |
|||
[ |
|||
( |
|||
10, |
|||
DataFrame( |
|||
{ |
|||
"close": { |
|||
(date(i), st): 10 if i == 10 and st == "GOOG" else 0 |
|||
for i in range(1, 11) |
|||
for st in ("GOOG", "GOOGL") |
|||
} |
|||
} |
|||
), |
|||
{"GOOG": 1.0, "GOOGL": 0.0}, |
|||
), |
|||
], |
|||
) |
|||
def test_sma(nb_values: int, data: DataFrame, expected: Series) -> None: |
|||
"""Test the SMA indicator.""" |
|||
sma = SMA(nb_values) |
|||
assert sma(data).to_dict() == expected |
Loading…
Reference in new issue