Class: Melaya::TradeAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/melaya/trade.rb

Overview

Live trading API — credentialed order placement, account state, and position management on a CONNECTED exchange.

Every method POSTs to api.melaya.org/api/v1/private/<op>; the server resolves your connected exchange credential (referenced by api_key_id — see AccountAPI#keys) and forwards the call to the venue through Melaya’s in-house Rust engine. Responses share an envelope: { ok, exchange, operation, orderId, clientOrderId, payload, data, … }.

WARNING: these hit the REAL venue with REAL funds. The write methods (create_order, cancel_order, amend_order, cancel_all_orders, cancel_plan_orders, close_position, set_leverage, set_margin_mode, set_position_mode) move money or change account state. For risk-free testing use the sim (paper) broker or a paper strategy instead.

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ TradeAPI

Returns a new instance of TradeAPI.



19
20
21
# File 'lib/melaya/trade.rb', line 19

def initialize(http)
  @http = http
end

Instance Method Details

#amend_order(exchange:, api_key_id: nil, order_id: nil, symbol: nil, amount: nil, price: nil) ⇒ Object

Amend (modify) a live order. WARNING.



119
120
121
122
123
# File 'lib/melaya/trade.rb', line 119

def amend_order(exchange:, api_key_id: nil, order_id: nil, symbol: nil,
                amount: nil, price: nil)
  op("amend-order", exchange: exchange, apiKeyId: api_key_id,
     orderId: order_id, symbol: symbol, amount: amount, price: price)
end

#balance(exchange:, api_key_id: nil, key_id: nil, market_type: nil, params: nil) ⇒ Object

Live account balance on a connected venue.



26
27
28
29
# File 'lib/melaya/trade.rb', line 26

def balance(exchange:, api_key_id: nil, key_id: nil, market_type: nil, params: nil)
  op("balance", exchange: exchange, apiKeyId: api_key_id, keyId: key_id,
     marketType: market_type, params: params)
end

#cancel_all_orders(exchange:, api_key_id: nil, symbol: nil, market_type: nil) ⇒ Object

Cancel every open order (optionally scoped to a symbol). WARNING.



126
127
128
129
# File 'lib/melaya/trade.rb', line 126

def cancel_all_orders(exchange:, api_key_id: nil, symbol: nil, market_type: nil)
  op("cancel-all-orders", exchange: exchange, apiKeyId: api_key_id,
     symbol: symbol, marketType: market_type)
end

#cancel_order(exchange:, api_key_id: nil, order_id: nil, client_order_id: nil, symbol: nil, market_type: nil) ⇒ Object

Cancel a live order by id. WARNING.



111
112
113
114
115
116
# File 'lib/melaya/trade.rb', line 111

def cancel_order(exchange:, api_key_id: nil, order_id: nil, client_order_id: nil,
                 symbol: nil, market_type: nil)
  op("cancel-order", exchange: exchange, apiKeyId: api_key_id,
     orderId: order_id, clientOrderId: client_order_id,
     symbol: symbol, marketType: market_type)
end

#cancel_plan_orders(exchange:, api_key_id: nil, symbol: nil, market_type: nil) ⇒ Object

Cancel resting plan/trigger orders. WARNING.



132
133
134
135
# File 'lib/melaya/trade.rb', line 132

def cancel_plan_orders(exchange:, api_key_id: nil, symbol: nil, market_type: nil)
  op("cancel-plan-orders", exchange: exchange, apiKeyId: api_key_id,
     symbol: symbol, marketType: market_type)
end

#close_position(exchange:, symbol:, api_key_id: nil, market_type: nil) ⇒ Object

Close an open position (market reduce-only). WARNING.



138
139
140
141
# File 'lib/melaya/trade.rb', line 138

def close_position(exchange:, symbol:, api_key_id: nil, market_type: nil)
  op("close-position", exchange: exchange, apiKeyId: api_key_id,
     symbol: symbol, marketType: market_type)
end

#closed_orders(exchange:, api_key_id: nil, market_type: nil, symbol: nil) ⇒ Object

Closed/filled orders.



56
57
58
59
# File 'lib/melaya/trade.rb', line 56

def closed_orders(exchange:, api_key_id: nil, market_type: nil, symbol: nil)
  op("closed-orders", exchange: exchange, apiKeyId: api_key_id,
     marketType: market_type, symbol: symbol)
end

#create_order(exchange:, symbol:, side:, amount:, api_key_id: nil, type: "market", price: nil, market_type: nil, stop_price: nil, take_profit_price: nil, reduce_only: nil, leverage: nil, client_order_id: nil, params: nil) ⇒ Object

Place a live order on the venue. WARNING: real money. stop_price, take_profit_price, and reduce_only are folded into params.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/melaya/trade.rb', line 95

def create_order(exchange:, symbol:, side:, amount:,
                 api_key_id: nil, type: "market", price: nil,
                 market_type: nil, stop_price: nil, take_profit_price: nil,
                 reduce_only: nil, leverage: nil, client_order_id: nil, params: nil)
  p = (params || {}).dup
  p["stopPrice"]       = stop_price       unless stop_price.nil?
  p["takeProfitPrice"] = take_profit_price unless take_profit_price.nil?
  p["reduceOnly"]      = reduce_only       unless reduce_only.nil?
  op("create-order",
     exchange: exchange, apiKeyId: api_key_id, symbol: symbol,
     side: side, amount: amount, type: type, price: price,
     marketType: market_type, leverage: leverage,
     clientOrderId: client_order_id, params: p.empty? ? nil : p)
end

#leverage(exchange:, api_key_id: nil, symbol: nil, market_type: nil) ⇒ Object

Current leverage for a symbol.



80
81
82
83
# File 'lib/melaya/trade.rb', line 80

def leverage(exchange:, api_key_id: nil, symbol: nil, market_type: nil)
  op("leverage", exchange: exchange, apiKeyId: api_key_id,
     symbol: symbol, marketType: market_type)
end

#leverage_tiers(exchange:, api_key_id: nil, symbol: nil, market_type: nil) ⇒ Object

Leverage tiers / brackets for a symbol.



86
87
88
89
# File 'lib/melaya/trade.rb', line 86

def leverage_tiers(exchange:, api_key_id: nil, symbol: nil, market_type: nil)
  op("leverage-tiers", exchange: exchange, apiKeyId: api_key_id,
     symbol: symbol, marketType: market_type)
end

#my_trades(exchange:, api_key_id: nil, market_type: nil, symbol: nil) ⇒ Object

Your trade (fill) history.



62
63
64
65
# File 'lib/melaya/trade.rb', line 62

def my_trades(exchange:, api_key_id: nil, market_type: nil, symbol: nil)
  op("my-trades", exchange: exchange, apiKeyId: api_key_id,
     marketType: market_type, symbol: symbol)
end

#my_trades_history(exchange:, api_key_id: nil, market_type: nil, symbol: nil) ⇒ Object

Extended trade history (venue-dependent).



68
69
70
71
# File 'lib/melaya/trade.rb', line 68

def my_trades_history(exchange:, api_key_id: nil, market_type: nil, symbol: nil)
  op("my-trades-history", exchange: exchange, apiKeyId: api_key_id,
     marketType: market_type, symbol: symbol)
end

#open_orders(exchange:, api_key_id: nil, market_type: nil, symbol: nil) ⇒ Object

Resting (open) orders.



44
45
46
47
# File 'lib/melaya/trade.rb', line 44

def open_orders(exchange:, api_key_id: nil, market_type: nil, symbol: nil)
  op("open-orders", exchange: exchange, apiKeyId: api_key_id,
     marketType: market_type, symbol: symbol)
end

#orders(exchange:, api_key_id: nil, market_type: nil, symbol: nil) ⇒ Object

All orders (open + recent).



50
51
52
53
# File 'lib/melaya/trade.rb', line 50

def orders(exchange:, api_key_id: nil, market_type: nil, symbol: nil)
  op("orders", exchange: exchange, apiKeyId: api_key_id,
     marketType: market_type, symbol: symbol)
end

#plan_orders(exchange:, api_key_id: nil, market_type: nil, symbol: nil) ⇒ Object

Resting conditional/plan (trigger) orders.



74
75
76
77
# File 'lib/melaya/trade.rb', line 74

def plan_orders(exchange:, api_key_id: nil, market_type: nil, symbol: nil)
  op("plan-orders", exchange: exchange, apiKeyId: api_key_id,
     marketType: market_type, symbol: symbol)
end

#positions(exchange:, api_key_id: nil, market_type: nil, symbol: nil, params: nil) ⇒ Object

Live open positions.



32
33
34
35
# File 'lib/melaya/trade.rb', line 32

def positions(exchange:, api_key_id: nil, market_type: nil, symbol: nil, params: nil)
  op("positions", exchange: exchange, apiKeyId: api_key_id,
     marketType: market_type, symbol: symbol, params: params)
end

#positions_history(exchange:, api_key_id: nil, market_type: nil, symbol: nil) ⇒ Object

Historical positions (venue-dependent).



38
39
40
41
# File 'lib/melaya/trade.rb', line 38

def positions_history(exchange:, api_key_id: nil, market_type: nil, symbol: nil)
  op("positions-history", exchange: exchange, apiKeyId: api_key_id,
     marketType: market_type, symbol: symbol)
end

#set_leverage(exchange:, symbol:, leverage:, api_key_id: nil, market_type: nil) ⇒ Object

Set leverage for a symbol. WARNING.



144
145
146
147
# File 'lib/melaya/trade.rb', line 144

def set_leverage(exchange:, symbol:, leverage:, api_key_id: nil, market_type: nil)
  op("set-leverage", exchange: exchange, apiKeyId: api_key_id,
     symbol: symbol, leverage: leverage, marketType: market_type)
end

#set_margin_mode(exchange:, margin_mode:, api_key_id: nil, symbol: nil, market_type: nil) ⇒ Object

Set margin mode (cross/isolated). WARNING.



150
151
152
153
# File 'lib/melaya/trade.rb', line 150

def set_margin_mode(exchange:, margin_mode:, api_key_id: nil, symbol: nil, market_type: nil)
  op("set-margin-mode", exchange: exchange, apiKeyId: api_key_id,
     marginMode: margin_mode, symbol: symbol, marketType: market_type)
end

#set_position_mode(exchange:, api_key_id: nil, hedged: nil, mode: nil, market_type: nil) ⇒ Object

Set position mode (one-way / hedge). WARNING.



156
157
158
159
# File 'lib/melaya/trade.rb', line 156

def set_position_mode(exchange:, api_key_id: nil, hedged: nil, mode: nil, market_type: nil)
  op("set-position-mode", exchange: exchange, apiKeyId: api_key_id,
     hedged: hedged, mode: mode, marketType: market_type)
end