Class: Melaya::Client

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

Overview

The Melaya client.

Examples:

require "melaya"
melaya = Melaya::Client.new(api_key: ENV["MK"])

# Market data
t = melaya.market.ticker(exchange: "binance", symbol: "BTC/USDT", market: "spot")
puts t["last"]

# Paper strategy
result = melaya.strategies.create(
  name: "my-bot", strategy_type: "custom", exchange: "binanceusdm",
  symbol: "BTC/USDT:USDT", market: "FUTURES", dry_run: true,
  params: { language: "rhai", definition: 'fn evaluate() { emit_long(param("qty")); }', qty: 0.001 }
)
sid = result["strategyId"]
melaya.strategies.stop(sid)
melaya.strategies.delete(sid)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: HttpClient::DEFAULT_BASE_URL, ws_url: StreamAPI::DEFAULT_WS_URL, verify_ssl: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    your Melaya API key, prefixed mk_

  • base_url (String) (defaults to: HttpClient::DEFAULT_BASE_URL)

    override the REST base URL

  • ws_url (String) (defaults to: StreamAPI::DEFAULT_WS_URL)

    override the WebSocket base URL

  • verify_ssl (Boolean) (defaults to: nil)

    set false to skip TLS verification (dev-box only). Prefer using ENV=“1” rather than passing this directly.

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/melaya.rb', line 55

def initialize(api_key:, base_url: HttpClient::DEFAULT_BASE_URL,
               ws_url: StreamAPI::DEFAULT_WS_URL, verify_ssl: nil)
  raise ArgumentError, "Melaya: api_key is required (create one at melaya.org -> Settings -> API Keys)." \
    if api_key.nil? || api_key.empty?
  raise ArgumentError, "Melaya: API keys must be prefixed 'mk_'." \
    unless api_key.start_with?("mk_")

  ssl = if verify_ssl.nil?
    ENV["MELAYA_INSECURE_TLS"] != "1"
  else
    verify_ssl
  end

  http = HttpClient.new(api_key: api_key, base_url: base_url, verify_ssl: ssl)

  @market     = MarketAPI.new(http)
  @account    = AccountAPI.new(http)
  @sim        = SimAPI.new(http)
  @strategies = StrategiesAPI.new(http)
  @backtest   = BacktestAPI.new(http)
  @stream     = StreamAPI.new(api_key, ws_url, http, verify_ssl: ssl)
  @trade      = TradeAPI.new(http)
end

Instance Attribute Details

#accountObject (readonly)

Authenticated account reads: connected keys, tier limits, usage.



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

def 
  @account
end

#backtestObject (readonly)

Historical backtests + parameter sweeps on the Rust engine.



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

def backtest
  @backtest
end

#marketObject (readonly)

REST market-data + reference endpoints (public plane).



36
37
38
# File 'lib/melaya.rb', line 36

def market
  @market
end

#simObject (readonly)

Paper trading (sim broker): virtual balance, positions, and orders.



40
41
42
# File 'lib/melaya.rb', line 40

def sim
  @sim
end

#strategiesObject (readonly)

Launch, control, and inspect trading strategies (paper + live).



42
43
44
# File 'lib/melaya.rb', line 42

def strategies
  @strategies
end

#streamObject (readonly)

WebSocket streaming endpoints (public market data + private feeds).



46
47
48
# File 'lib/melaya.rb', line 46

def stream
  @stream
end

#tradeObject (readonly)

Live trading — credentialed order placement and account state on a connected exchange. WARNING: real funds.



48
49
50
# File 'lib/melaya.rb', line 48

def trade
  @trade
end