@gbozee/ultimate 0.0.2-83 → 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.
@@ -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,187 @@ 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
+ console.log("Checking if focus position has quantity for ", symbol, kind);
65492
+ if (focus_position.quantity > 0) {
65493
+ console.log("Getting details for ", reverse_position.kind);
65494
+ reverse_action = await this.buildOppositeTrades({
65495
+ symbol,
65496
+ kind
65497
+ });
65498
+ console.log("Updating config for ", symbol, reverse_action.kind);
65499
+ await this.getPositionConfig({
65500
+ symbol,
65501
+ kind: reverse_action.kind,
65502
+ params: {
65503
+ entry: reverse_action.entry,
65504
+ stop: reverse_action.stop,
65505
+ risk: reverse_action.risk_per_trade,
65506
+ profit_percent: reverse_action.profit_percent,
65507
+ risk_reward: reverse_action.risk_reward
65508
+ }
65509
+ });
65510
+ console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
65511
+ const reverse_app_config = await this.buildAppConfig({
65512
+ entry: reverse_action.entry,
65513
+ stop: reverse_action.stop,
65514
+ risk_reward: reverse_action.risk_reward,
65515
+ risk: reverse_action.risk_per_trade,
65516
+ symbol
65517
+ });
65518
+ if (reverse_app_config.max_size != reverse_position.avg_qty) {
65519
+ reverse_orders_to_buy = await this.placeTrade({
65520
+ symbol,
65521
+ raw: true,
65522
+ kind: reverse_action.kind,
65523
+ ignore_config: true,
65524
+ place: false
65525
+ });
65526
+ let _reverse_config = {
65527
+ avg: reverse_action.avg,
65528
+ entry: reverse_action.entry,
65529
+ stop: reverse_action.stop,
65530
+ risk_per_trade: reverse_action.risk_per_trade,
65531
+ profit_percent: reverse_action.profit_percent,
65532
+ risk_reward: reverse_action.risk_reward
65533
+ };
65534
+ if (reverse_orders_to_buy.length > 0) {
65535
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
65536
+ let existing = await this.placeOppositeTradeAction({
65537
+ symbol,
65538
+ kind: reverse_action.kind,
65539
+ data: _reverse_config
65540
+ });
65541
+ _reverse_config = {
65542
+ ...existing,
65543
+ ..._reverse_config
65544
+ };
65545
+ }
65546
+ reverse_config = _reverse_config;
65547
+ }
65548
+ if (!reverse_config?.id) {
65549
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
65550
+ reverse_config = await this.getPositionConfig({
65551
+ symbol,
65552
+ kind: reverse_action.kind
65553
+ });
65554
+ }
65555
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
65556
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
65557
+ const max_size = app_config.max_size * 0.98;
65558
+ if (reverse_config.threshold_qty !== max_size) {
65559
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65560
+ follow: true,
65561
+ threshold_qty: max_size
65562
+ });
65563
+ }
65564
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
65565
+ } else {
65566
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65567
+ follow: false
65568
+ });
65569
+ }
65570
+ }
65571
+ return {
65572
+ reverse_config,
65573
+ reverse_action,
65574
+ reverse_orders_to_buy,
65575
+ positions: {
65576
+ long: long_position,
65577
+ short: short_position
65578
+ },
65579
+ orders_to_place,
65580
+ config_details: {
65581
+ app_config,
65582
+ last_value,
65583
+ config: config2,
65584
+ pnl
65585
+ }
65586
+ };
65587
+ }
65396
65588
  }
65397
65589
  function getExchangeKlass(exchange) {
65398
65590
  const func = exchange === "binance" ? BinanceExchange : BybitExchange;
@@ -65438,6 +65630,7 @@ async function getExchangeAccount(payload) {
65438
65630
  app_db
65439
65631
  });
65440
65632
  }
65633
+
65441
65634
  // src/app.ts
65442
65635
  class App {
65443
65636
  app_db;
@@ -65658,7 +65851,8 @@ class App {
65658
65851
  await exchange_account.placeTrade({
65659
65852
  symbol,
65660
65853
  kind: "long",
65661
- tp: true
65854
+ tp: true,
65855
+ limit: false
65662
65856
  });
65663
65857
  await new Promise((resolve) => setTimeout(resolve, 500));
65664
65858
  }
@@ -65705,6 +65899,20 @@ class App {
65705
65899
  });
65706
65900
  return result;
65707
65901
  }
65902
+ async runDbStrategyAccounts() {
65903
+ const strategies = await this.app_db.getRunningAccountStrategies();
65904
+ for (const strategy of strategies) {
65905
+ console.log("Running strategy for ", strategy.symbol, "for account", strategy.expand.account.owner, strategy.expand.account.exchange);
65906
+ await this.profitWithinGapStrategy({
65907
+ symbol: strategy.symbol,
65908
+ kind: strategy.kind,
65909
+ risk: strategy.risk,
65910
+ resistance: strategy.resistance,
65911
+ support: strategy.support,
65912
+ account: strategy.expand.account
65913
+ });
65914
+ }
65915
+ }
65708
65916
  async profitWithinGapStrategy(payload) {
65709
65917
  const {
65710
65918
  account,
@@ -65716,168 +65924,15 @@ class App {
65716
65924
  reward_factor = 1
65717
65925
  } = payload;
65718
65926
  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({
65927
+ const result = await exchange_account.profitWithinGapStrategy({
65757
65928
  symbol,
65758
65929
  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
65930
+ risk,
65931
+ resistance,
65932
+ support,
65933
+ reward_factor
65776
65934
  });
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
- };
65935
+ return result;
65881
65936
  }
65882
65937
  }
65883
65938
  async function initApp(payload) {
@@ -66010,18 +66065,6 @@ async function getPositions(payload) {
66010
66065
  ...payload,
66011
66066
  refresh: true
66012
66067
  });
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
66068
  const positions = await exchange_account.syncAccount({
66026
66069
  symbol: payload.symbol,
66027
66070
  as_view: true
@@ -66040,12 +66083,11 @@ async function getPositions(payload) {
66040
66083
  long: long_position,
66041
66084
  short: short_position
66042
66085
  },
66043
- runs: runs2,
66044
- strategy_config
66086
+ runs: runs2
66045
66087
  };
66046
66088
  }
66047
66089
  async function fetchExchangeDetails(payload) {
66048
- const { strategy_config, ...positions } = await getPositions({
66090
+ const { ...positions } = await getPositions({
66049
66091
  symbol: payload.symbol,
66050
66092
  account: payload.account,
66051
66093
  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-87",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",