Created
May 26, 2018 18:59
-
-
Save Kamikadze4GAME/efee6f69922b34765a8fbb0e3dd54c6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
et orderbookBUY = [ | |
[1, 0.1], | |
[2, 0.2], | |
[3, 0.25], | |
[4, 0.15], | |
[5, 0.4] | |
]; | |
let orderbookSELL = [ | |
[5, 0.1], | |
[4, 0.2], | |
[3, 0.25], | |
[2, 0.15], | |
[1, 0.4] | |
]; | |
let toBUYamount = 0.56; | |
let toSELLamount = 10; | |
let toBUYprice = 10; | |
let toSELLprice = 10; | |
let LESS = 0.1; | |
function Feetaker(rate, fee) | |
{ | |
return rate*(1+fee); | |
} | |
function buyAmount2(orderbook, amount, min, fee) { | |
let result = { | |
"orders": [], | |
// "amount": amount, | |
"left": amount | |
}; | |
let i = 0; | |
let left = amount; | |
let orders = []; | |
let tempamount = 0; | |
if (amount >= min) { | |
while(i < orderbook.length) { | |
tempamount = tempamount + orderbook[i][1] | |
if(tempamount >= min){ | |
if(left > tempamount) { | |
left -= tempamount; | |
if (left < min) { | |
tempamount=tempamount-(min-left); | |
left=min; | |
} | |
result.orders.push({"rate": Feetaker(orderbook[i][0]), "amount": tempamount}); | |
tempamount = 0; | |
} | |
else { | |
result.orders.push({"rate":Feetaker(orderbook[i][0]), "amount":left}); | |
left=0; | |
break; | |
} | |
} | |
i++; | |
} | |
result.left = left; | |
} | |
console.log(result); | |
} | |
function buyPrice2(orderbook, price, min) { | |
let result = { | |
orders: [/*****************/], | |
// price: price, | |
left: price | |
}; | |
let i = 0; | |
let left = price; | |
let orders = []; | |
let tempamount = 0; | |
if (price >= min) { | |
while(i < orderbook.length) { | |
tempamount = tempamount + Feetaker(orderbook[i][1]); | |
if(tempamount >= min){ | |
if(left > tempamount*Feetaker(orderbook[i][0])) { | |
left -= tempamount*Feetaker(orderbook[i][0]); | |
if ((left/Feetaker(orderbook[i+1][0]) < min)&&(min-left/Feetaker(orderbook[i+1][0]>0)){ | |
tempamount=tempamount-(min-left/Feetaker(orderbook[i+1][0])); | |
left=min*Feetaker(orderbook[i+1][0]); | |
} | |
result.orders.push({rate: Feetaker(orderbook[i][0]), ammount: tempamount}); | |
tempamount = 0; | |
} | |
else { | |
result.orders.push({rate:Feetaker(orderbook[i][0]), ammount: left}); | |
break; | |
} | |
} | |
i++; | |
} | |
result.left = left; | |
} | |
console.log(result); | |
} | |
buyAmount2(orderbookBUY, toBUYamount, LESS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment