LoginSignup
27
26

More than 5 years have passed since last update.

zaif(取引所)でAPIを使って取引する

Last updated at Posted at 2015-01-25

はじめに

  • この記事はnode.jsのモジュールzaif.jpを使った記事になります
  • private apiと呼ばれる個人認証が必要なAPIを説明した記事になります
  • monacoinなどの仮想通貨の取引所の記事になります

事前準備

1.モジュールをインストールします

npm install zaif.jp

2.zaifからapi keyとsecret keyを取得します

ログイン後以下のページにて作成
アカウント -> API Keys

api使用例

認証ファイルはソース埋め込みではなくjsonにしておくことをおすすめします
gitでソースを管理するのであればconfig.jsonは.gitignoreに登録しておくと大惨事を防げます。

config.json
{
 "apikey" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 "secretkey" : "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
}

JSONファイルの読み込み~初期化は以下のように行います

var fs = require('fs');
var config = JSON.parse(fs.readFileSync('./config.json'));
var zaif = require('zaif.jp');
var api = zaif.createPrivateApi(config.apikey, config.secretkey, 'user agent is node-zaif');

残高を取得

api.getInfo().then(function(res){
    console.log(res)
});

買い注文をする

monaを5円で10000枚買う

api.trade('mona_jpy', 'bid', 5, 10000).then(function(res){
    console.log(res)
});

売り注文をする

monaを5円で10000枚売る

api.trade('mona_jpy', 'ask', 5, 10000).then(function(res){
    console.log(res)
});

有効な注文中一覧を取得する

板情報に乗っている自分の注文を取得

api.activeOrders().then(function(res){
    console.log(res)
});

注文をキャンセルする

orderidは注文した時に保存しておくかactiveOrdersの結果から取得する

var orderid = 10001;
api.cancelOrder(orderid).then(function(res){
    console.log(res)
});

使い道

このライブラリを使うとしたら以下の様な用途だと思います

  • 暗号通貨を入金して円に自動で両替
  • 他取引所との裁定取引
  • トラップリピートイフダンなどのスプレッド売買
  • RSI、MACDなどを利用したテクニカル売買
  • 暗号通貨を時間をかけてゆっくり仕込みたい

このライブラリを公開した目的はもっとbitcoinやmonacoinの流動性が上がってほしいと思い公開しています。
pump and dumpとか正直困るので。。。

※MACD使えるnode.js製のトレーディングボットにgekkoというのがあります( https://github.com/askmike/gekko )

プログラム例

発注している注文を全てキャンセルする

var Promise = require('bluebird');
api.activeOrders().then(function(res){
    return Promise.all(
        Object.keys(res).map(function(key){
            return api.cancelOrder(key);
        })
    );
});

買い注文だけ消したい場合

var Promise = require('bluebird');
api.activeOrders().then(function(res){
    return Promise.all(
        Object.keys(res).filter(function(key){
            return res[key].action === 'bid'
        }).map(function(key){
            return api.cancelOrder(key);
        })
    );
});

その他

裁定取引を補佐するためのライブラリもあります https://github.com/you21979/node-trade-util

不具合あると思うので自己責任でお願いします。プルリクエストも歓迎しています

27
26
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
27
26