Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/adapt to candles improvements #23

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.