@gbozee/ultimate 0.0.2-24 → 0.0.2-26
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 +19 -1
- package/dist/index.js +94 -26
- package/package.json +1 -1
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(
|
|
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,6 +795,8 @@ 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 {
|
|
@@ -891,6 +900,15 @@ declare class App {
|
|
|
891
900
|
account: ExchangeType;
|
|
892
901
|
}) => Promise<any>;
|
|
893
902
|
}): Promise<void>;
|
|
903
|
+
placeTrade(payload: {
|
|
904
|
+
account: ExchangeType;
|
|
905
|
+
symbol: string;
|
|
906
|
+
kind: "long" | "short";
|
|
907
|
+
place?: boolean;
|
|
908
|
+
tp?: boolean;
|
|
909
|
+
cancel?: boolean;
|
|
910
|
+
raw?: boolean;
|
|
911
|
+
}): Promise<any>;
|
|
894
912
|
}
|
|
895
913
|
export declare function initApp(payload: {
|
|
896
914
|
db: {
|
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(
|
|
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
|
|
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(
|
|
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 /
|
|
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);
|
|
@@ -34552,7 +34562,7 @@ class BinanceExchange {
|
|
|
34552
34562
|
});
|
|
34553
34563
|
const _activeSymbols = await getActiveSymbols({ client: this.client });
|
|
34554
34564
|
const activeSymbols = _activeSymbols.map((s2) => s2.symbol);
|
|
34555
|
-
const delisted = movers.map((x) => x.symbol).filter((symbol) => !activeSymbols.includes(symbol));
|
|
34565
|
+
const delisted = payload.include_delisted ? [] : movers.map((x) => x.symbol).filter((symbol) => !activeSymbols.includes(symbol));
|
|
34556
34566
|
const validMovers = movers.filter((m) => !delisted.includes(m.symbol)).filter((m) => activeSymbols.includes(m.symbol));
|
|
34557
34567
|
const activeMovers = [];
|
|
34558
34568
|
const isActivePromises = validMovers.map(async (m) => {
|
|
@@ -34599,6 +34609,10 @@ class BinanceExchange {
|
|
|
34599
34609
|
}
|
|
34600
34610
|
return [];
|
|
34601
34611
|
}
|
|
34612
|
+
async getOpenPositions() {
|
|
34613
|
+
const response = await this.client.getPositionsV3();
|
|
34614
|
+
return Array.from(new Set(response.map((x) => x.symbol)));
|
|
34615
|
+
}
|
|
34602
34616
|
}
|
|
34603
34617
|
function getPricePlaces(target) {
|
|
34604
34618
|
const numStr = target.toString();
|
|
@@ -35247,6 +35261,9 @@ class BybitExchange {
|
|
|
35247
35261
|
async getAllOpenSymbols() {
|
|
35248
35262
|
return [];
|
|
35249
35263
|
}
|
|
35264
|
+
async getOpenPositions() {
|
|
35265
|
+
return [];
|
|
35266
|
+
}
|
|
35250
35267
|
async createLimitPurchaseOrders(payload) {
|
|
35251
35268
|
const {
|
|
35252
35269
|
orders,
|
|
@@ -36959,6 +36976,9 @@ class ExchangeAccount {
|
|
|
36959
36976
|
return false;
|
|
36960
36977
|
}
|
|
36961
36978
|
const db_instance = await this.app_db.get_exchange_db_instance(this.instance);
|
|
36979
|
+
if (!db_instance.bullish) {
|
|
36980
|
+
return false;
|
|
36981
|
+
}
|
|
36962
36982
|
if (db_instance.profit_percent) {
|
|
36963
36983
|
profit_percent = db_instance.profit_percent;
|
|
36964
36984
|
}
|
|
@@ -36970,18 +36990,21 @@ class ExchangeAccount {
|
|
|
36970
36990
|
const long_config = await this.buildConfigForSymbol({
|
|
36971
36991
|
symbol,
|
|
36972
36992
|
risk: bullish_instance.risk,
|
|
36973
|
-
risk_reward
|
|
36993
|
+
risk_reward: db_instance.risk_reward || risk_reward
|
|
36974
36994
|
});
|
|
36975
36995
|
let changed = false;
|
|
36976
36996
|
if (!position2?.config) {
|
|
36977
|
-
await this.
|
|
36978
|
-
entry: long_config.entry,
|
|
36979
|
-
stop: long_config.stop,
|
|
36980
|
-
risk_reward: long_config.risk_reward,
|
|
36981
|
-
risk: long_config.risk,
|
|
36997
|
+
await this.getPositionConfig({
|
|
36982
36998
|
symbol,
|
|
36983
|
-
|
|
36984
|
-
|
|
36999
|
+
kind: "long",
|
|
37000
|
+
params: {
|
|
37001
|
+
entry: long_config.entry,
|
|
37002
|
+
stop: long_config.stop,
|
|
37003
|
+
risk_reward: long_config.risk_reward,
|
|
37004
|
+
risk: long_config.risk,
|
|
37005
|
+
profit_percent,
|
|
37006
|
+
place_tp: true
|
|
37007
|
+
}
|
|
36985
37008
|
});
|
|
36986
37009
|
changed = true;
|
|
36987
37010
|
} else {
|
|
@@ -37048,12 +37071,28 @@ class ExchangeAccount {
|
|
|
37048
37071
|
no_position: true,
|
|
37049
37072
|
kind: "long"
|
|
37050
37073
|
});
|
|
37051
|
-
const all_open_symbols = await this.exchange.
|
|
37074
|
+
const all_open_symbols = await this.exchange.getOpenPositions();
|
|
37052
37075
|
await new Promise((resolve) => setTimeout(resolve, interval * 1000));
|
|
37053
37076
|
for (const symbol of Array.from(new Set(symbols.concat(all_open_symbols)))) {
|
|
37054
37077
|
await this.getLiveExchangeInstance({ symbol, refresh: true });
|
|
37055
37078
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
37056
37079
|
}
|
|
37080
|
+
for (const symbol of all_open_symbols) {
|
|
37081
|
+
await this.syncAccount({
|
|
37082
|
+
symbol,
|
|
37083
|
+
update: true
|
|
37084
|
+
});
|
|
37085
|
+
await this.syncOrders({
|
|
37086
|
+
symbol,
|
|
37087
|
+
kind: "long",
|
|
37088
|
+
update: true
|
|
37089
|
+
});
|
|
37090
|
+
await this.syncOrders({
|
|
37091
|
+
symbol,
|
|
37092
|
+
kind: "short",
|
|
37093
|
+
update: true
|
|
37094
|
+
});
|
|
37095
|
+
}
|
|
37057
37096
|
}
|
|
37058
37097
|
async updateAllPositionsWithNoConfig(payload) {
|
|
37059
37098
|
const { kind } = payload;
|
|
@@ -37083,7 +37122,7 @@ class ExchangeAccount {
|
|
|
37083
37122
|
}
|
|
37084
37123
|
async getNonEssentialSymbols() {
|
|
37085
37124
|
const [all_open_symbols, essential_symbols] = await Promise.all([
|
|
37086
|
-
this.exchange.
|
|
37125
|
+
this.exchange.getOpenPositions(),
|
|
37087
37126
|
this.app_db.getAllSymbolConfigs({
|
|
37088
37127
|
custom_filter: `essential = true`
|
|
37089
37128
|
})
|
|
@@ -37095,15 +37134,16 @@ class ExchangeAccount {
|
|
|
37095
37134
|
const bullish_symbols = Array.from(new Set(bullish_markets.updated_bullish.map((m) => m.symbol)));
|
|
37096
37135
|
const symbols = Array.from(new Set(essential_symbols.map((s2) => s2.symbol))).concat(bullish_symbols);
|
|
37097
37136
|
const not_symbols_filter = symbols.map((s2) => `symbol:lower != "${s2.toLowerCase()}"`).join(" && ");
|
|
37137
|
+
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
37138
|
const positions = await this.app_db.getPositions({
|
|
37099
|
-
custom_filter:
|
|
37139
|
+
custom_filter: filters,
|
|
37100
37140
|
symbol: "",
|
|
37101
37141
|
account: {
|
|
37102
37142
|
owner: "",
|
|
37103
37143
|
exchange: ""
|
|
37104
37144
|
}
|
|
37105
37145
|
});
|
|
37106
|
-
return Array.from(new Set(positions.map((p) => p.symbol).concat(non_essential_symbols)));
|
|
37146
|
+
return Array.from(new Set(positions.map((p) => p.symbol).concat(non_essential_symbols))).filter((k) => !["BTCUSDT", "BTCUSDC"].includes(k));
|
|
37107
37147
|
}
|
|
37108
37148
|
async _terminatePositions(payload) {
|
|
37109
37149
|
const { symbol } = payload;
|
|
@@ -37301,6 +37341,9 @@ class ExchangeAccount {
|
|
|
37301
37341
|
symbol,
|
|
37302
37342
|
kind: "short"
|
|
37303
37343
|
});
|
|
37344
|
+
await this.app_db.update_db_position(short_position, {
|
|
37345
|
+
config: null
|
|
37346
|
+
});
|
|
37304
37347
|
}
|
|
37305
37348
|
if (long_position && short_position && long_position.quantity === 0 && short_position.quantity === 0) {
|
|
37306
37349
|
await this.cancelOrders({
|
|
@@ -37322,26 +37365,31 @@ class ExchangeAccount {
|
|
|
37322
37365
|
const db_instance = await this.app_db.get_exchange_db_instance(this.instance);
|
|
37323
37366
|
const {
|
|
37324
37367
|
bullish,
|
|
37325
|
-
bearish
|
|
37368
|
+
bearish,
|
|
37326
37369
|
movePercent,
|
|
37327
37370
|
totalRisk,
|
|
37328
37371
|
max_non_essential = 0,
|
|
37329
|
-
exclude_coins
|
|
37372
|
+
exclude_coins,
|
|
37373
|
+
include_delisted = false
|
|
37330
37374
|
} = db_instance;
|
|
37331
37375
|
let dont_trade = exclude_coins?.bullish || [];
|
|
37332
37376
|
let bullishMarkets = [];
|
|
37333
37377
|
if (bullish) {
|
|
37334
37378
|
const { movers } = await this.exchange.checkDelistedMovers({
|
|
37335
|
-
movePercent
|
|
37379
|
+
movePercent,
|
|
37380
|
+
include_delisted
|
|
37336
37381
|
});
|
|
37337
37382
|
bullishMarkets = movers;
|
|
37338
37383
|
}
|
|
37339
37384
|
const non_essential_symbols = await this.getNonEssentialSymbols();
|
|
37340
37385
|
let symbols_to_remove = non_essential_symbols.filter((k) => !bullishMarkets.map((m) => m.symbol).includes(k)).slice(0, max_non_essential).concat(dont_trade);
|
|
37386
|
+
symbols_to_remove = Array.from(new Set(symbols_to_remove));
|
|
37341
37387
|
bullishMarkets = bullishMarkets.filter((m) => !symbols_to_remove.includes(m.symbol));
|
|
37342
|
-
if (
|
|
37343
|
-
|
|
37344
|
-
|
|
37388
|
+
if (bearish) {
|
|
37389
|
+
if (symbols_to_remove.length > 0) {
|
|
37390
|
+
for (const symbol of symbols_to_remove) {
|
|
37391
|
+
await this.terminatePositions({ symbol });
|
|
37392
|
+
}
|
|
37345
37393
|
}
|
|
37346
37394
|
}
|
|
37347
37395
|
const result = await this.app_db.getBullishMarkets({
|
|
@@ -37352,7 +37400,7 @@ class ExchangeAccount {
|
|
|
37352
37400
|
totalRisk,
|
|
37353
37401
|
max_count: max_non_essential
|
|
37354
37402
|
});
|
|
37355
|
-
if (result.updated_bullish.length > max_non_essential) {
|
|
37403
|
+
if (result.updated_bullish.length > max_non_essential || !bullish) {
|
|
37356
37404
|
return {
|
|
37357
37405
|
updated_bullish: [],
|
|
37358
37406
|
moved_to_winding: []
|
|
@@ -37393,11 +37441,18 @@ class ExchangeAccount {
|
|
|
37393
37441
|
return 0;
|
|
37394
37442
|
}
|
|
37395
37443
|
async placeTrade(payload) {
|
|
37396
|
-
const { symbol, kind, place, tp } = payload;
|
|
37444
|
+
const { symbol, kind, place, tp, raw: _raw, cancel } = payload;
|
|
37445
|
+
if (cancel) {
|
|
37446
|
+
await this.cancelOrders({
|
|
37447
|
+
symbol,
|
|
37448
|
+
kind
|
|
37449
|
+
});
|
|
37450
|
+
}
|
|
37397
37451
|
if (place) {
|
|
37398
37452
|
return await this.triggerTradeFromConfig({
|
|
37399
37453
|
symbol,
|
|
37400
|
-
kind
|
|
37454
|
+
kind,
|
|
37455
|
+
raw: payload.raw
|
|
37401
37456
|
});
|
|
37402
37457
|
}
|
|
37403
37458
|
await this.syncAccount({
|
|
@@ -37821,6 +37876,19 @@ class App {
|
|
|
37821
37876
|
}
|
|
37822
37877
|
}
|
|
37823
37878
|
}
|
|
37879
|
+
async placeTrade(payload) {
|
|
37880
|
+
const { account, symbol, kind, place, tp, raw, cancel } = payload;
|
|
37881
|
+
const exchange_account = await this.getExchangeAccount(account);
|
|
37882
|
+
const result = await exchange_account.placeTrade({
|
|
37883
|
+
symbol,
|
|
37884
|
+
kind,
|
|
37885
|
+
place,
|
|
37886
|
+
tp,
|
|
37887
|
+
raw,
|
|
37888
|
+
cancel
|
|
37889
|
+
});
|
|
37890
|
+
return result;
|
|
37891
|
+
}
|
|
37824
37892
|
}
|
|
37825
37893
|
async function initApp(payload) {
|
|
37826
37894
|
const pb = await initPocketBaseClient(payload.db);
|