def test_typical_price_invalid_data(self): self.close_data.append(1) with self.assertRaises(Exception) as cm: typical_price.typical_price(self.close_data, self.high_data, self.low_data) expected = ( "Error: mismatched data lengths, check to ensure that all input data is the same length and valid" ) self.assertEqual(str(cm.exception), expected)
def money_flow_index(close_data, high_data, low_data, volume, period): """ Money Flow Index. Formula: MFI = 100 - (100 / (1 + PMF / NMF)) """ catch_errors.check_for_input_len_diff( close_data, high_data, low_data, volume ) catch_errors.check_for_period_error(close_data, period) mf = money_flow(close_data, high_data, low_data, volume) tp = typical_price(close_data, high_data, low_data) flow = [tp[idx] > tp[idx-1] for idx in range(1, len(tp))] pf = [mf[idx] if flow[idx] else 0 for idx in range(0, len(flow))] nf = [mf[idx] if not flow[idx] else 0 for idx in range(0, len(flow))] pmf = [sum(pf[idx+1-period:idx+1]) for idx in range(period-1, len(pf))] nmf = [sum(nf[idx+1-period:idx+1]) for idx in range(period-1, len(nf))] # Dividing by 0 is not an issue, it turns the value into NaN which we would # want in that case with warnings.catch_warnings(): warnings.simplefilter("ignore", category=RuntimeWarning) money_ratio = np.array(pmf) / np.array(nmf) mfi = 100 - (100 / (1 + money_ratio)) mfi = fill_for_noncomputable_vals(close_data, mfi) return mfi
def center_band(close_data, high_data, low_data, period): """ Center Band. Formula: CB = SMA(TP) """ tp = typical_price(close_data, high_data, low_data) cb = sma(tp, period) return cb
def commodity_channel_index(close_data, high_data, low_data, period): """ Commodity Channel Index. Formula: CCI = (TP - SMA(TP)) / (0.015 * Mean Deviation) """ catch_errors.check_for_input_len_diff(close_data, high_data, low_data) catch_errors.check_for_period_error(close_data, period) tp = typical_price(close_data, high_data, low_data) cci = ((tp - sma(tp, period)) / (0.015 * np.mean(np.absolute(tp - np.mean(tp))))) return cci
def center_band(close_data, high_data, low_data, period, exp=False): """ Center Band. Formula: CB = SMA(TP) """ tp = typical_price(close_data, high_data, low_data) if exp: cb = ema(tp, period) else: cb = sma(tp, period) return cb
def test_typical_price(self): tp = typical_price.typical_price(self.close_data, self.high_data, self.low_data) np.testing.assert_array_equal(tp, self.tp_expected)