10 changed files with 181 additions and 22 deletions
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
|
||||
from naiback.data.feed import Feed, Bar |
||||
import csv |
||||
import datetime |
||||
import itertools |
||||
|
||||
class YahooCSVFeed(Feed): |
||||
|
||||
def __init__(self, fp): |
||||
self.bars = [] |
||||
self.ticker_ = None |
||||
reader = csv.reader(fp, delimiter=',') |
||||
next(reader) |
||||
for row in reader: |
||||
try: |
||||
self.ticker_ = row[0] |
||||
open_ = float(row[1]) |
||||
high = float(row[2]) |
||||
low = float(row[3]) |
||||
close = float(row[4]) |
||||
volume = int(row[6]) |
||||
date = row[0] |
||||
dt = datetime.datetime.strptime(date, "%Y-%m-%d") |
||||
self.bars.append(Bar(open_, high, low, close, volume, dt)) |
||||
except IndexError: |
||||
pass |
||||
|
||||
def type(self): |
||||
return 'bars' |
||||
|
||||
def items(self): |
||||
return self.bars |
||||
|
||||
def ticker(self): |
||||
return self.ticker_ |
||||
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
|
||||
import numpy as np |
||||
|
||||
from .atr import ATR |
||||
from .ema import EMA |
||||
from .smma import SMMA |
||||
|
||||
def ADX(bars, period): |
||||
dm_minus = np.zeros(len(bars.close)) |
||||
dm_plus = np.zeros(len(bars.close)) |
||||
adx = np.zeros(len(bars.close)) |
||||
atr = ATR(bars, period) |
||||
|
||||
if len(bars.close) == 0: |
||||
return np.array([]) |
||||
|
||||
for i in range(1, len(bars.close)): |
||||
plus = bars.high[i] - bars.high[i - 1] |
||||
minus = -bars.low[i] + bars.low[i - 1] |
||||
|
||||
if plus > 0 and plus > minus: |
||||
dm_plus[i] = plus |
||||
|
||||
if minus > 0 and minus > plus: |
||||
dm_minus[i] = minus |
||||
|
||||
di_plus = np.asarray(SMMA(dm_plus, period)) * 100 / atr |
||||
di_minus = np.asarray(SMMA(dm_minus, period)) * 100 / atr |
||||
|
||||
preprocessed_adx = np.abs(np.nan_to_num(np.divide(di_plus - di_minus, di_plus + di_minus))) |
||||
adx = 100 * np.asarray(SMMA(preprocessed_adx, period)) |
||||
|
||||
return (adx, di_plus, di_minus) |
||||
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
|
||||
import numpy as np |
||||
|
||||
def SMMA(values, period): |
||||
smma = np.zeros(len(values)) |
||||
alpha = (period - 1) / period |
||||
|
||||
smma[0] = values[0] |
||||
for i in range(1, len(values)): |
||||
smma[i] = smma[i - 1] * alpha + values[i] * (1 - alpha) |
||||
|
||||
return smma |
||||
|
||||
Loading…
Reference in new issue