API reference

This page contains documentation of all methods available in ziwia.

ziwia.Api This class is responsible for creatin Kraken API calls.
ziwia.Api.__init__ Constructor.
ziwia.Api.public Creates a public API call.
ziwia.Api.time Gets current server time.
ziwia.Api.assets Returns Assets from Kraken.
ziwia.Api.asset_pairs Returns Assets Pairs from Kraken.
ziwia.Api.ticker Gets pair name ticker values like.
ziwia.Api.ohlc Returns OLHC asset pairs values.
ziwia.Api.depth Returns market depth for selected asset pairs.
ziwia.Api.trades Get recent trades of selected asset pairs.
ziwia.Api.spread Get recent spread data of selected asset pairs.
ziwia.Api.private Perform a private API call.
ziwia.Api.balance Gets User account balance in all currencies.
ziwia.Api.trade_balance Gets User trade balance.
ziwia.Api.open_orders Gets User opened orders.
ziwia.Api.closed_orders Returns closed orders according to parameters.
ziwia.Api.query_orders Returns orders info.
ziwia.Api.trades_history Gets trades history.
ziwia.Api.query_trades Returns trade info.
ziwia.Api.open_positions Returns open positions list.
ziwia.Api.ledgers Returns ledgers list.
ziwia.Api.query_ledgers Returns ledger info.
ziwia.Api.trade_volume Gets trade volume.
ziwia.Api.add_order Adds exchange order to your account.
ziwia.Api.cancel_order Cancels order.

Methods

class ziwia.Api(public_key='', private_key='', timeout=30, proxy=None)

This class is responsible for creatin Kraken API calls. Ziwia 0.2 supports Kraken API version 0. It exposes two basic methods - public() which calls public part of API. It does not require you to fill public and private keys. Second one is private() method which, as name suggests, calls private part.

Apart of these two methods, there are a few ones supporting direct requests. They check inputs and sometimes could save you some time testing your application.

public(request, parameters=None)

Creates a public API call. It does not require public and private keys to be set.

Parameters:
  • request (str) – One of Time, Assets, AssetPairs, Ticker, OHLC, Depth, Trades and Spread.
  • parameters (dict) – Additional parameters for each request. See Kraken documentation for more information.
Returns:

JSON object representing Kraken API response.

Raises:

ValueError, if request string is not valid. ConnectionError and ConnectionTimeout on connection problem.

time()

Gets current server time. See Kraken documentation.

Output example:

{  
    "error":[  
    
    ],
    "result":{  
        "rfc1123":"Wed, 10 May 17 10:32:24 +0000",
        "unixtime":1494412344
    }
}
Returns:Response as JSON object.
Raises:Any of public() method exceptions.
assets(assets='')

Returns Assets from Kraken. See Kraken documentation

Output example:

{  
    "error":[  

    ],
    "result":{  
        "XMLN":{  
            "altname":"MLN",
            "decimals":10,
            "display_decimals":5,
            "aclass":"currency"
        },
        
        ...
    }
}
Parameters:assets (str) – Comma separated list of assets you are interested in, or empty for all.
Returns:Response as JSON object.
Raises:Same exceptions as public() method.
asset_pairs(info='info', pairs='')

Returns Assets Pairs from Kraken. See Kraken documentation.

Output example:

{  
    "error":[  

    ],
    "result":{  
        "XXRPXXBT":{  
            "pair_decimals":8,
            "lot":"unit",
            "margin_call":80,
            "fees_maker":[  
                [  
                    0,
                    0.16
                ],
                ...
            ],
            "altname":"XRPXBT",
            "quote":"XXBT",
            "fees":[  
                [  
                    0,
                    0.26
                ],
                ...
            ],
            "aclass_quote":"currency",
            "margin_stop":40,
            "base":"XXRP",
            "lot_multiplier":1,
            "fee_volume_currency":"ZUSD",
            "aclass_base":"currency",
            "leverage_sell":[  

            ],
            "leverage_buy":[  

            ],
            "lot_decimals":8
        },
        ...
    }
}
Parameters:
  • info – One of info, leverage, fees, margin.
  • pairs – Comma separated list of asset pairs you are interested in, or empty for all.
Returns:

Response as JSON object.

Raises:

ValueError if info parameter is not allowed.

ticker(pairs)

Gets pair name ticker values like. See Kraken documentation.

Output example:

{  
    "error":[  

    ],
    "result":{  
        "XZECZEUR":{  
            "t":[  
                789,
                1563
            ],
            "h":[  
                "93.88889",
                "93.88889"
            ],
            "l":[  
                "83.30000",
                "82.90067"
            ],
            "a":[  
                "91.13167",
                "1",
                "1.000"
            ],
            "b":[  
                "91.13166",
                "1",
                "1.000"
            ],
            "v":[  
                "1409.83478992",
                "3185.73131989"
            ],
            "p":[  
                "90.08671",
                "87.74001"
            ],
            "c":[  
                "91.13068",
                "3.23282073"
            ],
            "o":"85.87561"
        }
    }
}
Parameters:pairs (str) – Comma separated list of asset pairs you are insterested in.
Returns:Response as JSON object.
Raises:All of public() method exceptions.
ohlc(pairs, interval=1, since=0)

Returns OLHC asset pairs values. See Kraken documentation.

Output example:

{  
    "error":[  

    ],
    "result":{  
        "last":1494422340,
        "XZECZEUR":[  
            [  
                1494379260,
                "86.93479",
                "86.93479",
                "86.93479",
                "86.93479",
                "0.00000",
                "0.00000000",
                0
            ],
            ...
        ]
    }
}
Parameters:
  • pairs (str) – Comma separated list of asset pairs you are insterested in, or empty for all.
  • interval (int) – One of 1, 5, 15, 30, 60, 240, 1440, 10080 or 21600 representing time frame in minutes.
  • since (int) – ID of the last OLHC record since which you would like to get OLHC info, of zero for no limit.
Returns:

Response as JSON object.

Raises:

Any of public() method exceptions.

depth(pairs, count=0)

Returns market depth for selected asset pairs. See Kraken documentation.

Output example:

{  
    "error":[  

    ],
    "result":{  
        "XZECZEUR":{  
            "asks":[  
                [  
                    "89.92088",
                    "34.910",
                    1494422730
                ],
                ...
            ],
            "bids":[  
                [  
                    "88.42616",
                    "4.755",
                    1494422729
                ],
                ...
            ]
        }
    }
}
Parameters:
  • pairs (str) – Comma separated list of asset pairs you are insterested in, or empty for all.
  • count (int) – Maximum number of bids/asks or zero for no limit.
Returns:

Response as JSON object.

Raises:

Any of public() method exceptions.

trades(pairs, since=0)

Get recent trades of selected asset pairs. See Kraken documentation.

Output example:

{  
    "error":[  

    ],
    "result":{  
        "last":"1494423192560021193",
        "XZECZEUR":[  
            [  
                "86.20035",
                "0.44929000",
                1494369147.0533,
                "b",
                "l",
                ""
            ],
            ...
        ]
    }
}
Parameters:
  • pairs (str) – Comma separated list of asset pairs you are insterested in, or empty for all.
  • since (int) – ID of the last OLHC record since which you would like to get OLHC info, or zero for no limit.
Returns:

Response as JSON object.

Raises:

Any of public() method exceptions.

spread(pairs, since=0)

Get recent spread data of selected asset pairs. See Kraken documentation.

Output example:

{  
    "error":[  

    ],
    "result":{  
        "XZECZEUR":[  
            [  
                1494423011,
                "88.42539",
                "88.91550"
            ],
            ...
        ],
        "last":1494423011
    }
}
Parameters:
  • pairs (str) – Comma separated list of asset pairs you are insterested in, or empty for all.
  • since (int) – ID of the last OLHC record since which you would like to get OLHC info, of zero for no limit.
Returns:

Response as JSON object.

Raises:

Any of public() method exceptions.

private(request, parameters=None)

Perform a private API call.

Warning:

Calling this function requires public and private keys to be set in class constructor.

Parameters:
  • request – One of Balance, TradeBalance, OpenOrders, ClosedOrders, QueryOrders, TradesHistory, QueryTrades, OpenPositions, Ledgers, QueryLedgers, TradeVolume, AddOrder, CancelOrder.
  • parameters – Additional parameters for each request. See Kraken documentation for more information.
Returns:

A JSON object representing Kraken API response.

Raises:

ValueError is request string is not supported. ConnectionError and ConnectionTimeout on connection problem.

balance()

Gets User account balance in all currencies. See Kraken documentation.

Returns:Response as JSON object.
Raises:Any of private method exceptions.
trade_balance(asset='ZUSD')

Gets User trade balance. See Kraken documentation.

Parameters:asset – Base asset used to determine balance.
Returns:Response as JSON object.
Raises:Any of private method exceptions.
open_orders(trades=False, userref='')

Gets User opened orders. See Kraken documentation.

Parameters:
  • trades – Include trades in output? True/False.
  • userref – Restrict results to given user reference ID.
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

closed_orders(trades=False, userref='', start='', end='', offset='', closetime='both')

Returns closed orders according to parameters. See Kraken documentation.

Parameters:
  • trades – Include trades in response?
  • userref – Restrict response to given user reference ID.
  • start – Starting timestamp or order ID.
  • end – End timestamp or order ID.
  • offset – Result offset.
  • closetime – Which time to use. One of open, close, both.
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

query_orders(trades=False, userref='', txid='')

Returns orders info. See Kraken documentation.

Parameters:
  • trades – Include trades in output?
  • userref – User reference ID.
  • txid – Comma separated list of transaction IDs. Max 2O.
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

trades_history(ttype='all', trades=False, start='', end='', offset='')

Gets trades history. See Kraken documentation.

Parameters:
  • ttype – Trade type. Method checks right input - it can be one of all, any, closed, no.
  • trades – Include trades related to position in result?
  • start – Unix timestamp or trade ID.
  • end – Unix timestamp of trade ID.
  • offset – History offset.
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

query_trades(trades=False, txid='')

Returns trade info. See Kraken documentation.

Parameters:
  • trades – Include trades related to position in response.
  • txid – Comma separated list of transaction IDs. Max 2O.
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

open_positions(docalcs=False, txid='')

Returns open positions list. See Kraken documentation.

Parameters:
  • txid – Comma delimited list of transaction IDs to restrict output to
  • docalcs – Include profit/loss calculations.
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

ledgers(asset='', ltype='all', start=0, end=0, offset=0)

Returns ledgers list. See Kraken documentation.

Parameters:
  • asset (str) – Comma delimited list of assets to restrict output to.
  • ltype (str) – Type of ledger to retrieve. One of all, deposit, withdrawal , trade and margin.
  • or str start (int) – Starting unix timestamp or ledger ID of results.
  • or str end (int) – Ending unix timestamp or ledger ID of results.
  • offset (int) – Result offset.
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

query_ledgers(lid)

Returns ledger info. See Kraken documentation.

Parameters:lid – Comma delimited list of ledger ids to query info about. Max 2O.
Returns:Response as JSON object.
Raises:Any of private method exceptions.
trade_volume(pair='', fee_info=False)

Gets trade volume. See Kraken documentation.

Parameters:
  • pair – Comma delimited list of asset pairs to get fee info on.
  • fee_info – Include fee info in results?
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

add_order(pair, otype, ordertype, price, volume, price2=-1, leverage='none', oflags='', starttm=0, expiretm=0, userref='', validate=False)

Adds exchange order to your account. See Kraken documentation.

Parameters:
  • pair – Asset pair.
  • otype – Type of order (buy/sell).
  • ordertype – Order type. Method tests right input to this parameter and thus may raise ValueError. Only following values are allowed - market, stop-loss, take-profit, stop-loss-profit, stop-loss-profit-limit, stop-loss-limit, take-profit-limit, trailing-stop, trailing-stop-limit stop-loss-and-limit and settle-position. :param price: Price, meaning depends on order type. :param volume: Order volume in lots.
  • price2 – Price, meaning depends on order type.
  • leverage – Amount of desired leverage.
  • oflags – Comma separated flags: “viqc”, “fcib”, “fciq”, “nompp”.
  • starttm – Scheduled start time. Zero (now) or unix timestamp.
  • expiretm – Expiration time. Zero (never) or unix timestamp.
  • userref – User reference id. 32-bit signed number.
  • validate – Calidate inputs only. Do not submit order.
Returns:

Response as JSON object.

Raises:

Any of private method exceptions.

cancel_order(txid)

Cancels order. See Kraken documentation.

Parameters:txid – Transaction id.
Returns:Response as JSON object.
Raises:Any of private method exceptions.