@gbozee/ultimate 0.0.2-83 → 0.0.2-89

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.
@@ -58655,7 +58655,7 @@ class AppDatabase {
58655
58655
  profit: payload.profit !== undefined ? payload.profit : config2.profit
58656
58656
  });
58657
58657
  await this.update_db_position(db_position, {
58658
- config: config2.id
58658
+ config: null
58659
58659
  });
58660
58660
  for (const _config of configs) {
58661
58661
  if (_config.id !== config2.id) {
@@ -58677,7 +58677,7 @@ class AppDatabase {
58677
58677
  });
58678
58678
  }
58679
58679
  await this.pb.collection("positions").update(db_position.id, {
58680
- config: config2?.id
58680
+ config: null
58681
58681
  });
58682
58682
  return config2;
58683
58683
  }
@@ -58691,9 +58691,17 @@ class AppDatabase {
58691
58691
  }
58692
58692
  return null;
58693
58693
  }
58694
- async getPositionStrategy(account) {
58694
+ async getRunningAccountStrategies() {
58695
+ const result = await this.pb.collection("account_strategies").getFullList({
58696
+ filter: `running=true`,
58697
+ expand: "account"
58698
+ });
58699
+ return result;
58700
+ }
58701
+ async getAccountStrategy(payload) {
58702
+ const { symbol, account } = payload;
58695
58703
  const _strategy_instance = await this.pb.collection("account_strategies").getFullList({
58696
- filter: `account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
58704
+ filter: `symbol:lower="${symbol.toLowerCase()}" && account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
58697
58705
  expand: "account"
58698
58706
  });
58699
58707
  if (_strategy_instance.length > 0) {
@@ -64089,8 +64097,11 @@ class ExchangeAccount {
64089
64097
  async getCurrentPrice(symbol) {
64090
64098
  return await this.exchange.get_current_price(symbol);
64091
64099
  }
64092
- async getPositionStrategy() {
64093
- return await this.app_db.getPositionStrategy(this.instance);
64100
+ async getAccountStrategy(payload) {
64101
+ return await this.app_db.getAccountStrategy({
64102
+ symbol: payload.symbol,
64103
+ account: this.instance
64104
+ });
64094
64105
  }
64095
64106
  async buildReduceConfig(payload) {
64096
64107
  const positions = await this.syncAccount({
@@ -65416,6 +65427,196 @@ class ExchangeAccount {
65416
65427
  });
65417
65428
  }
65418
65429
  }
65430
+ async profitWithinGapStrategy(payload) {
65431
+ const {
65432
+ symbol,
65433
+ kind,
65434
+ risk,
65435
+ resistance,
65436
+ support,
65437
+ reward_factor = 1
65438
+ } = payload;
65439
+ console.log("Getting entry and stop for ", symbol, kind);
65440
+ const entry = kind === "long" ? resistance : support;
65441
+ const stop = kind === "long" ? support : resistance;
65442
+ console.log("Building app config for ", symbol, kind);
65443
+ const initial_app_config = await this.buildAppConfig({
65444
+ entry,
65445
+ stop,
65446
+ risk_reward: 199,
65447
+ risk,
65448
+ symbol
65449
+ });
65450
+ console.log("Computing risk reward for ", symbol, kind);
65451
+ const risk_reward = computeRiskReward({
65452
+ app_config: initial_app_config,
65453
+ entry: initial_app_config.entry,
65454
+ stop: initial_app_config.stop,
65455
+ risk_per_trade: initial_app_config.risk_per_trade
65456
+ });
65457
+ console.log("Re-computing app config for ", symbol, kind);
65458
+ const { entries, last_value, ...app_config } = await this.buildAppConfig({
65459
+ entry: initial_app_config.entry,
65460
+ stop: initial_app_config.stop,
65461
+ risk_reward,
65462
+ risk,
65463
+ symbol
65464
+ });
65465
+ console.log("Computing profit percent for ", symbol, kind);
65466
+ const pnl = reward_factor * risk;
65467
+ const profit_percent = to_f2(pnl * 100 / (last_value.avg_entry * last_value.avg_size), "%.4f");
65468
+ let config2 = {
65469
+ entry,
65470
+ stop,
65471
+ risk,
65472
+ risk_reward,
65473
+ profit_percent
65474
+ };
65475
+ console.log("Saving new config for ", symbol, kind);
65476
+ const data = await this.getPositionConfig({
65477
+ symbol,
65478
+ kind,
65479
+ params: config2
65480
+ });
65481
+ console.log("Checking orders to place for ", symbol, kind);
65482
+ const orders_to_place = await this.placeTrade({
65483
+ symbol,
65484
+ raw: true,
65485
+ kind,
65486
+ place: false,
65487
+ ignore_config: true
65488
+ });
65489
+ await this.placeTrade({
65490
+ symbol,
65491
+ kind,
65492
+ limit: false,
65493
+ ignore_config: true,
65494
+ tp: true
65495
+ });
65496
+ config2 = {
65497
+ ...config2,
65498
+ ...data
65499
+ };
65500
+ console.log("Fetching positions for ", symbol);
65501
+ const positions = await this.syncAccount({
65502
+ symbol,
65503
+ as_view: true
65504
+ });
65505
+ console.log("Getting long and short positions for ", symbol);
65506
+ const long_position = positions.find((k) => k.kind === "long");
65507
+ const short_position = positions.find((k) => k.kind === "short");
65508
+ console.log("Getting focus position for ", symbol, kind);
65509
+ const focus_position = kind === "long" ? long_position : short_position;
65510
+ const reverse_position = kind === "long" ? short_position : long_position;
65511
+ let reverse_action = null;
65512
+ let reverse_orders_to_buy = [];
65513
+ let reverse_config = null;
65514
+ if (focus_position.avg_qty < last_value.avg_size) {
65515
+ console.log("Placing trade for ", symbol, kind);
65516
+ await this.placeTrade({
65517
+ symbol,
65518
+ kind,
65519
+ ignore_config: true,
65520
+ place: true
65521
+ });
65522
+ }
65523
+ console.log("Checking if focus position has quantity for ", symbol, kind);
65524
+ if (focus_position.quantity > 0) {
65525
+ console.log("Getting details for ", reverse_position.kind);
65526
+ reverse_action = await this.buildOppositeTrades({
65527
+ symbol,
65528
+ kind
65529
+ });
65530
+ console.log("Updating config for ", symbol, reverse_action.kind);
65531
+ await this.getPositionConfig({
65532
+ symbol,
65533
+ kind: reverse_action.kind,
65534
+ params: {
65535
+ entry: reverse_action.entry,
65536
+ stop: reverse_action.stop,
65537
+ risk: reverse_action.risk_per_trade,
65538
+ profit_percent: reverse_action.profit_percent,
65539
+ risk_reward: reverse_action.risk_reward
65540
+ }
65541
+ });
65542
+ console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
65543
+ const reverse_app_config = await this.buildAppConfig({
65544
+ entry: reverse_action.entry,
65545
+ stop: reverse_action.stop,
65546
+ risk_reward: reverse_action.risk_reward,
65547
+ risk: reverse_action.risk_per_trade,
65548
+ symbol
65549
+ });
65550
+ if (reverse_app_config.max_size != reverse_position.avg_qty) {
65551
+ reverse_orders_to_buy = await this.placeTrade({
65552
+ symbol,
65553
+ raw: true,
65554
+ kind: reverse_action.kind,
65555
+ ignore_config: true,
65556
+ place: false
65557
+ });
65558
+ let _reverse_config = {
65559
+ avg: reverse_action.avg,
65560
+ entry: reverse_action.entry,
65561
+ stop: reverse_action.stop,
65562
+ risk_per_trade: reverse_action.risk_per_trade,
65563
+ profit_percent: reverse_action.profit_percent,
65564
+ risk_reward: reverse_action.risk_reward
65565
+ };
65566
+ if (reverse_orders_to_buy.length > 0) {
65567
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
65568
+ let existing = await this.placeOppositeTradeAction({
65569
+ symbol,
65570
+ kind: reverse_action.kind,
65571
+ data: _reverse_config
65572
+ });
65573
+ _reverse_config = {
65574
+ ...existing,
65575
+ ..._reverse_config
65576
+ };
65577
+ }
65578
+ reverse_config = _reverse_config;
65579
+ }
65580
+ if (!reverse_config?.id) {
65581
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
65582
+ reverse_config = await this.getPositionConfig({
65583
+ symbol,
65584
+ kind: reverse_action.kind
65585
+ });
65586
+ }
65587
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
65588
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
65589
+ const max_size = app_config.max_size * 0.98;
65590
+ if (reverse_config.threshold_qty !== max_size) {
65591
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65592
+ follow: true,
65593
+ threshold_qty: max_size
65594
+ });
65595
+ }
65596
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
65597
+ } else {
65598
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65599
+ follow: false
65600
+ });
65601
+ }
65602
+ }
65603
+ return {
65604
+ reverse_config,
65605
+ reverse_action,
65606
+ reverse_orders_to_buy,
65607
+ positions: {
65608
+ long: long_position,
65609
+ short: short_position
65610
+ },
65611
+ orders_to_place,
65612
+ config_details: {
65613
+ app_config,
65614
+ last_value,
65615
+ config: config2,
65616
+ pnl
65617
+ }
65618
+ };
65619
+ }
65419
65620
  }
65420
65621
  function getExchangeKlass(exchange) {
65421
65622
  const func = exchange === "binance" ? BinanceExchange : BybitExchange;
@@ -65461,6 +65662,7 @@ async function getExchangeAccount(payload) {
65461
65662
  app_db
65462
65663
  });
65463
65664
  }
65665
+
65464
65666
  // src/app.ts
65465
65667
  class App {
65466
65668
  app_db;
@@ -65681,7 +65883,8 @@ class App {
65681
65883
  await exchange_account.placeTrade({
65682
65884
  symbol,
65683
65885
  kind: "long",
65684
- tp: true
65886
+ tp: true,
65887
+ limit: false
65685
65888
  });
65686
65889
  await new Promise((resolve) => setTimeout(resolve, 500));
65687
65890
  }
@@ -65728,6 +65931,21 @@ class App {
65728
65931
  });
65729
65932
  return result;
65730
65933
  }
65934
+ async runDbStrategyAccounts(callback) {
65935
+ const strategies = await this.app_db.getRunningAccountStrategies();
65936
+ for (const strategy of strategies) {
65937
+ console.log("Running strategy for ", strategy.symbol, "for account", strategy.expand.account.owner, strategy.expand.account.exchange);
65938
+ await callback({
65939
+ symbol: strategy.symbol,
65940
+ kind: strategy.kind,
65941
+ risk: strategy.risk,
65942
+ resistance: strategy.resistance,
65943
+ support: strategy.support,
65944
+ account: strategy.expand.account,
65945
+ reward_factor: strategy.reward_factor
65946
+ });
65947
+ }
65948
+ }
65731
65949
  async profitWithinGapStrategy(payload) {
65732
65950
  const {
65733
65951
  account,
@@ -65739,168 +65957,15 @@ class App {
65739
65957
  reward_factor = 1
65740
65958
  } = payload;
65741
65959
  const exchange_account = await this.getExchangeAccount(account);
65742
- console.log("Getting entry and stop for ", symbol, kind);
65743
- const entry = kind === "long" ? resistance : support;
65744
- const stop = kind === "long" ? support : resistance;
65745
- console.log("Building app config for ", symbol, kind);
65746
- const initial_app_config = await exchange_account.buildAppConfig({
65747
- entry,
65748
- stop,
65749
- risk_reward: 199,
65750
- risk,
65751
- symbol
65752
- });
65753
- console.log("Computing risk reward for ", symbol, kind);
65754
- const risk_reward = computeRiskReward({
65755
- app_config: initial_app_config,
65756
- entry: initial_app_config.entry,
65757
- stop: initial_app_config.stop,
65758
- risk_per_trade: initial_app_config.risk_per_trade
65759
- });
65760
- console.log("Re-computing app config for ", symbol, kind);
65761
- const { entries, last_value, ...app_config } = await exchange_account.buildAppConfig({
65762
- entry: initial_app_config.entry,
65763
- stop: initial_app_config.stop,
65764
- risk_reward,
65765
- risk,
65766
- symbol
65767
- });
65768
- console.log("Computing profit percent for ", symbol, kind);
65769
- const pnl = reward_factor * risk;
65770
- const profit_percent = to_f2(pnl * 100 / (last_value.avg_entry * last_value.avg_size), "%.4f");
65771
- let config2 = {
65772
- entry,
65773
- stop,
65774
- risk,
65775
- risk_reward,
65776
- profit_percent
65777
- };
65778
- console.log("Saving new config for ", symbol, kind);
65779
- const data = await exchange_account.getPositionConfig({
65960
+ const result = await exchange_account.profitWithinGapStrategy({
65780
65961
  symbol,
65781
65962
  kind,
65782
- params: config2
65783
- });
65784
- console.log("Checking orders to place for ", symbol, kind);
65785
- const orders_to_place = await exchange_account.placeTrade({
65786
- symbol,
65787
- raw: true,
65788
- kind,
65789
- place: false
65790
- });
65791
- config2 = {
65792
- ...config2,
65793
- ...data
65794
- };
65795
- console.log("Fetching positions for ", symbol);
65796
- const positions = await exchange_account.syncAccount({
65797
- symbol,
65798
- as_view: true
65963
+ risk,
65964
+ resistance,
65965
+ support,
65966
+ reward_factor
65799
65967
  });
65800
- console.log("Getting long and short positions for ", symbol);
65801
- const long_position = positions.find((k) => k.kind === "long");
65802
- const short_position = positions.find((k) => k.kind === "short");
65803
- console.log("Getting focus position for ", symbol, kind);
65804
- const focus_position = kind === "long" ? long_position : short_position;
65805
- const reverse_position = kind === "long" ? short_position : long_position;
65806
- let reverse_action = null;
65807
- let reverse_orders_to_buy = [];
65808
- let reverse_config = null;
65809
- console.log("Checking if focus position has quantity for ", symbol, kind);
65810
- if (focus_position.quantity > 0) {
65811
- console.log("Getting details for ", reverse_position.kind);
65812
- reverse_action = await exchange_account.buildOppositeTrades({
65813
- symbol,
65814
- kind
65815
- });
65816
- console.log("Updating config for ", symbol, reverse_action.kind);
65817
- await exchange_account.getPositionConfig({
65818
- symbol,
65819
- kind: reverse_action.kind,
65820
- params: {
65821
- entry: reverse_action.entry,
65822
- stop: reverse_action.stop,
65823
- risk: reverse_action.risk_per_trade,
65824
- profit_percent: reverse_action.profit_percent,
65825
- risk_reward: reverse_action.risk_reward
65826
- }
65827
- });
65828
- console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
65829
- const reverse_app_config = await exchange_account.buildAppConfig({
65830
- entry: reverse_action.entry,
65831
- stop: reverse_action.stop,
65832
- risk_reward: reverse_action.risk_reward,
65833
- risk: reverse_action.risk_per_trade,
65834
- symbol
65835
- });
65836
- if (reverse_app_config.max_size != reverse_position.avg_qty) {
65837
- reverse_orders_to_buy = await exchange_account.placeTrade({
65838
- symbol,
65839
- raw: true,
65840
- kind: reverse_action.kind,
65841
- place: false
65842
- });
65843
- let _reverse_config = {
65844
- avg: reverse_action.avg,
65845
- entry: reverse_action.entry,
65846
- stop: reverse_action.stop,
65847
- risk_per_trade: reverse_action.risk_per_trade,
65848
- profit_percent: reverse_action.profit_percent,
65849
- risk_reward: reverse_action.risk_reward
65850
- };
65851
- if (reverse_orders_to_buy.length > 0) {
65852
- console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
65853
- let existing = await exchange_account.placeOppositeTradeAction({
65854
- symbol,
65855
- kind: reverse_action.kind,
65856
- data: _reverse_config
65857
- });
65858
- _reverse_config = {
65859
- ...existing,
65860
- ..._reverse_config
65861
- };
65862
- }
65863
- reverse_config = _reverse_config;
65864
- }
65865
- if (!reverse_config?.id) {
65866
- console.log("fetching reverse config for ", symbol, reverse_action.kind);
65867
- reverse_config = await exchange_account.getPositionConfig({
65868
- symbol,
65869
- kind: reverse_action.kind
65870
- });
65871
- }
65872
- if (reverse_position.quantity > 0 && reverse_config?.id) {
65873
- console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
65874
- const max_size = app_config.max_size * 0.98;
65875
- if (reverse_config.threshold_qty !== max_size) {
65876
- await this.app_db.updateScheduledTrade(reverse_config.id, {
65877
- follow: true,
65878
- threshold_qty: max_size
65879
- });
65880
- }
65881
- console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
65882
- } else {
65883
- await this.app_db.updateScheduledTrade(reverse_config.id, {
65884
- follow: false
65885
- });
65886
- }
65887
- }
65888
- return {
65889
- reverse_config,
65890
- reverse_action,
65891
- reverse_orders_to_buy,
65892
- positions: {
65893
- long: long_position,
65894
- short: short_position
65895
- },
65896
- orders_to_place,
65897
- config_details: {
65898
- app_config,
65899
- last_value,
65900
- config: config2,
65901
- pnl
65902
- }
65903
- };
65968
+ return result;
65904
65969
  }
65905
65970
  }
65906
65971
  async function initApp(payload) {
@@ -66033,18 +66098,6 @@ async function getPositions(payload) {
66033
66098
  ...payload,
66034
66099
  refresh: true
66035
66100
  });
66036
- const symbol_config = await exchange_account.recomputeSymbolConfig({
66037
- symbol: payload.symbol
66038
- });
66039
- const strategy2 = await exchange_account.getPositionStrategy();
66040
- const strategy_config = {
66041
- tp_percent: strategy2?.tp_percent,
66042
- short_tp_factor: strategy2?.short_tp_factor,
66043
- fee_percent: strategy2?.fee_percent,
66044
- budget: strategy2?.budget,
66045
- risk_reward: strategy2?.risk_reward,
66046
- global_config: symbol_config
66047
- };
66048
66101
  const positions = await exchange_account.syncAccount({
66049
66102
  symbol: payload.symbol,
66050
66103
  as_view: true
@@ -66063,12 +66116,11 @@ async function getPositions(payload) {
66063
66116
  long: long_position,
66064
66117
  short: short_position
66065
66118
  },
66066
- runs: runs2,
66067
- strategy_config
66119
+ runs: runs2
66068
66120
  };
66069
66121
  }
66070
66122
  async function fetchExchangeDetails(payload) {
66071
- const { strategy_config, ...positions } = await getPositions({
66123
+ const { ...positions } = await getPositions({
66072
66124
  symbol: payload.symbol,
66073
66125
  account: payload.account,
66074
66126
  kind: payload.kind