feat: apply dynamic margin ratio in bot position sizing

Made-with: Cursor
This commit is contained in:
21in7
2026-03-01 20:39:07 +09:00
parent 795689ac49
commit ab580b18af
2 changed files with 13 additions and 8 deletions

View File

@@ -85,9 +85,11 @@ class TradingBot:
async def _open_position(self, signal: str, df):
balance = await self.exchange.get_balance()
price = df["close"].iloc[-1]
margin_ratio = self.risk.get_dynamic_margin_ratio(balance)
quantity = self.exchange.calculate_quantity(
balance=balance, price=price, leverage=self.config.leverage
balance=balance, price=price, leverage=self.config.leverage, margin_ratio=margin_ratio
)
logger.info(f"포지션 크기: 잔고={balance:.2f} USDT, 증거금비율={margin_ratio:.1%}, 수량={quantity}")
stop_loss, take_profit = Indicators(df).get_atr_stop(df, signal, price)
notional = quantity * price
@@ -165,6 +167,9 @@ class TradingBot:
async def run(self):
logger.info(f"봇 시작: {self.config.symbol}, 레버리지 {self.config.leverage}x")
await self._recover_position()
balance = await self.exchange.get_balance()
self.risk.set_base_balance(balance)
logger.info(f"기준 잔고 설정: {balance:.2f} USDT (동적 증거금 비율 기준점)")
await self.stream.start(
api_key=self.config.api_key,
api_secret=self.config.api_secret,

View File

@@ -6,16 +6,16 @@ from src.config import Config
def test_config_loads_symbol():
os.environ["SYMBOL"] = "XRPUSDT"
os.environ["LEVERAGE"] = "10"
os.environ["RISK_PER_TRADE"] = "0.02"
cfg = Config()
assert cfg.symbol == "XRPUSDT"
assert cfg.leverage == 10
assert cfg.risk_per_trade == 0.02
def test_config_notion_keys():
os.environ["NOTION_TOKEN"] = "secret_test"
os.environ["NOTION_DATABASE_ID"] = "db_test_id"
def test_config_dynamic_margin_params():
os.environ["MARGIN_MAX_RATIO"] = "0.50"
os.environ["MARGIN_MIN_RATIO"] = "0.20"
os.environ["MARGIN_DECAY_RATE"] = "0.0006"
cfg = Config()
assert cfg.notion_token == "secret_test"
assert cfg.notion_database_id == "db_test_id"
assert cfg.margin_max_ratio == 0.50
assert cfg.margin_min_ratio == 0.20
assert cfg.margin_decay_rate == 0.0006