Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. const maxInvestment = 10000;
  2. const offers = [[]];
  3. let stop = false;
  4. let curInvestment = 0;
  5.  
  6. while (curInvestment <= maxInvestment && !stop) {
  7. const [curAsk, curBid] = [asks[0], bids[0]];
  8. const [curNextAsk, curNextBid] = [findOfferFromNextExchange(asks), findOfferFromNextExchange(bids)];
  9. let testA, testB;
  10. if (curAsk.exchange !== curBid.exchange || curBid.price - curAsk.price > curNextBid.price - curNextAsk.price) {
  11. testA = curAsk;
  12. testB = curBid;
  13. } else {
  14. testA = curNextAsk;
  15. testB = curNextBid;
  16. }
  17. if (testA.price > testB.price) stop = true;
  18. else {
  19. if (testA.volume > testB.volume) {
  20. if (testB.volume * testA.price > (maxInvestment - curInvestment)) {
  21. let lastVolume = (maxInvestment - curInvestment) / testA.price;
  22. offers.push([...offers[offers.length - 1], {
  23. ask: { price: testA.price, volume: lastVolume },
  24. bid: { price: testB.price, volume: lastVolume }
  25. }]);
  26. stop = true;
  27. curInvestment += testA.price * lastVolume ;
  28. } else {
  29. offers.push([...offers[offers.length - 1], {
  30. ask: { price: testA.price, volume: testB.volume },
  31. bid: { price: testB.price, volume: testB.volume }
  32. }]);
  33. bids = bids.filter(v => v !== testB);
  34. testA.volume -= testB.volume;
  35. curInvestment += testA.price * testB.volume;
  36. console.log('b', offers[offers.length - 1][offers[offers.length - 1].length - 1]);
  37. }
  38. } else {
  39. if (testA.volume * testA.price > (maxInvestment - curInvestment)) {
  40. let lastVolume = (maxInvestment - curInvestment) / testA.price;
  41. offers.push([...offers[offers.length - 1], {
  42. ask: { price: testA.price, volume: lastVolume },
  43. bid: { price: testB.price, volume: lastVolume },
  44. }]);
  45. stop = true;
  46. curInvestment += testA.price * lastVolume ;
  47. } else {
  48. offers.push([...offers[offers.length - 1], {
  49. ask: { price: testA.price, volume: testA.volume },
  50. bid: { price: testB.price, volume: testA.volume }
  51. }]);
  52. asks = asks.filter(v => v !== testA);
  53. testB.volume -= testA.volume;
  54. curInvestment += testA.price * testA.volume;
  55. console.log('a', offers[offers.length - 1][offers[offers.length - 1].length - 1]);
  56. }
  57.  
  58. }
  59. }
  60. }
  61. offers.shift();
  62. // console.log(offers)
  63. console.log(curInvestment);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement