Toss Auto-Trading Bot: Take-Profit/Stop-Loss Cycle Complete #23

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

πŸ’‘ Phase 7-5 at a glance

  • Goal: Pull per-symbol buy targets and take-profit/stop-loss ratios into config and run a full virtual trade cycle under DRY_RUN.
  • Key decision: Split notifier.py into its own module with severity-based dual-channel dispatch.
  • Verification: All 136 pytest cases pass; security_check.py runs a non-destructive audit of tokens, account link, and the 5-layer safety brake.

This round, the Toss auto-trading bot finally got a real trade cycle. Running under DRY_RUN doesn’t mean cutting corners β€” once take-profit/stop-loss logic is fully wired, flipping to live trading later is just a config flag away. For context, Toss Securities (ν† μŠ€μ¦κΆŒ) is a Korean brokerage, and this bot talks to its official trading API.

Toss auto-trading bot: take-profit/stop-loss cycle structure

Previously the bot just watched a target price and stopped after a single buy signal. Now it runs a full cycle:

  • config.TARGET_STRATEGIES defines per-symbol buy target, quantity, take_profit_pct (default +5%), stop_loss_pct (default -3%)
  • strategy.create_strategy() dynamically builds the right strategy object from that config dict
  • Virtual buy β†’ position opened β†’ return calculated β†’ virtual sell triggered (with alert + DB write to trades) once take-profit or stop-loss is hit

Say a virtual buy on Samsung Electronics (005930) fills at β‚©70,000 β€” hitting β‚©73,500 (+5%) triggers a take-profit sell, and β‚©67,900 (-3%) triggers a stop-loss sell. Symbols without these ratios configured still fall back to the old single-shot watch behavior, so backward compatibility holds.

Dual-channel alerts β€” notifier.py

Alert logic used to be scattered across auto_trader.py. It’s now fully isolated:

Severity routing: INFO goes to Telegram + DB only. WARNING/ERROR/CRITICAL go to both Telegram and email (SMTP).

The part I cared about most was making failures best-effort. During testing I actually hit this:

smtplib.SMTPServerDisconnected: please run connect() first
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded

None of that should ever take down the watch loop, so notifier.py wraps every send in try/except and just logs the failure. It also hooks into risk_control.py β€” once cumulative loss crosses MAX_CUMULATIVE_LOSS_PCT, monitoring auto-pauses and an emergency alert fires on both channels.

Security & permission audit β€” security_check.py

Before going live, this script bundles the checklist into one run:

  • Verifies .env exists and is listed in .gitignore, so credentials never get committed
  • Confirms the 5-layer safety brake: DRY_RUN=True, TARGET_BUY_PRICE=1 KRW, MAX_ORDER_QUANTITY_PER_TRADE=10 shares, MAX_CUMULATIVE_LOSS_PCT=10%, and a hard block on the order-placement API
  • Non-destructively checks OAuth2 token issuance (POST /api-v1/oauth2/token), account serial linkage, and quote API access (GET /api-v1/quote/{symbol}) without placing any real orders

136 pytest cases, all green

Between the new take-profit/stop-loss cycle tests, notifier.py’s severity-routing tests, and security_check.py’s audit tests, the suite now sits at 136 passing cases. The trickiest one deliberately forced an SMTP outage to confirm the watch loop survives it β€” and it did. The fixture/parametrize patterns came straight out of the official pytest docs.

What’s next

The last thing before going live is a small-scale test with real account linkage β€” that’ll be the next entry.

πŸ‘‰ Next entry isn’t published yet. The full series index is above.

FAQ

Q1. Does take-profit/stop-loss actually run in DRY_RUN mode?

Yes β€” no real orders go out, but the virtual buy/sell, return calculation, and DB logging all run exactly like the live path.

Q2. When do alerts go to email instead of just Telegram?

Only at WARNING severity and above. INFO-level events stay on Telegram + DB.

Q3. Does security_check.py touch the live account?

No β€” it’s a non-destructive audit that only reads token/permission/safety-brake state.

Q4. Can take-profit/stop-loss ratios differ per symbol?

Yes, each symbol in TARGET_STRATEGIES can have its own take_profit_pct and stop_loss_pct.

1 thought on “Toss Auto-Trading Bot: Take-Profit/Stop-Loss Cycle Complete #23”

  1. Pingback: ν† μŠ€μ¦κΆŒ μžλ™λ§€λ§€ 봇 μ΅μ ˆμ†μ ˆ 사이클 μ™„μ„± | ν† μŠ€ μžλ™λ§€λ§€ 개발기 #23 - TS ν€€νŠΈ: 주식 μžλ™λ§€λ§€ μ—°κ΅¬μ†Œ

Leave a Comment

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

Scroll to Top