feat: apply dynamic margin ratio in bot position sizing
Made-with: Cursor
This commit is contained in:
@@ -85,9 +85,11 @@ class TradingBot:
|
|||||||
async def _open_position(self, signal: str, df):
|
async def _open_position(self, signal: str, df):
|
||||||
balance = await self.exchange.get_balance()
|
balance = await self.exchange.get_balance()
|
||||||
price = df["close"].iloc[-1]
|
price = df["close"].iloc[-1]
|
||||||
|
margin_ratio = self.risk.get_dynamic_margin_ratio(balance)
|
||||||
quantity = self.exchange.calculate_quantity(
|
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)
|
stop_loss, take_profit = Indicators(df).get_atr_stop(df, signal, price)
|
||||||
|
|
||||||
notional = quantity * price
|
notional = quantity * price
|
||||||
@@ -165,6 +167,9 @@ class TradingBot:
|
|||||||
async def run(self):
|
async def run(self):
|
||||||
logger.info(f"봇 시작: {self.config.symbol}, 레버리지 {self.config.leverage}x")
|
logger.info(f"봇 시작: {self.config.symbol}, 레버리지 {self.config.leverage}x")
|
||||||
await self._recover_position()
|
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(
|
await self.stream.start(
|
||||||
api_key=self.config.api_key,
|
api_key=self.config.api_key,
|
||||||
api_secret=self.config.api_secret,
|
api_secret=self.config.api_secret,
|
||||||
|
|||||||
@@ -6,16 +6,16 @@ from src.config import Config
|
|||||||
def test_config_loads_symbol():
|
def test_config_loads_symbol():
|
||||||
os.environ["SYMBOL"] = "XRPUSDT"
|
os.environ["SYMBOL"] = "XRPUSDT"
|
||||||
os.environ["LEVERAGE"] = "10"
|
os.environ["LEVERAGE"] = "10"
|
||||||
os.environ["RISK_PER_TRADE"] = "0.02"
|
|
||||||
cfg = Config()
|
cfg = Config()
|
||||||
assert cfg.symbol == "XRPUSDT"
|
assert cfg.symbol == "XRPUSDT"
|
||||||
assert cfg.leverage == 10
|
assert cfg.leverage == 10
|
||||||
assert cfg.risk_per_trade == 0.02
|
|
||||||
|
|
||||||
|
|
||||||
def test_config_notion_keys():
|
def test_config_dynamic_margin_params():
|
||||||
os.environ["NOTION_TOKEN"] = "secret_test"
|
os.environ["MARGIN_MAX_RATIO"] = "0.50"
|
||||||
os.environ["NOTION_DATABASE_ID"] = "db_test_id"
|
os.environ["MARGIN_MIN_RATIO"] = "0.20"
|
||||||
|
os.environ["MARGIN_DECAY_RATE"] = "0.0006"
|
||||||
cfg = Config()
|
cfg = Config()
|
||||||
assert cfg.notion_token == "secret_test"
|
assert cfg.margin_max_ratio == 0.50
|
||||||
assert cfg.notion_database_id == "db_test_id"
|
assert cfg.margin_min_ratio == 0.20
|
||||||
|
assert cfg.margin_decay_rate == 0.0006
|
||||||
|
|||||||
Reference in New Issue
Block a user