def prepare_data(symbol): coin = symbol coin_1 = crypto_data(coin) df = coin_1 df['SMA_20'] = sma(df['Close'], 20) df['SMA_50'] = sma(df['Close'], 50) df['EMA_20'] = ema(df['Close'], 20) df['EMA_50'] = ema(df['Close'], 50) df['MACD'] = macd(df['Close'], 26, 12) df['per_k_stoch_10'] = percent_k(df['Close'], 10) df['per_d_stoch_10'] = percent_d(df['Close'], 10) df['OBV'] = obv(df['Close'], df['Volume']) fp = [] for price in df['Close']: fp.append(price) fp.pop(0) fp.append(df['Close'].mean()) df['FP'] = fp df_predict = df.tail(1) df.drop(df.tail(1).index, inplace=True) label = [] for i in range(len(df)): if df['FP'][i] > df['Close'][i]: label.append(1) else: label.append(0) df['goes_up'] = label df = df.drop(['FP'], axis=1) df = df.fillna(df.mean()) df_predict = df_predict.drop(['FP'], axis=1) return df, df_predict
# %matplotlib inline from matplotlib import style style.use('ggplot') pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) socket = fxcmpy.fxcmpy(config_file='fxcm.cfg') data = socket.get_candles(instrument='GBP/USD', period='D1', start=dt.datetime(2015, 1, 1), end=dt.datetime(2018, 6, 10)) data['percent_k'] = percent_k(data['askclose'], 20) data['percent_d'] = percent_d(data['askclose'], 20) fig = plt.figure(figsize=(12, 8)) ax1 = fig.add_subplot(111, xlabel='Date', ylabel='Close') data['askclose'].plot(ax=ax1, color='r', lw=1) ss_plot = plt.figure(figsize=(12, 8)) ax3 = ss_plot.add_subplot(111, ylabel='Percent') data['percent_k'].plot(ax=ax3, color='r') data['percent_d'].plot(ax=ax3, color='g') data['ovr'] = .80 data['ovr'].plot( ax=ax3, color='b', ) data['blw'] = .20
def test_percent_d_invalid_period(self): period = 128 with self.assertRaises(Exception) as cm: stochastic.percent_d(self.high_data, self.low_data, self.close_data, period) expected = "Error: data_len < period" self.assertEqual(str(cm.exception), expected)
def test_percent_d_period_10(self): period = 10 percent_d = stochastic.percent_d(self.high_data, self.low_data, self.close_data, period) np.testing.assert_array_equal(percent_d, self.percent_d_period_10_expected)
def test_percent_d_period_6(self): period = 6 percent_d = stochastic.percent_d(self.high_data, self.low_data, self.close_data, period) print (percent_d.tolist()) np.testing.assert_array_equal(percent_d, self.percent_d_period_6_expected)
def test_percent_d_period_8(self): period = 8 percent_d = stochastic.percent_d(self.data, period) np.testing.assert_array_equal(percent_d, self.percent_d_period_8_expected)