@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.
package/dist/index.js CHANGED
@@ -51899,7 +51899,7 @@ class AppDatabase {
51899
51899
  profit: payload.profit !== undefined ? payload.profit : config2.profit
51900
51900
  });
51901
51901
  await this.update_db_position(db_position, {
51902
- config: config2.id
51902
+ config: null
51903
51903
  });
51904
51904
  for (const _config of configs) {
51905
51905
  if (_config.id !== config2.id) {
@@ -51921,7 +51921,7 @@ class AppDatabase {
51921
51921
  });
51922
51922
  }
51923
51923
  await this.pb.collection("positions").update(db_position.id, {
51924
- config: config2?.id
51924
+ config: null
51925
51925
  });
51926
51926
  return config2;
51927
51927
  }
@@ -51935,9 +51935,17 @@ class AppDatabase {
51935
51935
  }
51936
51936
  return null;
51937
51937
  }
51938
- async getPositionStrategy(account) {
51938
+ async getRunningAccountStrategies() {
51939
+ const result = await this.pb.collection("account_strategies").getFullList({
51940
+ filter: `running=true`,
51941
+ expand: "account"
51942
+ });
51943
+ return result;
51944
+ }
51945
+ async getAccountStrategy(payload) {
51946
+ const { symbol, account } = payload;
51939
51947
  const _strategy_instance = await this.pb.collection("account_strategies").getFullList({
51940
- filter: `account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
51948
+ filter: `symbol:lower="${symbol.toLowerCase()}" && account.owner:lower="${account.owner.toLowerCase()}" && account.exchange:lower="${account.exchange.toLowerCase()}"`,
51941
51949
  expand: "account"
51942
51950
  });
51943
51951
  if (_strategy_instance.length > 0) {
@@ -57353,8 +57361,11 @@ class ExchangeAccount {
57353
57361
  async getCurrentPrice(symbol) {
57354
57362
  return await this.exchange.get_current_price(symbol);
57355
57363
  }
57356
- async getPositionStrategy() {
57357
- return await this.app_db.getPositionStrategy(this.instance);
57364
+ async getAccountStrategy(payload) {
57365
+ return await this.app_db.getAccountStrategy({
57366
+ symbol: payload.symbol,
57367
+ account: this.instance
57368
+ });
57358
57369
  }
57359
57370
  async buildReduceConfig(payload) {
57360
57371
  const positions = await this.syncAccount({
@@ -58680,6 +58691,196 @@ class ExchangeAccount {
58680
58691
  });
58681
58692
  }
58682
58693
  }
58694
+ async profitWithinGapStrategy(payload) {
58695
+ const {
58696
+ symbol,
58697
+ kind,
58698
+ risk,
58699
+ resistance,
58700
+ support,
58701
+ reward_factor = 1
58702
+ } = payload;
58703
+ console.log("Getting entry and stop for ", symbol, kind);
58704
+ const entry = kind === "long" ? resistance : support;
58705
+ const stop = kind === "long" ? support : resistance;
58706
+ console.log("Building app config for ", symbol, kind);
58707
+ const initial_app_config = await this.buildAppConfig({
58708
+ entry,
58709
+ stop,
58710
+ risk_reward: 199,
58711
+ risk,
58712
+ symbol
58713
+ });
58714
+ console.log("Computing risk reward for ", symbol, kind);
58715
+ const risk_reward = computeRiskReward({
58716
+ app_config: initial_app_config,
58717
+ entry: initial_app_config.entry,
58718
+ stop: initial_app_config.stop,
58719
+ risk_per_trade: initial_app_config.risk_per_trade
58720
+ });
58721
+ console.log("Re-computing app config for ", symbol, kind);
58722
+ const { entries, last_value, ...app_config } = await this.buildAppConfig({
58723
+ entry: initial_app_config.entry,
58724
+ stop: initial_app_config.stop,
58725
+ risk_reward,
58726
+ risk,
58727
+ symbol
58728
+ });
58729
+ console.log("Computing profit percent for ", symbol, kind);
58730
+ const pnl = reward_factor * risk;
58731
+ const profit_percent = to_f2(pnl * 100 / (last_value.avg_entry * last_value.avg_size), "%.4f");
58732
+ let config2 = {
58733
+ entry,
58734
+ stop,
58735
+ risk,
58736
+ risk_reward,
58737
+ profit_percent
58738
+ };
58739
+ console.log("Saving new config for ", symbol, kind);
58740
+ const data = await this.getPositionConfig({
58741
+ symbol,
58742
+ kind,
58743
+ params: config2
58744
+ });
58745
+ console.log("Checking orders to place for ", symbol, kind);
58746
+ const orders_to_place = await this.placeTrade({
58747
+ symbol,
58748
+ raw: true,
58749
+ kind,
58750
+ place: false,
58751
+ ignore_config: true
58752
+ });
58753
+ await this.placeTrade({
58754
+ symbol,
58755
+ kind,
58756
+ limit: false,
58757
+ ignore_config: true,
58758
+ tp: true
58759
+ });
58760
+ config2 = {
58761
+ ...config2,
58762
+ ...data
58763
+ };
58764
+ console.log("Fetching positions for ", symbol);
58765
+ const positions = await this.syncAccount({
58766
+ symbol,
58767
+ as_view: true
58768
+ });
58769
+ console.log("Getting long and short positions for ", symbol);
58770
+ const long_position = positions.find((k) => k.kind === "long");
58771
+ const short_position = positions.find((k) => k.kind === "short");
58772
+ console.log("Getting focus position for ", symbol, kind);
58773
+ const focus_position = kind === "long" ? long_position : short_position;
58774
+ const reverse_position = kind === "long" ? short_position : long_position;
58775
+ let reverse_action = null;
58776
+ let reverse_orders_to_buy = [];
58777
+ let reverse_config = null;
58778
+ if (focus_position.avg_qty < last_value.avg_size) {
58779
+ console.log("Placing trade for ", symbol, kind);
58780
+ await this.placeTrade({
58781
+ symbol,
58782
+ kind,
58783
+ ignore_config: true,
58784
+ place: true
58785
+ });
58786
+ }
58787
+ console.log("Checking if focus position has quantity for ", symbol, kind);
58788
+ if (focus_position.quantity > 0) {
58789
+ console.log("Getting details for ", reverse_position.kind);
58790
+ reverse_action = await this.buildOppositeTrades({
58791
+ symbol,
58792
+ kind
58793
+ });
58794
+ console.log("Updating config for ", symbol, reverse_action.kind);
58795
+ await this.getPositionConfig({
58796
+ symbol,
58797
+ kind: reverse_action.kind,
58798
+ params: {
58799
+ entry: reverse_action.entry,
58800
+ stop: reverse_action.stop,
58801
+ risk: reverse_action.risk_per_trade,
58802
+ profit_percent: reverse_action.profit_percent,
58803
+ risk_reward: reverse_action.risk_reward
58804
+ }
58805
+ });
58806
+ console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
58807
+ const reverse_app_config = await this.buildAppConfig({
58808
+ entry: reverse_action.entry,
58809
+ stop: reverse_action.stop,
58810
+ risk_reward: reverse_action.risk_reward,
58811
+ risk: reverse_action.risk_per_trade,
58812
+ symbol
58813
+ });
58814
+ if (reverse_app_config.max_size != reverse_position.avg_qty) {
58815
+ reverse_orders_to_buy = await this.placeTrade({
58816
+ symbol,
58817
+ raw: true,
58818
+ kind: reverse_action.kind,
58819
+ ignore_config: true,
58820
+ place: false
58821
+ });
58822
+ let _reverse_config = {
58823
+ avg: reverse_action.avg,
58824
+ entry: reverse_action.entry,
58825
+ stop: reverse_action.stop,
58826
+ risk_per_trade: reverse_action.risk_per_trade,
58827
+ profit_percent: reverse_action.profit_percent,
58828
+ risk_reward: reverse_action.risk_reward
58829
+ };
58830
+ if (reverse_orders_to_buy.length > 0) {
58831
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
58832
+ let existing = await this.placeOppositeTradeAction({
58833
+ symbol,
58834
+ kind: reverse_action.kind,
58835
+ data: _reverse_config
58836
+ });
58837
+ _reverse_config = {
58838
+ ...existing,
58839
+ ..._reverse_config
58840
+ };
58841
+ }
58842
+ reverse_config = _reverse_config;
58843
+ }
58844
+ if (!reverse_config?.id) {
58845
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
58846
+ reverse_config = await this.getPositionConfig({
58847
+ symbol,
58848
+ kind: reverse_action.kind
58849
+ });
58850
+ }
58851
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
58852
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
58853
+ const max_size = app_config.max_size * 0.98;
58854
+ if (reverse_config.threshold_qty !== max_size) {
58855
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
58856
+ follow: true,
58857
+ threshold_qty: max_size
58858
+ });
58859
+ }
58860
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
58861
+ } else {
58862
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
58863
+ follow: false
58864
+ });
58865
+ }
58866
+ }
58867
+ return {
58868
+ reverse_config,
58869
+ reverse_action,
58870
+ reverse_orders_to_buy,
58871
+ positions: {
58872
+ long: long_position,
58873
+ short: short_position
58874
+ },
58875
+ orders_to_place,
58876
+ config_details: {
58877
+ app_config,
58878
+ last_value,
58879
+ config: config2,
58880
+ pnl
58881
+ }
58882
+ };
58883
+ }
58683
58884
  }
58684
58885
  function getExchangeKlass(exchange) {
58685
58886
  const func = exchange === "binance" ? BinanceExchange : BybitExchange;
@@ -58725,6 +58926,7 @@ async function getExchangeAccount(payload) {
58725
58926
  app_db
58726
58927
  });
58727
58928
  }
58929
+
58728
58930
  // src/app.ts
58729
58931
  class App {
58730
58932
  app_db;
@@ -58945,7 +59147,8 @@ class App {
58945
59147
  await exchange_account.placeTrade({
58946
59148
  symbol,
58947
59149
  kind: "long",
58948
- tp: true
59150
+ tp: true,
59151
+ limit: false
58949
59152
  });
58950
59153
  await new Promise((resolve) => setTimeout(resolve, 500));
58951
59154
  }
@@ -58992,6 +59195,21 @@ class App {
58992
59195
  });
58993
59196
  return result;
58994
59197
  }
59198
+ async runDbStrategyAccounts(callback) {
59199
+ const strategies = await this.app_db.getRunningAccountStrategies();
59200
+ for (const strategy of strategies) {
59201
+ console.log("Running strategy for ", strategy.symbol, "for account", strategy.expand.account.owner, strategy.expand.account.exchange);
59202
+ await callback({
59203
+ symbol: strategy.symbol,
59204
+ kind: strategy.kind,
59205
+ risk: strategy.risk,
59206
+ resistance: strategy.resistance,
59207
+ support: strategy.support,
59208
+ account: strategy.expand.account,
59209
+ reward_factor: strategy.reward_factor
59210
+ });
59211
+ }
59212
+ }
58995
59213
  async profitWithinGapStrategy(payload) {
58996
59214
  const {
58997
59215
  account,
@@ -59003,168 +59221,15 @@ class App {
59003
59221
  reward_factor = 1
59004
59222
  } = payload;
59005
59223
  const exchange_account = await this.getExchangeAccount(account);
59006
- console.log("Getting entry and stop for ", symbol, kind);
59007
- const entry = kind === "long" ? resistance : support;
59008
- const stop = kind === "long" ? support : resistance;
59009
- console.log("Building app config for ", symbol, kind);
59010
- const initial_app_config = await exchange_account.buildAppConfig({
59011
- entry,
59012
- stop,
59013
- risk_reward: 199,
59014
- risk,
59015
- symbol
59016
- });
59017
- console.log("Computing risk reward for ", symbol, kind);
59018
- const risk_reward = computeRiskReward({
59019
- app_config: initial_app_config,
59020
- entry: initial_app_config.entry,
59021
- stop: initial_app_config.stop,
59022
- risk_per_trade: initial_app_config.risk_per_trade
59023
- });
59024
- console.log("Re-computing app config for ", symbol, kind);
59025
- const { entries, last_value, ...app_config } = await exchange_account.buildAppConfig({
59026
- entry: initial_app_config.entry,
59027
- stop: initial_app_config.stop,
59028
- risk_reward,
59029
- risk,
59030
- symbol
59031
- });
59032
- console.log("Computing profit percent for ", symbol, kind);
59033
- const pnl = reward_factor * risk;
59034
- const profit_percent = to_f2(pnl * 100 / (last_value.avg_entry * last_value.avg_size), "%.4f");
59035
- let config2 = {
59036
- entry,
59037
- stop,
59038
- risk,
59039
- risk_reward,
59040
- profit_percent
59041
- };
59042
- console.log("Saving new config for ", symbol, kind);
59043
- const data = await exchange_account.getPositionConfig({
59224
+ const result = await exchange_account.profitWithinGapStrategy({
59044
59225
  symbol,
59045
59226
  kind,
59046
- params: config2
59047
- });
59048
- console.log("Checking orders to place for ", symbol, kind);
59049
- const orders_to_place = await exchange_account.placeTrade({
59050
- symbol,
59051
- raw: true,
59052
- kind,
59053
- place: false
59054
- });
59055
- config2 = {
59056
- ...config2,
59057
- ...data
59058
- };
59059
- console.log("Fetching positions for ", symbol);
59060
- const positions = await exchange_account.syncAccount({
59061
- symbol,
59062
- as_view: true
59227
+ risk,
59228
+ resistance,
59229
+ support,
59230
+ reward_factor
59063
59231
  });
59064
- console.log("Getting long and short positions for ", symbol);
59065
- const long_position = positions.find((k) => k.kind === "long");
59066
- const short_position = positions.find((k) => k.kind === "short");
59067
- console.log("Getting focus position for ", symbol, kind);
59068
- const focus_position = kind === "long" ? long_position : short_position;
59069
- const reverse_position = kind === "long" ? short_position : long_position;
59070
- let reverse_action = null;
59071
- let reverse_orders_to_buy = [];
59072
- let reverse_config = null;
59073
- console.log("Checking if focus position has quantity for ", symbol, kind);
59074
- if (focus_position.quantity > 0) {
59075
- console.log("Getting details for ", reverse_position.kind);
59076
- reverse_action = await exchange_account.buildOppositeTrades({
59077
- symbol,
59078
- kind
59079
- });
59080
- console.log("Updating config for ", symbol, reverse_action.kind);
59081
- await exchange_account.getPositionConfig({
59082
- symbol,
59083
- kind: reverse_action.kind,
59084
- params: {
59085
- entry: reverse_action.entry,
59086
- stop: reverse_action.stop,
59087
- risk: reverse_action.risk_per_trade,
59088
- profit_percent: reverse_action.profit_percent,
59089
- risk_reward: reverse_action.risk_reward
59090
- }
59091
- });
59092
- console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
59093
- const reverse_app_config = await exchange_account.buildAppConfig({
59094
- entry: reverse_action.entry,
59095
- stop: reverse_action.stop,
59096
- risk_reward: reverse_action.risk_reward,
59097
- risk: reverse_action.risk_per_trade,
59098
- symbol
59099
- });
59100
- if (reverse_app_config.max_size != reverse_position.avg_qty) {
59101
- reverse_orders_to_buy = await exchange_account.placeTrade({
59102
- symbol,
59103
- raw: true,
59104
- kind: reverse_action.kind,
59105
- place: false
59106
- });
59107
- let _reverse_config = {
59108
- avg: reverse_action.avg,
59109
- entry: reverse_action.entry,
59110
- stop: reverse_action.stop,
59111
- risk_per_trade: reverse_action.risk_per_trade,
59112
- profit_percent: reverse_action.profit_percent,
59113
- risk_reward: reverse_action.risk_reward
59114
- };
59115
- if (reverse_orders_to_buy.length > 0) {
59116
- console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
59117
- let existing = await exchange_account.placeOppositeTradeAction({
59118
- symbol,
59119
- kind: reverse_action.kind,
59120
- data: _reverse_config
59121
- });
59122
- _reverse_config = {
59123
- ...existing,
59124
- ..._reverse_config
59125
- };
59126
- }
59127
- reverse_config = _reverse_config;
59128
- }
59129
- if (!reverse_config?.id) {
59130
- console.log("fetching reverse config for ", symbol, reverse_action.kind);
59131
- reverse_config = await exchange_account.getPositionConfig({
59132
- symbol,
59133
- kind: reverse_action.kind
59134
- });
59135
- }
59136
- if (reverse_position.quantity > 0 && reverse_config?.id) {
59137
- console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
59138
- const max_size = app_config.max_size * 0.98;
59139
- if (reverse_config.threshold_qty !== max_size) {
59140
- await this.app_db.updateScheduledTrade(reverse_config.id, {
59141
- follow: true,
59142
- threshold_qty: max_size
59143
- });
59144
- }
59145
- console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
59146
- } else {
59147
- await this.app_db.updateScheduledTrade(reverse_config.id, {
59148
- follow: false
59149
- });
59150
- }
59151
- }
59152
- return {
59153
- reverse_config,
59154
- reverse_action,
59155
- reverse_orders_to_buy,
59156
- positions: {
59157
- long: long_position,
59158
- short: short_position
59159
- },
59160
- orders_to_place,
59161
- config_details: {
59162
- app_config,
59163
- last_value,
59164
- config: config2,
59165
- pnl
59166
- }
59167
- };
59232
+ return result;
59168
59233
  }
59169
59234
  }
59170
59235
  async function initApp(payload) {