@gbozee/ultimate 0.0.2-1 → 0.0.2-5

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;
@@ -302,6 +312,18 @@ export declare class AppDatabase {
302
312
  strategy_instance: Strategy;
303
313
  focus_account: ExchangeAccount;
304
314
  }>;
315
+ createOrUpdateWindingDownMarket(symbol: string): Promise<import("pocketbase").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;
@@ -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: {
package/dist/index.js CHANGED
@@ -31920,6 +31920,140 @@ 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").getFullList({
31925
+ filter: `symbol:lower="${symbol.toLowerCase()}"`
31926
+ });
31927
+ if (existing_winding_down_market.length > 0) {
31928
+ return existing_winding_down_market[0];
31929
+ }
31930
+ return await this.pb.collection("winding_down_markets").create({ symbol });
31931
+ }
31932
+ async getWindingDownMarkets() {
31933
+ return await this.pb.collection("winding_down_markets").getFullList();
31934
+ }
31935
+ async getBullishMarkets(options) {
31936
+ if (!options) {
31937
+ return {
31938
+ updated_bullish: await this.pb.collection("bullish_markets").getFullList(),
31939
+ moved_to_winding: []
31940
+ };
31941
+ }
31942
+ const { new_markets, totalRisk } = options;
31943
+ const newMarketSymbols = new Set(new_markets.map((m) => m.symbol));
31944
+ console.log(`Processing ${new_markets.length} new top movers with total risk ${totalRisk}`);
31945
+ let currentBullish = [];
31946
+ try {
31947
+ currentBullish = await this.pb.collection("bullish_markets").getFullList();
31948
+ console.log(`Found ${currentBullish.length} existing bullish markets.`);
31949
+ } catch (error) {
31950
+ console.error("Error fetching current bullish markets:", error);
31951
+ currentBullish = [];
31952
+ }
31953
+ const currentBullishSymbols = new Set(currentBullish.map((m) => m.symbol));
31954
+ const moved_to_winding = [];
31955
+ const recordsToDeleteFromBullish = [];
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...");
31967
+ for (const market of currentBullish) {
31968
+ if (!newMarketSymbols.has(market.symbol)) {
31969
+ console.log(`Processing ${market.symbol} for winding down.`);
31970
+ recordsToDeleteFromBullish.push(market.id);
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
+ }
31980
+ }
31981
+ }
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
+ }
31993
+ }
31994
+ console.log(`Finished sequential deletion. Successfully deleted ${deletedCount} of ${recordsToDeleteFromBullish.length} records.`);
31995
+ }
31996
+ let updatedCount = 0;
31997
+ let createdMarkets = [];
31998
+ const recordsToDeleteFromWindingDown = [];
31999
+ if (totalRisk <= 0) {
32000
+ console.warn("Total percent movement is zero or negative. Cannot allocate risk.");
32001
+ } else {
32002
+ for (const newMarket of new_markets) {
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
+ }
32012
+ const existingMarket = currentBullish.find((m) => m.symbol === newMarket.symbol);
32013
+ if (existingMarket && !recordsToDeleteFromBullish.includes(existingMarket.id)) {
32014
+ if (existingMarket.risk !== calculatedRisk) {
32015
+ console.log(`Updating existing bullish market sequentially: ${newMarket.symbol} with risk ${calculatedRisk}`);
32016
+ try {
32017
+ await this.pb.collection("bullish_markets").update(existingMarket.id, {
32018
+ risk: calculatedRisk
32019
+ });
32020
+ updatedCount++;
32021
+ } catch (updateError) {
32022
+ console.error(`Error updating bullish market ${newMarket.symbol} sequentially:`, updateError);
32023
+ }
32024
+ }
32025
+ } else if (!currentBullishSymbols.has(newMarket.symbol)) {
32026
+ console.log(`Creating new bullish market sequentially: ${newMarket.symbol} with risk ${calculatedRisk}`);
32027
+ try {
32028
+ const createdMarket = await this.pb.collection("bullish_markets").create({
32029
+ symbol: newMarket.symbol,
32030
+ risk: calculatedRisk
32031
+ });
32032
+ createdMarkets.push(createdMarket);
32033
+ } catch (createError) {
32034
+ console.error(`Error creating bullish market ${newMarket.symbol} sequentially:`, createError);
32035
+ }
32036
+ }
32037
+ }
32038
+ }
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.`);
32054
+ const updated_bullish = await this.pb.collection("bullish_markets").getFullList();
32055
+ return { updated_bullish, moved_to_winding };
32056
+ }
31923
32057
  }
31924
32058
 
31925
32059
  // src/exchanges/binance.ts
@@ -35426,6 +35560,12 @@ class App {
35426
35560
  });
35427
35561
  }
35428
35562
  }
35563
+ async updateTopMovers(payload) {
35564
+ return await this.app_db.getBullishMarkets(payload);
35565
+ }
35566
+ async getWindingDownMarkets() {
35567
+ return await this.app_db.getWindingDownMarkets();
35568
+ }
35429
35569
  }
35430
35570
  async function initApp(payload) {
35431
35571
  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-1",
4
+ "version": "0.0.2-5",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",