kraken-grid 1.4.6 → 1.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/balancer.ts CHANGED
@@ -98,14 +98,15 @@ const Balancer = (config: BalancerConfig): BalancerInstance => {
98
98
  // processes the open orders.
99
99
  // -------------------------------------------------------
100
100
  // We are replacing the grid for p.pair, so let's cancel
101
- // all its orders.
102
- const toCancel = bot.portfolio.O.filter(o => [p.pair, po.altname].includes(o[1].descr.pair))
103
- .map(ae => ae[0]);
104
- console.log(`${bot.FLAGS.safe ? 'NOT ' : ''}Cancelling`, toCancel);
101
+ // all its orders if there are any.
102
+ if(bot.portfolio.O) {
103
+ const toCancel = bot.portfolio.O.filter(o => [p.pair, po.altname].includes(o[1].descr.pair))
104
+ .map(ae => ae[0]);
105
+ console.log(`${bot.FLAGS.safe ? 'NOT ' : ''}Cancelling`, toCancel);
105
106
 
106
- if (!bot.FLAGS.safe && toCancel)
107
- await bot.kill(toCancel);
108
-
107
+ if (!bot.FLAGS.safe && toCancel)
108
+ await bot.kill(toCancel);
109
+ }
109
110
  const Ordered = await bot.order(p.type, p.pair, p.price, p.amt,
110
111
  bot.getLev(bot.portfolio, p.type, p.price, p.amt, po.base, false),
111
112
  0, p.type === 'buy' ? sP : bP); // uref, close
package/bot.ts CHANGED
@@ -464,7 +464,7 @@ export default function Bot(config: any): BotInstance {
464
464
  const cd = new Date(oo.closetm * 1000);
465
465
  let gp:GridPoint | undefined = gPrices.find((x:any) => x.userref===ur); // If we already saw this grid point.
466
466
  if( counter < 5 ) {
467
- console.log(o,ur,op,od.type,od.close,`${cd.getFullYear()}/${1+cd.getMonth()
467
+ console.log(o,ur,op,od.type,oo.vol_exec,od.close,`${cd.getFullYear()}/${1+cd.getMonth()
468
468
  }/${cd.getDate()}`,cd.getHours(),cd.getMinutes(),cd.getSeconds());
469
469
  }
470
470
  counter -= 1;
@@ -858,7 +858,7 @@ console.log("[p,np,dp,t,hp,lp,b,ma,f,tot1,ov,a,a2,t2,t2s]:",
858
858
  const traded = c.type==='buy' ? 'sold' : 'bought';
859
859
  gp[traded]+=c.total;
860
860
  }
861
- // Do we need to extend the grid?
861
+ // Do we need to [extend] the grid?
862
862
  // If we don't have a buy and a sell, then yes.
863
863
  // --------------------------------------------
864
864
  bs = bSidesP.find(b => b.pair===c.sym); // Was sym+'USD' but #USD Refactor
package/dist/balancer.js CHANGED
@@ -68,12 +68,14 @@ const Balancer = (config) => {
68
68
  // processes the open orders.
69
69
  // -------------------------------------------------------
70
70
  // We are replacing the grid for p.pair, so let's cancel
71
- // all its orders.
72
- const toCancel = bot.portfolio.O.filter(o => [p.pair, po.altname].includes(o[1].descr.pair))
73
- .map(ae => ae[0]);
74
- console.log(`${bot.FLAGS.safe ? 'NOT ' : ''}Cancelling`, toCancel);
75
- if (!bot.FLAGS.safe && toCancel)
76
- await bot.kill(toCancel);
71
+ // all its orders if there are any.
72
+ if (bot.portfolio.O) {
73
+ const toCancel = bot.portfolio.O.filter(o => [p.pair, po.altname].includes(o[1].descr.pair))
74
+ .map(ae => ae[0]);
75
+ console.log(`${bot.FLAGS.safe ? 'NOT ' : ''}Cancelling`, toCancel);
76
+ if (!bot.FLAGS.safe && toCancel)
77
+ await bot.kill(toCancel);
78
+ }
77
79
  const Ordered = await bot.order(p.type, p.pair, p.price, p.amt, bot.getLev(bot.portfolio, p.type, p.price, p.amt, po.base, false), 0, p.type === 'buy' ? sP : bP); // uref, close
78
80
  if (Ordered.uref) { // Order placed, and there is the uref for the other side.
79
81
  await bot.order('buy', p.pair, bP, bAmt, bot.getLev(bot.portfolio, 'buy', bP, bAmt, po.base, false), p.type === 'buy' ? 0 : Ordered.uref, p.price);
@@ -1 +1 @@
1
- {"version":3,"file":"balancer.js","sourceRoot":"","sources":["../balancer.ts"],"names":[],"mappings":"AA2BA,MAAM,QAAQ,GAAG,CAAC,MAAsB,EAAoB,EAAE;IAC1D,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM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AC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;QACtD,CAAC;QAED,IAAI,GAAG,CAAC,KAAK,CAAC,OAAO;YACjB,OAAO,CAAC,GAAG,CAAC,EAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,EAAC,CAAC,CAAC;IAC7D,CAAC;IAED,KAAK,UAAU,SAAS,CAAC,IAAY,EAAE,MAAc,EAAE;QACnD,MAAM,CAAC,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,oCAAoC;QACjF,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;QAChC,MAAM,CAAC,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QAEhD,IAAI,CAAC,CAAC,EAAE;YAAE,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;aAC5D,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,OAAO;YAAE,MAAM,UAAU,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;aACnD,IAAI,CAAC,CAAC,IAAI,KAAK,EAAE;YAAE,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAC;;YACvE,OAAO,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;IACzD,CAAC;IAED,OAAO,EAAE,SAAS,EAAE,CAAC;AACzB,CAAC,CAAC;AAEF,eAAe,QAAQ,CAAC","sourcesContent":["import type { AllocationInstance } from './allocation.ts';\nimport type { BotInstance } from './bot.ts';\n\n// Configuration object passed to Balancer\nexport interface BalancerConfig {\n bot: BotInstance;\n}\n\n// Interface for a trade decision\nexport interface TradeDecision {\n pair: string;\n type: 'buy' | 'sell';\n amt: number;\n price: number;\n of?: number;\n isNumer?: boolean;\n}\n\n// Interface for Balancer instance returned by the constructor\nexport interface BalancerInstance {\n setTrades: (move: string, tkr?: string) => Promise<void>;\n}\n\n// Type for the Balancer constructor function\nexport type BalancerConstructor = \n (config: BalancerConfig) => BalancerInstance;\n \nconst Balancer = (config: BalancerConfig): BalancerInstance => {\n const { bot } = config;\n const target: AllocationInstance = (bot.portfolio?.Allocation \n && bot.portfolio.Allocation.size() > 0)\n ? bot.portfolio.Allocation\n : false; // Get desired allocation.\n const already: string[] = []; // Pairs that are already gridded\n const { sigdig } = target || { sigdig: (x: number, sig?: number, dp?: number) => x };\n \n // The first arg can be an order to place and that\n // indicates that we want to add the opposite\n // side using the moved price. To do that, we need to\n // calculate how much we want to trade at that new\n // price and then place the trade with a conditional\n // close at the price of the passed in pending order.\n // These are the two prices of a grid point and the\n // situation is as if only part of one side executed,\n // the order we are to place.\n // We assume that the whole crypto market will move and\n // so calculate when to sell or buy based on how the\n // move affects the allocation.\n // ----------------------------------------------------\n async function buyAndSell(p: TradeDecision, sMove: string): Promise<void> {\n const move = Number(sMove);\n if (already.includes(p.pair)) { // Sanity check.\n console.log(\"Already set trades for\", p.pair);\n return;\n }\n if(!bot.portfolio) {\n console.log(\"No Portfolio. Please make an issue on Github.\", p.pair);\n return;\n }\n already.push(p.pair); // Wait till trades are set?\n const po = bot.pairInfo(p.pair);\n const port = bot.getPortfolio();\n const qisn = (po.quote === port.Numeraire); // If the quote for the pair isn't our Numeraire\n // then we will do only 1/3 of the volume and convert the prices accordingly. (see qisn)\n const qf = qisn ? 1 : port[po.quote][1]; // Factor for adjustments\n\n let curP = port[po.base][1]; // Current price in Numeraire.\n const sP = sigdig(curP * (1 + move) / qf, 6, po.pair_decimals);\n const bP = sigdig((curP / (1 + move)) / (qisn ? 1 : port[po.quote][1]), 6, po.pair_decimals);\n let sAmt = -1 * await bot.howMuch(po.base, sP);\n let bAmt = await bot.howMuch(po.base, bP);\n // We have the amount to sell and buy BEFORE being in balance,\n // but if we get in balance by selling or buying p.amt, that \n // trade will be added to the buy or sell, so remove it for now.\n // -------------------------------------------------------------\n curP = p.price;\n if (p.type === 'buy') sAmt -= p.amt;\n if (p.type === 'sell') bAmt -= p.amt;\n \n bAmt = sigdig(bAmt, 6, po.lot_decimals);\n sAmt = sigdig(sAmt, 6, po.lot_decimals);\n console.log('buy', po.base, bP, bAmt, curP);\n console.log('sell', po.base, sP, sAmt, curP);\n \n if (bAmt < 0 || sAmt < 0) {\n console.log(\"We are too far out of balance. Try after some of the trade above is done.\");\n return;\n }\n \n // Now we have the trade to balance (p) and the\n // correct amounts for the two new trades to place.\n // The traded amount from p must be added to the other \n // side if it happens first, and if the other side\n // happens first, then its amt must be added to p.\n // bot.order() using p will return an object {txid,uref}\n // so that we can use that uref for the other side, and\n // that will add those amounts properly when bot.listOpens\n // processes the open orders.\n // -------------------------------------------------------\n // We are replacing the grid for p.pair, so let's cancel\n // all its orders.\n const toCancel = bot.portfolio.O.filter(o => [p.pair, po.altname].includes(o[1].descr.pair))\n .map(ae => ae[0]);\n console.log(`${bot.FLAGS.safe ? 'NOT ' : ''}Cancelling`, toCancel);\n \n if (!bot.FLAGS.safe && toCancel)\n await bot.kill(toCancel);\n \n const Ordered = await bot.order(p.type, p.pair, p.price, p.amt,\n bot.getLev(bot.portfolio, p.type, p.price, p.amt, po.base, false),\n 0, p.type === 'buy' ? sP : bP); // uref, close\n \n if (Ordered.uref) { // Order placed, and there is the uref for the other side.\n await bot.order('buy', p.pair, bP, bAmt,\n bot.getLev(bot.portfolio, 'buy', bP, bAmt, po.base, false),\n p.type === 'buy' ? 0 : Ordered.uref, p.price);\n await bot.order('sell', p.pair, sP, sAmt,\n bot.getLev(bot.portfolio, 'sell', sP, sAmt, po.base, false),\n p.type === 'buy' ? Ordered.uref : 0, p.price);\n }\n\n if (bot.FLAGS.verbose)\n console.log({qf, curP, sAmt, bAmt, sP, bP, Ordered});\n }\n\n async function setTrades(move: string, tkr: string = ''): Promise<void> { \n const c = await target.getAllocation(false); // Current alloc, and updates prices\n const total = target.getTotal();\n const p = await target.bestTrade(c, tkr, total);\n \n if (p.of) console.log(\"Still\", p.of - p.amt, p.pair, \"to go...\");\n else if (p.pair && p.isNumer) await buyAndSell(p, move);\n else if (p.pair === '') console.log(\"20: No tradable pair could be found.\");\n else console.log(\"21: One time trade to save fees.\");\n }\n \n return { setTrades };\n};\n\nexport default Balancer;"]}
1
+ {"version":3,"file":"balancer.js","sourceRoot":"","sources":["../balancer.ts"],"names":[],"mappings":"AA2BA,MAAM,QAAQ,GAAG,CAAC,MAAsB,EAAoB,EAAE;IAC1D,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC;IACvB,MAAM,MAAM,GAAuB,CAAC,GAAG,CAAC,SAAS,EAAE,UAAU;WACtD,GAAG,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACvC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,UAAU;QAC1B,CAAC,CAAC,KAAK,CAAC,CAAE,0BAA0B;IACxC,MAAM,OAAO,GAAa,EAAE,CAAC,CAAG,iCAAiC;IACjE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,IAAI,EAAE,MAAM,EAAE,CAAC,CAAS,EAAE,GAAY,EAAE,EAAW,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC;IAErF,kDAAkD;IAClD,6CAA6C;IAC7C,sDAAsD;IACtD,kDAAkD;IAClD,oDAAoD;IACpD,qDAAqD;IACrD,mDAAmD;IACnD,qDAAqD;IACrD,6BAA6B;IAC7B,uDAAuD;IACvD,oDAAoD;IACpD,+BAA+B;IAC/B,uDAAuD;IACvD,KAAK,UAAU,UAAU,CAAC,CAAgB,EAAE,KAAa;QACrD,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;QAC3B,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,gBAAgB;YAC5C,OAAO,CA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AM,UAAU,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;aACnD,IAAI,CAAC,CAAC,IAAI,KAAK,EAAE;YAAE,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAC;;YACvE,OAAO,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;IACzD,CAAC;IAED,OAAO,EAAE,SAAS,EAAE,CAAC;AACzB,CAAC,CAAC;AAEF,eAAe,QAAQ,CAAC","sourcesContent":["import type { AllocationInstance } from './allocation.ts';\nimport type { BotInstance } from './bot.ts';\n\n// Configuration object passed to Balancer\nexport interface BalancerConfig {\n bot: BotInstance;\n}\n\n// Interface for a trade decision\nexport interface TradeDecision {\n pair: string;\n type: 'buy' | 'sell';\n amt: number;\n price: number;\n of?: number;\n isNumer?: boolean;\n}\n\n// Interface for Balancer instance returned by the constructor\nexport interface BalancerInstance {\n setTrades: (move: string, tkr?: string) => Promise<void>;\n}\n\n// Type for the Balancer constructor function\nexport type BalancerConstructor = \n (config: BalancerConfig) => BalancerInstance;\n \nconst Balancer = (config: BalancerConfig): BalancerInstance => {\n const { bot } = config;\n const target: AllocationInstance = (bot.portfolio?.Allocation \n && bot.portfolio.Allocation.size() > 0)\n ? bot.portfolio.Allocation\n : false; // Get desired allocation.\n const already: string[] = []; // Pairs that are already gridded\n const { sigdig } = target || { sigdig: (x: number, sig?: number, dp?: number) => x };\n \n // The first arg can be an order to place and that\n // indicates that we want to add the opposite\n // side using the moved price. To do that, we need to\n // calculate how much we want to trade at that new\n // price and then place the trade with a conditional\n // close at the price of the passed in pending order.\n // These are the two prices of a grid point and the\n // situation is as if only part of one side executed,\n // the order we are to place.\n // We assume that the whole crypto market will move and\n // so calculate when to sell or buy based on how the\n // move affects the allocation.\n // ----------------------------------------------------\n async function buyAndSell(p: TradeDecision, sMove: string): Promise<void> {\n const move = Number(sMove);\n if (already.includes(p.pair)) { // Sanity check.\n console.log(\"Already set trades for\", p.pair);\n return;\n }\n if(!bot.portfolio) {\n console.log(\"No Portfolio. Please make an issue on Github.\", p.pair);\n return;\n }\n already.push(p.pair); // Wait till trades are set?\n const po = bot.pairInfo(p.pair);\n const port = bot.getPortfolio();\n const qisn = (po.quote === port.Numeraire); // If the quote for the pair isn't our Numeraire\n // then we will do only 1/3 of the volume and convert the prices accordingly. (see qisn)\n const qf = qisn ? 1 : port[po.quote][1]; // Factor for adjustments\n\n let curP = port[po.base][1]; // Current price in Numeraire.\n const sP = sigdig(curP * (1 + move) / qf, 6, po.pair_decimals);\n const bP = sigdig((curP / (1 + move)) / (qisn ? 1 : port[po.quote][1]), 6, po.pair_decimals);\n let sAmt = -1 * await bot.howMuch(po.base, sP);\n let bAmt = await bot.howMuch(po.base, bP);\n // We have the amount to sell and buy BEFORE being in balance,\n // but if we get in balance by selling or buying p.amt, that \n // trade will be added to the buy or sell, so remove it for now.\n // -------------------------------------------------------------\n curP = p.price;\n if (p.type === 'buy') sAmt -= p.amt;\n if (p.type === 'sell') bAmt -= p.amt;\n \n bAmt = sigdig(bAmt, 6, po.lot_decimals);\n sAmt = sigdig(sAmt, 6, po.lot_decimals);\n console.log('buy', po.base, bP, bAmt, curP);\n console.log('sell', po.base, sP, sAmt, curP);\n \n if (bAmt < 0 || sAmt < 0) {\n console.log(\"We are too far out of balance. Try after some of the trade above is done.\");\n return;\n }\n \n // Now we have the trade to balance (p) and the\n // correct amounts for the two new trades to place.\n // The traded amount from p must be added to the other \n // side if it happens first, and if the other side\n // happens first, then its amt must be added to p.\n // bot.order() using p will return an object {txid,uref}\n // so that we can use that uref for the other side, and\n // that will add those amounts properly when bot.listOpens\n // processes the open orders.\n // -------------------------------------------------------\n // We are replacing the grid for p.pair, so let's cancel\n // all its orders if there are any.\n if(bot.portfolio.O) { \n const toCancel = bot.portfolio.O.filter(o => [p.pair, po.altname].includes(o[1].descr.pair))\n .map(ae => ae[0]);\n console.log(`${bot.FLAGS.safe ? 'NOT ' : ''}Cancelling`, toCancel);\n \n if (!bot.FLAGS.safe && toCancel)\n await bot.kill(toCancel);\n } \n const Ordered = await bot.order(p.type, p.pair, p.price, p.amt,\n bot.getLev(bot.portfolio, p.type, p.price, p.amt, po.base, false),\n 0, p.type === 'buy' ? sP : bP); // uref, close\n \n if (Ordered.uref) { // Order placed, and there is the uref for the other side.\n await bot.order('buy', p.pair, bP, bAmt,\n bot.getLev(bot.portfolio, 'buy', bP, bAmt, po.base, false),\n p.type === 'buy' ? 0 : Ordered.uref, p.price);\n await bot.order('sell', p.pair, sP, sAmt,\n bot.getLev(bot.portfolio, 'sell', sP, sAmt, po.base, false),\n p.type === 'buy' ? Ordered.uref : 0, p.price);\n }\n\n if (bot.FLAGS.verbose)\n console.log({qf, curP, sAmt, bAmt, sP, bP, Ordered});\n }\n\n async function setTrades(move: string, tkr: string = ''): Promise<void> { \n const c = await target.getAllocation(false); // Current alloc, and updates prices\n const total = target.getTotal();\n const p = await target.bestTrade(c, tkr, total);\n \n if (p.of) console.log(\"Still\", p.of - p.amt, p.pair, \"to go...\");\n else if (p.pair && p.isNumer) await buyAndSell(p, move);\n else if (p.pair === '') console.log(\"20: No tradable pair could be found.\");\n else console.log(\"21: One time trade to save fees.\");\n }\n \n return { setTrades };\n};\n\nexport default Balancer;"]}
package/dist/bot.js CHANGED
@@ -401,7 +401,7 @@ export default function Bot(config) {
401
401
  const cd = new Date(oo.closetm * 1000);
402
402
  let gp = gPrices.find((x) => x.userref === ur); // If we already saw this grid point.
403
403
  if (counter < 5) {
404
- console.log(o, ur, op, od.type, od.close, `${cd.getFullYear()}/${1 + cd.getMonth()}/${cd.getDate()}`, cd.getHours(), cd.getMinutes(), cd.getSeconds());
404
+ console.log(o, ur, op, od.type, oo.vol_exec, od.close, `${cd.getFullYear()}/${1 + cd.getMonth()}/${cd.getDate()}`, cd.getHours(), cd.getMinutes(), cd.getSeconds());
405
405
  }
406
406
  counter -= 1;
407
407
  if (portfolio && portfolio.Pairs) {
@@ -804,7 +804,7 @@ export default function Bot(config) {
804
804
  const traded = c.type === 'buy' ? 'sold' : 'bought';
805
805
  gp[traded] += c.total;
806
806
  }
807
- // Do we need to extend the grid?
807
+ // Do we need to [extend] the grid?
808
808
  // If we don't have a buy and a sell, then yes.
809
809
  // --------------------------------------------
810
810
  bs = bSidesP.find(b => b.pair === c.sym); // Was sym+'USD' but #USD Refactor