@gbozee/ultimate 0.0.2-3 → 0.0.2-7

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
@@ -117,6 +117,10 @@ export interface BaseExchange {
117
117
  price_places?: string;
118
118
  decimal_places?: string;
119
119
  }): Promise<any>;
120
+ setLeverage(payload: {
121
+ symbol: string;
122
+ leverage: number;
123
+ }): Promise<any>;
120
124
  }
121
125
  export interface BaseSystemFields {
122
126
  id: string;
@@ -246,16 +250,16 @@ export declare class AppDatabase {
246
250
  config: ScheduledTrade;
247
251
  };
248
252
  })[]>;
249
- update_db_position(position: any, payload: any): Promise<RecordModel>;
253
+ update_db_position(position: any, payload: any): Promise<import("pocketbase").RecordModel>;
250
254
  getSymbolConfigFromDB(symbol: string): Promise<SymbolConfig>;
251
255
  getRunningInstanceFromDB(account: ExchangeType, symbol: string, options?: {
252
256
  delay?: number;
253
257
  }): Promise<TradeBlockTracking>;
254
- updateRunningInstance(id: string, running: boolean): Promise<RecordModel>;
258
+ updateRunningInstance(id: string, running: boolean): Promise<import("pocketbase").RecordModel>;
255
259
  getOrders(account: ExchangeType, options: {
256
260
  symbol: string;
257
261
  kind: "long" | "short";
258
- }): Promise<RecordModel[]>;
262
+ }): Promise<import("pocketbase").RecordModel[]>;
259
263
  deleteAndRecreateOrders(account: ExchangeType, options: {
260
264
  symbol: string;
261
265
  kind: "long" | "short";
@@ -268,7 +272,7 @@ export declare class AppDatabase {
268
272
  stop: number;
269
273
  order_id: string;
270
274
  triggerPrice?: number;
271
- }>): Promise<RecordModel[]>;
275
+ }>): Promise<import("pocketbase").RecordModel[]>;
272
276
  cancelOrders(payload: {
273
277
  cancelExchangeOrders: (payload: {
274
278
  symbol: string;
@@ -307,12 +311,12 @@ export declare class AppDatabase {
307
311
  symbol: string;
308
312
  kind: "long" | "short";
309
313
  account: ExchangeType;
310
- }): Promise<RecordModel>;
314
+ }): Promise<import("pocketbase").RecordModel>;
311
315
  getPositionStrategy(account: ExchangeType): Promise<{
312
316
  strategy_instance: Strategy;
313
317
  focus_account: ExchangeAccount;
314
318
  }>;
315
- createOrUpdateWindingDownMarket(symbol: string): Promise<RecordModel>;
319
+ createOrUpdateWindingDownMarket(symbol: string): Promise<import("pocketbase").RecordModel>;
316
320
  getWindingDownMarkets(): Promise<WindingDownMarket[]>;
317
321
  getBullishMarkets(options?: {
318
322
  new_markets: Array<{
@@ -324,6 +328,21 @@ export declare class AppDatabase {
324
328
  updated_bullish: BullishMarket[];
325
329
  moved_to_winding: WindingDownMarket[];
326
330
  }>;
331
+ updateSymbolConfigs(payload?: {
332
+ configs: Array<{
333
+ symbol: string;
334
+ support: number;
335
+ leverage: number;
336
+ min_size: number;
337
+ resistance: number;
338
+ price_places: string;
339
+ decimal_places: string;
340
+ }>;
341
+ }): Promise<{
342
+ updated: number;
343
+ created: number;
344
+ } | SymbolConfig[]>;
345
+ unwindSymbolFromDB(symbol: string): Promise<void>;
327
346
  }
328
347
  export interface CodeNode {
329
348
  minimum_pnl: number;
@@ -383,6 +402,7 @@ declare class ExchangeAccount$1 {
383
402
  kind?: "long" | "short";
384
403
  update?: boolean;
385
404
  as_view?: boolean;
405
+ leverage?: number;
386
406
  }): Promise<(PositionsView & {
387
407
  expand?: {
388
408
  config: ScheduledTrade;
@@ -632,6 +652,20 @@ declare class App {
632
652
  moved_to_winding: WindingDownMarket[];
633
653
  }>;
634
654
  getWindingDownMarkets(): Promise<WindingDownMarket[]>;
655
+ updateSymbolConfigs(payload: {
656
+ configs: {
657
+ symbol: string;
658
+ support: number;
659
+ leverage: number;
660
+ min_size: number;
661
+ resistance: number;
662
+ price_places: string;
663
+ decimal_places: string;
664
+ }[];
665
+ }): Promise<{
666
+ updated: number;
667
+ created: number;
668
+ } | SymbolConfig[]>;
635
669
  }
636
670
  export declare function initApp(payload: {
637
671
  db: {
package/dist/index.js CHANGED
@@ -31921,9 +31921,11 @@ class AppDatabase {
31921
31921
  return null;
31922
31922
  }
31923
31923
  async createOrUpdateWindingDownMarket(symbol) {
31924
- const existing_winding_down_market = await this.pb.collection("winding_down_markets").getFirstListItem(`symbol:lower="${symbol.toLowerCase()}"`);
31925
- if (existing_winding_down_market) {
31926
- return existing_winding_down_market;
31924
+ const existing_winding_down_market = await this.pb.collection("winding_down_markets").getFullList({
31925
+ filter: `symbol:lower="${symbol.toLowerCase()}"`
31926
+ });
31927
+ if (existing_winding_down_market.length > 0) {
31928
+ return existing_winding_down_market[0];
31927
31929
  }
31928
31930
  return await this.pb.collection("winding_down_markets").create({ symbol });
31929
31931
  }
@@ -31951,37 +31953,62 @@ class AppDatabase {
31951
31953
  const currentBullishSymbols = new Set(currentBullish.map((m) => m.symbol));
31952
31954
  const moved_to_winding = [];
31953
31955
  const recordsToDeleteFromBullish = [];
31954
- const windDownPromises = [];
31956
+ let windDownCreatedCount = 0;
31957
+ let currentWindingDown = [];
31958
+ let currentWindingDownSymbols = new Set;
31959
+ try {
31960
+ currentWindingDown = await this.getWindingDownMarkets();
31961
+ currentWindingDownSymbols = new Set(currentWindingDown.map((m) => m.symbol));
31962
+ console.log(`Found ${currentWindingDown.length} existing winding down markets.`);
31963
+ } catch (error) {
31964
+ console.error("Error fetching current winding down markets:", error);
31965
+ }
31966
+ console.log("Processing markets to potentially move to winding down...");
31955
31967
  for (const market of currentBullish) {
31956
31968
  if (!newMarketSymbols.has(market.symbol)) {
31957
- console.log(`Marking ${market.symbol} for winding down.`);
31969
+ console.log(`Processing ${market.symbol} for winding down.`);
31958
31970
  recordsToDeleteFromBullish.push(market.id);
31959
- windDownPromises.push(this.createOrUpdateWindingDownMarket(market.symbol));
31971
+ try {
31972
+ const windingDownRecord = await this.createOrUpdateWindingDownMarket(market.symbol);
31973
+ if (windingDownRecord) {
31974
+ moved_to_winding.push(windingDownRecord);
31975
+ windDownCreatedCount++;
31976
+ }
31977
+ } catch (windDownError) {
31978
+ console.error(`Error creating/updating winding down market for ${market.symbol} sequentially:`, windDownError);
31979
+ }
31960
31980
  }
31961
31981
  }
31962
- if (windDownPromises.length > 0) {
31963
- try {
31964
- const createdWindingDown = await Promise.all(windDownPromises);
31965
- moved_to_winding.push(...createdWindingDown);
31966
- console.log(`Successfully created ${moved_to_winding.length} records in winding_down_markets.`);
31967
- const deletePromises = [];
31968
- for (const id of recordsToDeleteFromBullish) {
31969
- deletePromises.push(this.pb.collection("bullish_markets").delete(id));
31970
- }
31971
- await Promise.all(deletePromises);
31972
- console.log(`Successfully deleted ${recordsToDeleteFromBullish.length} records from bullish_markets.`);
31973
- } catch (error) {
31974
- console.error("Error moving markets to winding down:", error);
31982
+ console.log(`Finished processing potential winding down markets. ${windDownCreatedCount} processed.`);
31983
+ let deletedCount = 0;
31984
+ if (recordsToDeleteFromBullish.length > 0) {
31985
+ console.log(`Attempting to delete ${recordsToDeleteFromBullish.length} records sequentially from bullish_markets.`);
31986
+ for (const id of recordsToDeleteFromBullish) {
31987
+ try {
31988
+ await this.pb.collection("bullish_markets").delete(id);
31989
+ deletedCount++;
31990
+ } catch (deleteError) {
31991
+ console.error(`Error deleting bullish market record ${id} sequentially:`, deleteError);
31992
+ }
31975
31993
  }
31994
+ console.log(`Finished sequential deletion. Successfully deleted ${deletedCount} of ${recordsToDeleteFromBullish.length} records.`);
31976
31995
  }
31977
- const totalPercent = new_markets.reduce((sum, market) => sum + (market.percent || 0), 0);
31978
31996
  let updatedCount = 0;
31979
- const createdMarkets = [];
31980
- if (totalPercent <= 0) {
31997
+ let createdMarkets = [];
31998
+ const recordsToDeleteFromWindingDown = [];
31999
+ if (totalRisk <= 0) {
31981
32000
  console.warn("Total percent movement is zero or negative. Cannot allocate risk.");
31982
32001
  } else {
31983
32002
  for (const newMarket of new_markets) {
31984
- const calculatedRisk = newMarket.percent / totalPercent * totalRisk;
32003
+ const calculatedRisk = newMarket.percent / totalRisk * totalRisk;
32004
+ if (currentWindingDownSymbols.has(newMarket.symbol)) {
32005
+ console.log(`Marking ${newMarket.symbol} for removal from winding down markets as it is now bullish.`);
32006
+ const windingDownRecord = currentWindingDown.find((w) => w.symbol === newMarket.symbol);
32007
+ if (windingDownRecord) {
32008
+ recordsToDeleteFromWindingDown.push(windingDownRecord.id);
32009
+ currentWindingDownSymbols.delete(newMarket.symbol);
32010
+ }
32011
+ }
31985
32012
  const existingMarket = currentBullish.find((m) => m.symbol === newMarket.symbol);
31986
32013
  if (existingMarket && !recordsToDeleteFromBullish.includes(existingMarket.id)) {
31987
32014
  if (existingMarket.risk !== calculatedRisk) {
@@ -32009,11 +32036,98 @@ class AppDatabase {
32009
32036
  }
32010
32037
  }
32011
32038
  }
32012
- console.log(`Successfully updated ${updatedCount} markets sequentially.`);
32013
- console.log(`Successfully created ${createdMarkets.length} markets sequentially.`);
32039
+ let windingDownDeletedCount = 0;
32040
+ if (recordsToDeleteFromWindingDown.length > 0) {
32041
+ console.log(`Attempting to delete ${recordsToDeleteFromWindingDown.length} records sequentially from winding_down_markets.`);
32042
+ for (const id of recordsToDeleteFromWindingDown) {
32043
+ try {
32044
+ await this.pb.collection("winding_down_markets").delete(id);
32045
+ windingDownDeletedCount++;
32046
+ } catch (deleteError) {
32047
+ console.error(`Error deleting winding down market record ${id} sequentially:`, deleteError);
32048
+ }
32049
+ }
32050
+ console.log(`Finished sequential deletion from winding down. Successfully deleted ${windingDownDeletedCount} of ${recordsToDeleteFromWindingDown.length} records.`);
32051
+ }
32052
+ console.log(`Successfully updated ${updatedCount} bullish markets sequentially.`);
32053
+ console.log(`Successfully created ${createdMarkets.length} bullish markets sequentially.`);
32014
32054
  const updated_bullish = await this.pb.collection("bullish_markets").getFullList();
32015
32055
  return { updated_bullish, moved_to_winding };
32016
32056
  }
32057
+ async updateSymbolConfigs(payload) {
32058
+ if (!payload || !payload.configs) {
32059
+ console.log("No payload provided. Fetching all symbol configs...");
32060
+ try {
32061
+ const allConfigs = await this.pb.collection("symbol_configs").getFullList();
32062
+ console.log(`Found ${allConfigs.length} symbol configs.`);
32063
+ return allConfigs;
32064
+ } catch (error) {
32065
+ console.error("Error fetching all symbol configs:", error);
32066
+ throw error;
32067
+ }
32068
+ }
32069
+ console.log(`Processing ${payload.configs.length} symbol config updates/creates...`);
32070
+ let existingConfigsMap = new Map;
32071
+ try {
32072
+ const existingConfigsList = await this.pb.collection("symbol_configs").getFullList();
32073
+ existingConfigsMap = new Map(existingConfigsList.map((c) => [c.symbol.toLowerCase(), c]));
32074
+ console.log(`Found ${existingConfigsMap.size} existing symbol configs in DB.`);
32075
+ } catch (error) {
32076
+ console.error("Error fetching existing symbol configs:", error);
32077
+ }
32078
+ let updatedCount = 0;
32079
+ let createdCount = 0;
32080
+ for (const config of payload.configs) {
32081
+ const lowerCaseSymbol = config.symbol.toLowerCase();
32082
+ const existingConfig = existingConfigsMap.get(lowerCaseSymbol);
32083
+ const dataToUpsert = {
32084
+ symbol: config.symbol,
32085
+ support: config.support,
32086
+ resistance: config.resistance,
32087
+ leverage: config.leverage,
32088
+ min_size: config.min_size,
32089
+ price_places: config.price_places,
32090
+ decimal_places: config.decimal_places
32091
+ };
32092
+ if (existingConfig) {
32093
+ try {
32094
+ if (Object.keys(dataToUpsert).some((key) => dataToUpsert[key] !== existingConfig[key])) {
32095
+ console.log(`Updating symbol config for: ${config.symbol}`);
32096
+ await this.pb.collection("symbol_configs").update(existingConfig.id, dataToUpsert);
32097
+ updatedCount++;
32098
+ } else {
32099
+ console.log(`No changes detected for symbol config: ${config.symbol}. Skipping update.`);
32100
+ }
32101
+ } catch (updateError) {
32102
+ console.error(`Error updating symbol config for ${config.symbol}:`, updateError);
32103
+ }
32104
+ } else {
32105
+ try {
32106
+ console.log(`Creating new symbol config for: ${config.symbol}`);
32107
+ await this.pb.collection("symbol_configs").create(dataToUpsert);
32108
+ createdCount++;
32109
+ } catch (createError) {
32110
+ console.error(`Error creating symbol config for ${config.symbol}:`, createError);
32111
+ }
32112
+ }
32113
+ }
32114
+ console.log(`Finished processing symbol configs. Updated: ${updatedCount}, Created: ${createdCount}`);
32115
+ return { updated: updatedCount, created: createdCount };
32116
+ }
32117
+ async unwindSymbolFromDB(symbol) {
32118
+ const symbol_configs = await this.pb.collection("symbol_configs").getFullList({
32119
+ filter: `symbol:lower="${symbol.toLowerCase()}"`
32120
+ });
32121
+ if (symbol_configs.length > 0) {
32122
+ await this.pb.collection("symbol_configs").delete(symbol_configs[0].id);
32123
+ }
32124
+ const found_unwinding_market = await this.pb.collection("unwinding_markets").getFullList({
32125
+ filter: `symbol:lower="${symbol.toLowerCase()}"`
32126
+ });
32127
+ if (found_unwinding_market.length > 0) {
32128
+ await this.pb.collection("unwinding_markets").delete(found_unwinding_market[0].id);
32129
+ }
32130
+ }
32017
32131
  }
32018
32132
 
32019
32133
  // src/exchanges/binance.ts
@@ -33584,6 +33698,9 @@ class BinanceExchange {
33584
33698
  decimal_places: payload.decimal_places
33585
33699
  });
33586
33700
  }
33701
+ async setLeverage(payload) {
33702
+ return await this.client.setLeverage(payload);
33703
+ }
33587
33704
  }
33588
33705
 
33589
33706
  // src/exchanges/bybit.ts
@@ -34188,6 +34305,14 @@ class BybitExchange {
34188
34305
  decimal_places: payload.decimal_places
34189
34306
  });
34190
34307
  }
34308
+ async setLeverage(payload) {
34309
+ return await this.client.setLeverage({
34310
+ category: "linear",
34311
+ symbol: payload.symbol,
34312
+ buyLeverage: payload.leverage.toString(),
34313
+ sellLeverage: payload.leverage.toString()
34314
+ });
34315
+ }
34191
34316
  }
34192
34317
 
34193
34318
  // src/helpers/accounts.ts
@@ -34873,6 +34998,9 @@ class ExchangeAccount {
34873
34998
  return db_positions2;
34874
34999
  }
34875
35000
  const active_account = await this.getActiveAccount(symbol);
35001
+ if (options.leverage) {
35002
+ await this.exchange.setLeverage({ symbol, leverage: options.leverage });
35003
+ }
34876
35004
  const db_positions = await this.app_db.createOrUpdatePositions(this.instance, {
34877
35005
  symbol,
34878
35006
  long_position: active_account.position.long,
@@ -35526,6 +35654,9 @@ class App {
35526
35654
  async getWindingDownMarkets() {
35527
35655
  return await this.app_db.getWindingDownMarkets();
35528
35656
  }
35657
+ async updateSymbolConfigs(payload) {
35658
+ return await this.app_db.updateSymbolConfigs(payload);
35659
+ }
35529
35660
  }
35530
35661
  async function initApp(payload) {
35531
35662
  const pb = await initPocketBaseClient(payload.db);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-3",
4
+ "version": "0.0.2-7",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",