Skip to content

Main

cli

cli(ctx: click.Context, debug: bool) -> None

BackLive CLI.

Source code in backlive/cli.py
11
12
13
14
15
16
17
18
19
@click.group()
@click.option("--debug/--no-debug", default=False, show_default=True, help="Enable debug mode.")
@click.pass_context
def cli(ctx: click.Context, debug: bool) -> None:
    """BackLive CLI."""
    ctx.ensure_object(dict)
    ctx.obj["DEBUG"] = debug
    if debug:
        click.echo("Debug mode is on.")

fetch

fetch(ctx: click.Context, url: str, symbol: str, start: datetime, end: datetime, limit: int, interval: str) -> None

Fetch data and save to the database.

Source code in backlive/cli.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@cli.command()
@click.option("--url", default="sqlite:///tickers.db", show_default=True, help="Database URL.")
@click.option("--symbol", required=True, help="Stock symbol (e.g., AAPL).")
@click.option("--start", type=click.DateTime(formats=["%Y-%m-%d"]), show_default=True, help="Start date (YYYY-MM-DD).")
@click.option("--end", type=click.DateTime(formats=["%Y-%m-%d"]), show_default=True, help="End date (YYYY-MM-DD).")
@click.option("--limit", type=int, default=10, show_default=True, help="Number of records to fetch.")
@click.option("--interval", default="1d", show_default=True, help="Interval between data points (e.g., 1d, 1h).")
@click.pass_context
def fetch(ctx: click.Context, url: str, symbol: str, start: datetime, end: datetime, limit: int, interval: str) -> None:
    """Fetch data and save to the database."""
    if ctx.obj["DEBUG"]:
        click.echo(f"Fetching data for {symbol} from {start} to {end} with interval {interval}")

    feed = YFinanceFeed()
    uow = UnitOfWork(url)

    ticker_service = TickerService(uow, feed)
    ticker = ticker_service.fetch_and_save_candles(symbol)

    click.echo(f"Saved ticker: {ticker.symbol} with {len(ticker.candles)} candle records")

init

init(ctx: click.Context, url: str) -> None

Initialize the database.

Source code in backlive/cli.py
22
23
24
25
26
27
28
29
30
31
32
@cli.command()
@click.option("--url", default="sqlite:///tickers.db", show_default=True, help="Database URL.")
@click.pass_context
def init(ctx: click.Context, url: str) -> None:
    """Initialize the database."""
    if ctx.obj["DEBUG"]:
        click.echo(f"Initializing database at {url}")

    db_initializer = DatabaseInitializer(url)
    db_initializer.initialize_database()
    click.echo("Database initialized successfully.")