Class: Melaya::SimAPI
- Inherits:
-
Object
- Object
- Melaya::SimAPI
- Defined in:
- lib/melaya/sim.rb
Overview
Paper-trading (sim broker) API.
The sim broker synthesises fills from Melaya’s live ticker tape and keeps a virtual wallet per strategy — no venue-side state changes, no exchange credentials needed. Every call is scoped to a strategy_id.
Create a paper strategy first:
result = melaya.strategies.create(name: "test", strategy_type: "custom", ...)
sid = result["strategyId"]
Instance Method Summary collapse
-
#balance(strategy_id:, asset: nil) ⇒ Object
Virtual balance for a paper strategy (equity, realized/unrealized PnL, free/used).
-
#cancel_order(strategy_id:, order_id:, symbol: nil, exchange: nil) ⇒ Object
Cancel a resting paper order.
-
#create_order(strategy_id:, exchange:, symbol:, side:, amount:, type: "market", price: nil, market: nil, leverage: nil, reduce_only: nil, sl_price: nil, tp_price: nil, client_order_id: nil, params: nil) ⇒ Object
Place a paper order.
-
#initialize(http) ⇒ SimAPI
constructor
A new instance of SimAPI.
-
#list_accounts ⇒ Object
Paper accounts (one virtual wallet per paper strategy).
-
#my_trades(strategy_id:) ⇒ Object
Filled paper trades for a strategy.
-
#open_orders(strategy_id:) ⇒ Object
Resting paper orders for a strategy.
-
#positions(strategy_id:) ⇒ Object
Open paper positions for a strategy.
Constructor Details
#initialize(http) ⇒ SimAPI
Returns a new instance of SimAPI.
14 15 16 |
# File 'lib/melaya/sim.rb', line 14 def initialize(http) @http = http end |
Instance Method Details
#balance(strategy_id:, asset: nil) ⇒ Object
Virtual balance for a paper strategy (equity, realized/unrealized PnL, free/used).
27 28 29 30 31 |
# File 'lib/melaya/sim.rb', line 27 def balance(strategy_id:, asset: nil) @http.get("/api/v1/private/sim/balance", "strategy_id" => strategy_id, "asset" => asset ) end |
#cancel_order(strategy_id:, order_id:, symbol: nil, exchange: nil) ⇒ Object
Cancel a resting paper order.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/melaya/sim.rb', line 99 def cancel_order(strategy_id:, order_id:, symbol: nil, exchange: nil) body = { "strategy_id" => strategy_id, "order_id" => order_id, "orderId" => order_id, "symbol" => symbol, "exchange" => exchange, }.reject { |_, v| v.nil? } @http.post("/api/v1/private/sim/cancel-order", body) end |
#create_order(strategy_id:, exchange:, symbol:, side:, amount:, type: "market", price: nil, market: nil, leverage: nil, reduce_only: nil, sl_price: nil, tp_price: nil, client_order_id: nil, params: nil) ⇒ Object
Place a paper order. Fills synthesise from the live ticker; nothing hits the venue.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/melaya/sim.rb', line 67 def create_order(strategy_id:, exchange:, symbol:, side:, amount:, type: "market", price: nil, market: nil, leverage: nil, reduce_only: nil, sl_price: nil, tp_price: nil, client_order_id: nil, params: nil) body = { "strategy_id" => strategy_id, "exchange" => exchange, "symbol" => symbol, "side" => side, "amount" => amount, "order_type" => type, "orderType" => type, "price" => price, "market" => market, "market_type" => market, "leverage" => leverage, "reduceOnly" => reduce_only, "slPrice" => sl_price, "tpPrice" => tp_price, "client_order_id" => client_order_id, "clientOrderId" => client_order_id, "params" => params, }.reject { |_, v| v.nil? } @http.post("/api/v1/private/sim/create-order", body) end |
#list_accounts ⇒ Object
Paper accounts (one virtual wallet per paper strategy).
19 20 21 22 |
# File 'lib/melaya/sim.rb', line 19 def list_accounts r = @http.get("/api/v1/private/sim/list-accounts") r.is_a?(Array) ? r : (r.is_a?(Hash) ? (r["accounts"] || []) : []) end |
#my_trades(strategy_id:) ⇒ Object
Filled paper trades for a strategy.
46 47 48 49 |
# File 'lib/melaya/sim.rb', line 46 def my_trades(strategy_id:) r = @http.get("/api/v1/private/sim/my-trades", "strategy_id" => strategy_id) r.is_a?(Array) ? r : (r.is_a?(Hash) ? (r["trades"] || []) : []) end |
#open_orders(strategy_id:) ⇒ Object
Resting paper orders for a strategy.
40 41 42 43 |
# File 'lib/melaya/sim.rb', line 40 def open_orders(strategy_id:) r = @http.get("/api/v1/private/sim/open-orders", "strategy_id" => strategy_id) r.is_a?(Array) ? r : (r.is_a?(Hash) ? (r["orders"] || []) : []) end |
#positions(strategy_id:) ⇒ Object
Open paper positions for a strategy.
34 35 36 37 |
# File 'lib/melaya/sim.rb', line 34 def positions(strategy_id:) r = @http.get("/api/v1/private/sim/positions", "strategy_id" => strategy_id) r.is_a?(Array) ? r : (r.is_a?(Hash) ? (r["positions"] || []) : []) end |