Skip to content

Commit

Permalink
binance and bittrex fee precision
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed Oct 9, 2017
1 parent 0a0207b commit e96c9b1
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 81 deletions.
74 changes: 56 additions & 18 deletions build/ccxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -1204,20 +1204,36 @@ public function common_currency_code ($currency) {
return $currency;
}

public function limit_price_to_precision ($symbol, $price) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['price'] . 'f', $price);
public function cost_to_precision ($symbol, $cost) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['price'] . 'f', floatval ($price));
}

public function limitPriceToPrecision ($symbol, $price) {
return $this->limit_price_to_precision ($symbol, $price);
public function costToPrecision ($symbol, $cost) {
return $this->price_to_precision ($symbol, $cost);
}

public function limit_amount_to_precision ($symbol, $amount) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['amount'] . 'f', $amount);
public function price_to_precision ($symbol, $price) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['price'] . 'f', floatval ($price));
}

public function limitAmountToPrecision ($symbol, $amount) {
return $this->limit_amount_to_precision ($symbol, $amount);
public function priceToPrecision ($symbol, $price) {
return $this->price_to_precision ($symbol, $price);
}

public function amount_to_precision ($symbol, $amount) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['amount'] . 'f', floatval ($amount));
}

public function amountToPrecision ($symbol, $amount) {
return $this->amount_to_precision ($symbol, $amount);
}

public function fee_to_precision ($symbol, $fee) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['price'] . 'f', floatval ($fee));
}

public function feeToPrecision ($symbol, $fee) {
return $this->fee_to_precision ($symbol, $fee);
}

public function commonCurrencyCode ($currency) {
Expand Down Expand Up @@ -2451,7 +2467,7 @@ public function calculate_fee ($symbol, $type, $side, $amount, $price, $takerOrM
$market = $this->markets[$symbol];
$key = 'quote';
$rate = $market[$takerOrMaker];
$cost = $amount * $rate;
$cost = $this->cost_to_precision ($symbol, $amount * $rate);
if ($side == 'sell') {
$cost *= $price;
} else {
Expand All @@ -2460,7 +2476,7 @@ public function calculate_fee ($symbol, $type, $side, $amount, $price, $takerOrM
return array (
'currency' => $market[$key],
'rate' => $rate,
'cost' => $cost,
'cost' => $this->fee_to_precision ($symbol, $cost),
);
}

Expand Down Expand Up @@ -2627,6 +2643,7 @@ public function parse_order ($order, $market = null) {
}
}
$timestamp = $order['time'];
$price = floatval ($order['price']);
$amount = floatval ($order['origQty']);
$filled = $this->safe_float ($order, 'executedQty', 0.0);
$remaining = max ($amount - $filled, 0.0);
Expand All @@ -2638,11 +2655,13 @@ public function parse_order ($order, $market = null) {
'symbol' => $symbol,
'type' => strtolower ($order['type']),
'side' => strtolower ($order['side']),
'price' => floatval ($order['price']),
'price' => $price,
'amount' => $amount,
'cost' => $price * $amount,
'filled' => $filled,
'remaining' => $remaining,
'status' => $status,
'fee' => null,
);
return $result;
}
Expand All @@ -2652,13 +2671,13 @@ public function create_order ($symbol, $type, $side, $amount, $price = null, $pa
$market = $this->market ($symbol);
$order = array (
'symbol' => $market['id'],
'quantity' => sprintf ('%' . $market['precision']['amount'] . 'f', $amount),
'quantity' => $this->amount_to_precision ($symbol, $amount),
'type' => strtoupper ($type),
'side' => strtoupper ($side),
);
if ($type == 'limit') {
$order = array_merge ($order, array (
'price' => sprintf ('%' . $market['precision']['price'] . 'f', $price),
'price' => $this->price_to_precision ($symbol, $price),
'timeInForce' => 'GTC', // 'GTC' = Good To Cancel (default), 'IOC' = Immediate Or Cancel
));
}
Expand Down Expand Up @@ -6274,6 +6293,14 @@ public function __construct ($options = array ()) {
), $options));
}

public function cost_to_precision ($symbol, $cost) {
return $this->truncate ($cost, $this->markets[$symbol].precision.price);
}

public function fee_to_precision ($symbol, $fee) {
return $this->truncate ($fee, $this->markets[$symbol]['precision']['price']);
}

public function fetch_markets () {
$markets = $this->publicGetMarkets ();
$result = array ();
Expand Down Expand Up @@ -6473,10 +6500,10 @@ public function create_order ($symbol, $type, $side, $amount, $price = null, $pa
$method = 'marketGet' . $this->capitalize ($side) . $type;
$order = array (
'market' => $market['id'],
'quantity' => sprintf ('%' . $market['precision']['amount'] . 'f', $amount),
'quantity' => $this->amount_to_precision ($symbol, $amount),
);
if ($type == 'limit')
$order['rate'] = sprintf ('%' . $market['precision']['price'] . 'f', $price);
$order['rate'] = $this->price_to_precision ($symbol, $price);
$response = $this->$method (array_merge ($order, $params));
$result = array (
'info' => $response,
Expand Down Expand Up @@ -6528,9 +6555,19 @@ public function parse_order ($order, $market = null) {
'currency' => $market['quote'],
);
}
$amount = $order['Quantity'];
$remaining = $order['QuantityRemaining'];
$price = $this->safe_float ($order, 'Limit');
$cost = $this->safe_float ($order, 'Price');
$amount = $this->safe_float ($order, 'Quantity');
$remaining = $this->safe_float ($order, 'QuantityRemaining', 0.0);
$filled = $amount - $remaining;
if (!$cost) {
if ($price && $amount)
$cost = $price * $amount;
}
if (!$price) {
if ($cost && $filled)
$price = $cost / $filled;
}
$result = array (
'info' => $order,
'id' => $order['OrderUuid'],
Expand All @@ -6539,7 +6576,8 @@ public function parse_order ($order, $market = null) {
'symbol' => $symbol,
'type' => 'limit',
'side' => $side,
'price' => $order['Price'],
'price' => $price,
'cost' => $cost,
'amount' => $amount,
'filled' => $filled,
'remaining' => $remaining,
Expand Down
75 changes: 54 additions & 21 deletions ccxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,14 +778,6 @@ const Exchange = function (config) {
return currency
}

this.limitPriceToPrecision = function (symbol, price) {
return price.toFixed (this.markets[symbol].precision.price)
}

this.limitAmountToPrecision = function (symbol, amount) {
return amount.toFixed (this.markets[symbol].precision.amount)
}

this.market = function (symbol) {
return (((typeof symbol === 'string') &&
(typeof this.markets != 'undefined') &&
Expand Down Expand Up @@ -940,13 +932,30 @@ const Exchange = function (config) {
return this.createOrder (symbol, 'market', 'sell', amount, undefined, params)
}

this.costToPrecision = function (symbol, cost) {
return parseFloat (cost).toFixed (this.markets[symbol].precision.price)
}

this.priceToPrecision = function (symbol, price) {
return parseFloat (price).toFixed (this.markets[symbol].precision.price)
}

this.amountToPrecision = function (symbol, amount) {
return parseFloat (amount).toFixed (this.markets[symbol].precision.amount)
}

this.feeToPrecision = function (symbol, fee) {
return parseFloat (fee).toFixed (this.markets[symbol].precision.price)
}

this.calculateFee = function (symbol, type, side, amount, price, takerOrMaker = 'taker', params = {}) {
let market = this.markets[symbol]
let rate = market[takerOrMaker]
let cost = this.costToPrecision (symbol, amount * price)
return {
'currency': market['quote'],
'rate': rate,
'cost': amount * price * rate,
'cost': this.feeToPrecision (symbol, rate * cost),
}
}

Expand Down Expand Up @@ -1056,8 +1065,10 @@ const Exchange = function (config) {
this.calculate_fee = this.calculateFee
this.calculate_fee_rate = this.calculateFeeRate
this.common_currency_code = this.commonCurrencyCode
this.limit_price_to_precision = this.limitPriceToPrecision
this.limit_amount_to_precision = this.limitAmountToPrecision
this.price_to_precision = this.priceToPrecision
this.amount_to_precision = this.amountToPrecision
this.fee_to_precision = this.feeToPrecision
this.cost_to_precision = this.costToPrecision

this.init ()
}
Expand Down Expand Up @@ -2231,7 +2242,7 @@ var binance = {
let market = this.markets[symbol];
let key = 'quote';
let rate = market[takerOrMaker];
let cost = amount * rate;
let cost = this.costToPrecision (symbol, amount * rate);
if (side == 'sell') {
cost *= price;
} else {
Expand All @@ -2240,7 +2251,7 @@ var binance = {
return {
'currency': market[key],
'rate': rate,
'cost': cost,
'cost': this.feeToPrecision (symbol, cost),
};
},

Expand Down Expand Up @@ -2407,6 +2418,7 @@ var binance = {
}
}
let timestamp = order['time'];
let price = parseFloat (order['price']);
let amount = parseFloat (order['origQty']);
let filled = this.safeFloat (order, 'executedQty', 0.0);
let remaining = Math.max (amount - filled, 0.0);
Expand All @@ -2418,11 +2430,13 @@ var binance = {
'symbol': symbol,
'type': order['type'].toLowerCase (),
'side': order['side'].toLowerCase (),
'price': parseFloat (order['price']),
'price': price,
'amount': amount,
'cost': price * amount,
'filled': filled,
'remaining': remaining,
'status': status,
'fee': undefined,
};
return result;
},
Expand All @@ -2432,13 +2446,13 @@ var binance = {
let market = this.market (symbol);
let order = {
'symbol': market['id'],
'quantity': this.limitAmountToPrecision (symbol, parseFloat (amount)),
'quantity': this.amountToPrecision (symbol, amount),
'type': type.toUpperCase (),
'side': side.toUpperCase (),
};
if (type == 'limit') {
order = this.extend (order, {
'price': this.limitPriceToPrecision (symbol, parseFloat (price)),
'price': this.priceToPrecision (symbol, price),
'timeInForce': 'GTC', // 'GTC' = Good To Cancel (default), 'IOC' = Immediate Or Cancel
});
}
Expand Down Expand Up @@ -5998,6 +6012,14 @@ var bittrex = {
},
},

costToPrecision (symbol, cost) {
return this.truncate (cost, this.markets[symbol].precision.price);
},

feeToPrecision (symbol, fee) {
return this.truncate (fee, this.markets[symbol]['precision']['price']);
},

async fetchMarkets () {
let markets = await this.publicGetMarkets ();
let result = [];
Expand Down Expand Up @@ -6197,10 +6219,10 @@ var bittrex = {
let method = 'marketGet' + this.capitalize (side) + type;
let order = {
'market': market['id'],
'quantity': amount.toFixed (market['precision']['amount']),
'quantity': this.amountToPrecision (symbol, amount),
};
if (type == 'limit')
order['rate'] = price.toFixed (market['precision']['price']);
order['rate'] = this.priceToPrecision (symbol, price);
let response = await this[method] (this.extend (order, params));
let result = {
'info': response,
Expand Down Expand Up @@ -6252,9 +6274,19 @@ var bittrex = {
'currency': market['quote'],
};
}
let amount = order['Quantity'];
let remaining = order['QuantityRemaining'];
let price = this.safeFloat (order, 'Limit');
let cost = this.safeFloat (order, 'Price');
let amount = this.safeFloat (order, 'Quantity');
let remaining = this.safeFloat (order, 'QuantityRemaining', 0.0);
let filled = amount - remaining;
if (!cost) {
if (price && amount)
cost = price * amount;
}
if (!price) {
if (cost && filled)
price = cost / filled;
}
let result = {
'info': order,
'id': order['OrderUuid'],
Expand All @@ -6263,7 +6295,8 @@ var bittrex = {
'symbol': symbol,
'type': 'limit',
'side': side,
'price': order['Price'],
'price': price,
'cost': cost,
'amount': amount,
'filled': filled,
'remaining': remaining,
Expand Down
32 changes: 24 additions & 8 deletions ccxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -1204,20 +1204,36 @@ public function common_currency_code ($currency) {
return $currency;
}

public function limit_price_to_precision ($symbol, $price) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['price'] . 'f', $price);
public function cost_to_precision ($symbol, $cost) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['price'] . 'f', floatval ($price));
}

public function limitPriceToPrecision ($symbol, $price) {
return $this->limit_price_to_precision ($symbol, $price);
public function costToPrecision ($symbol, $cost) {
return $this->price_to_precision ($symbol, $cost);
}

public function limit_amount_to_precision ($symbol, $amount) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['amount'] . 'f', $amount);
public function price_to_precision ($symbol, $price) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['price'] . 'f', floatval ($price));
}

public function limitAmountToPrecision ($symbol, $amount) {
return $this->limit_amount_to_precision ($symbol, $amount);
public function priceToPrecision ($symbol, $price) {
return $this->price_to_precision ($symbol, $price);
}

public function amount_to_precision ($symbol, $amount) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['amount'] . 'f', floatval ($amount));
}

public function amountToPrecision ($symbol, $amount) {
return $this->amount_to_precision ($symbol, $amount);
}

public function fee_to_precision ($symbol, $fee) {
return sprintf ('%.' . $this->markets[$symbol]['precision']['price'] . 'f', floatval ($fee));
}

public function feeToPrecision ($symbol, $fee) {
return $this->fee_to_precision ($symbol, $fee);
}

public function commonCurrencyCode ($currency) {
Expand Down
Loading

0 comments on commit e96c9b1

Please sign in to comment.