Skip to content

Commit

Permalink
Merge pull request #23 from hummingbot/feat/adapt_to_candles_improvem…
Browse files Browse the repository at this point in the history
…ents

Feat/adapt to candles improvements
  • Loading branch information
cardosofede authored Jul 12, 2024
2 parents 97a4d0d + e91e51b commit 51b3ea3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 117 deletions.
21 changes: 16 additions & 5 deletions routers/manage_backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from hummingbot.data_feed.candles_feed.candles_factory import CandlesFactory
from pydantic import BaseModel

from services.backtesting_engine import BacktestingEngine, DirectionalTradingBacktesting, MarketMakingBacktesting
from hummingbot.strategy_v2.backtesting.backtesting_engine_base import BacktestingEngineBase
from hummingbot.strategy_v2.backtesting.controllers_backtesting.directional_trading_backtesting import DirectionalTradingBacktesting
from hummingbot.strategy_v2.backtesting.controllers_backtesting.market_making_backtesting import MarketMakingBacktesting

from config import CONTROLLERS_MODULE, CONTROLLERS_PATH

router = APIRouter(tags=["Market Backtesting"])
candles_factory = CandlesFactory()
Expand All @@ -18,8 +22,8 @@


class BacktestingConfig(BaseModel):
start_time: int = 1672542000000 # 2023-01-01 00:00:00
end_time: int = 1672628400000 # 2023-01-01 23:59:00
start_time: int = 1672542000 # 2023-01-01 00:00:00
end_time: int = 1672628400 # 2023-01-01 23:59:00
backtesting_resolution: str = "1m"
trade_cost: float = 0.0006
config: Union[Dict, str]
Expand All @@ -29,9 +33,16 @@ class BacktestingConfig(BaseModel):
async def run_backtesting(backtesting_config: BacktestingConfig):
try:
if isinstance(backtesting_config.config, str):
controller_config = BacktestingEngine.get_controller_config_instance_from_yml(backtesting_config.config)
controller_config = BacktestingEngineBase.get_controller_config_instance_from_yml(
config_path=backtesting_config.config,
controllers_conf_dir_path=CONTROLLERS_PATH,
controllers_module=CONTROLLERS_MODULE
)
else:
controller_config = BacktestingEngine.get_controller_config_instance_from_dict(backtesting_config.config)
controller_config = BacktestingEngineBase.get_controller_config_instance_from_dict(
config_data=backtesting_config.config,
controllers_module=CONTROLLERS_MODULE
)
backtesting_engine = BACKTESTING_ENGINES.get(controller_config.controller_type)
if not backtesting_engine:
raise ValueError(f"Backtesting engine for controller type {controller_config.controller_type} not found.")
Expand Down
22 changes: 3 additions & 19 deletions routers/manage_market_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class HistoricalCandlesConfig(BaseModel):
connector_name: str = "binance_perpetual"
trading_pair: str = "BTC-USDT"
interval: str = "3m"
start_time: int = 1672542000000 # 2023-01-01 00:00:00
end_time: int = 1672628400000 # 2023-01-01 23:59:00
start_time: int = 1672542000
end_time: int = 1672628400


@router.post("/real-time-candles")
Expand All @@ -43,22 +43,6 @@ async def get_historical_candles(config: HistoricalCandlesConfig):
interval=config.interval
)
candles = candles_factory.get_candle(candles_config)
all_candles = []
current_start_time = config.start_time

while current_start_time <= config.end_time:
fetched_candles = await candles.fetch_candles(start_time=current_start_time)
if fetched_candles.size < 1:
break

all_candles.append(fetched_candles)
last_timestamp = fetched_candles[-1][0] # Assuming the first column is the timestamp
current_start_time = int(last_timestamp)

final_candles = np.concatenate(all_candles, axis=0) if all_candles else np.array([])
candles_df = pd.DataFrame(final_candles, columns=candles.columns)
candles_df.drop_duplicates(subset=["timestamp"], inplace=True)
candles_df["timestamp"] = candles_df["timestamp"] // 1e3
return candles_df
return await candles.get_historical_candles(config=config)
except Exception as e:
return {"error": str(e)}
93 changes: 0 additions & 93 deletions services/backtesting_engine.py

This file was deleted.

0 comments on commit 51b3ea3

Please sign in to comment.