LoginSignup
17
16

More than 5 years have passed since last update.

Zaif の Trade API を Ruby で操作する

Last updated at Posted at 2014-06-30

国内の BitCash, Monacoin の交換所である https://zaif.jp/ は,Trade API を提供しています.
これを使うと,自動トレードを行うbotを自作できます.
(私は,日曜日の趣味プログラミングで,作りました.)

コード例

コードを示したほうが早いと思うので.細かい説明は省きます.

API の説明ページにあるとおりに愚直に実装しています.
secret_key と public_key は https://zaif.jp/api_keys で生成します.
perms は,生成後に上記ページのチェックボックスを入れ,"Save"をクリックして設定します.
お使いになりたい API により決まりますが,info と trade は要るでしょう.

API クラスの使い方

public_key = ENV['public_key']
secret_key = ENV['secret_key']
api = Etwings::TradeApi.new(public_key, secret_key)

などしたあとで,

  info = api.post('get_info', {})

    trade = api.post('trade', { "currency_pair" => "mona_jpy", "action" => "ask", "price" => "#{new_price}", "amount" => "#{mona}" })

などします.

# Copyright (C) 2014 Masaki "monaka" Muranaka
# MIT License
#
# -*- coding: utf-8 -*-
require 'json'
require 'openssl'
require 'net/https'
require 'pp'

module Etwings
  class TradeApi
    attr_reader :result
    attr_reader :response

    def initialize public_key, secret_key
      @public_key = public_key
      @secret_key = secret_key
      @response = nil
      @result = nil
      @nonce = Time.new.to_i
    end

    def post(method, params)
      @nonce += 1
      hmac = OpenSSL::HMAC.new(@secret_key, OpenSSL::Digest::SHA512.new)
      params['nonce'] = @nonce.to_s
      params['method'] = method
      param = params.map { |k,v| "#{k}=#{v}" }.join("&")
      digest = hmac.update(param)

      http = Net::HTTP.new('exchange.etwings.com', 443)
      http.use_ssl = true
      http.set_debug_output(STDERR)
      headers = {
        'Content-Type' => 'application/x-www-form-urlencoded',
        'Key' => @public_key,
        'Sign' => digest.to_s }
      path = '/tapi'
      response = http.post(path, URI.escape(param), headers)

      if response.code.to_i == 200
        @result = JSON.parse(response.body)
      else
        @result = nil
      end
      @response = response
      @result
    end
  end

  class PublicApi
    attr_reader :uri
    attr_reader :response
    attr_reader :result
    def initialize(uri)
      @uri = uri
    end
    def get()
      max_retry_count = 5
      url = URI.parse(@uri)
      response = nil
      max_retry_count.times do |retry_count|
        http = Net::HTTP.new(url.host, url.port)
        http.use_ssl = true if (443==url.port)

        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        http.verify_depth = 5
        response = http.get( url.path )

        case response
        when Net::HTTPSuccess
          break
        when Net::HTTPRedirection
          url = URI.parse(response['Location'])
          next
        else
          break
        end
      end
      if response.code.to_i == 200
        @result = JSON.parse(response.body)
      else
        @result = nil
      end
      @response = response
      @result
    end
  end
end

17
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16