@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.
package/dist/index.js CHANGED
@@ -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,187 @@ 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
+ console.log("Checking if focus position has quantity for ", symbol, kind);
58779
+ if (focus_position.quantity > 0) {
58780
+ console.log("Getting details for ", reverse_position.kind);
58781
+ reverse_action = await this.buildOppositeTrades({
58782
+ symbol,
58783
+ kind
58784
+ });
58785
+ console.log("Updating config for ", symbol, reverse_action.kind);
58786
+ await this.getPositionConfig({
58787
+ symbol,
58788
+ kind: reverse_action.kind,
58789
+ params: {
58790
+ entry: reverse_action.entry,
58791
+ stop: reverse_action.stop,
58792
+ risk: reverse_action.risk_per_trade,
58793
+ profit_percent: reverse_action.profit_percent,
58794
+ risk_reward: reverse_action.risk_reward
58795
+ }
58796
+ });
58797
+ console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
58798
+ const reverse_app_config = await this.buildAppConfig({
58799
+ entry: reverse_action.entry,
58800
+ stop: reverse_action.stop,
58801
+ risk_reward: reverse_action.risk_reward,
58802
+ risk: reverse_action.risk_per_trade,
58803
+ symbol
58804
+ });
58805
+ if (reverse_app_config.max_size != reverse_position.avg_qty) {
58806
+ reverse_orders_to_buy = await this.placeTrade({
58807
+ symbol,
58808
+ raw: true,
58809
+ kind: reverse_action.kind,
58810
+ ignore_config: true,
58811
+ place: false
58812
+ });
58813
+ let _reverse_config = {
58814
+ avg: reverse_action.avg,
58815
+ entry: reverse_action.entry,
58816
+ stop: reverse_action.stop,
58817
+ risk_per_trade: reverse_action.risk_per_trade,
58818
+ profit_percent: reverse_action.profit_percent,
58819
+ risk_reward: reverse_action.risk_reward
58820
+ };
58821
+ if (reverse_orders_to_buy.length > 0) {
58822
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
58823
+ let existing = await this.placeOppositeTradeAction({
58824
+ symbol,
58825
+ kind: reverse_action.kind,
58826
+ data: _reverse_config
58827
+ });
58828
+ _reverse_config = {
58829
+ ...existing,
58830
+ ..._reverse_config
58831
+ };
58832
+ }
58833
+ reverse_config = _reverse_config;
58834
+ }
58835
+ if (!reverse_config?.id) {
58836
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
58837
+ reverse_config = await this.getPositionConfig({
58838
+ symbol,
58839
+ kind: reverse_action.kind
58840
+ });
58841
+ }
58842
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
58843
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
58844
+ const max_size = app_config.max_size * 0.98;
58845
+ if (reverse_config.threshold_qty !== max_size) {
58846
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
58847
+ follow: true,
58848
+ threshold_qty: max_size
58849
+ });
58850
+ }
58851
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
58852
+ } else {
58853
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
58854
+ follow: false
58855
+ });
58856
+ }
58857
+ }
58858
+ return {
58859
+ reverse_config,
58860
+ reverse_action,
58861
+ reverse_orders_to_buy,
58862
+ positions: {
58863
+ long: long_position,
58864
+ short: short_position
58865
+ },
58866
+ orders_to_place,
58867
+ config_details: {
58868
+ app_config,
58869
+ last_value,
58870
+ config: config2,
58871
+ pnl
58872
+ }
58873
+ };
58874
+ }
58683
58875
  }
58684
58876
  function getExchangeKlass(exchange) {
58685
58877
  const func = exchange === "binance" ? BinanceExchange : BybitExchange;
@@ -58725,6 +58917,7 @@ async function getExchangeAccount(payload) {
58725
58917
  app_db
58726
58918
  });
58727
58919
  }
58920
+
58728
58921
  // src/app.ts
58729
58922
  class App {
58730
58923
  app_db;
@@ -58945,7 +59138,9 @@ class App {
58945
59138
  await exchange_account.placeTrade({
58946
59139
  symbol,
58947
59140
  kind: "long",
58948
- tp: true
59141
+ tp: true,
59142
+ limit: false,
59143
+ place: true
58949
59144
  });
58950
59145
  await new Promise((resolve) => setTimeout(resolve, 500));
58951
59146
  }
@@ -58992,6 +59187,20 @@ class App {
58992
59187
  });
58993
59188
  return result;
58994
59189
  }
59190
+ async runDbStrategyAccounts() {
59191
+ const strategies = await this.app_db.getRunningAccountStrategies();
59192
+ for (const strategy of strategies) {
59193
+ console.log("Running strategy for ", strategy.symbol, "for account", strategy.expand.account.owner, strategy.expand.account.exchange);
59194
+ await this.profitWithinGapStrategy({
59195
+ symbol: strategy.symbol,
59196
+ kind: strategy.kind,
59197
+ risk: strategy.risk,
59198
+ resistance: strategy.resistance,
59199
+ support: strategy.support,
59200
+ account: strategy.expand.account
59201
+ });
59202
+ }
59203
+ }
58995
59204
  async profitWithinGapStrategy(payload) {
58996
59205
  const {
58997
59206
  account,
@@ -59003,168 +59212,15 @@ class App {
59003
59212
  reward_factor = 1
59004
59213
  } = payload;
59005
59214
  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({
59215
+ const result = await exchange_account.profitWithinGapStrategy({
59044
59216
  symbol,
59045
59217
  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
59218
+ risk,
59219
+ resistance,
59220
+ support,
59221
+ reward_factor
59063
59222
  });
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
- };
59223
+ return result;
59168
59224
  }
59169
59225
  }
59170
59226
  async function initApp(payload) {