Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const maxInvestment = 10000;
- const offers = [[]];
- let stop = false;
- let curInvestment = 0;
- while (curInvestment <= maxInvestment && !stop) {
- const [curAsk, curBid] = [asks[0], bids[0]];
- const [curNextAsk, curNextBid] = [findOfferFromNextExchange(asks), findOfferFromNextExchange(bids)];
- let testA, testB;
- if (curAsk.exchange !== curBid.exchange || curBid.price - curAsk.price > curNextBid.price - curNextAsk.price) {
- testA = curAsk;
- testB = curBid;
- } else {
- testA = curNextAsk;
- testB = curNextBid;
- }
- if (testA.price > testB.price) stop = true;
- else {
- if (testA.volume > testB.volume) {
- if (testB.volume * testA.price > (maxInvestment - curInvestment)) {
- let lastVolume = (maxInvestment - curInvestment) / testA.price;
- offers.push([...offers[offers.length - 1], {
- ask: { price: testA.price, volume: lastVolume },
- bid: { price: testB.price, volume: lastVolume }
- }]);
- stop = true;
- curInvestment += testA.price * lastVolume ;
- } else {
- offers.push([...offers[offers.length - 1], {
- ask: { price: testA.price, volume: testB.volume },
- bid: { price: testB.price, volume: testB.volume }
- }]);
- bids = bids.filter(v => v !== testB);
- testA.volume -= testB.volume;
- curInvestment += testA.price * testB.volume;
- console.log('b', offers[offers.length - 1][offers[offers.length - 1].length - 1]);
- }
- } else {
- if (testA.volume * testA.price > (maxInvestment - curInvestment)) {
- let lastVolume = (maxInvestment - curInvestment) / testA.price;
- offers.push([...offers[offers.length - 1], {
- ask: { price: testA.price, volume: lastVolume },
- bid: { price: testB.price, volume: lastVolume },
- }]);
- stop = true;
- curInvestment += testA.price * lastVolume ;
- } else {
- offers.push([...offers[offers.length - 1], {
- ask: { price: testA.price, volume: testA.volume },
- bid: { price: testB.price, volume: testA.volume }
- }]);
- asks = asks.filter(v => v !== testA);
- testB.volume -= testA.volume;
- curInvestment += testA.price * testA.volume;
- console.log('a', offers[offers.length - 1][offers[offers.length - 1].length - 1]);
- }
- }
- }
- }
- offers.shift();
- // console.log(offers)
- console.log(curInvestment);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement