Candles allow user to compose a trailing window of real-time market data in OHLCV (Open, High, Low, Close, Volume) form from certain supported exchanges.
It combines historical and real-time data to generate and maintain this window, allowing users to create custom technical indicators, leveraging pandas_ta.
Incorporate technical indicators to candle data for enhanced strategy insights:
defformat_status(self)->str:# Ensure market connectors are readyifnotself.ready_to_trade:return"Market connectors are not ready."lines=[]ifself.all_candles_ready:# Loop through each candle setforcandlesin[self.eth_1w_candles,self.eth_1m_candles,self.eth_1h_candles]:candles_df=candles.candles_df# Add RSI, BBANDS, and EMA indicatorscandles_df.ta.rsi(length=14,append=True)candles_df.ta.bbands(length=20,std=2,append=True)candles_df.ta.ema(length=14,offset=None,append=True)# Format and display candle datalines.extend([f"Candles: {candles.name} | Interval: {candles.interval}"])lines.extend([" "+lineforlineincandles_df.tail().to_string(index=False).split("\n")])else:lines.append(" No data collected.")return"\n".join(lines)
For strategies requiring multiple candle intervals or trading pairs, initialize separate instances:
fromhummingbot.data_feed.candles_feed.candles_factoryimportCandlesFactory,CandlesConfigclassInitializingCandlesExample(ScriptStrategyBase):# Configure two different sets of candlescandles_config_1=CandlesConfig(connector="binance",trading_pair="BTC-USDT",interval="3m")candles_config_2=CandlesConfig(connector="binance_perpetual",trading_pair="ETH-USDT",interval="1m")# Initialize candles using the configurationscandles_1=CandlesFactory.get_candle(candles_config_1)candles_2=CandlesFactory.get_candle(candles_config_2)
Modify the format_status method to display candlestick data:
defformat_status(self)->str:# Check if trading is readyifnotself.ready_to_trade:return"Market connectors are not ready."lines=["\n############################################ Market Data ############################################\n"]# Check if the candle data is readyifself.eth_1h_candles.is_ready:# Format and display the last few candle recordscandles_df=self.eth_1h_candles.candles_dfcandles_df["timestamp"]=pd.to_datetime(candles_df["timestamp"],unit="ms").dt.strftime('%Y-%m-%d %H:%M:%S')display_columns=["timestamp","open","high","low","close"]formatted_df=candles_df[display_columns].tail()lines.append("One-hour Candles for ETH-USDT:")lines.append(formatted_df.to_string(index=False))else:lines.append(" One-hour candle data is not ready.")return"\n".join(lines)