Class: DhanHQ::Risk::Checks::MaxLoss

Inherits:
Object
  • Object
show all
Defined in:
lib/DhanHQ/risk/checks/max_loss.rb

Overview

Enforces daily maximum loss limit across all positions.

Constant Summary collapse

DAILY_MAX_LOSS =
50_000

Class Method Summary collapse

Class Method Details

.run!(**_unused) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/DhanHQ/risk/checks/max_loss.rb', line 10

def self.run!(**_unused)
  positions = DhanHQ::Models::Position.all
  total_unrealized_loss = positions.sum do |p|
    p.unrealized_profit.to_f
  end

  return if total_unrealized_loss >= -DAILY_MAX_LOSS

  raise DhanHQ::RiskViolation,
        "Daily loss limit of ₹#{DAILY_MAX_LOSS} exceeded (current: ₹#{total_unrealized_loss.round(0)})"
end