|
| 1 | +import asyncio |
| 2 | +import datetime |
| 3 | +import logging |
| 4 | +import pathlib |
| 5 | + |
| 6 | +import polars as pl |
| 7 | +import structlog |
| 8 | + |
| 9 | +from ziplime.utils.bundle_utils import get_bundle_service |
| 10 | +from ziplime.utils.logging_utils import configure_logging |
| 11 | + |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytz |
| 15 | + |
| 16 | +from ziplime.core.ingest_data import get_asset_service |
| 17 | +from ziplime.core.run_simulation import run_simulation_iter |
| 18 | +from ziplime.finance.commission import PerShare, DEFAULT_PER_SHARE_COST, DEFAULT_MINIMUM_COST_PER_EQUITY_TRADE |
| 19 | + |
| 20 | +logger = structlog.get_logger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +async def _run_simulation(): |
| 24 | + start_date = datetime.datetime(year=2025, month=1, day=3, tzinfo=pytz.timezone("America/New_York")) |
| 25 | + end_date = datetime.datetime(year=2025, month=2, day=1, tzinfo=pytz.timezone("America/New_York")) |
| 26 | + |
| 27 | + bundle_service = get_bundle_service() |
| 28 | + |
| 29 | + asset_service = get_asset_service( |
| 30 | + clear_asset_db=False, |
| 31 | + db_path=str(pathlib.Path(__file__).parent.parent.resolve().joinpath("data", "assets.sqlite")) |
| 32 | + ) |
| 33 | + # Use aggregations if you ingested data of frequnecy less than 1 day |
| 34 | + aggregations = [ |
| 35 | + pl.col("open").first(), |
| 36 | + pl.col("high").max(), |
| 37 | + pl.col("low").min(), |
| 38 | + pl.col("close").last(), |
| 39 | + pl.col("volume").sum(), |
| 40 | + pl.col("symbol").last() |
| 41 | + ] |
| 42 | + market_data_bundle = await bundle_service.load_bundle(bundle_name="limex_us_minute_data", |
| 43 | + bundle_version=None, |
| 44 | + frequency=datetime.timedelta(days=1), |
| 45 | + start_date=start_date, |
| 46 | + end_date=end_date, |
| 47 | + symbols=["META", "AAPL", "AMZN", "NFLX", "GOOGL", |
| 48 | + ], |
| 49 | + start_auction_delta=datetime.timedelta(minutes=15), |
| 50 | + end_auction_delta=datetime.timedelta(minutes=15), |
| 51 | + aggregations=aggregations, |
| 52 | + ) |
| 53 | + |
| 54 | + custom_data_sources = [] |
| 55 | + # custom_data_sources.append( |
| 56 | + # await bundle_service.load_bundle(bundle_name="limex_us_fundamental_data", bundle_version=None)) |
| 57 | + |
| 58 | + equity_commission = PerShare( |
| 59 | + cost=DEFAULT_PER_SHARE_COST, |
| 60 | + min_trade_cost=DEFAULT_MINIMUM_COST_PER_EQUITY_TRADE, |
| 61 | + |
| 62 | + ) |
| 63 | + |
| 64 | + # run daily simulation |
| 65 | + async for status in run_simulation_iter( |
| 66 | + start_date=start_date, |
| 67 | + end_date=end_date, |
| 68 | + trading_calendar="NYSE", |
| 69 | + algorithm_file=str(Path("algorithms/test_algo/test_algo.py").absolute()), |
| 70 | + total_cash=100000.0, |
| 71 | + market_data_source=market_data_bundle, |
| 72 | + custom_data_sources=custom_data_sources, |
| 73 | + config_file=str(Path("algorithms/test_algo/test_algo_config.json").absolute()), |
| 74 | + emission_rate=datetime.timedelta(days=1), |
| 75 | + benchmark_asset_symbol="AAPL", |
| 76 | + benchmark_returns=None, |
| 77 | + stop_on_error=True, |
| 78 | + asset_service=asset_service, |
| 79 | + equity_commission=equity_commission |
| 80 | + ): |
| 81 | + print(status) |
| 82 | + if status.errors: |
| 83 | + logger.error(status.errors) |
| 84 | + if status.result: |
| 85 | + logger.info("Algorithm finished") |
| 86 | + # print(status.perf.head(n=10).to_markdown()) |
| 87 | + |
| 88 | + # Get cash from algo |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + configure_logging(level=logging.INFO, file_name="mylog.log") |
| 93 | + asyncio.run(_run_simulation()) |
0 commit comments