@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.
@@ -58446,7 +58446,6 @@ class AppDatabase {
58446
58446
  const result = await this.get_exchange_db_instance(account);
58447
58447
  if (result?.expand?.proxy) {
58448
58448
  const { type, ip_address } = result.expand.proxy;
58449
- console.log(type, ip_address);
58450
58449
  if (type === "http") {
58451
58450
  return new import_https_proxy_agent.HttpsProxyAgent(`http://${ip_address}`);
58452
58451
  }
@@ -58494,13 +58493,13 @@ class AppDatabase {
58494
58493
  table: "positions_view",
58495
58494
  params: {
58496
58495
  filter: `symbol:lower="${symbol.toLowerCase()}" && account:lower="${account.owner.toLowerCase()} ${account.exchange.toLowerCase()}"`,
58497
- expand: "config, account_strategy, p_account"
58496
+ expand: "config, account_strategy, p_account, proxy"
58498
58497
  }
58499
58498
  } : {
58500
58499
  table: "positions",
58501
58500
  params: {
58502
58501
  filter: `symbol:lower="${symbol.toLowerCase()}" && account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
58503
- expand: "account,config"
58502
+ expand: "account,config, proxy"
58504
58503
  }
58505
58504
  };
58506
58505
  if (custom_filter) {
@@ -61010,7 +61009,14 @@ function generateOptimumAppConfig(config2, payload, position2) {
61010
61009
  });
61011
61010
  return best_app_config;
61012
61011
  }
61013
- function determineOptimumReward(app_config, increase = true, low_range = 30, high_range = 199) {
61012
+ function determineOptimumReward(payload) {
61013
+ const {
61014
+ app_config,
61015
+ increase = true,
61016
+ low_range = 1,
61017
+ high_range = 199,
61018
+ target_loss
61019
+ } = payload;
61014
61020
  const criterion = app_config.strategy || "quantity";
61015
61021
  const risk_rewards = createArray(low_range, high_range, 1);
61016
61022
  let func = risk_rewards.map((trade_no) => {
@@ -61050,10 +61056,33 @@ function determineOptimumReward(app_config, increase = true, low_range = 30, hig
61050
61056
  entry
61051
61057
  };
61052
61058
  });
61053
- func = func.filter((r2) => Boolean(r2)).filter((r2) => {
61054
- let foundIndex = r2?.result.findIndex((e2) => e2.quantity === r2.max);
61055
- return criterion === "quantity" ? foundIndex === 0 : true;
61056
- });
61059
+ func = func.filter((r2) => Boolean(r2));
61060
+ if (target_loss === undefined) {
61061
+ func = func.filter((r2) => {
61062
+ let foundIndex = r2?.result.findIndex((e2) => e2.quantity === r2.max);
61063
+ return criterion === "quantity" ? foundIndex === 0 : true;
61064
+ });
61065
+ }
61066
+ if (target_loss !== undefined) {
61067
+ const validResults = func.filter((r2) => Math.abs(r2.neg_pnl) <= target_loss);
61068
+ if (validResults.length > 0) {
61069
+ validResults.sort((a, b) => {
61070
+ const diffA = target_loss - Math.abs(a.neg_pnl);
61071
+ const diffB = target_loss - Math.abs(b.neg_pnl);
61072
+ return diffA - diffB;
61073
+ });
61074
+ if (app_config.raw) {
61075
+ return validResults[0];
61076
+ }
61077
+ return validResults[0]?.value;
61078
+ } else {
61079
+ func.sort((a, b) => Math.abs(a.neg_pnl) - Math.abs(b.neg_pnl));
61080
+ if (app_config.raw) {
61081
+ return func[0];
61082
+ }
61083
+ return func[0]?.value;
61084
+ }
61085
+ }
61057
61086
  const highest = criterion === "quantity" ? Math.max(...func.map((o) => o.max)) : Math.min(...func.map((o) => o.entry));
61058
61087
  const key = criterion === "quantity" ? "max" : "entry";
61059
61088
  const index = findIndexByCondition(func, app_config.kind, (x) => x[key] == highest, criterion);
@@ -61165,13 +61194,13 @@ function determineOptimumRisk(config2, payload, params) {
61165
61194
  };
61166
61195
  }
61167
61196
  function computeRiskReward(payload) {
61168
- const { app_config, entry, stop, risk_per_trade } = payload;
61197
+ const { app_config, entry, stop, risk_per_trade, target_loss } = payload;
61169
61198
  const kind = entry > stop ? "long" : "short";
61170
61199
  app_config.kind = kind;
61171
61200
  app_config.entry = entry;
61172
61201
  app_config.stop = stop;
61173
61202
  app_config.risk_per_trade = risk_per_trade;
61174
- const result = determineOptimumReward(app_config);
61203
+ const result = determineOptimumReward({ app_config, target_loss });
61175
61204
  return result;
61176
61205
  }
61177
61206
  function getRiskReward(payload) {
@@ -61500,6 +61529,67 @@ function determineCompoundLongTrade(payload) {
61500
61529
  short_max_size
61501
61530
  };
61502
61531
  }
61532
+ function generateOppositeTradeConfig(payload) {
61533
+ const {
61534
+ kind,
61535
+ entry,
61536
+ quantity,
61537
+ target_pnl,
61538
+ ratio = 0.5,
61539
+ global_config
61540
+ } = payload;
61541
+ const diff = target_pnl / quantity;
61542
+ const tp = kind === "long" ? entry + diff : entry - diff;
61543
+ const stop = kind === "long" ? entry - diff : entry + diff;
61544
+ const opposite_pnl = target_pnl * ratio;
61545
+ const app_config = constructAppConfig({
61546
+ account: {
61547
+ expand: {
61548
+ b_config: {
61549
+ entry,
61550
+ stop,
61551
+ symbol: global_config.symbol,
61552
+ risk: target_pnl
61553
+ }
61554
+ }
61555
+ },
61556
+ global_config
61557
+ });
61558
+ const risk_reward = computeRiskReward({
61559
+ app_config,
61560
+ entry: stop,
61561
+ stop: tp,
61562
+ risk_per_trade: opposite_pnl,
61563
+ target_loss: opposite_pnl
61564
+ });
61565
+ return {
61566
+ entry: to_f(stop, global_config.price_places),
61567
+ stop: to_f(tp, global_config.price_places),
61568
+ risk: to_f(opposite_pnl, "%.2f"),
61569
+ risk_reward
61570
+ };
61571
+ }
61572
+ function constructAppConfig(payload) {
61573
+ const { account, global_config, kelly_config } = payload;
61574
+ const config2 = account.expand?.b_config;
61575
+ if (!config2) {
61576
+ return null;
61577
+ }
61578
+ const kelly = config2.kelly;
61579
+ const options = {
61580
+ entry: config2?.entry,
61581
+ stop: config2?.stop,
61582
+ risk_reward: config2?.risk_reward,
61583
+ risk: config2?.risk,
61584
+ symbol: account.symbol,
61585
+ use_kelly: kelly_config?.use_kelly ?? kelly?.use_kelly,
61586
+ kelly_confidence_factor: kelly_config?.kelly_confidence_factor ?? kelly?.kelly_confidence_factor,
61587
+ kelly_minimum_risk: kelly_config?.kelly_minimum_risk ?? kelly?.kelly_minimum_risk,
61588
+ kelly_prediction_model: kelly_config?.kelly_prediction_model ?? kelly?.kelly_prediction_model
61589
+ };
61590
+ const appConfig = buildAppConfig(global_config, options);
61591
+ return appConfig;
61592
+ }
61503
61593
  // src/helpers/strategy.ts
61504
61594
  class Strategy {
61505
61595
  position;
@@ -64421,6 +64511,9 @@ async function reduceMajorPositionEntry(input, accountInfo, trigger2, exchange_i
64421
64511
  }
64422
64512
 
64423
64513
  // src/position.ts
64514
+ var import_https_proxy_agent2 = __toESM(require_dist2());
64515
+ var import_socks_proxy_agent2 = __toESM(require_dist3());
64516
+
64424
64517
  class ExchangePosition {
64425
64518
  exchange;
64426
64519
  symbol_config;
@@ -64434,6 +64527,13 @@ class ExchangePosition {
64434
64527
  this.instance = payload.instance;
64435
64528
  this.exchange_account = payload.exchange_account;
64436
64529
  }
64530
+ async initialize() {
64531
+ const proxy = await this.getProxyForAccount();
64532
+ if (proxy) {
64533
+ this.exchange.client.globalRequestOptions.httpAgent = proxy;
64534
+ this.exchange.client.globalRequestOptions.httpsAgent = proxy;
64535
+ }
64536
+ }
64437
64537
  getInstance() {
64438
64538
  return this.instance;
64439
64539
  }
@@ -64447,6 +64547,20 @@ class ExchangePosition {
64447
64547
  const { p_account } = this.instance.expand;
64448
64548
  return p_account;
64449
64549
  }
64550
+ async getProxyForAccount() {
64551
+ if (this.instance.expand.proxy) {
64552
+ const result = this.instance.expand.proxy;
64553
+ const { type, ip_address } = result;
64554
+ console.log("position", type, ip_address);
64555
+ if (type === "http") {
64556
+ return new import_https_proxy_agent2.HttpsProxyAgent(`http://${ip_address}`);
64557
+ }
64558
+ if (type === "socks5") {
64559
+ return new import_socks_proxy_agent2.SocksProxyAgent(`socks://${ip_address}`);
64560
+ }
64561
+ }
64562
+ return null;
64563
+ }
64450
64564
  async cancelOrders(payload) {
64451
64565
  const { limit, price: _price, raw } = payload;
64452
64566
  if (limit) {
@@ -65254,6 +65368,46 @@ class ExchangePosition {
65254
65368
  });
65255
65369
  return app_config;
65256
65370
  }
65371
+ async getOrCreatePositionConfig(payload) {
65372
+ const { risk = 50, risk_reward = 199 } = payload;
65373
+ const config2 = await this.getConfig();
65374
+ if (!config2) {
65375
+ const long_c = await this.buildConfigForSymbol({
65376
+ risk,
65377
+ risk_reward
65378
+ });
65379
+ return this.getConfig({
65380
+ params: {
65381
+ entry: config2.entry,
65382
+ stop: config2.stop,
65383
+ risk_reward: long_c.risk_reward,
65384
+ risk: long_c.risk
65385
+ }
65386
+ });
65387
+ }
65388
+ return config2;
65389
+ }
65390
+ getOppositeConfig(payload) {
65391
+ return generateOppositeTradeConfig({
65392
+ kind: this.kind,
65393
+ entry: this.instance.entry,
65394
+ quantity: this.instance.quantity,
65395
+ target_pnl: this.instance.target_pnl,
65396
+ global_config: this.symbol_config,
65397
+ ratio: payload.ratio
65398
+ });
65399
+ }
65400
+ async getOptimumRiskReward() {
65401
+ const app_config = await this.tradeConfig({});
65402
+ const risk_reward = computeRiskReward({
65403
+ app_config,
65404
+ entry: app_config.entry,
65405
+ stop: app_config.stop,
65406
+ risk_per_trade: app_config.risk_per_trade,
65407
+ target_loss: app_config.risk_per_trade
65408
+ });
65409
+ return risk_reward;
65410
+ }
65257
65411
  }
65258
65412
 
65259
65413
  // src/exchange-account.ts
@@ -65326,6 +65480,7 @@ class ExchangeAccount {
65326
65480
  app_db: this.app_db,
65327
65481
  without_view: raw_positions.find((x) => x.kind === "long")
65328
65482
  });
65483
+ await this.long_position.initialize();
65329
65484
  this.short_position = new ExchangePosition({
65330
65485
  symbol_config,
65331
65486
  exchange: this.exchange,
@@ -65334,6 +65489,7 @@ class ExchangeAccount {
65334
65489
  app_db: this.app_db,
65335
65490
  without_view: raw_positions.find((x) => x.kind === "short")
65336
65491
  });
65492
+ await this.short_position.initialize();
65337
65493
  return payload.kind === "long" ? this.long_position : this.short_position;
65338
65494
  }
65339
65495
  async getActiveAccount(payload) {
@@ -65573,29 +65729,8 @@ class ExchangeAccount {
65573
65729
  return result;
65574
65730
  }
65575
65731
  async getOrCreatePositionConfig(payload) {
65576
- const { symbol, kind, risk = 50, risk_reward = 199 } = payload;
65577
- const config2 = await this.getPositionConfig({
65578
- symbol: payload.symbol,
65579
- kind: payload.kind
65580
- });
65581
- if (!config2) {
65582
- const long_c = await this.buildConfigForSymbol({
65583
- symbol,
65584
- risk,
65585
- risk_reward
65586
- });
65587
- return this.getPositionConfig({
65588
- symbol,
65589
- kind,
65590
- params: {
65591
- entry: kind === "long" ? long_c.entry : long_c.stop,
65592
- stop: kind === "long" ? long_c.stop : long_c.entry,
65593
- risk_reward: long_c.risk_reward,
65594
- risk: long_c.risk
65595
- }
65596
- });
65597
- }
65598
- return config2;
65732
+ const focus_position = await this.getFocusPosition(payload);
65733
+ return await focus_position.getOrCreatePositionConfig(payload);
65599
65734
  }
65600
65735
  async getPositionConfig(payload) {
65601
65736
  const focus_position = await this.getFocusPosition(payload);
@@ -66329,15 +66464,9 @@ class ExchangeAccount {
66329
66464
  const all_open_symbols = await this.exchange.getOpenPositions();
66330
66465
  await new Promise((resolve) => setTimeout(resolve, interval * 1000));
66331
66466
  for (const symbol of Array.from(new Set(symbols.concat(all_open_symbols)))) {
66332
- await this.getLiveExchangeInstance({ symbol, refresh: true });
66467
+ await this.initializePositions({ symbol, kind: "long", update: true });
66333
66468
  await new Promise((resolve) => setTimeout(resolve, 1000));
66334
66469
  }
66335
- for (const symbol of all_open_symbols) {
66336
- await this.syncAccount({
66337
- symbol,
66338
- update: true
66339
- });
66340
- }
66341
66470
  }
66342
66471
  async updateAllPositionsWithNoConfig(payload) {
66343
66472
  const { kind } = payload;
@@ -66720,6 +66849,7 @@ class ExchangeAccount {
66720
66849
  return;
66721
66850
  }
66722
66851
  const kind = strategy2.kind;
66852
+ const reward_factor = strategy2.reward_factor;
66723
66853
  const { entries, last_value, ...app_config } = await this.tradeConfig({
66724
66854
  symbol,
66725
66855
  kind
@@ -66741,6 +66871,36 @@ class ExchangeAccount {
66741
66871
  });
66742
66872
  }
66743
66873
  console.log("Checking if focus position has quantity for ", symbol, kind);
66874
+ const focusPosition = await this.getFocusPosition({
66875
+ symbol,
66876
+ kind
66877
+ });
66878
+ const reversePosition = await this.getFocusPosition({
66879
+ symbol,
66880
+ kind: kind === "long" ? "short" : "long"
66881
+ });
66882
+ const opposite_config = focusPosition.getOppositeConfig({
66883
+ ratio: reward_factor
66884
+ });
66885
+ const reverse_config = await reversePosition.getConfig();
66886
+ 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) {
66887
+ console.log("Updating reverse config for ", symbol, kind, "with opposite config", opposite_config);
66888
+ await reversePosition.getConfig({
66889
+ params: {
66890
+ entry: opposite_config.entry,
66891
+ stop: opposite_config.stop,
66892
+ risk: opposite_config.risk,
66893
+ risk_reward: opposite_config.risk_reward
66894
+ }
66895
+ });
66896
+ console.log("Placing trade for ", symbol, reversePosition.kind);
66897
+ await reversePosition.placeTrade({
66898
+ ignore_config: true,
66899
+ limit: true,
66900
+ place: true
66901
+ });
66902
+ }
66903
+ console.log("Opposite config for ", symbol, kind, opposite_config);
66744
66904
  return {
66745
66905
  config_details: {
66746
66906
  app_config,