def VWMA_n(N, prices, volumes): # cannot deal with zero volume, then set it to 1 will have no effect on the result, juste give a price for i, v in enumerate(volumes): if v <= 0: volumes[i] = 1.0 # pvs = MM_n(N, np.array(prices)*np.array(volumes)) pvs = ta_SMA(np.array(prices) * np.array(volumes), N) # vs = MM_n(N, volumes) vs = ta_SMA(volumes, N) return pvs / vs
def compute(self, timestamp, high, low, close): # Breakout Indicator Inputs bb_basis = ta_EMA(close, self._bb_L) if self._use_EMA else ta_SMA( close, self._bb_L) fast_ma = ta_EMA(close, self._fast_MA_L) # Deviation (a simple BBAND) # dev = ta_STDDEV(close, self._bb_L) # bb_dev_inner = self._base_multiplier * dev # Upper bands # inner_high = bb_basis + bb_dev_inner # Lower Bands # inner_low = bb_basis - bb_dev_inner # Calculate Awesome Oscillator hl2 = (high + low) * 0.5 xSMA1_hl2 = ta_SMA(hl2, self._awesome_fast_L) xSMA2_hl2 = ta_SMA(hl2, self._awesome_slow_L) xSMA1_SMA2 = xSMA1_hl2 - xSMA2_hl2 # Calculate direction of AO if xSMA1_SMA2[-1] >= 0: if xSMA1_SMA2[-1] > xSMA1_SMA2[-2]: AO = 1 else: AO = 2 else: if xSMA1_SMA2[-1] > xSMA1_SMA2[-2]: AO = -1 else: AO = -2 # Calc breakouts break_down = crossunder( fast_ma, bb_basis) and close[-1] < bb_basis[-1] and AO < 0 # abs(AO)==2 break_up = crossover( fast_ma, bb_basis) and close[-1] > bb_basis[-1] and AO > 0 # abs(AO)==1 self._signal = 1 if break_up else -1 if break_down else 0 self._last_timestamp = timestamp return self._signal
def compute(self, timestamp, prices): self._prev = self._last # self._smas = SMAIndicator.SMA_n_sf(self._length, prices) # , self._step, self._filtering) # self._smas = MM_n(self._length, prices) self._smas = ta_SMA(prices, self._length) self._last = self._smas[-1] self._last_timestamp = timestamp return self._smas
def compute(self, timestamp, high, low, close): # Breakout Indicator Inputs bb_basis = ta_EMA(close, self._bb_L) if self._use_EMA else ta_SMA( close, self._bb_L) fast_ma = ta_EMA(close, self._fast_MA_L) # Calculate Awesome Oscillator hl2 = (high + low) * 0.5 xSMA1_hl2 = ta_SMA(hl2, self._awesome_fast_L) xSMA2_hl2 = ta_SMA(hl2, self._awesome_slow_L) xSMA1_SMA2 = xSMA1_hl2 - xSMA2_hl2 # Calculate direction of AO if xSMA1_SMA2[-1] >= 0: if xSMA1_SMA2[-1] > xSMA1_SMA2[-2]: AO = 1 else: AO = 2 else: if xSMA1_SMA2[-1] > xSMA1_SMA2[-2]: AO = -1 else: AO = -2 # Calc breakouts break_down = crossunder( fast_ma, bb_basis) and close[-1] < bb_basis[-1] and AO < 0 # abs(AO)==2 break_up = crossover( fast_ma, bb_basis) and close[-1] > bb_basis[-1] and AO > 0 # abs(AO)==1 self._signal = 1 if break_up else -1 if break_down else 0 self._last_timestamp = timestamp return self._signal
def HMA_n(N, data): N_2 = int(N / 2) N_sqrt = int(math.sqrt(N)) weights = np.array([x for x in range(1, len(data)+1)]) # 1) calculate a WMA with period n / 2 and multiply it by 2 # hma12 = 2 * MM_n(N_2, data*weights) / MM_n(N_2, weights) hma12 = 2 * ta_SMA(data*weights, N_2) / ta_SMA(weights, N_2) # 2) calculate a WMA for period n and subtract if from step 1 # hma12 = hma12 - (MM_n(N, data*weights) / MM_n(N, weights)) hma12 = hma12 - (ta_SMA(data*weights, N) / ta_SMA(weights, N)) # 3) calculate a WMA with period sqrt(n) using the data from step 2 # hma = (MM_n(N_sqrt, hma12*weights) / MM_n(N_sqrt, weights)) hma = (ta_SMA(hma12*weights, N_sqrt) / ta_SMA(weights, N_sqrt)) return hma
def compute(self, timestamp, timestamps, high, low, close): size = len(close) basis = ta_SMA(close, self._length) atrs = ta_ATR(high, low, close, timeperiod=self._length) dev = atrs * self._coeff upper = basis + dev lower = basis - dev bbr = (close - lower) / (upper - lower) bbe = ta_EMA(bbr, self._length_MA) if len(self._tup) != size: self._tup = np.zeros(size) self._tdn = np.zeros(size) for i in range(2, size): if bbe[i - 1] > bbe[i] and bbe[i - 2] < bbe[i - 1]: last = self._tup[i] = bbe[i] else: self._tup[i] = np.NaN for i in range(2, size): if bbe[i - 1] < bbe[i] and bbe[i - 2] < bbe[i - 1]: self._tdn[i] = bbe[i] else: self._tdn[i] = np.NaN highest = ta_MAX(high, 3) lowest = ta_MIN(low, 3) last_up = 0.0 last_dn = 0.0 for i in range(0, size): if not np.isnan(self._tup[i]): last_up = self._tup[i] = highest[i] else: self._tup[i] = last_up if not np.isnan(self._tdn[i]): last_dn = self._tdn[i] = lowest[i] else: self._tdn[i] = last_dn # compact timeserie delta = min(int((timestamp - self._last_timestamp) / self._timeframe), len(timestamps)) # base index num = len(timestamps) for b in range(num - delta, num): if timestamps[b] > self._last_timestamp: if b > 0: last_up = self._tup[b - 1] last_dn = self._tdn[b - 1] else: last_up = 0.0 last_dn = 0.0 if not np.isnan(self._tup[b]) and self._tup[b] != last_up: last_up = self._tup[b] self._up.append(last_up) self._both.append(last_up) if len(self._up) > self._max_history: self._up.pop(0) if len(self._both) > 2 * self._max_history: self._both.pop(0) if not np.isnan(self._tdn[b]) and self._tdn[b] != last_dn: last_dn = self._tdn[b] self._down.append(last_dn) self._both.append(last_dn) if len(self._down) > self._max_history: self._down.pop(0) if len(self._both) > 2 * self._max_history: self._both.pop(0) # logger.info("%s %s" % (self._tup, self._tdn)) # logger.info("%s %s" % (self._up, self._down)) # logger.info("%s" % (self._both)) self._last_timestamp = timestamp return self._up, self._down
def compute(self, timestamp, timestamps, high, low, close): size = len(close) basis = ta_SMA(close, self._length) atrs = ta_ATR(high, low, close, timeperiod=self._length) dev = atrs * self._coeff upper = basis + dev lower = basis - dev with np.errstate(divide='ignore', invalid='ignore'): # replace by 0 when divisor is 0 bbr = (close - lower) / (upper - lower) bbr[bbr == np.inf] = 0.0 bbe = ta_EMA(bbr, self._length_MA) if len(self._tup) != size: self._tup = np.zeros(size) self._tdn = np.zeros(size) self._tup[0] = self._tup[1] = np.NaN self._tdn[0] = self._tdn[1] = np.NaN for i in range(2, size): if bbe[i - 1] > bbe[i] and bbe[i - 2] < bbe[i - 1]: self._tup[i] = bbe[i] else: self._tup[i] = np.NaN for i in range(2, size): if bbe[i - 1] < bbe[i] and bbe[i - 2] > bbe[i - 1]: self._tdn[i] = bbe[i] else: self._tdn[i] = np.NaN highest = ta_MAX(high, 3) lowest = ta_MIN(low, 3) last_up = 0.0 last_dn = 0.0 for i in range(2, size): if not np.isnan(self._tup[i]): last_up = self._tup[i] = highest[i] elif last_up > 0.0: self._tup[i] = last_up if not np.isnan(self._tdn[i]): last_dn = self._tdn[i] = lowest[i] elif last_dn > 0.0: self._tdn[i] = last_dn # logger.debug("%s %s %s" % (self._tdn[-3], self._tdn[-2], self._tdn[-1])) # logger.debug("%s - %s %s / %s %s %s / %s %s %s / %s %s %s / %s %s %s" % ( # datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'), # datetime.utcfromtimestamp(timestamps[-2]).strftime('%Y-%m-%d %H:%M:%S'), # datetime.utcfromtimestamp(timestamps[-1]).strftime('%Y-%m-%d %H:%M:%S'), # self._tdn[-3], self._tdn[-2], self._tdn[-1], close[-3], close[-2], close[-1], low[-3], low[-2], low[-1], lowest[-3], lowest[-2], lowest[-1])) # compact timeserie from_timestamp = Instrument.basetime(self.timeframe, self._last_timestamp) # inclusive to_timestamp = Instrument.basetime(self.timeframe, timestamp) # exclusive delta = min( int((to_timestamp - from_timestamp) / self._timeframe) + 1, len(timestamps)) # base index num = len(timestamps) last_up = self._up[-1] if len(self._up) else np.NaN last_dn = self._down[-1] if len(self._down) else np.NaN # if len(self._tdn) and not np.isnan(self._tdn[-1]) and self._tdn[-1] != last_dn: # self._down.append(self._tdn[-1]) # last_dn = self._tdn[-1] # logger.info("> %s %s" % (datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'), last_dn)) # if len(self._tup) and not np.isnan(self._tup[-1]) and self._tup[-1] != last_up: # self._up.append(self._tup[-1]) for b in range(num - delta, num): # only most recent and complete if from_timestamp <= timestamps[b] < to_timestamp: if not np.isnan( self._tup[b] ) and self._tup[b] != last_up and self._tup[b] > 0.0: last_up = self._tup[b] self._up.append(last_up) self._both.append(last_up) if len(self._up) > self._max_history: self._up.pop(0) if len(self._both) > 2 * self._max_history: self._both.pop(0) if not np.isnan( self._tdn[b] ) and self._tdn[b] != last_dn and self._tdn[b] > 0.0: last_dn = self._tdn[b] # logger.info("%s %s" % (datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'), last_dn)) self._down.append(last_dn) self._both.append(last_dn) if len(self._down) > self._max_history: self._down.pop(0) if len(self._both) > 2 * self._max_history: self._both.pop(0) # if self.timeframe == 60: # logger.info("%s %s" % (self._tup, self._tdn)) # logger.info("%s %s" % (self._up, self._down)) # if self.timeframe == 60*60*24: # logger.info("%s" % (self._both)) if len(atrs): # retains last ATR value self._last_atr = atrs[-1] self._last_timestamp = timestamp return self._up, self._down