@gbozee/ultimate 0.0.2-0 → 0.0.2-3

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
@@ -2,11 +2,12 @@
2
2
 
3
3
  import { HttpsProxyAgent } from 'https-proxy-agent';
4
4
  import PocketBase from 'pocketbase';
5
+ import { RecordModel } from 'pocketbase';
5
6
  import { SocksProxyAgent } from 'socks-proxy-agent';
6
7
 
7
8
  interface Position$1 {
8
9
  id: number;
9
- kind: string;
10
+ kind: "long" | "short";
10
11
  entry: number;
11
12
  symbol: string;
12
13
  quantity: number;
@@ -16,6 +17,9 @@ interface Position$1 {
16
17
  take_profit: number;
17
18
  tp_quantity: number;
18
19
  stop_quantity: number;
20
+ target_pnl?: number;
21
+ reduce_ratio?: number;
22
+ use_full?: boolean;
19
23
  }
20
24
  export interface Account {
21
25
  id: number | string;
@@ -114,9 +118,6 @@ export interface BaseExchange {
114
118
  decimal_places?: string;
115
119
  }): Promise<any>;
116
120
  }
117
- /**
118
- * TypeScript type definitions for PocketBase collections and views
119
- */
120
121
  export interface BaseSystemFields {
121
122
  id: string;
122
123
  created: string;
@@ -204,6 +205,15 @@ export interface PositionsView {
204
205
  avg_liquidation?: any;
205
206
  balance?: any;
206
207
  }
208
+ export interface BullishMarket extends RecordModel {
209
+ id: string;
210
+ symbol: string;
211
+ risk: number;
212
+ }
213
+ export interface WindingDownMarket extends RecordModel {
214
+ id: string;
215
+ symbol: string;
216
+ }
207
217
  export type ExchangeType = {
208
218
  owner: string;
209
219
  exchange: string;
@@ -236,16 +246,16 @@ export declare class AppDatabase {
236
246
  config: ScheduledTrade;
237
247
  };
238
248
  })[]>;
239
- update_db_position(position: any, payload: any): Promise<import("pocketbase").RecordModel>;
249
+ update_db_position(position: any, payload: any): Promise<RecordModel>;
240
250
  getSymbolConfigFromDB(symbol: string): Promise<SymbolConfig>;
241
251
  getRunningInstanceFromDB(account: ExchangeType, symbol: string, options?: {
242
252
  delay?: number;
243
253
  }): Promise<TradeBlockTracking>;
244
- updateRunningInstance(id: string, running: boolean): Promise<import("pocketbase").RecordModel>;
254
+ updateRunningInstance(id: string, running: boolean): Promise<RecordModel>;
245
255
  getOrders(account: ExchangeType, options: {
246
256
  symbol: string;
247
257
  kind: "long" | "short";
248
- }): Promise<import("pocketbase").RecordModel[]>;
258
+ }): Promise<RecordModel[]>;
249
259
  deleteAndRecreateOrders(account: ExchangeType, options: {
250
260
  symbol: string;
251
261
  kind: "long" | "short";
@@ -258,7 +268,7 @@ export declare class AppDatabase {
258
268
  stop: number;
259
269
  order_id: string;
260
270
  triggerPrice?: number;
261
- }>): Promise<import("pocketbase").RecordModel[]>;
271
+ }>): Promise<RecordModel[]>;
262
272
  cancelOrders(payload: {
263
273
  cancelExchangeOrders: (payload: {
264
274
  symbol: string;
@@ -297,11 +307,23 @@ export declare class AppDatabase {
297
307
  symbol: string;
298
308
  kind: "long" | "short";
299
309
  account: ExchangeType;
300
- }): Promise<import("pocketbase").RecordModel>;
310
+ }): Promise<RecordModel>;
301
311
  getPositionStrategy(account: ExchangeType): Promise<{
302
312
  strategy_instance: Strategy;
303
313
  focus_account: ExchangeAccount;
304
314
  }>;
315
+ createOrUpdateWindingDownMarket(symbol: string): Promise<RecordModel>;
316
+ getWindingDownMarkets(): Promise<WindingDownMarket[]>;
317
+ getBullishMarkets(options?: {
318
+ new_markets: Array<{
319
+ symbol: string;
320
+ percent: number;
321
+ }>;
322
+ totalRisk: number;
323
+ }): Promise<{
324
+ updated_bullish: BullishMarket[];
325
+ moved_to_winding: WindingDownMarket[];
326
+ }>;
305
327
  }
306
328
  export interface CodeNode {
307
329
  minimum_pnl: number;
@@ -484,7 +506,7 @@ declare class ExchangeAccount$1 {
484
506
  declare class App {
485
507
  private app_db;
486
508
  private getCredentials;
487
- constructor(app_db: AppDatabase, getCredentials: (account: string, exchange: string) => (account: string) => Promise<{
509
+ constructor(app_db: AppDatabase, getCredentials: (account: string, exchange: string) => (account: string, exchange: string) => Promise<{
488
510
  api_key: string;
489
511
  api_secret: string;
490
512
  }>);
@@ -599,6 +621,17 @@ declare class App {
599
621
  kind: "long" | "short";
600
622
  revert?: boolean;
601
623
  }): Promise<import("pocketbase").RecordModel[]>;
624
+ updateTopMovers(payload?: {
625
+ new_markets: {
626
+ symbol: string;
627
+ percent: number;
628
+ }[];
629
+ totalRisk: number;
630
+ }): Promise<{
631
+ updated_bullish: BullishMarket[];
632
+ moved_to_winding: WindingDownMarket[];
633
+ }>;
634
+ getWindingDownMarkets(): Promise<WindingDownMarket[]>;
602
635
  }
603
636
  export declare function initApp(payload: {
604
637
  db: {
@@ -606,7 +639,7 @@ export declare function initApp(payload: {
606
639
  email: string;
607
640
  password: string;
608
641
  };
609
- getCredentials: (account: string, exchange: string) => (account: string) => Promise<{
642
+ getCredentials: (account: string, exchange: string) => (account: string, exchange: string) => Promise<{
610
643
  api_key: string;
611
644
  api_secret: string;
612
645
  }>;
package/dist/index.js CHANGED
@@ -31920,6 +31920,100 @@ class AppDatabase {
31920
31920
  }
31921
31921
  return null;
31922
31922
  }
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;
31927
+ }
31928
+ return await this.pb.collection("winding_down_markets").create({ symbol });
31929
+ }
31930
+ async getWindingDownMarkets() {
31931
+ return await this.pb.collection("winding_down_markets").getFullList();
31932
+ }
31933
+ async getBullishMarkets(options) {
31934
+ if (!options) {
31935
+ return {
31936
+ updated_bullish: await this.pb.collection("bullish_markets").getFullList(),
31937
+ moved_to_winding: []
31938
+ };
31939
+ }
31940
+ const { new_markets, totalRisk } = options;
31941
+ const newMarketSymbols = new Set(new_markets.map((m) => m.symbol));
31942
+ console.log(`Processing ${new_markets.length} new top movers with total risk ${totalRisk}`);
31943
+ let currentBullish = [];
31944
+ try {
31945
+ currentBullish = await this.pb.collection("bullish_markets").getFullList();
31946
+ console.log(`Found ${currentBullish.length} existing bullish markets.`);
31947
+ } catch (error) {
31948
+ console.error("Error fetching current bullish markets:", error);
31949
+ currentBullish = [];
31950
+ }
31951
+ const currentBullishSymbols = new Set(currentBullish.map((m) => m.symbol));
31952
+ const moved_to_winding = [];
31953
+ const recordsToDeleteFromBullish = [];
31954
+ const windDownPromises = [];
31955
+ for (const market of currentBullish) {
31956
+ if (!newMarketSymbols.has(market.symbol)) {
31957
+ console.log(`Marking ${market.symbol} for winding down.`);
31958
+ recordsToDeleteFromBullish.push(market.id);
31959
+ windDownPromises.push(this.createOrUpdateWindingDownMarket(market.symbol));
31960
+ }
31961
+ }
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);
31975
+ }
31976
+ }
31977
+ const totalPercent = new_markets.reduce((sum, market) => sum + (market.percent || 0), 0);
31978
+ let updatedCount = 0;
31979
+ const createdMarkets = [];
31980
+ if (totalPercent <= 0) {
31981
+ console.warn("Total percent movement is zero or negative. Cannot allocate risk.");
31982
+ } else {
31983
+ for (const newMarket of new_markets) {
31984
+ const calculatedRisk = newMarket.percent / totalPercent * totalRisk;
31985
+ const existingMarket = currentBullish.find((m) => m.symbol === newMarket.symbol);
31986
+ if (existingMarket && !recordsToDeleteFromBullish.includes(existingMarket.id)) {
31987
+ if (existingMarket.risk !== calculatedRisk) {
31988
+ console.log(`Updating existing bullish market sequentially: ${newMarket.symbol} with risk ${calculatedRisk}`);
31989
+ try {
31990
+ await this.pb.collection("bullish_markets").update(existingMarket.id, {
31991
+ risk: calculatedRisk
31992
+ });
31993
+ updatedCount++;
31994
+ } catch (updateError) {
31995
+ console.error(`Error updating bullish market ${newMarket.symbol} sequentially:`, updateError);
31996
+ }
31997
+ }
31998
+ } else if (!currentBullishSymbols.has(newMarket.symbol)) {
31999
+ console.log(`Creating new bullish market sequentially: ${newMarket.symbol} with risk ${calculatedRisk}`);
32000
+ try {
32001
+ const createdMarket = await this.pb.collection("bullish_markets").create({
32002
+ symbol: newMarket.symbol,
32003
+ risk: calculatedRisk
32004
+ });
32005
+ createdMarkets.push(createdMarket);
32006
+ } catch (createError) {
32007
+ console.error(`Error creating bullish market ${newMarket.symbol} sequentially:`, createError);
32008
+ }
32009
+ }
32010
+ }
32011
+ }
32012
+ console.log(`Successfully updated ${updatedCount} markets sequentially.`);
32013
+ console.log(`Successfully created ${createdMarkets.length} markets sequentially.`);
32014
+ const updated_bullish = await this.pb.collection("bullish_markets").getFullList();
32015
+ return { updated_bullish, moved_to_winding };
32016
+ }
31923
32017
  }
31924
32018
 
31925
32019
  // src/exchanges/binance.ts
@@ -35084,7 +35178,7 @@ function getExchangeKlass(exchange) {
35084
35178
  const func = exchange === "binance" ? BinanceExchange : BybitExchange;
35085
35179
  const clientFunc = exchange === "binance" ? initClient : initClient2;
35086
35180
  return async (payload) => {
35087
- const credentials = await payload.getCredentials(payload.account);
35181
+ const credentials = await payload.getCredentials(payload.account, exchange);
35088
35182
  const client = await clientFunc(credentials, {
35089
35183
  type: "future",
35090
35184
  proxyAgent: payload.proxyAgent
@@ -35426,6 +35520,12 @@ class App {
35426
35520
  });
35427
35521
  }
35428
35522
  }
35523
+ async updateTopMovers(payload) {
35524
+ return await this.app_db.getBullishMarkets(payload);
35525
+ }
35526
+ async getWindingDownMarkets() {
35527
+ return await this.app_db.getWindingDownMarkets();
35528
+ }
35429
35529
  }
35430
35530
  async function initApp(payload) {
35431
35531
  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-0",
4
+ "version": "0.0.2-3",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -12,9 +12,11 @@
12
12
  "require": "./dist/index.cjs"
13
13
  },
14
14
  "scripts": {
15
+ "yarn:publish": "yarn publish --prerelease",
15
16
  "build": "bun run build.ts",
16
17
  "prepublishOnly": "bun run build",
17
- "test": "bun test --timeout 300000 test"
18
+ "test": "bun test --timeout 300000 test",
19
+ "npm:publish": "npm publish --access public --registry=https://registry.npmjs.org/"
18
20
  },
19
21
  "files": [
20
22
  "dist"