Repository navigation
feat: add yfinance-backed paper trading mode #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
efc90df
docs: add yfinance paper mode plan
jhojin7 b1536ae
feat: add yfinance paper broker
jhojin7 3dfcf94
fix: harden yfinance paper mode storage defaults
jhojin7 12342fd
chore: drop local implementation plan from PR
jhojin7 77cb494
Merge pull request #1 from jhojin7/plan/yfinance-paper-mode
jhojin7 92e5765
Resolve PR merge conflicts
jhojin7 469e58c
Address PR review comments
jhojin7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| from .base import Broker | ||
| from .kis.client import KISBroker | ||
| from .yfinance import YFinanceBroker | ||
|
|
||
| __all__ = ["KISBroker"] | ||
| __all__ = ["Broker", "KISBroker", "YFinanceBroker"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| """Broker protocol shared by trading components.""" | ||
|
|
||
| from decimal import Decimal | ||
| from typing import Protocol | ||
|
|
||
| from src.core.types import Bar, Market, Order, Position | ||
|
|
||
|
|
||
| class Broker(Protocol): | ||
| """Async broker interface used by TradingBot, StrategyEngine, and OrderManager.""" | ||
|
|
||
| paper_trading: bool | ||
|
|
||
| @property | ||
| def is_connected(self) -> bool: | ||
| """Whether the broker is connected.""" | ||
| ... | ||
|
|
||
| @property | ||
| def account_no(self) -> str: | ||
| """Account identifier for logs/dashboard.""" | ||
| ... | ||
|
|
||
| async def connect(self) -> None: | ||
| ... | ||
|
|
||
| async def disconnect(self) -> None: | ||
| ... | ||
|
|
||
| async def get_current_price( | ||
| self, | ||
| symbol: str, | ||
| market: Market = Market.KRX, | ||
| ) -> Decimal: | ||
| ... | ||
|
|
||
| async def get_historical_bars( | ||
| self, | ||
| symbol: str, | ||
| market: Market = Market.KRX, | ||
| days: int = 100, | ||
| ) -> list[Bar]: | ||
| ... | ||
|
|
||
| async def get_positions( | ||
| self, | ||
| market: Market, | ||
| ) -> list[Position]: | ||
| ... | ||
|
|
||
| async def get_account_balance( | ||
| self, | ||
| market: Market, | ||
| ) -> dict: | ||
| ... | ||
|
|
||
| async def submit_order(self, order: Order) -> str: | ||
| ... | ||
|
|
||
| async def cancel_order( | ||
| self, | ||
| order_id: str, | ||
| market: Market, | ||
| ) -> bool: | ||
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Broker factory for selecting runtime broker implementations.""" | ||
|
|
||
| from decimal import Decimal | ||
|
|
||
| from src.broker.base import Broker | ||
| from src.broker.kis.client import KISBroker | ||
| from src.broker.paper import PaperBroker | ||
| from src.broker.yfinance import YFinanceBroker | ||
| from src.core.events import EventBus | ||
| from src.utils.config import Config | ||
|
|
||
|
|
||
| def create_broker(config: Config, event_bus: EventBus) -> Broker: | ||
| """Create the configured broker implementation.""" | ||
| broker_name = config.get_broker_name() | ||
| trading_mode = config.get_trading_mode() | ||
|
|
||
| if broker_name == "yfinance": | ||
| if trading_mode != "paper": | ||
| raise ValueError("BROKER=yfinance only supports TRADING_MODE=paper") | ||
| return YFinanceBroker( | ||
| event_bus=event_bus, | ||
| initial_cash_usd=Decimal(str(config.get("trading.paper_cash_usd", 10000))), | ||
| initial_cash_krw=Decimal(str(config.get("trading.paper_cash_krw", 0))), | ||
| ) | ||
|
|
||
| if broker_name == "kis": | ||
| kis_config = config.get_kis_config() | ||
| real_broker = KISBroker( | ||
| event_bus=event_bus, | ||
| app_key=kis_config["app_key"], | ||
| app_secret=kis_config["app_secret"], | ||
| account_no=kis_config["account_no"], | ||
| paper_trading=kis_config["paper_trading"], | ||
| hts_id=kis_config.get("hts_id", ""), | ||
| slippage_buffer=float(config.get("trading.slippage_buffer_pct", 0.01)), | ||
| ) | ||
| if kis_config["paper_trading"]: | ||
| return PaperBroker( | ||
| event_bus=event_bus, | ||
| real_broker=real_broker, | ||
| initial_cash_usd=Decimal(str(config.get("trading.paper_cash_usd", 10000))), | ||
| initial_cash_krw=Decimal(str(config.get("trading.paper_cash_krw", 0))), | ||
| ) | ||
| return real_broker | ||
|
|
||
| raise ValueError(f"Unsupported BROKER: {broker_name}") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.