@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.
package/dist/index.d.ts CHANGED
@@ -140,6 +140,7 @@ export interface PositionsView {
140
140
  expand?: {
141
141
  p_account?: ExchangeAccount;
142
142
  b_config?: ScheduledTrade;
143
+ proxy?: Proxy$1;
143
144
  };
144
145
  }
145
146
  export interface BullishMarket extends RecordModel {
@@ -624,6 +625,7 @@ export declare class AppDatabase {
624
625
  expand?: {
625
626
  config: ScheduledTrade;
626
627
  account: ExchangeAccount;
628
+ proxy: Proxy$1;
627
629
  };
628
630
  })[]>;
629
631
  private _createOrUpdatePosition;
@@ -636,6 +638,7 @@ export declare class AppDatabase {
636
638
  expand?: {
637
639
  config: ScheduledTrade;
638
640
  account: ExchangeAccount;
641
+ proxy: Proxy$1;
639
642
  };
640
643
  })[]>;
641
644
  update_db_position(position: any, payload: any): Promise<import("pocketbase").RecordModel>;
@@ -1436,7 +1439,13 @@ export declare function generateOptimumAppConfig(config: GlobalConfig, payload:
1436
1439
  quantity: number;
1437
1440
  kind: "long" | "short";
1438
1441
  }): AppConfig | null;
1439
- export declare function determineOptimumReward(app_config: AppConfig, increase?: boolean, low_range?: number, high_range?: number): number | {
1442
+ export declare function determineOptimumReward(payload: {
1443
+ app_config: AppConfig;
1444
+ increase?: boolean;
1445
+ low_range?: number;
1446
+ high_range?: number;
1447
+ target_loss?: number;
1448
+ }): number | {
1440
1449
  result: any[];
1441
1450
  value: number;
1442
1451
  total: number;
@@ -1472,6 +1481,7 @@ export declare function computeRiskReward(payload: {
1472
1481
  entry: number;
1473
1482
  stop: number;
1474
1483
  risk_per_trade: number;
1484
+ target_loss?: number;
1475
1485
  }): number | {
1476
1486
  result: any[];
1477
1487
  value: number;
@@ -1656,6 +1666,38 @@ export declare function determineCompoundLongTrade(payload: {
1656
1666
  result: any;
1657
1667
  short_max_size: any;
1658
1668
  };
1669
+ export declare function generateOppositeTradeConfig(payload: {
1670
+ kind: "long" | "short";
1671
+ entry: number;
1672
+ quantity: number;
1673
+ target_pnl: number;
1674
+ global_config: GlobalConfig;
1675
+ ratio?: number;
1676
+ }): {
1677
+ entry: number;
1678
+ stop: number;
1679
+ risk: number;
1680
+ risk_reward: number | {
1681
+ result: any[];
1682
+ value: number;
1683
+ total: number;
1684
+ risk_per_trade: number;
1685
+ max: number;
1686
+ min: number;
1687
+ neg_pnl: any;
1688
+ entry: any;
1689
+ };
1690
+ };
1691
+ export declare function constructAppConfig(payload: {
1692
+ account: PositionsView;
1693
+ global_config: GlobalConfig;
1694
+ kelly_config?: {
1695
+ use_kelly: boolean;
1696
+ kelly_confidence_factor: number;
1697
+ kelly_minimum_risk: number;
1698
+ kelly_prediction_model: string;
1699
+ };
1700
+ }): AppConfig;
1659
1701
  declare class ExchangePosition {
1660
1702
  exchange: BaseExchange;
1661
1703
  symbol_config: SymbolConfig;
@@ -1670,10 +1712,12 @@ declare class ExchangePosition {
1670
1712
  exchange_account: ExchangeAccount$1;
1671
1713
  without_view?: PositionsView;
1672
1714
  });
1715
+ initialize(): Promise<void>;
1673
1716
  getInstance(): PositionsView;
1674
1717
  get symbol(): any;
1675
1718
  get kind(): any;
1676
1719
  get account(): ExchangeAccount;
1720
+ getProxyForAccount(): Promise<HttpsProxyAgent<`http://${string}`> | SocksProxyAgent>;
1677
1721
  cancelOrders(payload: {
1678
1722
  limit?: boolean;
1679
1723
  price?: number;
@@ -1939,6 +1983,37 @@ declare class ExchangePosition {
1939
1983
  tradeConfig(payload: {
1940
1984
  override?: any;
1941
1985
  }): Promise<AppConfig>;
1986
+ getOrCreatePositionConfig(payload: {
1987
+ risk?: number;
1988
+ risk_reward?: number;
1989
+ }): Promise<import("pocketbase").RecordModel | ScheduledTrade>;
1990
+ getOppositeConfig(payload: {
1991
+ ratio?: number;
1992
+ }): {
1993
+ entry: number;
1994
+ stop: number;
1995
+ risk: number;
1996
+ risk_reward: number | {
1997
+ result: any[];
1998
+ value: number;
1999
+ total: number;
2000
+ risk_per_trade: number;
2001
+ max: number;
2002
+ min: number;
2003
+ neg_pnl: any;
2004
+ entry: any;
2005
+ };
2006
+ };
2007
+ getOptimumRiskReward(): Promise<number | {
2008
+ result: any[];
2009
+ value: number;
2010
+ total: number;
2011
+ risk_per_trade: number;
2012
+ max: number;
2013
+ min: number;
2014
+ neg_pnl: any;
2015
+ entry: any;
2016
+ }>;
1942
2017
  }
1943
2018
  declare class ExchangeAccount$1 {
1944
2019
  instance: {
@@ -1992,6 +2067,7 @@ declare class ExchangeAccount$1 {
1992
2067
  expand?: {
1993
2068
  config: ScheduledTrade;
1994
2069
  account: ExchangeAccount;
2070
+ proxy: Proxy$1;
1995
2071
  };
1996
2072
  })[]>;
1997
2073
  syncAccount(options: {
@@ -2005,6 +2081,7 @@ declare class ExchangeAccount$1 {
2005
2081
  expand?: {
2006
2082
  config: ScheduledTrade;
2007
2083
  account: ExchangeAccount;
2084
+ proxy: Proxy$1;
2008
2085
  };
2009
2086
  })[]>;
2010
2087
  getRunningInstanceFromDB(symbol: string): Promise<TradeBlockTracking>;
@@ -2653,6 +2730,7 @@ declare class App {
2653
2730
  expand?: {
2654
2731
  config: ScheduledTrade;
2655
2732
  account: ExchangeAccount;
2733
+ proxy: Proxy$1;
2656
2734
  };
2657
2735
  })[]>;
2658
2736
  syncOrders(payload: {
package/dist/index.js CHANGED
@@ -51692,7 +51692,6 @@ class AppDatabase {
51692
51692
  const result = await this.get_exchange_db_instance(account);
51693
51693
  if (result?.expand?.proxy) {
51694
51694
  const { type, ip_address } = result.expand.proxy;
51695
- console.log(type, ip_address);
51696
51695
  if (type === "http") {
51697
51696
  return new import_https_proxy_agent.HttpsProxyAgent(`http://${ip_address}`);
51698
51697
  }
@@ -51740,13 +51739,13 @@ class AppDatabase {
51740
51739
  table: "positions_view",
51741
51740
  params: {
51742
51741
  filter: `symbol:lower="${symbol.toLowerCase()}" && account:lower="${account.owner.toLowerCase()} ${account.exchange.toLowerCase()}"`,
51743
- expand: "config, account_strategy, p_account"
51742
+ expand: "config, account_strategy, p_account, proxy"
51744
51743
  }
51745
51744
  } : {
51746
51745
  table: "positions",
51747
51746
  params: {
51748
51747
  filter: `symbol:lower="${symbol.toLowerCase()}" && account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
51749
- expand: "account,config"
51748
+ expand: "account,config, proxy"
51750
51749
  }
51751
51750
  };
51752
51751
  if (custom_filter) {
@@ -54276,7 +54275,14 @@ function generateOptimumAppConfig(config2, payload, position2) {
54276
54275
  });
54277
54276
  return best_app_config;
54278
54277
  }
54279
- function determineOptimumReward(app_config, increase = true, low_range = 30, high_range = 199) {
54278
+ function determineOptimumReward(payload) {
54279
+ const {
54280
+ app_config,
54281
+ increase = true,
54282
+ low_range = 1,
54283
+ high_range = 199,
54284
+ target_loss
54285
+ } = payload;
54280
54286
  const criterion = app_config.strategy || "quantity";
54281
54287
  const risk_rewards = createArray(low_range, high_range, 1);
54282
54288
  let func = risk_rewards.map((trade_no) => {
@@ -54316,10 +54322,33 @@ function determineOptimumReward(app_config, increase = true, low_range = 30, hig
54316
54322
  entry
54317
54323
  };
54318
54324
  });
54319
- func = func.filter((r2) => Boolean(r2)).filter((r2) => {
54320
- let foundIndex = r2?.result.findIndex((e2) => e2.quantity === r2.max);
54321
- return criterion === "quantity" ? foundIndex === 0 : true;
54322
- });
54325
+ func = func.filter((r2) => Boolean(r2));
54326
+ if (target_loss === undefined) {
54327
+ func = func.filter((r2) => {
54328
+ let foundIndex = r2?.result.findIndex((e2) => e2.quantity === r2.max);
54329
+ return criterion === "quantity" ? foundIndex === 0 : true;
54330
+ });
54331
+ }
54332
+ if (target_loss !== undefined) {
54333
+ const validResults = func.filter((r2) => Math.abs(r2.neg_pnl) <= target_loss);
54334
+ if (validResults.length > 0) {
54335
+ validResults.sort((a, b) => {
54336
+ const diffA = target_loss - Math.abs(a.neg_pnl);
54337
+ const diffB = target_loss - Math.abs(b.neg_pnl);
54338
+ return diffA - diffB;
54339
+ });
54340
+ if (app_config.raw) {
54341
+ return validResults[0];
54342
+ }
54343
+ return validResults[0]?.value;
54344
+ } else {
54345
+ func.sort((a, b) => Math.abs(a.neg_pnl) - Math.abs(b.neg_pnl));
54346
+ if (app_config.raw) {
54347
+ return func[0];
54348
+ }
54349
+ return func[0]?.value;
54350
+ }
54351
+ }
54323
54352
  const highest = criterion === "quantity" ? Math.max(...func.map((o) => o.max)) : Math.min(...func.map((o) => o.entry));
54324
54353
  const key = criterion === "quantity" ? "max" : "entry";
54325
54354
  const index = findIndexByCondition(func, app_config.kind, (x) => x[key] == highest, criterion);
@@ -54431,13 +54460,13 @@ function determineOptimumRisk(config2, payload, params) {
54431
54460
  };
54432
54461
  }
54433
54462
  function computeRiskReward(payload) {
54434
- const { app_config, entry, stop, risk_per_trade } = payload;
54463
+ const { app_config, entry, stop, risk_per_trade, target_loss } = payload;
54435
54464
  const kind = entry > stop ? "long" : "short";
54436
54465
  app_config.kind = kind;
54437
54466
  app_config.entry = entry;
54438
54467
  app_config.stop = stop;
54439
54468
  app_config.risk_per_trade = risk_per_trade;
54440
- const result = determineOptimumReward(app_config);
54469
+ const result = determineOptimumReward({ app_config, target_loss });
54441
54470
  return result;
54442
54471
  }
54443
54472
  function getRiskReward(payload) {
@@ -54772,6 +54801,67 @@ function determineCompoundLongTrade(payload) {
54772
54801
  short_max_size
54773
54802
  };
54774
54803
  }
54804
+ function generateOppositeTradeConfig(payload) {
54805
+ const {
54806
+ kind,
54807
+ entry,
54808
+ quantity,
54809
+ target_pnl,
54810
+ ratio = 0.5,
54811
+ global_config
54812
+ } = payload;
54813
+ const diff = target_pnl / quantity;
54814
+ const tp = kind === "long" ? entry + diff : entry - diff;
54815
+ const stop = kind === "long" ? entry - diff : entry + diff;
54816
+ const opposite_pnl = target_pnl * ratio;
54817
+ const app_config = constructAppConfig({
54818
+ account: {
54819
+ expand: {
54820
+ b_config: {
54821
+ entry,
54822
+ stop,
54823
+ symbol: global_config.symbol,
54824
+ risk: target_pnl
54825
+ }
54826
+ }
54827
+ },
54828
+ global_config
54829
+ });
54830
+ const risk_reward = computeRiskReward({
54831
+ app_config,
54832
+ entry: stop,
54833
+ stop: tp,
54834
+ risk_per_trade: opposite_pnl,
54835
+ target_loss: opposite_pnl
54836
+ });
54837
+ return {
54838
+ entry: to_f(stop, global_config.price_places),
54839
+ stop: to_f(tp, global_config.price_places),
54840
+ risk: to_f(opposite_pnl, "%.2f"),
54841
+ risk_reward
54842
+ };
54843
+ }
54844
+ function constructAppConfig(payload) {
54845
+ const { account, global_config, kelly_config } = payload;
54846
+ const config2 = account.expand?.b_config;
54847
+ if (!config2) {
54848
+ return null;
54849
+ }
54850
+ const kelly = config2.kelly;
54851
+ const options = {
54852
+ entry: config2?.entry,
54853
+ stop: config2?.stop,
54854
+ risk_reward: config2?.risk_reward,
54855
+ risk: config2?.risk,
54856
+ symbol: account.symbol,
54857
+ use_kelly: kelly_config?.use_kelly ?? kelly?.use_kelly,
54858
+ kelly_confidence_factor: kelly_config?.kelly_confidence_factor ?? kelly?.kelly_confidence_factor,
54859
+ kelly_minimum_risk: kelly_config?.kelly_minimum_risk ?? kelly?.kelly_minimum_risk,
54860
+ kelly_prediction_model: kelly_config?.kelly_prediction_model ?? kelly?.kelly_prediction_model
54861
+ };
54862
+ const appConfig = buildAppConfig(global_config, options);
54863
+ return appConfig;
54864
+ }
54775
54865
  // src/helpers/strategy.ts
54776
54866
  class Strategy {
54777
54867
  position;
@@ -57693,6 +57783,9 @@ async function reduceMajorPositionEntry(input, accountInfo, trigger2, exchange_i
57693
57783
  }
57694
57784
 
57695
57785
  // src/position.ts
57786
+ var import_https_proxy_agent2 = __toESM(require_dist2(), 1);
57787
+ var import_socks_proxy_agent2 = __toESM(require_dist3(), 1);
57788
+
57696
57789
  class ExchangePosition {
57697
57790
  exchange;
57698
57791
  symbol_config;
@@ -57706,6 +57799,13 @@ class ExchangePosition {
57706
57799
  this.instance = payload.instance;
57707
57800
  this.exchange_account = payload.exchange_account;
57708
57801
  }
57802
+ async initialize() {
57803
+ const proxy = await this.getProxyForAccount();
57804
+ if (proxy) {
57805
+ this.exchange.client.globalRequestOptions.httpAgent = proxy;
57806
+ this.exchange.client.globalRequestOptions.httpsAgent = proxy;
57807
+ }
57808
+ }
57709
57809
  getInstance() {
57710
57810
  return this.instance;
57711
57811
  }
@@ -57719,6 +57819,20 @@ class ExchangePosition {
57719
57819
  const { p_account } = this.instance.expand;
57720
57820
  return p_account;
57721
57821
  }
57822
+ async getProxyForAccount() {
57823
+ if (this.instance.expand.proxy) {
57824
+ const result = this.instance.expand.proxy;
57825
+ const { type, ip_address } = result;
57826
+ console.log("position", type, ip_address);
57827
+ if (type === "http") {
57828
+ return new import_https_proxy_agent2.HttpsProxyAgent(`http://${ip_address}`);
57829
+ }
57830
+ if (type === "socks5") {
57831
+ return new import_socks_proxy_agent2.SocksProxyAgent(`socks://${ip_address}`);
57832
+ }
57833
+ }
57834
+ return null;
57835
+ }
57722
57836
  async cancelOrders(payload) {
57723
57837
  const { limit, price: _price, raw } = payload;
57724
57838
  if (limit) {
@@ -58526,6 +58640,46 @@ class ExchangePosition {
58526
58640
  });
58527
58641
  return app_config;
58528
58642
  }
58643
+ async getOrCreatePositionConfig(payload) {
58644
+ const { risk = 50, risk_reward = 199 } = payload;
58645
+ const config2 = await this.getConfig();
58646
+ if (!config2) {
58647
+ const long_c = await this.buildConfigForSymbol({
58648
+ risk,
58649
+ risk_reward
58650
+ });
58651
+ return this.getConfig({
58652
+ params: {
58653
+ entry: config2.entry,
58654
+ stop: config2.stop,
58655
+ risk_reward: long_c.risk_reward,
58656
+ risk: long_c.risk
58657
+ }
58658
+ });
58659
+ }
58660
+ return config2;
58661
+ }
58662
+ getOppositeConfig(payload) {
58663
+ return generateOppositeTradeConfig({
58664
+ kind: this.kind,
58665
+ entry: this.instance.entry,
58666
+ quantity: this.instance.quantity,
58667
+ target_pnl: this.instance.target_pnl,
58668
+ global_config: this.symbol_config,
58669
+ ratio: payload.ratio
58670
+ });
58671
+ }
58672
+ async getOptimumRiskReward() {
58673
+ const app_config = await this.tradeConfig({});
58674
+ const risk_reward = computeRiskReward({
58675
+ app_config,
58676
+ entry: app_config.entry,
58677
+ stop: app_config.stop,
58678
+ risk_per_trade: app_config.risk_per_trade,
58679
+ target_loss: app_config.risk_per_trade
58680
+ });
58681
+ return risk_reward;
58682
+ }
58529
58683
  }
58530
58684
 
58531
58685
  // src/exchange-account.ts
@@ -58598,6 +58752,7 @@ class ExchangeAccount {
58598
58752
  app_db: this.app_db,
58599
58753
  without_view: raw_positions.find((x) => x.kind === "long")
58600
58754
  });
58755
+ await this.long_position.initialize();
58601
58756
  this.short_position = new ExchangePosition({
58602
58757
  symbol_config,
58603
58758
  exchange: this.exchange,
@@ -58606,6 +58761,7 @@ class ExchangeAccount {
58606
58761
  app_db: this.app_db,
58607
58762
  without_view: raw_positions.find((x) => x.kind === "short")
58608
58763
  });
58764
+ await this.short_position.initialize();
58609
58765
  return payload.kind === "long" ? this.long_position : this.short_position;
58610
58766
  }
58611
58767
  async getActiveAccount(payload) {
@@ -58845,29 +59001,8 @@ class ExchangeAccount {
58845
59001
  return result;
58846
59002
  }
58847
59003
  async getOrCreatePositionConfig(payload) {
58848
- const { symbol, kind, risk = 50, risk_reward = 199 } = payload;
58849
- const config2 = await this.getPositionConfig({
58850
- symbol: payload.symbol,
58851
- kind: payload.kind
58852
- });
58853
- if (!config2) {
58854
- const long_c = await this.buildConfigForSymbol({
58855
- symbol,
58856
- risk,
58857
- risk_reward
58858
- });
58859
- return this.getPositionConfig({
58860
- symbol,
58861
- kind,
58862
- params: {
58863
- entry: kind === "long" ? long_c.entry : long_c.stop,
58864
- stop: kind === "long" ? long_c.stop : long_c.entry,
58865
- risk_reward: long_c.risk_reward,
58866
- risk: long_c.risk
58867
- }
58868
- });
58869
- }
58870
- return config2;
59004
+ const focus_position = await this.getFocusPosition(payload);
59005
+ return await focus_position.getOrCreatePositionConfig(payload);
58871
59006
  }
58872
59007
  async getPositionConfig(payload) {
58873
59008
  const focus_position = await this.getFocusPosition(payload);
@@ -59601,15 +59736,9 @@ class ExchangeAccount {
59601
59736
  const all_open_symbols = await this.exchange.getOpenPositions();
59602
59737
  await new Promise((resolve) => setTimeout(resolve, interval * 1000));
59603
59738
  for (const symbol of Array.from(new Set(symbols.concat(all_open_symbols)))) {
59604
- await this.getLiveExchangeInstance({ symbol, refresh: true });
59739
+ await this.initializePositions({ symbol, kind: "long", update: true });
59605
59740
  await new Promise((resolve) => setTimeout(resolve, 1000));
59606
59741
  }
59607
- for (const symbol of all_open_symbols) {
59608
- await this.syncAccount({
59609
- symbol,
59610
- update: true
59611
- });
59612
- }
59613
59742
  }
59614
59743
  async updateAllPositionsWithNoConfig(payload) {
59615
59744
  const { kind } = payload;
@@ -59992,6 +60121,7 @@ class ExchangeAccount {
59992
60121
  return;
59993
60122
  }
59994
60123
  const kind = strategy2.kind;
60124
+ const reward_factor = strategy2.reward_factor;
59995
60125
  const { entries, last_value, ...app_config } = await this.tradeConfig({
59996
60126
  symbol,
59997
60127
  kind
@@ -60013,6 +60143,36 @@ class ExchangeAccount {
60013
60143
  });
60014
60144
  }
60015
60145
  console.log("Checking if focus position has quantity for ", symbol, kind);
60146
+ const focusPosition = await this.getFocusPosition({
60147
+ symbol,
60148
+ kind
60149
+ });
60150
+ const reversePosition = await this.getFocusPosition({
60151
+ symbol,
60152
+ kind: kind === "long" ? "short" : "long"
60153
+ });
60154
+ const opposite_config = focusPosition.getOppositeConfig({
60155
+ ratio: reward_factor
60156
+ });
60157
+ const reverse_config = await reversePosition.getConfig();
60158
+ 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) {
60159
+ console.log("Updating reverse config for ", symbol, kind, "with opposite config", opposite_config);
60160
+ await reversePosition.getConfig({
60161
+ params: {
60162
+ entry: opposite_config.entry,
60163
+ stop: opposite_config.stop,
60164
+ risk: opposite_config.risk,
60165
+ risk_reward: opposite_config.risk_reward
60166
+ }
60167
+ });
60168
+ console.log("Placing trade for ", symbol, reversePosition.kind);
60169
+ await reversePosition.placeTrade({
60170
+ ignore_config: true,
60171
+ limit: true,
60172
+ place: true
60173
+ });
60174
+ }
60175
+ console.log("Opposite config for ", symbol, kind, opposite_config);
60016
60176
  return {
60017
60177
  config_details: {
60018
60178
  app_config,
@@ -60655,6 +60815,7 @@ export {
60655
60815
  getHedgeZone,
60656
60816
  generate_config_params,
60657
60817
  generateOptimumAppConfig,
60818
+ generateOppositeTradeConfig,
60658
60819
  generateGapTp,
60659
60820
  exports_exchange_account as exchange_account,
60660
60821
  determine_break_even_price,
@@ -60666,6 +60827,7 @@ export {
60666
60827
  determineCompoundLongTrade,
60667
60828
  exports_database as database,
60668
60829
  createArray,
60830
+ constructAppConfig,
60669
60831
  computeRiskReward,
60670
60832
  computeProfitDetail,
60671
60833
  buildConfig,