Class: GrowwMcp::Tools::CreateSmartOrder

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/groww_mcp/tools/smart_order_tools.rb

Class Method Summary collapse

Class Method Details

.call(server_context:, smart_order_type:, trading_symbol:, transaction_type:, quantity:, product_type:, exchange: "NSE", segment: "CASH", duration: "GTC", trigger_price: nil, trigger_direction: nil, order_type: nil, price: nil, net_position_quantity: nil, target_trigger_price: nil, target_order_type: nil, target_price: nil, stop_loss_trigger_price: nil, stop_loss_order_type: nil, stop_loss_price: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/groww_mcp/tools/smart_order_tools.rb', line 69

def call(server_context:, smart_order_type:, trading_symbol:, transaction_type:, quantity:,
         product_type:, exchange: "NSE", segment: "CASH", duration: "GTC",
         trigger_price: nil, trigger_direction: nil, order_type: nil, price: nil,
         net_position_quantity: nil, target_trigger_price: nil, target_order_type: nil,
         target_price: nil, stop_loss_trigger_price: nil, stop_loss_order_type: nil,
         stop_loss_price: nil)
  client = server_context[:client]

  order = {
    smart_order_type: smart_order_type,
    segment: segment,
    trading_symbol: trading_symbol,
    quantity: quantity,
    product_type: product_type,
    exchange: exchange,
    duration: duration,
  }

  if smart_order_type == "GTT"
    missing = { trigger_price: trigger_price, trigger_direction: trigger_direction,
                order_type: order_type }.select { |_, v| v.nil? }.keys
    return validation_error("GTT order missing required fields: #{missing.join(', ')}") unless missing.empty?

    order[:trigger_price] = trigger_price.to_s
    order[:trigger_direction] = trigger_direction
    leg = { order_type: order_type, transaction_type: transaction_type }
    leg[:price] = price if price
    order[:order] = leg
  else # OCO
    missing = { target_trigger_price: target_trigger_price, target_order_type: target_order_type,
                stop_loss_trigger_price: stop_loss_trigger_price,
                stop_loss_order_type: stop_loss_order_type }.select { |_, v| v.nil? }.keys
    return validation_error("OCO order missing required fields: #{missing.join(', ')}") unless missing.empty?

    order[:transaction_type] = transaction_type
    order[:net_position_quantity] = net_position_quantity if net_position_quantity

    target = { trigger_price: target_trigger_price, order_type: target_order_type }
    target[:price] = target_price if target_price
    order[:target] = target

    stop_loss = { trigger_price: stop_loss_trigger_price, order_type: stop_loss_order_type }
    stop_loss[:price] = stop_loss_price if stop_loss_price
    order[:stop_loss] = stop_loss
  end

  result = client.create_smart_order(order)
  format_response(result)
rescue GrowwMcp::ApiError => e
  error_response(e)
end