Wiring the Trading Engine Into SQLite Logging | Toss Auto-Trading Dev Journal #11 (Phase 3-3)

🌐 ν•œκ΅­μ–΄λ‘œ 보기

πŸ“š Toss Auto-Trading Dev Journal (Phase 3 / 11 posts so far)

  • Phase 0 β€” Foundation (#0)
  • Phase 0-1 β€” Currency unit bug (#0-1)
  • Phase 1 β€” Config split & retry logic (#3)
  • Phase 1-2 β€” Config/retry follow-up (#4)
  • Phase 1-3 β€” Adding pytest (#5)
  • Phase 1-4 β€” Log rotation (#6)
  • Phase 2-1 β€” Strategy class abstraction (#7)
  • Phase 2-2 β€” Multi-symbol monitoring (#8)
  • Phase 3-1 β€” SQLite schema design (#9)
  • Phase 3-2 β€” db.py implementation (#10)
  • Phase 3-3 β€” Wiring it into the engine (this post, #11)

πŸ’‘ Phase 3-3 at a glance

  • Goal: Connect the db.py module (built last session) to the live trading engine and portfolio dashboard, closing out Phase 3
  • Key decision: send_notification() now takes level/symbol params so every alert lands in the alerts table regardless of whether Telegram is configured
  • Verification: mocked simulation + local run, full pytest suite at 58 passed

db.py itself was done last time, so I figured this part would be easy. Turns out deciding *when* to call init_db() without opening redundant connections took more thought than expected.

Calling init_db() at engine startup

The fix was simple in the end: call db.init_db() once at the top of auto_trader.py’s entry point. That way, even if trading_history.db doesn’t exist yet, the bot never hits a sqlite3.OperationalError: no such table: trades. toss_portfolio.py got the same treatment at its own startup.

A quick note on Korean brokerage context

For readers unfamiliar with the setup: this bot trades through Toss Securities’ brokerage API, a Korean-market equivalent of something like Alpaca or Interactive Brokers, with prices tracked in KRW. That context matters here because the dashboard now snapshots both KRW and USD prices into the same price_snapshots table.

Adding level/symbol to send_notification()

Previously, send_notification() just printed to console if Telegram wasn’t configured. Now it takes a level (INFO/WARNING/ERROR) and an optional symbol, and every call writes to the alerts table regardless of whether a message actually goes out over Telegram. Separating “was it sent” from “was it recorded” turned out to be the important distinction.

Tagging trades with strategy_name

execute_order() now also logs which strategy class (the abstract one from Phase 2-1) triggered a given simulated buy. The real order API call is still commented out, so I left a comment marking exactly where the same logging call needs to go once live trading is switched on.

Verification: mock run, then local run

First, a mocked run with two placeholder symbols (one instant buy, one dropped for a simulated network error) confirmed trade and alert records were correct. Then I ran auto_trader.py locally for real, watching four symbols for a few seconds, and queried the resulting trading_history.db directly to confirm an “engine started” alert landed with the right level and timestamp. Full pytest suite afterward: 58 passed, with no changes to any safety-related values.

Phase 0 through 3, done β€” backtesting is next

That closes out Phases 0 through 3. As price_snapshots keeps accumulating, Phase 4 will use that history to backtest strategies against past prices instead of just watching the market live.

πŸ‘‰ See the full series index above, or check back for the Phase 4 backtesting write-up once it’s underway.

3 thoughts on “Wiring the Trading Engine Into SQLite Logging | Toss Auto-Trading Dev Journal #11 (Phase 3-3)”

  1. Pingback: SQLite μ—°λ™μœΌλ‘œ μ™„μ„±ν•œ Phase 3, λ§€λ§€Β·μ•Œλ¦Ό μžλ™ 기둝기 | ν† μŠ€ μžλ™λ§€λ§€ 개발기 #11 (Phase 3-3) - TS ν€€νŠΈ: 주식 μžλ™λ§€λ§€ μ—°κ΅¬μ†Œ

  2. Pingback: Plugin Interface for Trading Strategies | Toss Auto-Trading Dev Journal #12 (Phase 2-3) - Orbit - Space & ETF Investing

  3. Pingback: Backtesting on Just a Price List | Toss Auto-Trading Dev Journal #13 (Phase 4) - Orbit - Space & ETF Investing

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top