Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ccxt-dev/ccxt
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed Jun 5, 2018
2 parents b7b8d08 + c9c7019 commit bda5cbd
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 21 deletions.
11 changes: 8 additions & 3 deletions build/ccxt.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ccxt.js
Expand Up @@ -37,7 +37,7 @@ const Exchange = require ('./js/base/Exchange')
//-----------------------------------------------------------------------------
// this is updated by vss.js when building

const version = '1.14.133'
const version = '1.14.134'

Exchange.ccxtVersion = version

Expand Down
69 changes: 61 additions & 8 deletions js/base/Exchange.js
Expand Up @@ -182,15 +182,68 @@ module.exports = class Exchange {
this.proxy = ''
this.origin = '*' // CORS origin

this.iso8601 = timestamp => ((typeof timestamp === 'undefined') ? timestamp : new Date (timestamp).toISOString ())
this.parse8601 = x => Date.parse ((((x.indexOf ('+') >= 0) || (x.slice (-1) === 'Z')) ? x : (x + 'Z').replace (/\s(\d\d):/, 'T$1:')))
this.parseDate = (x) => {
if (typeof x === 'undefined')
return x
return ((x.indexOf ('GMT') >= 0) ?
Date.parse (x) :
this.parse8601 (x))
this.iso8601 = (timestamp) => {
const _timestampNumber = parseInt (timestamp, 10);

// undefined, null and lots of nasty non-numeric values yield NaN
if (isNaN (_timestampNumber) || _timestampNumber < 0) {
return undefined;
}

if (_timestampNumber < 0) {
return undefined;
}

// last line of defence
try {
return new Date (_timestampNumber).toISOString ();
} catch (e) {
return undefined;
}
}

this.parse8601 = (x) => {
if (typeof x !== 'string' || !x) {
return undefined;
}

if (x.match (/^[0-9]+$/)) {
// a valid number in a string, not a date.
return undefined;
}

if (x.indexOf ('-') < 0 || x.indexOf (':') < 0) { // no date can be without a dash and a colon
return undefined;
}

// last line of defence
try {
const candidate = Date.parse (((x.indexOf ('+') >= 0) || (x.slice (-1) === 'Z')) ? x : (x + 'Z').replace (/\s(\d\d):/, 'T$1:'));
if (isNaN (candidate)) {
return undefined;
}
return candidate;
} catch (e) {
return undefined;
}
}

this.parseDate = (x) => {
if (typeof x !== 'string' || !x) {
return undefined;
}

if (x.indexOf ('GMT') >= 0) {
try {
return Date.parse (x);
} catch (e) {
return undefined;
}
}

return this.parse8601 (x);
}

this.microseconds = () => now () * 1000 // TODO: utilize performance.now for that purpose
this.seconds = () => Math.floor (now () / 1000)

Expand Down
72 changes: 72 additions & 0 deletions js/test/base/functions/test.datetime.js
@@ -0,0 +1,72 @@
'use strict'

/* ------------------------------------------------------------------------ */

const { setTimeout_safe
, timeout
, now
, isWindows,
Exchange } = require ('../../../../ccxt')

// const { Exchange, keys, values, unique, index, aggregate } = require ('../../../ccxt')

const { strictEqual: equal } = require ('assert')

/* ------------------------------------------------------------------------ */

it ('iso8601', done => {
const exchange = new Exchange ({
'id': 'chernobyl'
});

equal (exchange.iso8601(514862627000), '1986-04-26T01:23:47.000Z');
equal (exchange.iso8601(0), '1970-01-01T00:00:00.000Z');

equal (exchange.iso8601(-1), undefined);
equal (exchange.iso8601(), undefined);
equal (exchange.iso8601(null), undefined);
equal (exchange.iso8601(''), undefined);
equal (exchange.iso8601('a'), undefined);
equal (exchange.iso8601({}), undefined);

done();
});

/* ------------------------------------------------------------------------ */

it ('parse8601', done => {
const exchange = new Exchange ({
'id': 'fukusima'
});

equal(exchange.parse8601('1986-04-26T01:23:47.000Z'), 514862627000);

equal(exchange.parse8601('1977-13-13T00:00:00.000Z'), undefined);
equal(exchange.parse8601('1986-04-26T25:71:47.000Z'), undefined);

equal(exchange.parse8601('3333'), undefined);
equal(exchange.parse8601('Sr90'), undefined);
equal(exchange.parse8601(''), undefined);
equal(exchange.parse8601(), undefined);
equal(exchange.parse8601(null), undefined);
equal(exchange.parse8601({}), undefined);
equal(exchange.parse8601(33), undefined);

done();
});

/* ------------------------------------------------------------------------ */

it ('parseDate', done => {
const exchange = new Exchange ({
'id': 'TMI'
});

equal(exchange.parseDate('1986-04-26 00:00:00'), 514857600000);
equal(exchange.parseDate('1986-04-26T01:23:47.000Z'), 514862627000);
equal(exchange.parseDate('1986-13-13 00:00:00'), undefined);

done();
});

/* ------------------------------------------------------------------------ */
1 change: 1 addition & 0 deletions js/test/base/test.base.js
Expand Up @@ -19,6 +19,7 @@ describe ('ccxt base code', () => {
require ('./functions/test.number')
require ('./functions/test.time')
require ('./functions/test.type')
require ('./functions/test.datetime')

/* ------------------------------------------------------------------------ */

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "ccxt",
"version": "1.14.133",
"version": "1.14.134",
"description": "A JavaScript / Python / PHP cryptocurrency trading library with support for 100+ exchanges",
"main": "./ccxt.js",
"unpkg": "build/ccxt.browser.js",
Expand Down
2 changes: 1 addition & 1 deletion php/Exchange.php
Expand Up @@ -30,7 +30,7 @@

namespace ccxt;

$version = '1.14.133';
$version = '1.14.134';

// rounding mode
const TRUNCATE = 0;
Expand Down
7 changes: 6 additions & 1 deletion php/kraken.php
Expand Up @@ -629,8 +629,13 @@ public function create_order ($symbol, $type, $side, $amount, $price = null, $pa
'ordertype' => $type,
'volume' => $this->amount_to_precision($symbol, $amount),
);
if ($type === 'limit')
$priceIsDefined = ($price !== null);
$marketOrder = ($type === 'market');
$limitOrder = ($type === 'limit');
$shouldIncludePrice = $limitOrder || (!$marketOrder && $priceIsDefined);
if ($shouldIncludePrice) {
$order['price'] = $this->price_to_precision($symbol, $price);
}
$response = $this->privatePostAddOrder (array_merge ($order, $params));
$id = $this->safe_value($response['result'], 'txid');
if ($id !== null) {
Expand Down
2 changes: 1 addition & 1 deletion python/ccxt/__init__.py
Expand Up @@ -22,7 +22,7 @@

# ----------------------------------------------------------------------------

__version__ = '1.14.133'
__version__ = '1.14.134'

# ----------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion python/ccxt/async/__init__.py
Expand Up @@ -4,7 +4,7 @@

# -----------------------------------------------------------------------------

__version__ = '1.14.133'
__version__ = '1.14.134'

# -----------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion python/ccxt/async/base/exchange.py
Expand Up @@ -2,7 +2,7 @@

# -----------------------------------------------------------------------------

__version__ = '1.14.133'
__version__ = '1.14.134'

# -----------------------------------------------------------------------------

Expand Down
6 changes: 5 additions & 1 deletion python/ccxt/async/kraken.py
Expand Up @@ -615,7 +615,11 @@ async def create_order(self, symbol, type, side, amount, price=None, params={}):
'ordertype': type,
'volume': self.amount_to_precision(symbol, amount),
}
if type == 'limit':
priceIsDefined = (price is not None)
marketOrder = (type == 'market')
limitOrder = (type == 'limit')
shouldIncludePrice = limitOrder or (not marketOrder and priceIsDefined)
if shouldIncludePrice:
order['price'] = self.price_to_precision(symbol, price)
response = await self.privatePostAddOrder(self.extend(order, params))
id = self.safe_value(response['result'], 'txid')
Expand Down
2 changes: 1 addition & 1 deletion python/ccxt/base/exchange.py
Expand Up @@ -4,7 +4,7 @@

# -----------------------------------------------------------------------------

__version__ = '1.14.133'
__version__ = '1.14.134'

# -----------------------------------------------------------------------------

Expand Down
6 changes: 5 additions & 1 deletion python/ccxt/kraken.py
Expand Up @@ -615,7 +615,11 @@ def create_order(self, symbol, type, side, amount, price=None, params={}):
'ordertype': type,
'volume': self.amount_to_precision(symbol, amount),
}
if type == 'limit':
priceIsDefined = (price is not None)
marketOrder = (type == 'market')
limitOrder = (type == 'limit')
shouldIncludePrice = limitOrder or (not marketOrder and priceIsDefined)
if shouldIncludePrice:
order['price'] = self.price_to_precision(symbol, price)
response = self.privatePostAddOrder(self.extend(order, params))
id = self.safe_value(response['result'], 'txid')
Expand Down

0 comments on commit bda5cbd

Please sign in to comment.