@gbozee/ultimate 0.0.2-79 → 0.0.2-82

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.
@@ -610,7 +610,7 @@ class Signal {
610
610
  new_stop
611
611
  };
612
612
  });
613
- if (greater_than_min_size.length !== less_than_min_size.length) {
613
+ if (greater_than_min_size.length !== total_orders.length) {
614
614
  payload = greater_than_min_size.concat(less_than_min_size);
615
615
  }
616
616
  return payload;
package/dist/index.cjs CHANGED
@@ -53114,7 +53114,7 @@ class Signal {
53114
53114
  new_stop
53115
53115
  };
53116
53116
  });
53117
- if (greater_than_min_size.length !== less_than_min_size.length) {
53117
+ if (greater_than_min_size.length !== total_orders.length) {
53118
53118
  payload = greater_than_min_size.concat(less_than_min_size);
53119
53119
  }
53120
53120
  return payload;
@@ -58770,7 +58770,6 @@ async function getExchangeAccount(payload) {
58770
58770
  app_db
58771
58771
  });
58772
58772
  }
58773
-
58774
58773
  // src/app.ts
58775
58774
  class App {
58776
58775
  app_db;
@@ -58829,37 +58828,6 @@ class App {
58829
58828
  stop: payload.stop
58830
58829
  });
58831
58830
  }
58832
- async updateReduceRatio(payload) {
58833
- const { symbol } = payload;
58834
- const exchange_account = await this.getExchangeAccount(payload.account);
58835
- const positions = await exchange_account.syncAccount({
58836
- as_view: true,
58837
- symbol
58838
- });
58839
- const active_account = await exchange_account.getActiveAccount({
58840
- symbol
58841
- });
58842
- const long_position = active_account.position.long;
58843
- const short_position = active_account.position.short;
58844
- const long_db_position = positions.find((p) => p.kind === "long");
58845
- const short_db_position = positions.find((p) => p.kind === "short");
58846
- const balance = active_account.usd_balance || 0;
58847
- const long_liquidation = long_db_position.avg_price * long_db_position.avg_qty / long_db_position.leverage - balance / long_db_position.avg_qty + long_db_position.avg_price;
58848
- const short_liquidation = -(short_db_position.avg_price * short_db_position.avg_qty / short_db_position.leverage) + balance / short_db_position.avg_qty + short_db_position.avg_price;
58849
- long_position.avg_liquidation = to_f2(long_liquidation, "%.3f");
58850
- short_position.avg_liquidation = to_f2(short_liquidation, "%.3f");
58851
- const long_ratio = Math.max(long_position.avg_liquidation, long_position.avg_entry) / Math.min(long_position.avg_liquidation, long_position.avg_entry) - 1;
58852
- const short_ratio = Math.max(short_position.avg_liquidation, short_position.avg_entry) / Math.min(short_position.avg_liquidation, short_position.avg_entry) - 1;
58853
- long_position.liquidation_ratio = to_f2(long_ratio, "%.3f") * 100;
58854
- short_position.liquidation_ratio = to_f2(short_ratio, "%.3f") * 100;
58855
- return {
58856
- long_position,
58857
- short_position,
58858
- long_db_position,
58859
- short_db_position,
58860
- balance: active_account.usd_balance
58861
- };
58862
- }
58863
58831
  async getWindingDownMarkets() {
58864
58832
  return await this.app_db.getWindingDownMarkets();
58865
58833
  }
@@ -59069,6 +59037,171 @@ class App {
59069
59037
  });
59070
59038
  return result;
59071
59039
  }
59040
+ async profitWithinGapStrategy(payload) {
59041
+ const {
59042
+ account,
59043
+ symbol,
59044
+ kind,
59045
+ risk,
59046
+ resistance,
59047
+ support,
59048
+ reward_factor = 1
59049
+ } = payload;
59050
+ const exchange_account = await this.getExchangeAccount(account);
59051
+ console.log("Getting entry and stop for ", symbol, kind);
59052
+ const entry = kind === "long" ? resistance : support;
59053
+ const stop = kind === "long" ? support : resistance;
59054
+ console.log("Building app config for ", symbol, kind);
59055
+ const initial_app_config = await exchange_account.buildAppConfig({
59056
+ entry,
59057
+ stop,
59058
+ risk_reward: 199,
59059
+ risk,
59060
+ symbol
59061
+ });
59062
+ console.log("Computing risk reward for ", symbol, kind);
59063
+ const risk_reward = computeRiskReward({
59064
+ app_config: initial_app_config,
59065
+ entry: initial_app_config.entry,
59066
+ stop: initial_app_config.stop,
59067
+ risk_per_trade: initial_app_config.risk_per_trade
59068
+ });
59069
+ console.log("Re-computing app config for ", symbol, kind);
59070
+ const { entries, last_value, ...app_config } = await exchange_account.buildAppConfig({
59071
+ entry: initial_app_config.entry,
59072
+ stop: initial_app_config.stop,
59073
+ risk_reward,
59074
+ risk,
59075
+ symbol
59076
+ });
59077
+ console.log("Computing profit percent for ", symbol, kind);
59078
+ const pnl = reward_factor * risk;
59079
+ const profit_percent = to_f2(pnl * 100 / (last_value.avg_entry * last_value.avg_size), "%.4f");
59080
+ let config2 = {
59081
+ entry,
59082
+ stop,
59083
+ risk,
59084
+ risk_reward,
59085
+ profit_percent
59086
+ };
59087
+ console.log("Saving new config for ", symbol, kind);
59088
+ const data = await exchange_account.getPositionConfig({
59089
+ symbol,
59090
+ kind,
59091
+ params: config2
59092
+ });
59093
+ console.log("Checking orders to place for ", symbol, kind);
59094
+ const orders_to_place = await exchange_account.placeTrade({
59095
+ symbol,
59096
+ raw: true,
59097
+ kind,
59098
+ place: false
59099
+ });
59100
+ config2 = {
59101
+ ...config2,
59102
+ ...data
59103
+ };
59104
+ console.log("Fetching positions for ", symbol);
59105
+ const positions = await exchange_account.syncAccount({
59106
+ symbol,
59107
+ as_view: true
59108
+ });
59109
+ console.log("Getting long and short positions for ", symbol);
59110
+ const long_position = positions.find((k) => k.kind === "long");
59111
+ const short_position = positions.find((k) => k.kind === "short");
59112
+ console.log("Getting focus position for ", symbol, kind);
59113
+ const focus_position = kind === "long" ? long_position : short_position;
59114
+ const reverse_position = kind === "long" ? short_position : long_position;
59115
+ let reverse_action = null;
59116
+ let reverse_orders_to_buy = [];
59117
+ let reverse_config = null;
59118
+ console.log("Checking if focus position has quantity for ", symbol, kind);
59119
+ if (focus_position.quantity > 0) {
59120
+ console.log("Getting details for ", reverse_position.kind);
59121
+ reverse_action = await exchange_account.buildOppositeTrades({
59122
+ symbol,
59123
+ kind
59124
+ });
59125
+ console.log("Updating config for ", symbol, reverse_action.kind);
59126
+ await exchange_account.getPositionConfig({
59127
+ symbol,
59128
+ kind: reverse_action.kind,
59129
+ params: {
59130
+ entry: reverse_action.entry,
59131
+ stop: reverse_action.stop,
59132
+ risk: reverse_action.risk_per_trade,
59133
+ profit_percent: reverse_action.profit_percent,
59134
+ risk_reward: reverse_action.risk_reward
59135
+ }
59136
+ });
59137
+ console.log("Checking reverse orders to buy for ", symbol, reverse_action.kind);
59138
+ reverse_orders_to_buy = await exchange_account.placeTrade({
59139
+ symbol,
59140
+ raw: true,
59141
+ kind: reverse_action.kind,
59142
+ place: false
59143
+ });
59144
+ let _reverse_config = {
59145
+ avg: reverse_action.avg,
59146
+ entry: reverse_action.entry,
59147
+ stop: reverse_action.stop,
59148
+ risk_per_trade: reverse_action.risk_per_trade,
59149
+ profit_percent: reverse_action.profit_percent,
59150
+ risk_reward: reverse_action.risk_reward
59151
+ };
59152
+ if (reverse_orders_to_buy.length > 0) {
59153
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
59154
+ let existing = await exchange_account.placeOppositeTradeAction({
59155
+ symbol,
59156
+ kind: reverse_action.kind,
59157
+ data: _reverse_config
59158
+ });
59159
+ _reverse_config = {
59160
+ ...existing,
59161
+ ..._reverse_config
59162
+ };
59163
+ }
59164
+ reverse_config = _reverse_config;
59165
+ if (!reverse_config?.id) {
59166
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
59167
+ reverse_config = await exchange_account.getPositionConfig({
59168
+ symbol,
59169
+ kind: reverse_action.kind
59170
+ });
59171
+ }
59172
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
59173
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
59174
+ const max_size = app_config.max_size * 0.98;
59175
+ if (reverse_config.threshold_qty !== max_size) {
59176
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
59177
+ follow: true,
59178
+ threshold_qty: max_size
59179
+ });
59180
+ }
59181
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
59182
+ } else {
59183
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
59184
+ follow: false
59185
+ });
59186
+ }
59187
+ }
59188
+ return {
59189
+ reverse_config,
59190
+ reverse_action,
59191
+ reverse_orders_to_buy,
59192
+ positions: {
59193
+ long: long_position,
59194
+ short: short_position
59195
+ },
59196
+ orders_to_place,
59197
+ config_details: {
59198
+ app_config,
59199
+ last_value,
59200
+ config: config2,
59201
+ pnl
59202
+ }
59203
+ };
59204
+ }
59072
59205
  }
59073
59206
  async function initApp(payload) {
59074
59207
  const pb = await initPocketBaseClient(payload.db);
package/dist/index.d.ts CHANGED
@@ -1737,16 +1737,6 @@ declare class App {
1737
1737
  message?: undefined;
1738
1738
  exchange_result?: undefined;
1739
1739
  }>;
1740
- updateReduceRatio(payload: {
1741
- account: ExchangeType;
1742
- symbol: string;
1743
- }): Promise<{
1744
- long_position: any;
1745
- short_position: any;
1746
- long_db_position: any;
1747
- short_db_position: any;
1748
- balance: any;
1749
- }>;
1750
1740
  getWindingDownMarkets(): Promise<WindingDownMarket[]>;
1751
1741
  updateSymbolConfigs(payload: {
1752
1742
  configs: {
@@ -1793,6 +1783,102 @@ declare class App {
1793
1783
  cancel?: boolean;
1794
1784
  raw?: boolean;
1795
1785
  }): Promise<any>;
1786
+ profitWithinGapStrategy(payload: {
1787
+ account: ExchangeType;
1788
+ symbol: string;
1789
+ kind: "long" | "short";
1790
+ risk: number;
1791
+ resistance: number;
1792
+ support: number;
1793
+ reward_factor?: number;
1794
+ }): Promise<{
1795
+ reverse_config: any;
1796
+ reverse_action: {
1797
+ avg: {
1798
+ entry: number;
1799
+ price: number;
1800
+ quantity: number;
1801
+ };
1802
+ loss: number;
1803
+ profit_percent: number;
1804
+ fee: number;
1805
+ risk_per_trade: number;
1806
+ risk_reward: number;
1807
+ symbol?: string;
1808
+ focus: number;
1809
+ budget: number;
1810
+ support: number;
1811
+ resistance: number;
1812
+ percent_change: number;
1813
+ tradeSplit?: number;
1814
+ take_profit?: number;
1815
+ kind: "long" | "short";
1816
+ entry: number;
1817
+ stop: number;
1818
+ min_size: number;
1819
+ price_places?: string;
1820
+ strategy?: "quantity" | "entry";
1821
+ as_array?: boolean;
1822
+ decimal_places?: string;
1823
+ min_profit?: number;
1824
+ raw?: boolean;
1825
+ gap?: number;
1826
+ rr?: number;
1827
+ max_size?: number;
1828
+ };
1829
+ reverse_orders_to_buy: any;
1830
+ positions: {
1831
+ long: PositionsView;
1832
+ short: PositionsView;
1833
+ };
1834
+ orders_to_place: any;
1835
+ config_details: {
1836
+ app_config: {
1837
+ fee: number;
1838
+ risk_per_trade: number;
1839
+ risk_reward: number;
1840
+ symbol?: string;
1841
+ focus: number;
1842
+ budget: number;
1843
+ support: number;
1844
+ resistance: number;
1845
+ percent_change: number;
1846
+ tradeSplit?: number;
1847
+ take_profit?: number;
1848
+ kind: "long" | "short";
1849
+ entry: number;
1850
+ stop: number;
1851
+ min_size: number;
1852
+ price_places?: string;
1853
+ strategy?: "quantity" | "entry";
1854
+ as_array?: boolean;
1855
+ decimal_places?: string;
1856
+ min_profit?: number;
1857
+ raw?: boolean;
1858
+ gap?: number;
1859
+ rr?: number;
1860
+ max_size?: number;
1861
+ };
1862
+ last_value: any;
1863
+ config: {
1864
+ entry: number;
1865
+ stop: number;
1866
+ risk: number;
1867
+ risk_reward: number | {
1868
+ result: any[];
1869
+ value: number;
1870
+ total: number;
1871
+ risk_per_trade: number;
1872
+ max: number;
1873
+ min: number;
1874
+ neg_pnl: any;
1875
+ entry: any;
1876
+ };
1877
+ profit_percent: number;
1878
+ };
1879
+ pnl: number;
1880
+ };
1881
+ }>;
1796
1882
  }
1797
1883
  export declare function initApp(payload: {
1798
1884
  db: {
package/dist/index.js CHANGED
@@ -53069,7 +53069,7 @@ class Signal {
53069
53069
  new_stop
53070
53070
  };
53071
53071
  });
53072
- if (greater_than_min_size.length !== less_than_min_size.length) {
53072
+ if (greater_than_min_size.length !== total_orders.length) {
53073
53073
  payload = greater_than_min_size.concat(less_than_min_size);
53074
53074
  }
53075
53075
  return payload;
@@ -58725,7 +58725,6 @@ async function getExchangeAccount(payload) {
58725
58725
  app_db
58726
58726
  });
58727
58727
  }
58728
-
58729
58728
  // src/app.ts
58730
58729
  class App {
58731
58730
  app_db;
@@ -58784,37 +58783,6 @@ class App {
58784
58783
  stop: payload.stop
58785
58784
  });
58786
58785
  }
58787
- async updateReduceRatio(payload) {
58788
- const { symbol } = payload;
58789
- const exchange_account = await this.getExchangeAccount(payload.account);
58790
- const positions = await exchange_account.syncAccount({
58791
- as_view: true,
58792
- symbol
58793
- });
58794
- const active_account = await exchange_account.getActiveAccount({
58795
- symbol
58796
- });
58797
- const long_position = active_account.position.long;
58798
- const short_position = active_account.position.short;
58799
- const long_db_position = positions.find((p) => p.kind === "long");
58800
- const short_db_position = positions.find((p) => p.kind === "short");
58801
- const balance = active_account.usd_balance || 0;
58802
- const long_liquidation = long_db_position.avg_price * long_db_position.avg_qty / long_db_position.leverage - balance / long_db_position.avg_qty + long_db_position.avg_price;
58803
- const short_liquidation = -(short_db_position.avg_price * short_db_position.avg_qty / short_db_position.leverage) + balance / short_db_position.avg_qty + short_db_position.avg_price;
58804
- long_position.avg_liquidation = to_f2(long_liquidation, "%.3f");
58805
- short_position.avg_liquidation = to_f2(short_liquidation, "%.3f");
58806
- const long_ratio = Math.max(long_position.avg_liquidation, long_position.avg_entry) / Math.min(long_position.avg_liquidation, long_position.avg_entry) - 1;
58807
- const short_ratio = Math.max(short_position.avg_liquidation, short_position.avg_entry) / Math.min(short_position.avg_liquidation, short_position.avg_entry) - 1;
58808
- long_position.liquidation_ratio = to_f2(long_ratio, "%.3f") * 100;
58809
- short_position.liquidation_ratio = to_f2(short_ratio, "%.3f") * 100;
58810
- return {
58811
- long_position,
58812
- short_position,
58813
- long_db_position,
58814
- short_db_position,
58815
- balance: active_account.usd_balance
58816
- };
58817
- }
58818
58786
  async getWindingDownMarkets() {
58819
58787
  return await this.app_db.getWindingDownMarkets();
58820
58788
  }
@@ -59024,6 +58992,171 @@ class App {
59024
58992
  });
59025
58993
  return result;
59026
58994
  }
58995
+ async profitWithinGapStrategy(payload) {
58996
+ const {
58997
+ account,
58998
+ symbol,
58999
+ kind,
59000
+ risk,
59001
+ resistance,
59002
+ support,
59003
+ reward_factor = 1
59004
+ } = payload;
59005
+ 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({
59044
+ symbol,
59045
+ 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
59063
+ });
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
+ reverse_orders_to_buy = await exchange_account.placeTrade({
59094
+ symbol,
59095
+ raw: true,
59096
+ kind: reverse_action.kind,
59097
+ place: false
59098
+ });
59099
+ let _reverse_config = {
59100
+ avg: reverse_action.avg,
59101
+ entry: reverse_action.entry,
59102
+ stop: reverse_action.stop,
59103
+ risk_per_trade: reverse_action.risk_per_trade,
59104
+ profit_percent: reverse_action.profit_percent,
59105
+ risk_reward: reverse_action.risk_reward
59106
+ };
59107
+ if (reverse_orders_to_buy.length > 0) {
59108
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
59109
+ let existing = await exchange_account.placeOppositeTradeAction({
59110
+ symbol,
59111
+ kind: reverse_action.kind,
59112
+ data: _reverse_config
59113
+ });
59114
+ _reverse_config = {
59115
+ ...existing,
59116
+ ..._reverse_config
59117
+ };
59118
+ }
59119
+ reverse_config = _reverse_config;
59120
+ if (!reverse_config?.id) {
59121
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
59122
+ reverse_config = await exchange_account.getPositionConfig({
59123
+ symbol,
59124
+ kind: reverse_action.kind
59125
+ });
59126
+ }
59127
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
59128
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
59129
+ const max_size = app_config.max_size * 0.98;
59130
+ if (reverse_config.threshold_qty !== max_size) {
59131
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
59132
+ follow: true,
59133
+ threshold_qty: max_size
59134
+ });
59135
+ }
59136
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
59137
+ } else {
59138
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
59139
+ follow: false
59140
+ });
59141
+ }
59142
+ }
59143
+ return {
59144
+ reverse_config,
59145
+ reverse_action,
59146
+ reverse_orders_to_buy,
59147
+ positions: {
59148
+ long: long_position,
59149
+ short: short_position
59150
+ },
59151
+ orders_to_place,
59152
+ config_details: {
59153
+ app_config,
59154
+ last_value,
59155
+ config: config2,
59156
+ pnl
59157
+ }
59158
+ };
59159
+ }
59027
59160
  }
59028
59161
  async function initApp(payload) {
59029
59162
  const pb = await initPocketBaseClient(payload.db);
@@ -59818,7 +59818,7 @@ class Signal {
59818
59818
  new_stop
59819
59819
  };
59820
59820
  });
59821
- if (greater_than_min_size.length !== less_than_min_size.length) {
59821
+ if (greater_than_min_size.length !== total_orders.length) {
59822
59822
  payload = greater_than_min_size.concat(less_than_min_size);
59823
59823
  }
59824
59824
  return payload;
@@ -65461,7 +65461,6 @@ async function getExchangeAccount(payload) {
65461
65461
  app_db
65462
65462
  });
65463
65463
  }
65464
-
65465
65464
  // src/app.ts
65466
65465
  class App {
65467
65466
  app_db;
@@ -65520,37 +65519,6 @@ class App {
65520
65519
  stop: payload.stop
65521
65520
  });
65522
65521
  }
65523
- async updateReduceRatio(payload) {
65524
- const { symbol } = payload;
65525
- const exchange_account = await this.getExchangeAccount(payload.account);
65526
- const positions = await exchange_account.syncAccount({
65527
- as_view: true,
65528
- symbol
65529
- });
65530
- const active_account = await exchange_account.getActiveAccount({
65531
- symbol
65532
- });
65533
- const long_position = active_account.position.long;
65534
- const short_position = active_account.position.short;
65535
- const long_db_position = positions.find((p) => p.kind === "long");
65536
- const short_db_position = positions.find((p) => p.kind === "short");
65537
- const balance = active_account.usd_balance || 0;
65538
- const long_liquidation = long_db_position.avg_price * long_db_position.avg_qty / long_db_position.leverage - balance / long_db_position.avg_qty + long_db_position.avg_price;
65539
- const short_liquidation = -(short_db_position.avg_price * short_db_position.avg_qty / short_db_position.leverage) + balance / short_db_position.avg_qty + short_db_position.avg_price;
65540
- long_position.avg_liquidation = to_f2(long_liquidation, "%.3f");
65541
- short_position.avg_liquidation = to_f2(short_liquidation, "%.3f");
65542
- const long_ratio = Math.max(long_position.avg_liquidation, long_position.avg_entry) / Math.min(long_position.avg_liquidation, long_position.avg_entry) - 1;
65543
- const short_ratio = Math.max(short_position.avg_liquidation, short_position.avg_entry) / Math.min(short_position.avg_liquidation, short_position.avg_entry) - 1;
65544
- long_position.liquidation_ratio = to_f2(long_ratio, "%.3f") * 100;
65545
- short_position.liquidation_ratio = to_f2(short_ratio, "%.3f") * 100;
65546
- return {
65547
- long_position,
65548
- short_position,
65549
- long_db_position,
65550
- short_db_position,
65551
- balance: active_account.usd_balance
65552
- };
65553
- }
65554
65522
  async getWindingDownMarkets() {
65555
65523
  return await this.app_db.getWindingDownMarkets();
65556
65524
  }
@@ -65760,6 +65728,171 @@ class App {
65760
65728
  });
65761
65729
  return result;
65762
65730
  }
65731
+ async profitWithinGapStrategy(payload) {
65732
+ const {
65733
+ account,
65734
+ symbol,
65735
+ kind,
65736
+ risk,
65737
+ resistance,
65738
+ support,
65739
+ reward_factor = 1
65740
+ } = payload;
65741
+ 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({
65780
+ symbol,
65781
+ 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
65799
+ });
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
+ };
65895
+ }
65763
65896
  }
65764
65897
  async function initApp(payload) {
65765
65898
  const pb = await initPocketBaseClient(payload.db);
@@ -65894,13 +66027,13 @@ async function getPositions(payload) {
65894
66027
  const symbol_config = await exchange_account.recomputeSymbolConfig({
65895
66028
  symbol: payload.symbol
65896
66029
  });
65897
- const strategy = await exchange_account.getPositionStrategy();
66030
+ const strategy2 = await exchange_account.getPositionStrategy();
65898
66031
  const strategy_config = {
65899
- tp_percent: strategy?.tp_percent,
65900
- short_tp_factor: strategy?.short_tp_factor,
65901
- fee_percent: strategy?.fee_percent,
65902
- budget: strategy?.budget,
65903
- risk_reward: strategy?.risk_reward,
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,
65904
66037
  global_config: symbol_config
65905
66038
  };
65906
66039
  const positions = await exchange_account.syncAccount({
@@ -59795,7 +59795,7 @@ class Signal {
59795
59795
  new_stop
59796
59796
  };
59797
59797
  });
59798
- if (greater_than_min_size.length !== less_than_min_size.length) {
59798
+ if (greater_than_min_size.length !== total_orders.length) {
59799
59799
  payload = greater_than_min_size.concat(less_than_min_size);
59800
59800
  }
59801
59801
  return payload;
@@ -65438,7 +65438,6 @@ async function getExchangeAccount(payload) {
65438
65438
  app_db
65439
65439
  });
65440
65440
  }
65441
-
65442
65441
  // src/app.ts
65443
65442
  class App {
65444
65443
  app_db;
@@ -65497,37 +65496,6 @@ class App {
65497
65496
  stop: payload.stop
65498
65497
  });
65499
65498
  }
65500
- async updateReduceRatio(payload) {
65501
- const { symbol } = payload;
65502
- const exchange_account = await this.getExchangeAccount(payload.account);
65503
- const positions = await exchange_account.syncAccount({
65504
- as_view: true,
65505
- symbol
65506
- });
65507
- const active_account = await exchange_account.getActiveAccount({
65508
- symbol
65509
- });
65510
- const long_position = active_account.position.long;
65511
- const short_position = active_account.position.short;
65512
- const long_db_position = positions.find((p) => p.kind === "long");
65513
- const short_db_position = positions.find((p) => p.kind === "short");
65514
- const balance = active_account.usd_balance || 0;
65515
- const long_liquidation = long_db_position.avg_price * long_db_position.avg_qty / long_db_position.leverage - balance / long_db_position.avg_qty + long_db_position.avg_price;
65516
- const short_liquidation = -(short_db_position.avg_price * short_db_position.avg_qty / short_db_position.leverage) + balance / short_db_position.avg_qty + short_db_position.avg_price;
65517
- long_position.avg_liquidation = to_f2(long_liquidation, "%.3f");
65518
- short_position.avg_liquidation = to_f2(short_liquidation, "%.3f");
65519
- const long_ratio = Math.max(long_position.avg_liquidation, long_position.avg_entry) / Math.min(long_position.avg_liquidation, long_position.avg_entry) - 1;
65520
- const short_ratio = Math.max(short_position.avg_liquidation, short_position.avg_entry) / Math.min(short_position.avg_liquidation, short_position.avg_entry) - 1;
65521
- long_position.liquidation_ratio = to_f2(long_ratio, "%.3f") * 100;
65522
- short_position.liquidation_ratio = to_f2(short_ratio, "%.3f") * 100;
65523
- return {
65524
- long_position,
65525
- short_position,
65526
- long_db_position,
65527
- short_db_position,
65528
- balance: active_account.usd_balance
65529
- };
65530
- }
65531
65499
  async getWindingDownMarkets() {
65532
65500
  return await this.app_db.getWindingDownMarkets();
65533
65501
  }
@@ -65737,6 +65705,171 @@ class App {
65737
65705
  });
65738
65706
  return result;
65739
65707
  }
65708
+ async profitWithinGapStrategy(payload) {
65709
+ const {
65710
+ account,
65711
+ symbol,
65712
+ kind,
65713
+ risk,
65714
+ resistance,
65715
+ support,
65716
+ reward_factor = 1
65717
+ } = payload;
65718
+ 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({
65757
+ symbol,
65758
+ 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
65776
+ });
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
+ reverse_orders_to_buy = await exchange_account.placeTrade({
65807
+ symbol,
65808
+ raw: true,
65809
+ kind: reverse_action.kind,
65810
+ place: false
65811
+ });
65812
+ let _reverse_config = {
65813
+ avg: reverse_action.avg,
65814
+ entry: reverse_action.entry,
65815
+ stop: reverse_action.stop,
65816
+ risk_per_trade: reverse_action.risk_per_trade,
65817
+ profit_percent: reverse_action.profit_percent,
65818
+ risk_reward: reverse_action.risk_reward
65819
+ };
65820
+ if (reverse_orders_to_buy.length > 0) {
65821
+ console.log("Placing opposite trade action for ", symbol, reverse_action.kind);
65822
+ let existing = await exchange_account.placeOppositeTradeAction({
65823
+ symbol,
65824
+ kind: reverse_action.kind,
65825
+ data: _reverse_config
65826
+ });
65827
+ _reverse_config = {
65828
+ ...existing,
65829
+ ..._reverse_config
65830
+ };
65831
+ }
65832
+ reverse_config = _reverse_config;
65833
+ if (!reverse_config?.id) {
65834
+ console.log("fetching reverse config for ", symbol, reverse_action.kind);
65835
+ reverse_config = await exchange_account.getPositionConfig({
65836
+ symbol,
65837
+ kind: reverse_action.kind
65838
+ });
65839
+ }
65840
+ if (reverse_position.quantity > 0 && reverse_config?.id) {
65841
+ console.log("Checking if reverse position has quantity for ", symbol, reverse_action.kind);
65842
+ const max_size = app_config.max_size * 0.98;
65843
+ if (reverse_config.threshold_qty !== max_size) {
65844
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65845
+ follow: true,
65846
+ threshold_qty: max_size
65847
+ });
65848
+ }
65849
+ console.log("Updating follow and threshold for ", symbol, reverse_action.kind);
65850
+ } else {
65851
+ await this.app_db.updateScheduledTrade(reverse_config.id, {
65852
+ follow: false
65853
+ });
65854
+ }
65855
+ }
65856
+ return {
65857
+ reverse_config,
65858
+ reverse_action,
65859
+ reverse_orders_to_buy,
65860
+ positions: {
65861
+ long: long_position,
65862
+ short: short_position
65863
+ },
65864
+ orders_to_place,
65865
+ config_details: {
65866
+ app_config,
65867
+ last_value,
65868
+ config: config2,
65869
+ pnl
65870
+ }
65871
+ };
65872
+ }
65740
65873
  }
65741
65874
  async function initApp(payload) {
65742
65875
  const pb = await initPocketBaseClient(payload.db);
@@ -65871,13 +66004,13 @@ async function getPositions(payload) {
65871
66004
  const symbol_config = await exchange_account.recomputeSymbolConfig({
65872
66005
  symbol: payload.symbol
65873
66006
  });
65874
- const strategy = await exchange_account.getPositionStrategy();
66007
+ const strategy2 = await exchange_account.getPositionStrategy();
65875
66008
  const strategy_config = {
65876
- tp_percent: strategy?.tp_percent,
65877
- short_tp_factor: strategy?.short_tp_factor,
65878
- fee_percent: strategy?.fee_percent,
65879
- budget: strategy?.budget,
65880
- risk_reward: strategy?.risk_reward,
66009
+ tp_percent: strategy2?.tp_percent,
66010
+ short_tp_factor: strategy2?.short_tp_factor,
66011
+ fee_percent: strategy2?.fee_percent,
66012
+ budget: strategy2?.budget,
66013
+ risk_reward: strategy2?.risk_reward,
65881
66014
  global_config: symbol_config
65882
66015
  };
65883
66016
  const positions = await exchange_account.syncAccount({
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-79",
4
+ "version": "0.0.2-82",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",