@gbozee/ultimate 0.0.2-24 → 0.0.2-27

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.d.ts CHANGED
@@ -133,6 +133,7 @@ export interface BaseExchange {
133
133
  }): Promise<any>;
134
134
  checkDelistedMovers(payload: {
135
135
  movePercent: number;
136
+ include_delisted?: boolean;
136
137
  }): Promise<any>;
137
138
  closePosition(payload: {
138
139
  symbol: string;
@@ -149,6 +150,7 @@ export interface BaseExchange {
149
150
  symbol: string;
150
151
  }): Promise<any>;
151
152
  getDelistedSpotSymbols(): Promise<any>;
153
+ getOpenPositions(): Promise<any>;
152
154
  }
153
155
  export interface BaseSystemFields {
154
156
  id: string;
@@ -169,9 +171,11 @@ export interface ExchangeAccount extends BaseSystemFields {
169
171
  totalRisk?: number;
170
172
  max_non_essential?: number;
171
173
  profit_percent?: number;
174
+ risk_reward?: number;
172
175
  exclude_coins?: {
173
176
  bullish?: string[];
174
177
  };
178
+ include_delisted?: boolean;
175
179
  }
176
180
  export interface SymbolConfig extends BaseSystemFields {
177
181
  symbol: string;
@@ -392,7 +396,10 @@ export declare class AppDatabase {
392
396
  strategy_instance: Strategy;
393
397
  focus_account: ExchangeAccount;
394
398
  }>;
395
- createOrUpdateWindingDownMarket(symbol: string): Promise<import("pocketbase").RecordModel>;
399
+ createOrUpdateWindingDownMarket(payload: {
400
+ symbol: string;
401
+ risk_reward?: number;
402
+ }): Promise<import("pocketbase").RecordModel>;
396
403
  getWindingDownMarkets(symbol?: string): Promise<WindingDownMarket[]>;
397
404
  getBullishMarket(symbol: string): Promise<BullishMarket>;
398
405
  getBullishMarkets(options?: {
@@ -788,14 +795,23 @@ declare class ExchangeAccount$1 {
788
795
  kind: "long" | "short";
789
796
  place?: boolean;
790
797
  tp?: boolean;
798
+ raw?: boolean;
799
+ cancel?: boolean;
791
800
  }): Promise<any>;
792
801
  }
793
802
  declare class App {
794
803
  app_db: AppDatabase;
804
+ proxyOptions?: {
805
+ proxy?: any;
806
+ ignore_proxy?: boolean;
807
+ };
795
808
  private getCredentials;
796
809
  constructor(app_db: AppDatabase, getCredentials: (account: string, exchange: string) => {
797
810
  api_key: string;
798
811
  api_secret: string;
812
+ }, proxyOptions?: {
813
+ proxy?: any;
814
+ ignore_proxy?: boolean;
799
815
  });
800
816
  getExchangeAccount(account: ExchangeType): Promise<ExchangeAccount$1>;
801
817
  syncAccount(payload: {
@@ -891,6 +907,15 @@ declare class App {
891
907
  account: ExchangeType;
892
908
  }) => Promise<any>;
893
909
  }): Promise<void>;
910
+ placeTrade(payload: {
911
+ account: ExchangeType;
912
+ symbol: string;
913
+ kind: "long" | "short";
914
+ place?: boolean;
915
+ tp?: boolean;
916
+ cancel?: boolean;
917
+ raw?: boolean;
918
+ }): Promise<any>;
894
919
  }
895
920
  export declare function initApp(payload: {
896
921
  db: {
@@ -903,8 +928,14 @@ export declare function initApp(payload: {
903
928
  api_key: string;
904
929
  api_secret: string;
905
930
  };
931
+ proxy?: any;
932
+ ignore_proxy?: boolean;
933
+ }): Promise<App>;
934
+ export declare function initialize(payload: {
935
+ password?: string;
936
+ proxy?: any;
937
+ ignore_proxy?: boolean;
906
938
  }): Promise<App>;
907
- export declare function initialize(password?: string): Promise<App>;
908
939
 
909
940
  export {
910
941
  ExchangeAccount$1 as ExchangeAccount,
package/dist/index.js CHANGED
@@ -32527,6 +32527,9 @@ class AppDatabase {
32527
32527
  place_tp: payload.place_tp !== undefined ? payload.place_tp : true,
32528
32528
  profit: payload.profit !== undefined ? payload.profit : config.profit
32529
32529
  });
32530
+ await this.update_db_position(db_position, {
32531
+ config: config.id
32532
+ });
32530
32533
  for (const _config of configs) {
32531
32534
  if (_config.id !== config.id) {
32532
32535
  await this.pb.collection("scheduled_trades").delete(_config.id);
@@ -32571,14 +32574,15 @@ class AppDatabase {
32571
32574
  }
32572
32575
  return null;
32573
32576
  }
32574
- async createOrUpdateWindingDownMarket(symbol) {
32577
+ async createOrUpdateWindingDownMarket(payload) {
32578
+ const { symbol, risk_reward = 30 } = payload;
32575
32579
  const existing_winding_down_market = await this.pb.collection("winding_down_markets").getFullList({
32576
32580
  filter: `symbol:lower="${symbol.toLowerCase()}"`
32577
32581
  });
32578
32582
  if (existing_winding_down_market.length > 0) {
32579
32583
  return existing_winding_down_market[0];
32580
32584
  }
32581
- return await this.pb.collection("winding_down_markets").create({ symbol, risk_reward: 199 });
32585
+ return await this.pb.collection("winding_down_markets").create({ symbol, risk_reward });
32582
32586
  }
32583
32587
  async getWindingDownMarkets(symbol) {
32584
32588
  const result = await this.pb.collection("winding_down_markets").getFullList();
@@ -32628,7 +32632,9 @@ class AppDatabase {
32628
32632
  console.log(`Processing ${market.symbol} for winding down.`);
32629
32633
  recordsToDeleteFromBullish.push(market.id);
32630
32634
  try {
32631
- const windingDownRecord = await this.createOrUpdateWindingDownMarket(market.symbol);
32635
+ const windingDownRecord = await this.createOrUpdateWindingDownMarket({
32636
+ symbol: market.symbol
32637
+ });
32632
32638
  if (windingDownRecord) {
32633
32639
  moved_to_winding.push(windingDownRecord);
32634
32640
  windDownCreatedCount++;
@@ -32655,11 +32661,15 @@ class AppDatabase {
32655
32661
  let updatedCount = 0;
32656
32662
  let createdMarkets = [];
32657
32663
  const recordsToDeleteFromWindingDown = [];
32664
+ console.log("new_markets", new_markets);
32658
32665
  if (totalRisk <= 0) {
32659
32666
  console.warn("Total percent movement is zero or negative. Cannot allocate risk.");
32660
32667
  } else {
32668
+ const cumulative_risk = new_markets.reduce((acc, curr) => {
32669
+ return acc + curr.percent;
32670
+ }, 0);
32661
32671
  for (const newMarket of new_markets) {
32662
- const calculatedRisk = newMarket.percent / totalRisk * totalRisk;
32672
+ const calculatedRisk = newMarket.percent / cumulative_risk * totalRisk;
32663
32673
  if (currentWindingDownSymbols.has(newMarket.symbol)) {
32664
32674
  console.log(`Marking ${newMarket.symbol} for removal from winding down markets as it is now bullish.`);
32665
32675
  const windingDownRecord = currentWindingDown.find((w) => w.symbol === newMarket.symbol);
@@ -33789,16 +33799,20 @@ function createGapPairs(arr, gap, item) {
33789
33799
  async function initClient(credentials, options) {
33790
33800
  const { proxyAgent, type = "future" } = options || {};
33791
33801
  try {
33802
+ const axiosOptions = proxyAgent ? {
33803
+ httpAgent: proxyAgent,
33804
+ httpsAgent: proxyAgent
33805
+ } : undefined;
33806
+ if (!proxyAgent) {
33807
+ console.log("using no proxy");
33808
+ }
33792
33809
  const Klass = type === "future" ? import_binance.USDMClient : type === "spot" ? import_binance.MainClient : import_binance.MainClient;
33793
33810
  const client = new Klass({
33794
33811
  api_key: credentials.api_key,
33795
33812
  api_secret: credentials.api_secret,
33796
33813
  recvWindow: 1e4,
33797
33814
  beautifyResponses: true
33798
- }, {
33799
- httpAgent: proxyAgent,
33800
- httpsAgent: proxyAgent
33801
- });
33815
+ }, axiosOptions);
33802
33816
  return client;
33803
33817
  } catch (e2) {
33804
33818
  console.log(e2);
@@ -34552,7 +34566,7 @@ class BinanceExchange {
34552
34566
  });
34553
34567
  const _activeSymbols = await getActiveSymbols({ client: this.client });
34554
34568
  const activeSymbols = _activeSymbols.map((s2) => s2.symbol);
34555
- const delisted = movers.map((x) => x.symbol).filter((symbol) => !activeSymbols.includes(symbol));
34569
+ const delisted = payload.include_delisted ? [] : movers.map((x) => x.symbol).filter((symbol) => !activeSymbols.includes(symbol));
34556
34570
  const validMovers = movers.filter((m) => !delisted.includes(m.symbol)).filter((m) => activeSymbols.includes(m.symbol));
34557
34571
  const activeMovers = [];
34558
34572
  const isActivePromises = validMovers.map(async (m) => {
@@ -34599,6 +34613,10 @@ class BinanceExchange {
34599
34613
  }
34600
34614
  return [];
34601
34615
  }
34616
+ async getOpenPositions() {
34617
+ const response = await this.client.getPositionsV3();
34618
+ return Array.from(new Set(response.map((x) => x.symbol)));
34619
+ }
34602
34620
  }
34603
34621
  function getPricePlaces(target) {
34604
34622
  const numStr = target.toString();
@@ -34614,14 +34632,15 @@ var import_bybit_api = __toESM(require_lib2(), 1);
34614
34632
  async function initClient2(credentials, options) {
34615
34633
  const { proxyAgent } = options;
34616
34634
  try {
34635
+ const axiosOptions = proxyAgent ? {
34636
+ httpAgent: proxyAgent,
34637
+ httpsAgent: proxyAgent
34638
+ } : undefined;
34617
34639
  const client = new import_bybit_api.RestClientV5({
34618
34640
  key: credentials.api_key,
34619
34641
  secret: credentials.api_secret,
34620
34642
  recv_window: 1e4
34621
- }, {
34622
- httpAgent: proxyAgent,
34623
- httpsAgent: proxyAgent
34624
- });
34643
+ }, axiosOptions);
34625
34644
  return client;
34626
34645
  } catch (e2) {
34627
34646
  console.log(e2);
@@ -35247,6 +35266,9 @@ class BybitExchange {
35247
35266
  async getAllOpenSymbols() {
35248
35267
  return [];
35249
35268
  }
35269
+ async getOpenPositions() {
35270
+ return [];
35271
+ }
35250
35272
  async createLimitPurchaseOrders(payload) {
35251
35273
  const {
35252
35274
  orders,
@@ -36959,6 +36981,9 @@ class ExchangeAccount {
36959
36981
  return false;
36960
36982
  }
36961
36983
  const db_instance = await this.app_db.get_exchange_db_instance(this.instance);
36984
+ if (!db_instance.bullish) {
36985
+ return false;
36986
+ }
36962
36987
  if (db_instance.profit_percent) {
36963
36988
  profit_percent = db_instance.profit_percent;
36964
36989
  }
@@ -36970,18 +36995,21 @@ class ExchangeAccount {
36970
36995
  const long_config = await this.buildConfigForSymbol({
36971
36996
  symbol,
36972
36997
  risk: bullish_instance.risk,
36973
- risk_reward
36998
+ risk_reward: db_instance.risk_reward || risk_reward
36974
36999
  });
36975
37000
  let changed = false;
36976
37001
  if (!position2?.config) {
36977
- await this.buildAppConfig({
36978
- entry: long_config.entry,
36979
- stop: long_config.stop,
36980
- risk_reward: long_config.risk_reward,
36981
- risk: long_config.risk,
37002
+ await this.getPositionConfig({
36982
37003
  symbol,
36983
- profit_percent,
36984
- update_db: true
37004
+ kind: "long",
37005
+ params: {
37006
+ entry: long_config.entry,
37007
+ stop: long_config.stop,
37008
+ risk_reward: long_config.risk_reward,
37009
+ risk: long_config.risk,
37010
+ profit_percent,
37011
+ place_tp: true
37012
+ }
36985
37013
  });
36986
37014
  changed = true;
36987
37015
  } else {
@@ -37048,12 +37076,28 @@ class ExchangeAccount {
37048
37076
  no_position: true,
37049
37077
  kind: "long"
37050
37078
  });
37051
- const all_open_symbols = await this.exchange.getAllOpenSymbols();
37079
+ const all_open_symbols = await this.exchange.getOpenPositions();
37052
37080
  await new Promise((resolve) => setTimeout(resolve, interval * 1000));
37053
37081
  for (const symbol of Array.from(new Set(symbols.concat(all_open_symbols)))) {
37054
37082
  await this.getLiveExchangeInstance({ symbol, refresh: true });
37055
37083
  await new Promise((resolve) => setTimeout(resolve, 1000));
37056
37084
  }
37085
+ for (const symbol of all_open_symbols) {
37086
+ await this.syncAccount({
37087
+ symbol,
37088
+ update: true
37089
+ });
37090
+ await this.syncOrders({
37091
+ symbol,
37092
+ kind: "long",
37093
+ update: true
37094
+ });
37095
+ await this.syncOrders({
37096
+ symbol,
37097
+ kind: "short",
37098
+ update: true
37099
+ });
37100
+ }
37057
37101
  }
37058
37102
  async updateAllPositionsWithNoConfig(payload) {
37059
37103
  const { kind } = payload;
@@ -37083,7 +37127,7 @@ class ExchangeAccount {
37083
37127
  }
37084
37128
  async getNonEssentialSymbols() {
37085
37129
  const [all_open_symbols, essential_symbols] = await Promise.all([
37086
- this.exchange.getAllOpenSymbols(),
37130
+ this.exchange.getOpenPositions(),
37087
37131
  this.app_db.getAllSymbolConfigs({
37088
37132
  custom_filter: `essential = true`
37089
37133
  })
@@ -37095,15 +37139,16 @@ class ExchangeAccount {
37095
37139
  const bullish_symbols = Array.from(new Set(bullish_markets.updated_bullish.map((m) => m.symbol)));
37096
37140
  const symbols = Array.from(new Set(essential_symbols.map((s2) => s2.symbol))).concat(bullish_symbols);
37097
37141
  const not_symbols_filter = symbols.map((s2) => `symbol:lower != "${s2.toLowerCase()}"`).join(" && ");
37142
+ let filters = not_symbols_filter ? `${not_symbols_filter} && account.owner:lower = "${this.instance.owner.toLowerCase()}" && account.exchange:lower = "${this.instance.exchange.toLowerCase()}"` : `account.owner:lower = "${this.instance.owner.toLowerCase()}" && account.exchange:lower = "${this.instance.exchange.toLowerCase()}"`;
37098
37143
  const positions = await this.app_db.getPositions({
37099
- custom_filter: `${not_symbols_filter} && account.owner:lower = "${this.instance.owner.toLowerCase()}" && account.exchange:lower = "${this.instance.exchange.toLowerCase()}"`,
37144
+ custom_filter: filters,
37100
37145
  symbol: "",
37101
37146
  account: {
37102
37147
  owner: "",
37103
37148
  exchange: ""
37104
37149
  }
37105
37150
  });
37106
- return Array.from(new Set(positions.map((p) => p.symbol).concat(non_essential_symbols)));
37151
+ return Array.from(new Set(positions.map((p) => p.symbol).concat(non_essential_symbols))).filter((k) => !["BTCUSDT", "BTCUSDC"].includes(k));
37107
37152
  }
37108
37153
  async _terminatePositions(payload) {
37109
37154
  const { symbol } = payload;
@@ -37301,6 +37346,9 @@ class ExchangeAccount {
37301
37346
  symbol,
37302
37347
  kind: "short"
37303
37348
  });
37349
+ await this.app_db.update_db_position(short_position, {
37350
+ config: null
37351
+ });
37304
37352
  }
37305
37353
  if (long_position && short_position && long_position.quantity === 0 && short_position.quantity === 0) {
37306
37354
  await this.cancelOrders({
@@ -37322,26 +37370,31 @@ class ExchangeAccount {
37322
37370
  const db_instance = await this.app_db.get_exchange_db_instance(this.instance);
37323
37371
  const {
37324
37372
  bullish,
37325
- bearish: _bearish,
37373
+ bearish,
37326
37374
  movePercent,
37327
37375
  totalRisk,
37328
37376
  max_non_essential = 0,
37329
- exclude_coins
37377
+ exclude_coins,
37378
+ include_delisted = false
37330
37379
  } = db_instance;
37331
37380
  let dont_trade = exclude_coins?.bullish || [];
37332
37381
  let bullishMarkets = [];
37333
37382
  if (bullish) {
37334
37383
  const { movers } = await this.exchange.checkDelistedMovers({
37335
- movePercent
37384
+ movePercent,
37385
+ include_delisted
37336
37386
  });
37337
37387
  bullishMarkets = movers;
37338
37388
  }
37339
37389
  const non_essential_symbols = await this.getNonEssentialSymbols();
37340
37390
  let symbols_to_remove = non_essential_symbols.filter((k) => !bullishMarkets.map((m) => m.symbol).includes(k)).slice(0, max_non_essential).concat(dont_trade);
37391
+ symbols_to_remove = Array.from(new Set(symbols_to_remove));
37341
37392
  bullishMarkets = bullishMarkets.filter((m) => !symbols_to_remove.includes(m.symbol));
37342
- if (symbols_to_remove.length > 0) {
37343
- for (const symbol of symbols_to_remove) {
37344
- await this.terminatePositions({ symbol });
37393
+ if (bearish) {
37394
+ if (symbols_to_remove.length > 0) {
37395
+ for (const symbol of symbols_to_remove) {
37396
+ await this.terminatePositions({ symbol });
37397
+ }
37345
37398
  }
37346
37399
  }
37347
37400
  const result = await this.app_db.getBullishMarkets({
@@ -37352,7 +37405,7 @@ class ExchangeAccount {
37352
37405
  totalRisk,
37353
37406
  max_count: max_non_essential
37354
37407
  });
37355
- if (result.updated_bullish.length > max_non_essential) {
37408
+ if (result.updated_bullish.length > max_non_essential || !bullish) {
37356
37409
  return {
37357
37410
  updated_bullish: [],
37358
37411
  moved_to_winding: []
@@ -37393,11 +37446,18 @@ class ExchangeAccount {
37393
37446
  return 0;
37394
37447
  }
37395
37448
  async placeTrade(payload) {
37396
- const { symbol, kind, place, tp } = payload;
37449
+ const { symbol, kind, place, tp, raw: _raw, cancel } = payload;
37450
+ if (cancel) {
37451
+ await this.cancelOrders({
37452
+ symbol,
37453
+ kind
37454
+ });
37455
+ }
37397
37456
  if (place) {
37398
37457
  return await this.triggerTradeFromConfig({
37399
37458
  symbol,
37400
- kind
37459
+ kind,
37460
+ raw: payload.raw
37401
37461
  });
37402
37462
  }
37403
37463
  await this.syncAccount({
@@ -37446,8 +37506,9 @@ function getExchangeKlass(exchange) {
37446
37506
  };
37447
37507
  }
37448
37508
  async function getExchangeAccount(payload) {
37449
- const { account, app_db } = payload;
37450
- const proxyAgent = await app_db.getProxyForAccount(account);
37509
+ const { account, app_db, proxyOptions = {} } = payload;
37510
+ const _proxyAgent = await app_db.getProxyForAccount(account);
37511
+ const proxyAgent = proxyOptions?.ignore_proxy ? null : proxyOptions?.proxy || _proxyAgent;
37451
37512
  const exchange_instance = await getExchangeKlass(account.exchange)({
37452
37513
  account: account.owner,
37453
37514
  getCredentials: payload.getCredentials,
@@ -37462,16 +37523,19 @@ async function getExchangeAccount(payload) {
37462
37523
  // src/app.ts
37463
37524
  class App {
37464
37525
  app_db;
37526
+ proxyOptions;
37465
37527
  getCredentials;
37466
- constructor(app_db, getCredentials) {
37528
+ constructor(app_db, getCredentials, proxyOptions) {
37467
37529
  this.app_db = app_db;
37468
37530
  this.getCredentials = getCredentials;
37531
+ this.proxyOptions = proxyOptions;
37469
37532
  }
37470
37533
  async getExchangeAccount(account) {
37471
37534
  return await getExchangeAccount({
37472
37535
  account,
37473
37536
  app_db: this.app_db,
37474
- getCredentials: this.getCredentials
37537
+ getCredentials: this.getCredentials,
37538
+ proxyOptions: this.proxyOptions
37475
37539
  });
37476
37540
  }
37477
37541
  async syncAccount(payload) {
@@ -37821,6 +37885,19 @@ class App {
37821
37885
  }
37822
37886
  }
37823
37887
  }
37888
+ async placeTrade(payload) {
37889
+ const { account, symbol, kind, place, tp, raw, cancel } = payload;
37890
+ const exchange_account = await this.getExchangeAccount(account);
37891
+ const result = await exchange_account.placeTrade({
37892
+ symbol,
37893
+ kind,
37894
+ place,
37895
+ tp,
37896
+ raw,
37897
+ cancel
37898
+ });
37899
+ return result;
37900
+ }
37824
37901
  }
37825
37902
  async function initApp(payload) {
37826
37903
  const pb = await initPocketBaseClient(payload.db);
@@ -37845,7 +37922,10 @@ async function initApp(payload) {
37845
37922
  console.log("error", error);
37846
37923
  }
37847
37924
  }
37848
- const app = new App(app_db, _getCredentials);
37925
+ const app = new App(app_db, _getCredentials, {
37926
+ proxy: payload.proxy,
37927
+ ignore_proxy: payload.ignore_proxy
37928
+ });
37849
37929
  return app;
37850
37930
  }
37851
37931
  function getCredentials(account, exchange) {
@@ -37883,7 +37963,8 @@ function getCredentials(account, exchange) {
37883
37963
  api_secret: apiSecret
37884
37964
  };
37885
37965
  }
37886
- async function initialize(password) {
37966
+ async function initialize(payload) {
37967
+ const { password, proxy, ignore_proxy } = payload;
37887
37968
  const app = await initApp({
37888
37969
  db: {
37889
37970
  host: process.env.POCKETBASE_HOST,
@@ -37891,7 +37972,9 @@ async function initialize(password) {
37891
37972
  password: process.env.POCKETBASE_PASSWORD
37892
37973
  },
37893
37974
  password,
37894
- getCredentials
37975
+ getCredentials,
37976
+ proxy,
37977
+ ignore_proxy
37895
37978
  });
37896
37979
  return app;
37897
37980
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-24",
4
+ "version": "0.0.2-27",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",