def __init__(self, duration="h", num_data=300, backtest=False, product_code="BTC_JPY"): self.product_code = product_code self.ticker = get_data.Ticker(key.api_key, key.api_secret, code=self.product_code) self.duration = duration self.backtest = backtest if self.backtest is True: self.datas = Candle_BackTest self.num_data = self.datas.objects.all().count() else: self.datas = eval("Candle_1" + duration + self.product_code) self.num_data = num_data self.t = ai.Technical(candles=self.datas, product_code=self.product_code) self.df = self.MakeDf() self.fig = make_subplots(rows=3, shared_xaxes=True, row_heights=[0.6, 0.2, 0.2], specs=[[{ "secondary_y": True }], [{}], [{}]])
def test_EMA(self): """[summary]calculate EMA from models.Candle_1h timeperiod=10 Returns: [type]: [description] """ createdf.connectandsave() timeperiod = 5 t = ai.Technical(eval("Candle_1h")) ema = t.Ema(timeperiod=timeperiod) element_num = len(t.candle_dict["close"]) self.assertEqual(ema.shape, (element_num, ))
def test_init(self): """[summary]testing class returns dict Returns: [type]dict: [description] {time:... ,open:...,close:..., high:...,low:... } """ t = ai.Technical(eval("Candle_1h")) keys = [ "time", "product_code", "open", "close", "high", "low", "volume" ] t_keys = list(t.candle_dict.keys()) self.assertEqual(keys, t_keys)
def test_ATR(self): createdf.connectandsave() t = ai.Technical(eval("Candle_1h")) atr = t.ATR() element_num = len(t.candle_dict["close"]) times = np.array(t.candle_dict["time"]).reshape(-1, ) fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(2, 1, 2) ax1.plot(times, atr, label="ATR") ax2.plot(times, t.close, label="CLOSE") ax1.legend() ax2.legend() plt.show() self.assertEqual(atr.shape, (element_num, ))
def test_Macd(self): """[summary]calculate Macd from models.Candle_1h timeperiods=12,26,9. Check return arraies shape Returns: [type]: [description] """ createdf.connectandsave() t = ai.Technical(eval("Candle_1h")) macd, macdsignal, macdhist = t.Macd() element_num = len(t.candle_dict["close"]) self.assertEqual(macd.shape, (element_num, )) self.assertEqual(macd.shape, macdsignal.shape) self.assertEqual(macd.shape, macdhist.shape)
def test_Ichimoku(self): """[summary]calculate Hv from models.Candle_1h timeperiods=5, ndev=1 Check return larger than zero Returns: [type]: [description] """ createdf.connectandsave() t = ai.Technical(eval("Candle_1h")) tenkan, kijun, senkouA, senkouB, chikou = t.Ichimoku() element_num = len(t.candle_dict["close"]) self.assertEqual(tenkan.shape, (element_num, )) self.assertEqual(tenkan.shape, kijun.shape) self.assertEqual(tenkan.shape, senkouA.shape) self.assertEqual(tenkan.shape, senkouB.shape) self.assertEqual(tenkan.shape, chikou.shape)
def test_Hv(self): """[summary]calculate Hv from models.Candle_1h timeperiods=5, ndev=1 Check return larger than zero Returns: [type]: [description] """ createdf.connectandsave() t = ai.Technical(eval("Candle_1h")) timeperiod = 5 ndev = 1 value = t.Hv(timeperiod, ndev)[timeperiod:] lager_zero = np.all(value > 0) self.assertEqual(lager_zero, True)
def test_Bbands(self): """[summary]calculate Bbands from models.Candle_1h timeperiod=5. Check return arraies shape, upperbound > lowerbound Returns: [type]: [description] """ createdf.connectandsave() t = ai.Technical(eval("Candle_1h")) timeperiod = 5 upperband, middleband, lowerband = t.Bbands(timeperiod=timeperiod) element_num = len(t.candle_dict["close"]) is_one = 1 * (upperband > lowerband)[timeperiod:] is_one = np.prod(is_one) self.assertEqual(upperband.shape, (element_num, )) self.assertEqual(upperband.shape, lowerband.shape) self.assertEqual(is_one, 1)
def test_Rsi(self): """[summary]calculate Rsi from models.Candle_1h timeperiods=14 Check return array shape, values in [0,100] Returns: [type]: [description] """ createdf.connectandsave() t = ai.Technical(eval("Candle_1h")) timeperiod = 14 values = t.Rsi(timeperiod) real_values = values[timeperiod:] element_num = len(t.candle_dict["close"]) lager_zero = np.all(0 <= real_values) lower_hundred = np.all(100 >= real_values) self.assertEqual(values.shape, (element_num, )) self.assertEqual(lager_zero, True) self.assertEqual(lower_hundred, True)