@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.
@@ -58632,7 +58632,7 @@ class AppDatabase {
58632
58632
  profit: payload.profit !== undefined ? payload.profit : config2.profit
58633
58633
  });
58634
58634
  await this.update_db_position(db_position, {
58635
- config: config2.id
58635
+ config: null
58636
58636
  });
58637
58637
  for (const _config of configs) {
58638
58638
  if (_config.id !== config2.id) {
@@ -58654,7 +58654,7 @@ class AppDatabase {
58654
58654
  });
58655
58655
  }
58656
58656
  await this.pb.collection("positions").update(db_position.id, {
58657
- config: config2?.id
58657
+ config: null
58658
58658
  });
58659
58659
  return config2;
58660
58660
  }
@@ -58668,9 +58668,17 @@ class AppDatabase {
58668
58668
  }
58669
58669
  return null;
58670
58670
  }
58671
- async getPositionStrategy(account) {
58671
+ async getRunningAccountStrategies() {
58672
+ const result = await this.pb.collection("account_strategies").getFullList({
58673
+ filter: `running=true`,
58674
+ expand: "account"
58675
+ });
58676
+ return result;
58677
+ }
58678
+ async getAccountStrategy(payload) {
58679
+ const { symbol, account } = payload;
58672
58680
  const _strategy_instance = await this.pb.collection("account_strategies").getFullList({
58673
- filter: `account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
58681
+ filter: `symbol:lower="${symbol.toLowerCase()}" && account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
58674
58682
  expand: "account"
58675
58683
  });
58676
58684
  if (_strategy_instance.length > 0) {
@@ -64066,8 +64074,11 @@ class ExchangeAccount {
64066
64074
  async getCurrentPrice(symbol) {
64067
64075
  return await this.exchange.get_current_price(symbol);
64068
64076
  }
64069
- async getPositionStrategy() {
64070
- return await this.app_db.getPositionStrategy(this.instance);
64077
+ async getAccountStrategy(payload) {
64078
+ return await this.app_db.getAccountStrategy({
64079
+ symbol: payload.symbol,
64080
+ account: this.instance
64081
+ });
64071
64082
  }
64072
64083
  async buildReduceConfig(payload) {
64073
64084
  const positions = await this.syncAccount({
@@ -65393,6 +65404,196 @@ class ExchangeAccount {
65393
65404
  });
65394
65405
  }
65395
65406
  }
65407
+ async profitWithinGapStrategy(payload) {
65408
+ const {
65409
+ symbol,
65410
+ kind,
65411
+ risk,
65412
+ resistance,
65413
+ support,
65414
+ reward_factor = 1
65415
+ } = payload;
65416
+ console.log("Getting entry and stop for ", symbol, kind);
65417
+ const entry = kind === "long" ? resistance : support;
65418
+ const stop = kind === "long" ? support : resistance;
65419
+ console.log("Building app config for ", symbol, kind);
65420
+ const initial_app_config = await this.buildAppConfig({
65421
+ entry,
65422
+ stop,
65423
+ risk_reward: 199,
65424
+ risk,
65425
+ symbol
65426
+ });
65427
+ console.log("Computing risk reward for ", symbol, kind);
65428
+ const risk_reward = computeRiskReward({
65429
+ app_config: initial_app_config,
65430
+ entry: initial_app_config.entry,
65431
+ stop: initial_app_config.stop,
65432
+ risk_per_trade: initial_app_config.risk_per_trade
65433
+ });
65434
+ console.log("Re-computing app config for ", symbol, kind);
65435
+ const { entries, last_value, ...app_config } = await this.buildAppConfig({
65436
+ entry: initial_app_config.entry,
65437
+ stop: initial_app_config.stop,
65438
+ risk_reward,
65439
+ risk,
65440
+ symbol
65441
+ });
65442
+ console.log("Computing profit percent for ", symbol, kind);
65443
+ const pnl = reward_factor * risk;
65444
+ const profit_percent = to_f2(pnl * 100 / (last_value.avg_entry * last_value.avg_size), "%.4f");
65445
+ let config2 = {
65446
+ entry,
65447
+ stop,
65448
+ risk,
65449
+ risk_reward,
65450
+ profit_percent
65451
+ };
65452
+ console.log("Saving new config for ", symbol, kind);
65453
+ const data = await this.getPositionConfig({
65454
+ symbol,
65455
+ kind,
65456
+ params: config2
65457
+ });
65458
+ console.log("Checking orders to place for ", symbol, kind);
65459
+ const orders_to_place = await this.placeTrade({
65460
+ symbol,
65461
+ raw: true,
65462
+ kind,
65463
+ place: false,
65464
+ ignore_config: true
65465
+ });
65466
+ await this.placeTrade({
65467
+ symbol,
65468
+ kind,
65469
+ limit: false,
65470
+ ignore_config: true,
65471
+ tp: true
65472
+ });
65473
+ config2 = {
65474
+ ...config2,
65475
+ ...data
65476
+ };
65477
+ console.log("Fetching positions for ", symbol);
65478
+ const positions = await this.syncAccount({
65479
+ symbol,
65480
+ as_view: true
65481
+ });
65482
+ console.log("Getting long and short positions for ", symbol);
65483
+ const long_position = positions.find((k) => k.kind === "long");
65484
+ const short_position = positions.find((k) => k.kind === "short");
65485
+ console.log("Getting focus position for ", symbol, kind);
65486
+ const focus_position = kind === "long" ? long_position : short_position;
65487
+ const reverse_position = kind === "long" ? short_position : long_position;
65488
+ let reverse_action = null;
65489
+ let reverse_orders_to_buy = [];
65490
+ let reverse_config = null;
65491
+ if (focus_position.avg_qty < last_value.avg_size) {
65492
+ console.log("Placing trade for ", symbol, kind);
65493
+ await this.placeTrade({
65494
+ symbol,
65495
+ kind,
65496
+ ignore_config: true,
65497
+ place: true
65498
+ });
65499
+ }
65500
+ console.log("Checking if focus position has quantity for ", symbol, kind);
65501
+ if (focus_position.quantity > 0) {
65502
+ console.log("Getting details for ", reverse_position.kind);
65503
+ reverse_action = await this.buildOppositeTrades({
65504
+ symbol,
65505
+ kind
65506
+ });
65507
+ console.log("Updating config for ", symbol, reverse_action.kind);
65508
+ await this.getPositionConfig({
65509
+ symbol,
65510
+ kind: reverse_action.kind,
65511
+ params: {
65512
+ entry: reverse_action.entry,
65513
+ stop: reverse_action.stop,
65514
+ risk: reverse_action.risk_per_trade,
65515
+ profit_percent: reverse_action.profit_percent,
65516
+ risk_reward: reverse_action.risk_reward
65517
+ }
65518
+ });
65519
+ console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
65520
+ const reverse_app_config = await this.buildAppConfig({
65521
+ entry: reverse_action.entry,
65522
+ stop: reverse_action.stop,
65523
+ risk_reward: reverse_action.risk_reward,
65524
+ risk: reverse_action.risk_per_trade,
65525
+ symbol
65526
+ });
65527
+ if (reverse_app_config.max_size != reverse_position.avg_qty) {
65528
+ reverse_orders_to_buy = await this.placeTrade({
65529
+ symbol,
65530
+ raw: true,
65531
+ kind: reverse_action.kind,
65532
+ ignore_config: true,
65533
+ place: false
65534
+ });
65535
+ let _reverse_config = {
65536
+ avg: reverse_action.avg,
65537
+ entry: reverse_action.entry,
65538
+ stop: reverse_action.stop,
65539
+ risk_per_trade: reverse_action.risk_per_trade,
65540
+ profit_percent: reverse_action.profit_percent,
65541
+ risk_reward: reverse_action.risk_reward
65542
+ };
65543
+ if (reverse_orders_to_buy.length > 0) {
65544
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
65545
+ let existing = await this.placeOppositeTradeAction({
65546
+ symbol,
65547
+ kind: reverse_action.kind,
65548
+ data: _reverse_config
65549
+ });
65550
+ _reverse_config = {
65551
+ ...existing,
65552
+ ..._reverse_config
65553
+ };
65554
+ }
65555
+ reverse_config = _reverse_config;
65556
+ }
65557
+ if (!reverse_config?.id) {
65558
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
65559
+ reverse_config = await this.getPositionConfig({
65560
+ symbol,
65561
+ kind: reverse_action.kind
65562
+ });
65563
+ }
65564
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
65565
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
65566
+ const max_size = app_config.max_size * 0.98;
65567
+ if (reverse_config.threshold_qty !== max_size) {
65568
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65569
+ follow: true,
65570
+ threshold_qty: max_size
65571
+ });
65572
+ }
65573
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
65574
+ } else {
65575
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65576
+ follow: false
65577
+ });
65578
+ }
65579
+ }
65580
+ return {
65581
+ reverse_config,
65582
+ reverse_action,
65583
+ reverse_orders_to_buy,
65584
+ positions: {
65585
+ long: long_position,
65586
+ short: short_position
65587
+ },
65588
+ orders_to_place,
65589
+ config_details: {
65590
+ app_config,
65591
+ last_value,
65592
+ config: config2,
65593
+ pnl
65594
+ }
65595
+ };
65596
+ }
65396
65597
  }
65397
65598
  function getExchangeKlass(exchange) {
65398
65599
  const func = exchange === "binance" ? BinanceExchange : BybitExchange;
@@ -65438,6 +65639,7 @@ async function getExchangeAccount(payload) {
65438
65639
  app_db
65439
65640
  });
65440
65641
  }
65642
+
65441
65643
  // src/app.ts
65442
65644
  class App {
65443
65645
  app_db;
@@ -65658,7 +65860,8 @@ class App {
65658
65860
  await exchange_account.placeTrade({
65659
65861
  symbol,
65660
65862
  kind: "long",
65661
- tp: true
65863
+ tp: true,
65864
+ limit: false
65662
65865
  });
65663
65866
  await new Promise((resolve) => setTimeout(resolve, 500));
65664
65867
  }
@@ -65705,6 +65908,21 @@ class App {
65705
65908
  });
65706
65909
  return result;
65707
65910
  }
65911
+ async runDbStrategyAccounts(callback) {
65912
+ const strategies = await this.app_db.getRunningAccountStrategies();
65913
+ for (const strategy of strategies) {
65914
+ console.log("Running strategy for ", strategy.symbol, "for account", strategy.expand.account.owner, strategy.expand.account.exchange);
65915
+ await callback({
65916
+ symbol: strategy.symbol,
65917
+ kind: strategy.kind,
65918
+ risk: strategy.risk,
65919
+ resistance: strategy.resistance,
65920
+ support: strategy.support,
65921
+ account: strategy.expand.account,
65922
+ reward_factor: strategy.reward_factor
65923
+ });
65924
+ }
65925
+ }
65708
65926
  async profitWithinGapStrategy(payload) {
65709
65927
  const {
65710
65928
  account,
@@ -65716,168 +65934,15 @@ class App {
65716
65934
  reward_factor = 1
65717
65935
  } = payload;
65718
65936
  const exchange_account = await this.getExchangeAccount(account);
65719
- console.log("Getting entry and stop for ", symbol, kind);
65720
- const entry = kind === "long" ? resistance : support;
65721
- const stop = kind === "long" ? support : resistance;
65722
- console.log("Building app config for ", symbol, kind);
65723
- const initial_app_config = await exchange_account.buildAppConfig({
65724
- entry,
65725
- stop,
65726
- risk_reward: 199,
65727
- risk,
65728
- symbol
65729
- });
65730
- console.log("Computing risk reward for ", symbol, kind);
65731
- const risk_reward = computeRiskReward({
65732
- app_config: initial_app_config,
65733
- entry: initial_app_config.entry,
65734
- stop: initial_app_config.stop,
65735
- risk_per_trade: initial_app_config.risk_per_trade
65736
- });
65737
- console.log("Re-computing app config for ", symbol, kind);
65738
- const { entries, last_value, ...app_config } = await exchange_account.buildAppConfig({
65739
- entry: initial_app_config.entry,
65740
- stop: initial_app_config.stop,
65741
- risk_reward,
65742
- risk,
65743
- symbol
65744
- });
65745
- console.log("Computing profit percent for ", symbol, kind);
65746
- const pnl = reward_factor * risk;
65747
- const profit_percent = to_f2(pnl * 100 / (last_value.avg_entry * last_value.avg_size), "%.4f");
65748
- let config2 = {
65749
- entry,
65750
- stop,
65751
- risk,
65752
- risk_reward,
65753
- profit_percent
65754
- };
65755
- console.log("Saving new config for ", symbol, kind);
65756
- const data = await exchange_account.getPositionConfig({
65937
+ const result = await exchange_account.profitWithinGapStrategy({
65757
65938
  symbol,
65758
65939
  kind,
65759
- params: config2
65760
- });
65761
- console.log("Checking orders to place for ", symbol, kind);
65762
- const orders_to_place = await exchange_account.placeTrade({
65763
- symbol,
65764
- raw: true,
65765
- kind,
65766
- place: false
65767
- });
65768
- config2 = {
65769
- ...config2,
65770
- ...data
65771
- };
65772
- console.log("Fetching positions for ", symbol);
65773
- const positions = await exchange_account.syncAccount({
65774
- symbol,
65775
- as_view: true
65940
+ risk,
65941
+ resistance,
65942
+ support,
65943
+ reward_factor
65776
65944
  });
65777
- console.log("Getting long and short positions for ", symbol);
65778
- const long_position = positions.find((k) => k.kind === "long");
65779
- const short_position = positions.find((k) => k.kind === "short");
65780
- console.log("Getting focus position for ", symbol, kind);
65781
- const focus_position = kind === "long" ? long_position : short_position;
65782
- const reverse_position = kind === "long" ? short_position : long_position;
65783
- let reverse_action = null;
65784
- let reverse_orders_to_buy = [];
65785
- let reverse_config = null;
65786
- console.log("Checking if focus position has quantity for ", symbol, kind);
65787
- if (focus_position.quantity > 0) {
65788
- console.log("Getting details for ", reverse_position.kind);
65789
- reverse_action = await exchange_account.buildOppositeTrades({
65790
- symbol,
65791
- kind
65792
- });
65793
- console.log("Updating config for ", symbol, reverse_action.kind);
65794
- await exchange_account.getPositionConfig({
65795
- symbol,
65796
- kind: reverse_action.kind,
65797
- params: {
65798
- entry: reverse_action.entry,
65799
- stop: reverse_action.stop,
65800
- risk: reverse_action.risk_per_trade,
65801
- profit_percent: reverse_action.profit_percent,
65802
- risk_reward: reverse_action.risk_reward
65803
- }
65804
- });
65805
- console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
65806
- const reverse_app_config = await exchange_account.buildAppConfig({
65807
- entry: reverse_action.entry,
65808
- stop: reverse_action.stop,
65809
- risk_reward: reverse_action.risk_reward,
65810
- risk: reverse_action.risk_per_trade,
65811
- symbol
65812
- });
65813
- if (reverse_app_config.max_size != reverse_position.avg_qty) {
65814
- reverse_orders_to_buy = await exchange_account.placeTrade({
65815
- symbol,
65816
- raw: true,
65817
- kind: reverse_action.kind,
65818
- place: false
65819
- });
65820
- let _reverse_config = {
65821
- avg: reverse_action.avg,
65822
- entry: reverse_action.entry,
65823
- stop: reverse_action.stop,
65824
- risk_per_trade: reverse_action.risk_per_trade,
65825
- profit_percent: reverse_action.profit_percent,
65826
- risk_reward: reverse_action.risk_reward
65827
- };
65828
- if (reverse_orders_to_buy.length > 0) {
65829
- console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
65830
- let existing = await exchange_account.placeOppositeTradeAction({
65831
- symbol,
65832
- kind: reverse_action.kind,
65833
- data: _reverse_config
65834
- });
65835
- _reverse_config = {
65836
- ...existing,
65837
- ..._reverse_config
65838
- };
65839
- }
65840
- reverse_config = _reverse_config;
65841
- }
65842
- if (!reverse_config?.id) {
65843
- console.log("fetching reverse config for ", symbol, reverse_action.kind);
65844
- reverse_config = await exchange_account.getPositionConfig({
65845
- symbol,
65846
- kind: reverse_action.kind
65847
- });
65848
- }
65849
- if (reverse_position.quantity > 0 && reverse_config?.id) {
65850
- console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
65851
- const max_size = app_config.max_size * 0.98;
65852
- if (reverse_config.threshold_qty !== max_size) {
65853
- await this.app_db.updateScheduledTrade(reverse_config.id, {
65854
- follow: true,
65855
- threshold_qty: max_size
65856
- });
65857
- }
65858
- console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
65859
- } else {
65860
- await this.app_db.updateScheduledTrade(reverse_config.id, {
65861
- follow: false
65862
- });
65863
- }
65864
- }
65865
- return {
65866
- reverse_config,
65867
- reverse_action,
65868
- reverse_orders_to_buy,
65869
- positions: {
65870
- long: long_position,
65871
- short: short_position
65872
- },
65873
- orders_to_place,
65874
- config_details: {
65875
- app_config,
65876
- last_value,
65877
- config: config2,
65878
- pnl
65879
- }
65880
- };
65945
+ return result;
65881
65946
  }
65882
65947
  }
65883
65948
  async function initApp(payload) {
@@ -66010,18 +66075,6 @@ async function getPositions(payload) {
66010
66075
  ...payload,
66011
66076
  refresh: true
66012
66077
  });
66013
- const symbol_config = await exchange_account.recomputeSymbolConfig({
66014
- symbol: payload.symbol
66015
- });
66016
- const strategy2 = await exchange_account.getPositionStrategy();
66017
- const strategy_config = {
66018
- tp_percent: strategy2?.tp_percent,
66019
- short_tp_factor: strategy2?.short_tp_factor,
66020
- fee_percent: strategy2?.fee_percent,
66021
- budget: strategy2?.budget,
66022
- risk_reward: strategy2?.risk_reward,
66023
- global_config: symbol_config
66024
- };
66025
66078
  const positions = await exchange_account.syncAccount({
66026
66079
  symbol: payload.symbol,
66027
66080
  as_view: true
@@ -66040,12 +66093,11 @@ async function getPositions(payload) {
66040
66093
  long: long_position,
66041
66094
  short: short_position
66042
66095
  },
66043
- runs: runs2,
66044
- strategy_config
66096
+ runs: runs2
66045
66097
  };
66046
66098
  }
66047
66099
  async function fetchExchangeDetails(payload) {
66048
- const { strategy_config, ...positions } = await getPositions({
66100
+ const { ...positions } = await getPositions({
66049
66101
  symbol: payload.symbol,
66050
66102
  account: payload.account,
66051
66103
  kind: payload.kind
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-83",
4
+ "version": "0.0.2-89",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",