@gbozee/ultimate 0.0.2-82 → 0.0.2-87

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.
@@ -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,187 @@ 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
+ console.log("Checking if focus position has quantity for ", symbol, kind);
65515
+ if (focus_position.quantity > 0) {
65516
+ console.log("Getting details for ", reverse_position.kind);
65517
+ reverse_action = await this.buildOppositeTrades({
65518
+ symbol,
65519
+ kind
65520
+ });
65521
+ console.log("Updating config for ", symbol, reverse_action.kind);
65522
+ await this.getPositionConfig({
65523
+ symbol,
65524
+ kind: reverse_action.kind,
65525
+ params: {
65526
+ entry: reverse_action.entry,
65527
+ stop: reverse_action.stop,
65528
+ risk: reverse_action.risk_per_trade,
65529
+ profit_percent: reverse_action.profit_percent,
65530
+ risk_reward: reverse_action.risk_reward
65531
+ }
65532
+ });
65533
+ console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
65534
+ const reverse_app_config = await this.buildAppConfig({
65535
+ entry: reverse_action.entry,
65536
+ stop: reverse_action.stop,
65537
+ risk_reward: reverse_action.risk_reward,
65538
+ risk: reverse_action.risk_per_trade,
65539
+ symbol
65540
+ });
65541
+ if (reverse_app_config.max_size != reverse_position.avg_qty) {
65542
+ reverse_orders_to_buy = await this.placeTrade({
65543
+ symbol,
65544
+ raw: true,
65545
+ kind: reverse_action.kind,
65546
+ ignore_config: true,
65547
+ place: false
65548
+ });
65549
+ let _reverse_config = {
65550
+ avg: reverse_action.avg,
65551
+ entry: reverse_action.entry,
65552
+ stop: reverse_action.stop,
65553
+ risk_per_trade: reverse_action.risk_per_trade,
65554
+ profit_percent: reverse_action.profit_percent,
65555
+ risk_reward: reverse_action.risk_reward
65556
+ };
65557
+ if (reverse_orders_to_buy.length > 0) {
65558
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
65559
+ let existing = await this.placeOppositeTradeAction({
65560
+ symbol,
65561
+ kind: reverse_action.kind,
65562
+ data: _reverse_config
65563
+ });
65564
+ _reverse_config = {
65565
+ ...existing,
65566
+ ..._reverse_config
65567
+ };
65568
+ }
65569
+ reverse_config = _reverse_config;
65570
+ }
65571
+ if (!reverse_config?.id) {
65572
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
65573
+ reverse_config = await this.getPositionConfig({
65574
+ symbol,
65575
+ kind: reverse_action.kind
65576
+ });
65577
+ }
65578
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
65579
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
65580
+ const max_size = app_config.max_size * 0.98;
65581
+ if (reverse_config.threshold_qty !== max_size) {
65582
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65583
+ follow: true,
65584
+ threshold_qty: max_size
65585
+ });
65586
+ }
65587
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
65588
+ } else {
65589
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65590
+ follow: false
65591
+ });
65592
+ }
65593
+ }
65594
+ return {
65595
+ reverse_config,
65596
+ reverse_action,
65597
+ reverse_orders_to_buy,
65598
+ positions: {
65599
+ long: long_position,
65600
+ short: short_position
65601
+ },
65602
+ orders_to_place,
65603
+ config_details: {
65604
+ app_config,
65605
+ last_value,
65606
+ config: config2,
65607
+ pnl
65608
+ }
65609
+ };
65610
+ }
65419
65611
  }
65420
65612
  function getExchangeKlass(exchange) {
65421
65613
  const func = exchange === "binance" ? BinanceExchange : BybitExchange;
@@ -65461,6 +65653,7 @@ async function getExchangeAccount(payload) {
65461
65653
  app_db
65462
65654
  });
65463
65655
  }
65656
+
65464
65657
  // src/app.ts
65465
65658
  class App {
65466
65659
  app_db;
@@ -65681,7 +65874,8 @@ class App {
65681
65874
  await exchange_account.placeTrade({
65682
65875
  symbol,
65683
65876
  kind: "long",
65684
- tp: true
65877
+ tp: true,
65878
+ limit: false
65685
65879
  });
65686
65880
  await new Promise((resolve) => setTimeout(resolve, 500));
65687
65881
  }
@@ -65728,6 +65922,20 @@ class App {
65728
65922
  });
65729
65923
  return result;
65730
65924
  }
65925
+ async runDbStrategyAccounts() {
65926
+ const strategies = await this.app_db.getRunningAccountStrategies();
65927
+ for (const strategy of strategies) {
65928
+ console.log("Running strategy for ", strategy.symbol, "for account", strategy.expand.account.owner, strategy.expand.account.exchange);
65929
+ await this.profitWithinGapStrategy({
65930
+ symbol: strategy.symbol,
65931
+ kind: strategy.kind,
65932
+ risk: strategy.risk,
65933
+ resistance: strategy.resistance,
65934
+ support: strategy.support,
65935
+ account: strategy.expand.account
65936
+ });
65937
+ }
65938
+ }
65731
65939
  async profitWithinGapStrategy(payload) {
65732
65940
  const {
65733
65941
  account,
@@ -65739,159 +65947,15 @@ class App {
65739
65947
  reward_factor = 1
65740
65948
  } = payload;
65741
65949
  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({
65950
+ const result = await exchange_account.profitWithinGapStrategy({
65780
65951
  symbol,
65781
65952
  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
65953
+ risk,
65954
+ resistance,
65955
+ support,
65956
+ reward_factor
65799
65957
  });
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
- reverse_orders_to_buy = await exchange_account.placeTrade({
65830
- symbol,
65831
- raw: true,
65832
- kind: reverse_action.kind,
65833
- place: false
65834
- });
65835
- let _reverse_config = {
65836
- avg: reverse_action.avg,
65837
- entry: reverse_action.entry,
65838
- stop: reverse_action.stop,
65839
- risk_per_trade: reverse_action.risk_per_trade,
65840
- profit_percent: reverse_action.profit_percent,
65841
- risk_reward: reverse_action.risk_reward
65842
- };
65843
- if (reverse_orders_to_buy.length > 0) {
65844
- console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
65845
- let existing = await exchange_account.placeOppositeTradeAction({
65846
- symbol,
65847
- kind: reverse_action.kind,
65848
- data: _reverse_config
65849
- });
65850
- _reverse_config = {
65851
- ...existing,
65852
- ..._reverse_config
65853
- };
65854
- }
65855
- reverse_config = _reverse_config;
65856
- if (!reverse_config?.id) {
65857
- console.log("fetching reverse config for ", symbol, reverse_action.kind);
65858
- reverse_config = await exchange_account.getPositionConfig({
65859
- symbol,
65860
- kind: reverse_action.kind
65861
- });
65862
- }
65863
- if (reverse_position.quantity > 0 && reverse_config?.id) {
65864
- console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
65865
- const max_size = app_config.max_size * 0.98;
65866
- if (reverse_config.threshold_qty !== max_size) {
65867
- await this.app_db.updateScheduledTrade(reverse_config.id, {
65868
- follow: true,
65869
- threshold_qty: max_size
65870
- });
65871
- }
65872
- console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
65873
- } else {
65874
- await this.app_db.updateScheduledTrade(reverse_config.id, {
65875
- follow: false
65876
- });
65877
- }
65878
- }
65879
- return {
65880
- reverse_config,
65881
- reverse_action,
65882
- reverse_orders_to_buy,
65883
- positions: {
65884
- long: long_position,
65885
- short: short_position
65886
- },
65887
- orders_to_place,
65888
- config_details: {
65889
- app_config,
65890
- last_value,
65891
- config: config2,
65892
- pnl
65893
- }
65894
- };
65958
+ return result;
65895
65959
  }
65896
65960
  }
65897
65961
  async function initApp(payload) {
@@ -66024,18 +66088,6 @@ async function getPositions(payload) {
66024
66088
  ...payload,
66025
66089
  refresh: true
66026
66090
  });
66027
- const symbol_config = await exchange_account.recomputeSymbolConfig({
66028
- symbol: payload.symbol
66029
- });
66030
- const strategy2 = await exchange_account.getPositionStrategy();
66031
- const strategy_config = {
66032
- tp_percent: strategy2?.tp_percent,
66033
- short_tp_factor: strategy2?.short_tp_factor,
66034
- fee_percent: strategy2?.fee_percent,
66035
- budget: strategy2?.budget,
66036
- risk_reward: strategy2?.risk_reward,
66037
- global_config: symbol_config
66038
- };
66039
66091
  const positions = await exchange_account.syncAccount({
66040
66092
  symbol: payload.symbol,
66041
66093
  as_view: true
@@ -66054,12 +66106,11 @@ async function getPositions(payload) {
66054
66106
  long: long_position,
66055
66107
  short: short_position
66056
66108
  },
66057
- runs: runs2,
66058
- strategy_config
66109
+ runs: runs2
66059
66110
  };
66060
66111
  }
66061
66112
  async function fetchExchangeDetails(payload) {
66062
- const { strategy_config, ...positions } = await getPositions({
66113
+ const { ...positions } = await getPositions({
66063
66114
  symbol: payload.symbol,
66064
66115
  account: payload.account,
66065
66116
  kind: payload.kind