@gbozee/ultimate 0.0.2-151 → 0.0.2-154

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.
@@ -58423,7 +58423,6 @@ class AppDatabase {
58423
58423
  const result = await this.get_exchange_db_instance(account);
58424
58424
  if (result?.expand?.proxy) {
58425
58425
  const { type, ip_address } = result.expand.proxy;
58426
- console.log(type, ip_address);
58427
58426
  if (type === "http") {
58428
58427
  return new import_https_proxy_agent.HttpsProxyAgent(`http://${ip_address}`);
58429
58428
  }
@@ -58471,13 +58470,13 @@ class AppDatabase {
58471
58470
  table: "positions_view",
58472
58471
  params: {
58473
58472
  filter: `symbol:lower="${symbol.toLowerCase()}" && account:lower="${account.owner.toLowerCase()} ${account.exchange.toLowerCase()}"`,
58474
- expand: "config, account_strategy, p_account"
58473
+ expand: "config, account_strategy, p_account, proxy"
58475
58474
  }
58476
58475
  } : {
58477
58476
  table: "positions",
58478
58477
  params: {
58479
58478
  filter: `symbol:lower="${symbol.toLowerCase()}" && account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
58480
- expand: "account,config"
58479
+ expand: "account,config, proxy"
58481
58480
  }
58482
58481
  };
58483
58482
  if (custom_filter) {
@@ -60987,7 +60986,14 @@ function generateOptimumAppConfig(config2, payload, position2) {
60987
60986
  });
60988
60987
  return best_app_config;
60989
60988
  }
60990
- function determineOptimumReward(app_config, increase = true, low_range = 30, high_range = 199) {
60989
+ function determineOptimumReward(payload) {
60990
+ const {
60991
+ app_config,
60992
+ increase = true,
60993
+ low_range = 1,
60994
+ high_range = 199,
60995
+ target_loss
60996
+ } = payload;
60991
60997
  const criterion = app_config.strategy || "quantity";
60992
60998
  const risk_rewards = createArray(low_range, high_range, 1);
60993
60999
  let func = risk_rewards.map((trade_no) => {
@@ -61027,10 +61033,33 @@ function determineOptimumReward(app_config, increase = true, low_range = 30, hig
61027
61033
  entry
61028
61034
  };
61029
61035
  });
61030
- func = func.filter((r2) => Boolean(r2)).filter((r2) => {
61031
- let foundIndex = r2?.result.findIndex((e2) => e2.quantity === r2.max);
61032
- return criterion === "quantity" ? foundIndex === 0 : true;
61033
- });
61036
+ func = func.filter((r2) => Boolean(r2));
61037
+ if (target_loss === undefined) {
61038
+ func = func.filter((r2) => {
61039
+ let foundIndex = r2?.result.findIndex((e2) => e2.quantity === r2.max);
61040
+ return criterion === "quantity" ? foundIndex === 0 : true;
61041
+ });
61042
+ }
61043
+ if (target_loss !== undefined) {
61044
+ const validResults = func.filter((r2) => Math.abs(r2.neg_pnl) <= target_loss);
61045
+ if (validResults.length > 0) {
61046
+ validResults.sort((a, b) => {
61047
+ const diffA = target_loss - Math.abs(a.neg_pnl);
61048
+ const diffB = target_loss - Math.abs(b.neg_pnl);
61049
+ return diffA - diffB;
61050
+ });
61051
+ if (app_config.raw) {
61052
+ return validResults[0];
61053
+ }
61054
+ return validResults[0]?.value;
61055
+ } else {
61056
+ func.sort((a, b) => Math.abs(a.neg_pnl) - Math.abs(b.neg_pnl));
61057
+ if (app_config.raw) {
61058
+ return func[0];
61059
+ }
61060
+ return func[0]?.value;
61061
+ }
61062
+ }
61034
61063
  const highest = criterion === "quantity" ? Math.max(...func.map((o) => o.max)) : Math.min(...func.map((o) => o.entry));
61035
61064
  const key = criterion === "quantity" ? "max" : "entry";
61036
61065
  const index = findIndexByCondition(func, app_config.kind, (x) => x[key] == highest, criterion);
@@ -61142,13 +61171,13 @@ function determineOptimumRisk(config2, payload, params) {
61142
61171
  };
61143
61172
  }
61144
61173
  function computeRiskReward(payload) {
61145
- const { app_config, entry, stop, risk_per_trade } = payload;
61174
+ const { app_config, entry, stop, risk_per_trade, target_loss } = payload;
61146
61175
  const kind = entry > stop ? "long" : "short";
61147
61176
  app_config.kind = kind;
61148
61177
  app_config.entry = entry;
61149
61178
  app_config.stop = stop;
61150
61179
  app_config.risk_per_trade = risk_per_trade;
61151
- const result = determineOptimumReward(app_config);
61180
+ const result = determineOptimumReward({ app_config, target_loss });
61152
61181
  return result;
61153
61182
  }
61154
61183
  function getRiskReward(payload) {
@@ -61477,6 +61506,67 @@ function determineCompoundLongTrade(payload) {
61477
61506
  short_max_size
61478
61507
  };
61479
61508
  }
61509
+ function generateOppositeTradeConfig(payload) {
61510
+ const {
61511
+ kind,
61512
+ entry,
61513
+ quantity,
61514
+ target_pnl,
61515
+ ratio = 0.5,
61516
+ global_config
61517
+ } = payload;
61518
+ const diff = target_pnl / quantity;
61519
+ const tp = kind === "long" ? entry + diff : entry - diff;
61520
+ const stop = kind === "long" ? entry - diff : entry + diff;
61521
+ const opposite_pnl = target_pnl * ratio;
61522
+ const app_config = constructAppConfig({
61523
+ account: {
61524
+ expand: {
61525
+ b_config: {
61526
+ entry,
61527
+ stop,
61528
+ symbol: global_config.symbol,
61529
+ risk: target_pnl
61530
+ }
61531
+ }
61532
+ },
61533
+ global_config
61534
+ });
61535
+ const risk_reward = computeRiskReward({
61536
+ app_config,
61537
+ entry: stop,
61538
+ stop: tp,
61539
+ risk_per_trade: opposite_pnl,
61540
+ target_loss: opposite_pnl
61541
+ });
61542
+ return {
61543
+ entry: to_f(stop, global_config.price_places),
61544
+ stop: to_f(tp, global_config.price_places),
61545
+ risk: to_f(opposite_pnl, "%.2f"),
61546
+ risk_reward
61547
+ };
61548
+ }
61549
+ function constructAppConfig(payload) {
61550
+ const { account, global_config, kelly_config } = payload;
61551
+ const config2 = account.expand?.b_config;
61552
+ if (!config2) {
61553
+ return null;
61554
+ }
61555
+ const kelly = config2.kelly;
61556
+ const options = {
61557
+ entry: config2?.entry,
61558
+ stop: config2?.stop,
61559
+ risk_reward: config2?.risk_reward,
61560
+ risk: config2?.risk,
61561
+ symbol: account.symbol,
61562
+ use_kelly: kelly_config?.use_kelly ?? kelly?.use_kelly,
61563
+ kelly_confidence_factor: kelly_config?.kelly_confidence_factor ?? kelly?.kelly_confidence_factor,
61564
+ kelly_minimum_risk: kelly_config?.kelly_minimum_risk ?? kelly?.kelly_minimum_risk,
61565
+ kelly_prediction_model: kelly_config?.kelly_prediction_model ?? kelly?.kelly_prediction_model
61566
+ };
61567
+ const appConfig = buildAppConfig(global_config, options);
61568
+ return appConfig;
61569
+ }
61480
61570
  // src/helpers/strategy.ts
61481
61571
  class Strategy {
61482
61572
  position;
@@ -64398,6 +64488,9 @@ async function reduceMajorPositionEntry(input, accountInfo, trigger2, exchange_i
64398
64488
  }
64399
64489
 
64400
64490
  // src/position.ts
64491
+ var import_https_proxy_agent2 = __toESM(require_dist2(), 1);
64492
+ var import_socks_proxy_agent2 = __toESM(require_dist3(), 1);
64493
+
64401
64494
  class ExchangePosition {
64402
64495
  exchange;
64403
64496
  symbol_config;
@@ -64411,6 +64504,13 @@ class ExchangePosition {
64411
64504
  this.instance = payload.instance;
64412
64505
  this.exchange_account = payload.exchange_account;
64413
64506
  }
64507
+ async initialize() {
64508
+ const proxy = await this.getProxyForAccount();
64509
+ if (proxy) {
64510
+ this.exchange.client.globalRequestOptions.httpAgent = proxy;
64511
+ this.exchange.client.globalRequestOptions.httpsAgent = proxy;
64512
+ }
64513
+ }
64414
64514
  getInstance() {
64415
64515
  return this.instance;
64416
64516
  }
@@ -64424,6 +64524,20 @@ class ExchangePosition {
64424
64524
  const { p_account } = this.instance.expand;
64425
64525
  return p_account;
64426
64526
  }
64527
+ async getProxyForAccount() {
64528
+ if (this.instance.expand.proxy) {
64529
+ const result = this.instance.expand.proxy;
64530
+ const { type, ip_address } = result;
64531
+ console.log("position", type, ip_address);
64532
+ if (type === "http") {
64533
+ return new import_https_proxy_agent2.HttpsProxyAgent(`http://${ip_address}`);
64534
+ }
64535
+ if (type === "socks5") {
64536
+ return new import_socks_proxy_agent2.SocksProxyAgent(`socks://${ip_address}`);
64537
+ }
64538
+ }
64539
+ return null;
64540
+ }
64427
64541
  async cancelOrders(payload) {
64428
64542
  const { limit, price: _price, raw } = payload;
64429
64543
  if (limit) {
@@ -65231,6 +65345,46 @@ class ExchangePosition {
65231
65345
  });
65232
65346
  return app_config;
65233
65347
  }
65348
+ async getOrCreatePositionConfig(payload) {
65349
+ const { risk = 50, risk_reward = 199 } = payload;
65350
+ const config2 = await this.getConfig();
65351
+ if (!config2) {
65352
+ const long_c = await this.buildConfigForSymbol({
65353
+ risk,
65354
+ risk_reward
65355
+ });
65356
+ return this.getConfig({
65357
+ params: {
65358
+ entry: config2.entry,
65359
+ stop: config2.stop,
65360
+ risk_reward: long_c.risk_reward,
65361
+ risk: long_c.risk
65362
+ }
65363
+ });
65364
+ }
65365
+ return config2;
65366
+ }
65367
+ getOppositeConfig(payload) {
65368
+ return generateOppositeTradeConfig({
65369
+ kind: this.kind,
65370
+ entry: this.instance.entry,
65371
+ quantity: this.instance.quantity,
65372
+ target_pnl: this.instance.target_pnl,
65373
+ global_config: this.symbol_config,
65374
+ ratio: payload.ratio
65375
+ });
65376
+ }
65377
+ async getOptimumRiskReward() {
65378
+ const app_config = await this.tradeConfig({});
65379
+ const risk_reward = computeRiskReward({
65380
+ app_config,
65381
+ entry: app_config.entry,
65382
+ stop: app_config.stop,
65383
+ risk_per_trade: app_config.risk_per_trade,
65384
+ target_loss: app_config.risk_per_trade
65385
+ });
65386
+ return risk_reward;
65387
+ }
65234
65388
  }
65235
65389
 
65236
65390
  // src/exchange-account.ts
@@ -65303,6 +65457,7 @@ class ExchangeAccount {
65303
65457
  app_db: this.app_db,
65304
65458
  without_view: raw_positions.find((x) => x.kind === "long")
65305
65459
  });
65460
+ await this.long_position.initialize();
65306
65461
  this.short_position = new ExchangePosition({
65307
65462
  symbol_config,
65308
65463
  exchange: this.exchange,
@@ -65311,6 +65466,7 @@ class ExchangeAccount {
65311
65466
  app_db: this.app_db,
65312
65467
  without_view: raw_positions.find((x) => x.kind === "short")
65313
65468
  });
65469
+ await this.short_position.initialize();
65314
65470
  return payload.kind === "long" ? this.long_position : this.short_position;
65315
65471
  }
65316
65472
  async getActiveAccount(payload) {
@@ -65550,29 +65706,8 @@ class ExchangeAccount {
65550
65706
  return result;
65551
65707
  }
65552
65708
  async getOrCreatePositionConfig(payload) {
65553
- const { symbol, kind, risk = 50, risk_reward = 199 } = payload;
65554
- const config2 = await this.getPositionConfig({
65555
- symbol: payload.symbol,
65556
- kind: payload.kind
65557
- });
65558
- if (!config2) {
65559
- const long_c = await this.buildConfigForSymbol({
65560
- symbol,
65561
- risk,
65562
- risk_reward
65563
- });
65564
- return this.getPositionConfig({
65565
- symbol,
65566
- kind,
65567
- params: {
65568
- entry: kind === "long" ? long_c.entry : long_c.stop,
65569
- stop: kind === "long" ? long_c.stop : long_c.entry,
65570
- risk_reward: long_c.risk_reward,
65571
- risk: long_c.risk
65572
- }
65573
- });
65574
- }
65575
- return config2;
65709
+ const focus_position = await this.getFocusPosition(payload);
65710
+ return await focus_position.getOrCreatePositionConfig(payload);
65576
65711
  }
65577
65712
  async getPositionConfig(payload) {
65578
65713
  const focus_position = await this.getFocusPosition(payload);
@@ -66306,15 +66441,9 @@ class ExchangeAccount {
66306
66441
  const all_open_symbols = await this.exchange.getOpenPositions();
66307
66442
  await new Promise((resolve) => setTimeout(resolve, interval * 1000));
66308
66443
  for (const symbol of Array.from(new Set(symbols.concat(all_open_symbols)))) {
66309
- await this.getLiveExchangeInstance({ symbol, refresh: true });
66444
+ await this.initializePositions({ symbol, kind: "long", update: true });
66310
66445
  await new Promise((resolve) => setTimeout(resolve, 1000));
66311
66446
  }
66312
- for (const symbol of all_open_symbols) {
66313
- await this.syncAccount({
66314
- symbol,
66315
- update: true
66316
- });
66317
- }
66318
66447
  }
66319
66448
  async updateAllPositionsWithNoConfig(payload) {
66320
66449
  const { kind } = payload;
@@ -66697,6 +66826,7 @@ class ExchangeAccount {
66697
66826
  return;
66698
66827
  }
66699
66828
  const kind = strategy2.kind;
66829
+ const reward_factor = strategy2.reward_factor;
66700
66830
  const { entries, last_value, ...app_config } = await this.tradeConfig({
66701
66831
  symbol,
66702
66832
  kind
@@ -66718,6 +66848,36 @@ class ExchangeAccount {
66718
66848
  });
66719
66849
  }
66720
66850
  console.log("Checking if focus position has quantity for ", symbol, kind);
66851
+ const focusPosition = await this.getFocusPosition({
66852
+ symbol,
66853
+ kind
66854
+ });
66855
+ const reversePosition = await this.getFocusPosition({
66856
+ symbol,
66857
+ kind: kind === "long" ? "short" : "long"
66858
+ });
66859
+ const opposite_config = focusPosition.getOppositeConfig({
66860
+ ratio: reward_factor
66861
+ });
66862
+ const reverse_config = await reversePosition.getConfig();
66863
+ if ((reverse_config.entry !== opposite_config.entry || reverse_config.stop !== opposite_config.stop || reverse_config.risk !== opposite_config.risk || reverse_config.risk_reward !== opposite_config.risk_reward) && focusPosition.getInstance().quantity > 0) {
66864
+ console.log("Updating reverse config for ", symbol, kind, "with opposite config", opposite_config);
66865
+ await reversePosition.getConfig({
66866
+ params: {
66867
+ entry: opposite_config.entry,
66868
+ stop: opposite_config.stop,
66869
+ risk: opposite_config.risk,
66870
+ risk_reward: opposite_config.risk_reward
66871
+ }
66872
+ });
66873
+ console.log("Placing trade for ", symbol, reversePosition.kind);
66874
+ await reversePosition.placeTrade({
66875
+ ignore_config: true,
66876
+ limit: true,
66877
+ place: true
66878
+ });
66879
+ }
66880
+ console.log("Opposite config for ", symbol, kind, opposite_config);
66721
66881
  return {
66722
66882
  config_details: {
66723
66883
  app_config,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-151",
4
+ "version": "0.0.2-154",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",