Module: Live::OrderUpdateGuardSupport

Defined in:
app/services/live/order_update_guard_support.rb

Overview

Guard-related helpers for interpreting order update payloads.

Constant Summary collapse

SEGMENT_MAP =
{
  %w[NSE E] => "NSE_EQ",
  %w[BSE E] => "BSE_EQ",
  %w[NSE D] => "NSE_FNO",
  %w[BSE D] => "BSE_FNO",
  %w[NSE C] => "NSE_CURRENCY",
  %w[BSE C] => "BSE_CURRENCY",
  %w[MCX M] => "MCX_COMM"
}.freeze
DEFAULT_SL_PCT =
0.15
DEFAULT_TP_PCT =
0.30
DEFAULT_TRAIL_PCT =
0.01

Class Method Summary collapse

Class Method Details

.average_entry(order_data) ⇒ Object



56
57
58
59
60
61
# File 'app/services/live/order_update_guard_support.rb', line 56

def average_entry(order_data)
  first_price = [order_data[:AvgTradedPrice], order_data[:TradedPrice], order_data[:Price]]
                .compact
                .first
  first_price.to_f
end

.default_sl_pct(_segment, _security_id) ⇒ Object



63
64
65
# File 'app/services/live/order_update_guard_support.rb', line 63

def default_sl_pct(_segment, _security_id)
  DEFAULT_SL_PCT
end

.default_tp_pct(_segment, _security_id) ⇒ Object



67
68
69
# File 'app/services/live/order_update_guard_support.rb', line 67

def default_tp_pct(_segment, _security_id)
  DEFAULT_TP_PCT
end

.default_trail_pct(_segment, _security_id) ⇒ Object



71
72
73
# File 'app/services/live/order_update_guard_support.rb', line 71

def default_trail_pct(_segment, _security_id)
  DEFAULT_TRAIL_PCT
end

.guard_base_payload(segment, security_id, order_data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/live/order_update_guard_support.rb', line 32

def guard_base_payload(segment, security_id, order_data)
  {
    pos_id: nil,
    exchange_segment: segment,
    security_id: security_id,
    entry: average_entry(order_data),
    qty: order_data[:TradedQty].to_i,
    placed_as: placed_as(order_data),
    super_order_id: order_data[:OrderNo].to_s
  }
end

.guard_percentage_payload(segment, security_id) ⇒ Object



44
45
46
47
48
49
50
# File 'app/services/live/order_update_guard_support.rb', line 44

def guard_percentage_payload(segment, security_id)
  {
    sl_pct: default_sl_pct(segment, security_id),
    tp_pct: default_tp_pct(segment, security_id),
    trail_pct: default_trail_pct(segment, security_id)
  }
end

.map_segment(exchange, segment) ⇒ Object



22
23
24
25
# File 'app/services/live/order_update_guard_support.rb', line 22

def map_segment(exchange, segment)
  key = [exchange.to_s.upcase, segment.to_s.upcase]
  SEGMENT_MAP.fetch(key, "NSE_EQ")
end

.placed_as(order_data) ⇒ Object



52
53
54
# File 'app/services/live/order_update_guard_support.rb', line 52

def placed_as(order_data)
  order_data[:Remarks].to_s.match?(/Super Order/i) ? "super" : "plain"
end

.position_guard_payload(segment, security_id, order_data) ⇒ Object



27
28
29
30
# File 'app/services/live/order_update_guard_support.rb', line 27

def position_guard_payload(segment, security_id, order_data)
  guard_base_payload(segment, security_id, order_data)
    .merge(guard_percentage_payload(segment, security_id))
end