def is1hSMA50200Bull(self): try: if self.isSimulation() and isinstance(self.sma50200_1h_cache, pd.DataFrame): df_data = self.sma50200_1h_cache if self.exchange == 'coinbasepro': api = CBPublicAPI() df_data = api.getHistoricalData(self.market, 3600) self.sma50200_1h_cache = df_data elif self.exchange == 'binance': api = BPublicAPI() df_data = api.getHistoricalData(self.market, '1h') self.sma50200_1h_cache = df_data else: return False ta = TechnicalAnalysis(df_data) if 'sma50' not in df_data: ta.addSMA(50) if 'sma200' not in df_data: ta.addSMA(200) df_last = ta.getDataFrame().copy().iloc[-1, :] df_last['bull'] = df_last['sma50'] > df_last['sma200'] return bool(df_last['bull']) except Exception: return False
def is6hSMA50200Bull(self): try: if self.exchange == 'coinbasepro': api = CBPublicAPI() df_data = api.getHistoricalData(self.market, 21600) elif self.exchange == 'binance': api = BPublicAPI() df_data = api.getHistoricalData(self.market, '6h') else: return False ta = TechnicalAnalysis(df_data) ta.addSMA(50) ta.addSMA(200) df_last = ta.getDataFrame().copy().iloc[-1, :] df_last['bull'] = df_last['sma50'] > df_last['sma200'] return bool(df_last['bull']) except Exception: return False
def is1hSMA50200Bull(self, iso8601end: str = ''): try: if self.isSimulation() and isinstance(self.sma50200_1h_cache, pd.DataFrame): df_data = self.sma50200_1h_cache[( self.sma50200_1h_cache['date'] <= iso8601end)] elif self.exchange == 'coinbasepro': api = CBPublicAPI() df_data = api.getHistoricalData(self.market, 3600) self.sma50200_1h_cache = df_data elif self.exchange == 'binance': api = BPublicAPI() df_data = api.getHistoricalData(self.market, '1h') self.sma50200_1h_cache = df_data else: return False ta = TechnicalAnalysis(df_data) if 'sma50' not in df_data: ta.addSMA(50) if 'sma200' not in df_data: ta.addSMA(200) df_last = ta.getDataFrame().copy().iloc[-1, :] Logger.debug("---- SMA50200 1H Check----") if self.isSimulation(): Logger.debug("simdate: " + str(df_last['date'])) Logger.debug("sma50 1h: " + str(df_last['sma50'])) Logger.debug("sma200 1h: " + str(df_last['sma200'])) Logger.debug("bull 1h: " + str(df_last['sma50'] > df_last['sma200'])) df_last['bull'] = df_last['sma50'] > df_last['sma200'] return bool(df_last['bull']) except Exception: return False
def isCryptoRecession(self): try: if self.exchange == 'coinbasepro': api = CBPublicAPI() df_data = api.getHistoricalData(self.market, 86400) elif self.exchange == 'binance': api = BPublicAPI() df_data = api.getHistoricalData(self.market, '1d') else: return False # if there is an API issue, default to False to avoid hard sells if len(df_data) <= 200: return False # if there is unsufficient data, default to False to avoid hard sells ta = TechnicalAnalysis(df_data) ta.addSMA(50) ta.addSMA(200) df_last = ta.getDataFrame().copy().iloc[-1, :] df_last['crypto_recession'] = df_last['sma50'] < df_last['sma200'] return bool(df_last['crypto_recession']) except Exception: return False
def test_should_calculate_addSMA_20(): """ Add the Simple Moving Average (SMA) to the DataFrame : """ # GIVEN a series of values closes_list = [ 0.0003, 0.0004, 0.0010, 0.0020, 0.0009, 0.0008, 0.0009, 0.0010, 0.0012, 0.0015, 0.0025, 0.0015, 0.0014, 0.0016, 0.0030, 0.0032, 0.0035, 0.0024, 0.0023, 0.0022, 0.0021, 0.0020 ] df = pd.DataFrame({ 'date': [ '2021-10-10 14:30:00', '2021-10-10 14:31:00', '2021-10-10 14:32:00', '2021-10-10 14:33:00', '2021-10-10 14:34:00', '2021-10-10 14:35:00', '2021-10-10 14:36:00', '2021-10-10 14:37:00', '2021-10-10 14:38:00', '2021-10-10 14:39:00', '2021-10-10 14:40:00', '2021-10-10 14:41:00', '2021-10-10 14:42:00', '2021-10-10 14:43:00', '2021-10-10 14:44:00', '2021-10-10 14:45:00', '2021-10-10 14:46:00', '2021-10-10 14:47:00', '2021-10-10 14:48:00', '2021-10-10 14:49:00', '2021-10-10 14:50:00', '2021-10-10 14:51:00' ], 'close': closes_list }) df['date'] = pd.to_datetime(df['date'], format="%Y-%d-%m %H:%M:%S") df.set_index(['date']) ta = TechnicalAnalysis(df) # WHEN calculate the cumulative moving average 20 ta.addSMA(20) # THEN actual = ta.getDataFrame() expected = pd.DataFrame({ 'date': [ '2021-10-10 14:30:00', '2021-10-10 14:31:00', '2021-10-10 14:32:00', '2021-10-10 14:33:00', '2021-10-10 14:34:00', '2021-10-10 14:35:00', '2021-10-10 14:36:00', '2021-10-10 14:37:00', '2021-10-10 14:38:00', '2021-10-10 14:39:00', '2021-10-10 14:40:00', '2021-10-10 14:41:00', '2021-10-10 14:42:00', '2021-10-10 14:43:00', '2021-10-10 14:44:00', '2021-10-10 14:45:00', '2021-10-10 14:46:00', '2021-10-10 14:47:00', '2021-10-10 14:48:00', '2021-10-10 14:49:00', '2021-10-10 14:50:00', '2021-10-10 14:51:00' ], 'close': closes_list, 'sma20': [ calculate_mean_on_range(0, 1, closes_list), calculate_mean_on_range(0, 2, closes_list), calculate_mean_on_range(0, 3, closes_list), calculate_mean_on_range(0, 4, closes_list), calculate_mean_on_range(0, 5, closes_list), calculate_mean_on_range(0, 6, closes_list), calculate_mean_on_range(0, 7, closes_list), calculate_mean_on_range(0, 8, closes_list), calculate_mean_on_range(0, 9, closes_list), calculate_mean_on_range(0, 10, closes_list), calculate_mean_on_range(0, 11, closes_list), calculate_mean_on_range(0, 12, closes_list), calculate_mean_on_range(0, 13, closes_list), calculate_mean_on_range(0, 14, closes_list), calculate_mean_on_range(0, 15, closes_list), calculate_mean_on_range(0, 16, closes_list), calculate_mean_on_range(0, 17, closes_list), calculate_mean_on_range(0, 18, closes_list), calculate_mean_on_range(0, 19, closes_list), calculate_mean_on_range(0, 20, closes_list), calculate_mean_on_range(1, 21, closes_list), calculate_mean_on_range(2, 22, closes_list) ] }) expected['date'] = pd.to_datetime(df['date'], format="%Y-%d-%m %H:%M:%S") expected.set_index(['date']) assert_frame_equal(actual, expected)