Skip to content

add barslast function #23

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions funcat/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import numpy as np

from .time_series import MarketDataSeries
from .time_series import MarketDataSeries, NumericSeries
from .func import (
SumSeries,
AbsSeries,
Expand All @@ -19,7 +19,7 @@
llv,
Ref,
iif,
)
barslast)
from .context import (
symbol,
set_current_security,
Expand Down Expand Up @@ -59,6 +59,7 @@
HHV = hhv
LLV = llv
IF = IIF = iif
BARSLAST=barslast

S = set_current_security
T = set_current_date
Expand Down Expand Up @@ -90,6 +91,7 @@
"HHV",
"LLV",
"IF", "IIF",
"BARSLAST",

"S",
"T",
Expand All @@ -101,4 +103,6 @@
"set_start_date",
"set_data_backend",
"set_current_freq",

"NumericSeries",
]
36 changes: 36 additions & 0 deletions funcat/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
)


# delete nan of series for error made by some operator
def filter_begin_nan(series):
i = 0
for x in series:
if np.isnan(x):
i += 1
else:
break
return series[i:]


class OneArgumentSeries(NumericSeries):
func = talib.MA

Expand All @@ -29,6 +40,7 @@ def __init__(self, series, arg):
try:
series[series == np.inf] = np.nan
series = self.func(series, arg)
series = filter_begin_nan(series)
except Exception as e:
raise FormulaException(e)
super(OneArgumentSeries, self).__init__(series)
Expand Down Expand Up @@ -64,6 +76,7 @@ def __init__(self, series, arg1, arg2):
try:
series[series == np.inf] = np.nan
series = self.func(series, arg1, arg2)
series = filter_begin_nan(series)
except Exception as e:
raise FormulaException(e)
super(TwoArgumentSeries, self).__init__(series)
Expand Down Expand Up @@ -129,6 +142,8 @@ def CrossOver(s1, s2):


def Ref(s1, n):
if isinstance(n, NumericSeries):
return s1[int(n.value)]
return s1[n]


Expand Down Expand Up @@ -213,3 +228,24 @@ def iif(condition, true_statement, false_statement):
series[cond_series] = series1[cond_series]

return NumericSeries(series)


@handle_numpy_warning
def barslast(statement):
series = get_series(statement)
size = len(series)
end = size
begin = size - 1

try:
result = np.full(size, 1e16, dtype=np.int64)
except ValueError as e:
raise FormulaException(e)

for s in series[::-1]:
if s:
result[begin:end] = range(0, end - begin)
end = begin
begin -= 1

return NumericSeries(result)
15 changes: 14 additions & 1 deletion funcat/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def __invert__(self):
def __repr__(self):
return str(self.value)

def __int__(self):
return int(self.value)


class NumericSeries(TimeSeries):
def __init__(self, series=[]):
Expand All @@ -216,7 +219,13 @@ def series(self):
return self._series

def __getitem__(self, index):
assert isinstance(index, int) and index >= 0
assert (isinstance(index, int) and index >= 0) \
or (isinstance(index, NumericSeries))

if isinstance(index, NumericSeries):
index = int(index.value)
assert index >= 0

return self.__class__(series=self.series[:len(self.series) - index], **self.extra_create_kwargs)


Expand Down Expand Up @@ -255,6 +264,10 @@ def __getitem__(self, index):
if isinstance(index, int):
assert index >= 0

if isinstance(index, NumericSeries):
index = int(index.value)
assert index >= 0

if isinstance(index, six.string_types):
unit = index[-1]
period = int(index[:-1])
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ lxml
requests
TA-Lib
cached-property
bs4
tushare
1 change: 1 addition & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_000001():
T("20161216")
S("000001.XSHG")

assert np.equal(REF(C, BARSLAST(C > 0)).value, C.value)
assert np.equal(round(CLOSE.value, 2), 3122.98)
assert np.equal(round(OPEN[2].value, 2), 3149.38)
assert np.equal(round((CLOSE - OPEN).value, 2), 11.47)
Expand Down