Class: Melaya::SimAPI

Inherits:
Object
  • Object
show all
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

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).

Parameters:

  • strategy_id (String)
  • asset (String, nil) (defaults to: nil)


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.

Parameters:

  • strategy_id (String)
  • order_id (String)
  • symbol (String, nil) (defaults to: nil)
  • exchange (String, nil) (defaults to: nil)


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.

Parameters:

  • strategy_id (String)
  • exchange (String)
  • symbol (String)
  • side (String)

    “buy” or “sell”

  • amount (Numeric)
  • type (String) (defaults to: "market")

    “market” or “limit” (default “market”)

  • price (Numeric, nil) (defaults to: nil)

    required for limit orders

  • market (String, nil) (defaults to: nil)
  • leverage (Numeric, nil) (defaults to: nil)
  • reduce_only (Boolean, nil) (defaults to: nil)
  • sl_price (Numeric, nil) (defaults to: nil)
  • tp_price (Numeric, nil) (defaults to: nil)
  • client_order_id (String, nil) (defaults to: nil)
  • params (Hash, nil) (defaults to: nil)


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_accountsObject

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