GithubHelp home page GithubHelp logo

bogdan-pr / okcoin-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from trexmatt/okcoin-api

0.0 1.0 0.0 140 KB

Python wrapper around the API for the Chinese Bitcoin exchange OKCoin

License: MIT License

okcoin-api's Introduction

OKCoin-API

This is a simple Python wrapper around the OKCoin.com public and trading API.

It took me a while to figure out how to use the API as all the documentation is in Chinese (and in some cases just bad), so I hope this saves someone some time and frustration. In case you'd like to write your own code to access the API, I explain how to create the signed string you need for requests in the last section. For reference, the OkCoin trade API page is http://www.okcoin.com/t-1000097.html

If this helps you and you'd like to donate, please send coins to

BTC 19smwQpihXPeXKihG7RxvSvwMoyyggvw3g
LTC LfPfo5dn7xSkwrXMgd6ZymPTeQGFJtrcgX

Happy trading :)

Disclaimer

While I've used this code myself and believe it to be error free, I cannot guarantee nothing will go wrong. Please read through the code and make changes as you see fit. If you see anything that doesn't seem right, let me know!

IMPORTANT NOTE: OKCoin does not use a nonce value! This means that if something goes wrong and your program keeps sending the same trade request, OKCoin will keep executing it until you run out of money. I saw someone had requested they implement a nonce on the support site and hopefully this happens soon.

OKCoin is not affiliated with this project. Real trading APIs are included. Use at your own risk.

Getting Started

  1. The first thing to do is request a partner id and secret key from the OKCoin support on QQ. You can use QQ if you're not in China. Support was pretty fast when I did this and it only took ~15 minutes.

  2. Next download the okcoin.py file. I haven't included a setup.py as the whole API is quite small. Only the following standard libraries are needed

     urllib, urllib2, hashlib, simplejson
    
  3. Initialize the trade api with your secret key and partner id. It's probably best to load these from an external file rather than saving them in your trading program. For getting market data you don't need to do this.

     # Get account balance
    
     import okcoin
    
     partner = your_partner_int_here
     secret_key = your_secret_key_str_here
    
     T = okcoin.TradeAPI(partner, secret_key)
    
     print( T.getinfo() )
    

Request Structure

If you're using the okcoin.py file as is, ignore this section.

If you'd like to write your own code for the API, I explain the request structure below. This took a while to figure out as the documentation on the OKCoin website is lacking (and Google Translate probably didn't help).

  1. Take the list of parameters and values you're requesting, sort it alphabetically and then join each with "&" Note that all parameter names must be lowercase and have no spaces. The example they show is on the page (http://www.okcoin.com/t-1000097.html) is actually incorrect because of this.

     e.g. "amount=1.0&partner=2088101568338364&rate=680&symbol=btc_cny&type=buy"
    
  2. Take the string you made above and add your secret key to the end of it with no spaces OR "&"

     e.g. "amount=1.0&partner=2088101568338364&rate=680&symbol=btc_cny&type=buy111111111111111111"
    
  3. MD5 hash that string, convert it to hex, make it uppercase then URL encode it and POST to the relevant page (depending on what request you're making).

     # Calculating signed string to get account balance
    
     import urllib
     import urllib2
     import hashlib
     import simplejson
     
     # partner is int
     # secret_key is string
    
     partner = 1111111111
     secret_key = 'THISISNOTANACTUALKEYOBVIOUSLY'
      
     user_info_url = 'https://www.okcoin.com/api/userinfo.do'
     sign_string = 'partner=' + str(partner)
      
     m = hashlib.md5()
     m.update(sign_string + secret_key)
     signed = m.hexdigest().upper()
      
     values = {'partner' : partner,
               'sign' : signed}
      
     data = urllib.urlencode(values)
     req = urllib2.Request(user_info_url, data)
     response = urllib2.urlopen(req)
     result = simplejson.load(response)
      
     print( result['info']['funds']['free'] )
    

okcoin-api's People

Contributors

trexmatt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.