python交易所搬砖_ccxt使⽤笔记1-单币种跨交易所搬砖练习ccxt很⽅便,把各个交易所的接⼝统⼀化了,使⽤起来很⽅便,⼊门笔记记录在此,熟悉了这些api后就可以⽅便的搬砖了。
school rumble⾃⼰写了个跨交易所搬砖的demo,⽬前可以在指定交易对和交易所的情况下找出当前时间卖的最便宜的(最低卖⼀)进⾏买⼊,和买的最贵的(最⾼买⼀)进⾏卖出,这样低买⾼卖搬砖套利,⾃动下单交易的代码,那⼀部分需要接⼊个⼈账户的key,另外看到这种⽅法实际搬砖利润不⼤,还有⼿续费,交易滑点等因素就没有实际操作,代码详见
⽂档地址:
Exchange 交易所
['binance', 'fcoin', 'gateio', 'huobi', 'kucoin', 'okex']
['BTC', 'ETH', 'XRP', 'BCH', 'EOS', 'XLM', 'LTC', 'ADA', 'XMR', 'TRX', 'BNB', 'ONT', 'NEO', 'DCR']
The structure of the library can be outlined as follows:
Ur
+-------------------------------------------------------------+
| CCXT |
+------------------------------+------------------------------+
| Public | Private |
+=============================================================+
│ . |
│ The Unified CCXT API |
│ . |
| loadMarkets . fetchBalance |
| fetchMarkets . createOrder |
| fetchCurrencies . cancelOrder |
| fetchTicker . fetchOrder |
| fetchTickers . fetchOrders |
| fetchOrderBook . fetchOpenOrders |
| fetchOHLCV . fetchClodOrders |
| fetchTrades . fetchMyTrades |
| . deposit |
| . withdraw |
│ . |
+=============================================================+
│ . |
| Custom Exchange API |
| (Derived Class And Their Implicit Methods) |
│ . |
| . |
| . |
| . |
| . |
| . sign |
│ . |godbless
+=============================================================+
│ . |
| Ba Exchange Class |
│ . |
+=============================================================+
实例化交易所
# Python
import ccxt
exchange = ccxt.okcoinusd () # default id
okcoin1 = ccxt.okcoinusd ({ 'id': 'okcoin1' })
okcoin2 = ccxt.okcoinusd ({ 'id': 'okcoin2' })
id = 'btcchina'
btcchina = eval ('ccxt.%s ()' % id)
gdax = getattr (ccxt, 'gdax') ()
# from variable idfavorite sport
exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': 'YOUR_API_KEY',
'cret': 'YOUR_SECRET',
'timeout': 30000,
'enableRateLimit': True,
})
Exchange Structure 交易所结构
Every exchange has a t of properties and methods, most of which you can override by passing an associative array of params to an exchange constructor. You can also make a subclass and override everything.
我的生日派对Here's an overview of ba exchange properties with values added for example:
{
'id': 'exchange' // lowerca string exchange id
'name': 'Exchange' // human-readable string
'countries': [ 'US', 'CN', 'EU' ], // array of ISO country codes
alke
'urls': {
'api': '/data', // string or dictionary of ba API URLs 'www': '' // string website URL
'doc': '/api', // string URL or array of URLs
},
'version': 'v1', // string ending with digits
'api': { ... }, // dictionary of api endpoints
'has': { // exchange capabilities
'CORS': fal,
'publicAPI': true,
'privateAPI': true,
'cancelOrder': true,
'createDepositAddress': fal,
'createOrder': true,
'deposit': fal,
'fetchBalance': true,
'fetchClodOrders': fal,
'fetchCurrencies': fal,
'fetchDepositAddress': fal,
'fetchMarkets': true,
'fetchMyTrades': fal,
'fetchOHLCV': fal,
'fetchOpenOrders': fal,
'fetchOrder': fal,
'fetchOrderBook': true,
'fetchOrders': fal,
'fetchTicker': true,
'fetchTickers': fal,
'fetchBidsAsks': fal,
'fetchTrades': true,
'withdraw': fal,
},
'timeframes': { // empty if the exchange !has.fetchOHLCV
'1m': '1minute',
'1h': '1hour',
'1d': '1day',
'1M': '1month',
'1y': '1year',
},jailbreak
'timeout': 10000, // number in milliconds
'rateLimit': 2000, // number in milliconds
'urAgent': 'ccxt/1.1.1 ...' // string, HTTP Ur-Agent header
'verbo': fal, // boolean, output error details
英语角主题
'markets': { ... } // dictionary of markets/pairs by symbol
'symbols': [ ... ] // sorted list of string symbols (traded pairs)
'currencies': { ... } // dictionary of currencies by currency code
'markets_by_id': { ... }, // dictionary of dictionaries (markets) by id
'proxy': '/', // string URL
'apiKey': '', // string public apiKey (ASCII, hex, Ba64, ...)
'cret': '9aHjPmW+' // string private cret key
'password': '6kszf4aci8r', // string password
'uid': '123456', // string ur id
}
Markets 市场交易对信息
Each exchange is a place for trading some kinds of valuables. Sometimes they are called with various different terms like instruments, symbols, trading pairs, currencies, tokens, stocks, commodities, contracts, etc, but they all mean the same – a trading pair, a symbol or a financial instrument.
In terms of the ccxt library, every exchange offers multiple markets within itlf. The t of markets differs from exchange to exchange opening possibilities for cross-exchange and cross-market arbitrage. A market is usually a pair of traded
crypto/fiat currencies.
Market Structure
{
'id': 'btcusd', // string literal for referencing within an exchange
'symbol': 'BTC/USD', // upperca string literal of a pair of currencies
'ba': 'BTC', // upperca string, ba currency, 3 or more letters
awkwardly'quote': 'USD', // upperca string, quote currency, 3 or more letters
'active': true, // boolean, market status
'precision': { // number of decimal digits "after the dot"
'price': 8, // integer
'amount': 8, // integer
'cost': 8, // integer
},
'limits': { // value limits when placing orders on this market
'amount': {
'min': 0.01, // order amount should be > min
'max': 1000, // order amount should be < max
},
office培训
'price': { ... }, // same min/max limits for the price of the order
'cost': { ... }, // same limits for order cost = price * amount
},
'info': { ... }, // the original unpard market info from the exchange
}
gate_markets['BTC/USDT']
Naming Consistency
food safety
There is a bit of term ambiguity across various exchanges that may cau confusion among newcoming traders. Some exchanges call markets as pairs, whereas other exchanges call symbols as products. In terms of the ccxt library, each exchange contains one or more trading markets. Each market has an id and a symbol. Most symbols are pairs of ba currency and quote currency.
Exchanges → Markets → Symbols → Currencies
remember the following rule: ba is always before the slash, quote is always after the slash in any symbol and with any market.
ba currency ↓
BTC / USDT
ETH / BTC
DASH / ETH
↑ quote currency
API 接⼝列表
The endpoints definition is a full list of ALL API URLs expod by an exchange. This list gets converted to callable methods upon exchange instantiation. Each URL in the API endpoint list gets a corresponding callable method. This is done automatically for all exchanges, therefore the ccxt library supports all possible URLs offered by crypto exchanges.
gate.api['public']['get']