nautechsystems / nautilus_trader

A high-performance algorithmic trading platform and event-driven backtester
https://nautilustrader.io
GNU Lesser General Public License v3.0
1.71k stars 402 forks source link

Sell order denied due to CUM_NOTIONAL_EXCEEDS_FREE_BALANCE when there is enough balance #1630

Closed rubenmarias closed 1 month ago

rubenmarias commented 2 months ago

Bug Report

Expected Behavior

Sell order is submitted without being blocked by the risk engine

Actual Behavior

Risk engine denies an order due to CUM_NOTIONAL_EXCEEDS_FREE_BALANCE even though there is enough balance.

Steps to Reproduce the Problem

  1. Starting balance: 1000000 FDUSD, 0 BTC
  2. Subscribe to provided quote ticks and run provided strategy

Specifications

cjdsellers commented 2 months ago

Hi @rubenmarias

I've been able to reproduce the order denial, I used ETHUSDT as a placeholder instrument here but the starting conditions and maths should be the same I think.

After some buy and sell cycles the first occurrence is here:

2024-04-26T02:02:37.539078627Z [DEBUG] BACKTESTER-001.RiskEngine: Total: Money('0.17814000', ETH)
2024-04-26T02:02:37.539078627Z [DEBUG] BACKTESTER-001.RiskEngine: Locked: Money('0.00000000', ETH)
2024-04-26T02:02:37.539078627Z [DEBUG] BACKTESTER-001.RiskEngine: Free: Money('0.17814000', ETH)
2024-04-26T02:02:37.539078627Z [DEBUG] BACKTESTER-001.RiskEngine: Cumulative notional SELL: Money('0.20000000', ETH)
2024-04-26T02:02:37.539078627Z [WARN] BACKTESTER-001.RiskEngine: SubmitOrder for O-20240426-0202-001-000-625 DENIED: CUM_NOTIONAL_EXCEEDS_FREE_BALANCE: free=0.17814000 ETH, cum_notional=0.20000000 ETH

This looks like an order to sell 0.2 ETHUSDT when the available ETH balance is 0.17814000, does this match the observation you see at this timestamp?

rubenmarias commented 2 months ago

Yes @cjdsellers, a couple things that I didn't mention:

thanks

cjdsellers commented 2 months ago

Indeed, I see at that timestamp both the strategy inventory and portfolio.net_position both agree at LONG 0.20702.

cjdsellers commented 1 month ago

Now fixed from ae9677a559c4be36fbeaf017a7ad92e23a82de63.