@gbozee/ultimate 0.0.2-77 → 0.0.2-79

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.
@@ -1203,9 +1203,7 @@ function buildConfig(app_config, {
1203
1203
  }
1204
1204
  const condition = (kind === "long" ? entry > app_config.support : entry >= app_config.support) && stop >= app_config.support * 0.999;
1205
1205
  if (kind === "short") {
1206
- console.log("condition", condition, entry === stop);
1207
1206
  }
1208
- console.log({ entry, support: app_config.support, stop });
1209
1207
  const result = entry === stop ? [] : condition ? instance.build_entry({
1210
1208
  current_price: entry,
1211
1209
  stop_loss: stop,
@@ -1534,7 +1532,6 @@ function generateOptimumAppConfig(config, payload, position2) {
1534
1532
  const tolerance = 0.1;
1535
1533
  const max_iterations = 150;
1536
1534
  let iterations = 0;
1537
- console.log(`Starting risk search for ${position2.kind} position. Target Entry: ${position2.entry}, Initial Risk Range: [${low_risk}, ${high_risk}]`);
1538
1535
  while (high_risk - low_risk > tolerance && iterations < max_iterations) {
1539
1536
  iterations++;
1540
1537
  const mid_risk = (low_risk + high_risk) / 2;
@@ -1580,9 +1577,7 @@ function generateOptimumAppConfig(config, payload, position2) {
1580
1577
  price_places: current_app_config.price_places,
1581
1578
  decimal_places: current_app_config.decimal_places
1582
1579
  });
1583
- console.log(`Iteration ${iterations}: Risk=${mid_risk.toFixed(2)}, Low=${low_risk.toFixed(2)}, High=${high_risk.toFixed(2)}`);
1584
1580
  if (full_trades.length === 0) {
1585
- console.log(` -> No trades generated by sortedBuildConfig at Risk=${mid_risk.toFixed(2)}. Adjusting high_risk down.`);
1586
1581
  high_risk = mid_risk;
1587
1582
  continue;
1588
1583
  }
@@ -1595,29 +1590,23 @@ function generateOptimumAppConfig(config, payload, position2) {
1595
1590
  existingOrders: []
1596
1591
  });
1597
1592
  if (trades.length === 0) {
1598
- console.log(` -> No trades met quantity requirement after filtering at Risk=${mid_risk.toFixed(2)}. Adjusting low_risk up.`);
1599
1593
  low_risk = mid_risk;
1600
1594
  continue;
1601
1595
  }
1602
1596
  const last_trade = trades[trades.length - 1];
1603
1597
  const last_entry = last_trade.entry;
1604
- console.log(` -> Last Trade Entry (Filtered): ${last_entry.toFixed(4)}`);
1605
1598
  if (position2.kind === "long") {
1606
1599
  if (last_entry > position2.entry) {
1607
- console.log(` -> Constraint VIOLATED (Long): last_entry (${last_entry.toFixed(4)}) > position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
1608
1600
  high_risk = mid_risk;
1609
1601
  } else {
1610
- console.log(` -> Constraint MET (Long): last_entry (${last_entry.toFixed(4)}) <= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
1611
1602
  best_risk = mid_risk;
1612
1603
  best_app_config = current_app_config;
1613
1604
  low_risk = mid_risk;
1614
1605
  }
1615
1606
  } else {
1616
1607
  if (last_entry < position2.entry) {
1617
- console.log(` -> Constraint VIOLATED (Short): last_entry (${last_entry.toFixed(4)}) < position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
1618
1608
  high_risk = mid_risk;
1619
1609
  } else {
1620
- console.log(` -> Constraint MET (Short): last_entry (${last_entry.toFixed(4)}) >= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
1621
1610
  best_risk = mid_risk;
1622
1611
  best_app_config = current_app_config;
1623
1612
  low_risk = mid_risk;
@@ -1854,7 +1843,6 @@ class Strategy {
1854
1843
  quantity,
1855
1844
  kind
1856
1845
  };
1857
- console.log({ second_payload, third_payload });
1858
1846
  const app_config = generateOptimumAppConfig(this.config.global_config, second_payload, third_payload);
1859
1847
  let entries = [];
1860
1848
  let risk_per_trade = this.config.budget;
@@ -1867,6 +1855,7 @@ class Strategy {
1867
1855
  if (ignore_entries) {
1868
1856
  entries = [];
1869
1857
  }
1858
+ console.log({ app_config });
1870
1859
  }
1871
1860
  const risk = this.to_f(risk_per_trade);
1872
1861
  let below_reverse_entries = kind === "long" ? entries.filter((u) => {
package/dist/index.cjs CHANGED
@@ -51509,6 +51509,18 @@ class AppDatabase {
51509
51509
  await this.saveCredentials(password, credentials);
51510
51510
  }
51511
51511
  }
51512
+ async getAccountWithActivePositions() {
51513
+ const positions = await this.pb.collection("positions").getFullList({
51514
+ filter: `quantity > 0`,
51515
+ fields: `account`
51516
+ });
51517
+ const accounts = Array.from(new Set(positions.map((p) => p.account)));
51518
+ const filter = accounts.map((a) => `id = "${a}"`).join(" || ");
51519
+ const account_instances = await this.pb.collection("exchange_accounts").getFullList({
51520
+ filter
51521
+ });
51522
+ return account_instances;
51523
+ }
51512
51524
  async getAllSymbolsFromPositions(options) {
51513
51525
  const { no_position = false, kind = "long", custom_filter } = options || {};
51514
51526
  let filter = custom_filter || (no_position ? `quantity = 0` : undefined);
@@ -53262,9 +53274,7 @@ function buildConfig(app_config, {
53262
53274
  }
53263
53275
  const condition = (kind === "long" ? entry > app_config.support : entry >= app_config.support) && stop >= app_config.support * 0.999;
53264
53276
  if (kind === "short") {
53265
- console.log("condition", condition, entry === stop);
53266
53277
  }
53267
- console.log({ entry, support: app_config.support, stop });
53268
53278
  const result = entry === stop ? [] : condition ? instance.build_entry({
53269
53279
  current_price: entry,
53270
53280
  stop_loss: stop,
@@ -53593,7 +53603,6 @@ function generateOptimumAppConfig(config2, payload, position2) {
53593
53603
  const tolerance = 0.1;
53594
53604
  const max_iterations = 150;
53595
53605
  let iterations = 0;
53596
- console.log(`Starting risk search for ${position2.kind} position. Target Entry: ${position2.entry}, Initial Risk Range: [${low_risk}, ${high_risk}]`);
53597
53606
  while (high_risk - low_risk > tolerance && iterations < max_iterations) {
53598
53607
  iterations++;
53599
53608
  const mid_risk = (low_risk + high_risk) / 2;
@@ -53639,9 +53648,7 @@ function generateOptimumAppConfig(config2, payload, position2) {
53639
53648
  price_places: current_app_config.price_places,
53640
53649
  decimal_places: current_app_config.decimal_places
53641
53650
  });
53642
- console.log(`Iteration ${iterations}: Risk=${mid_risk.toFixed(2)}, Low=${low_risk.toFixed(2)}, High=${high_risk.toFixed(2)}`);
53643
53651
  if (full_trades.length === 0) {
53644
- console.log(` -> No trades generated by sortedBuildConfig at Risk=${mid_risk.toFixed(2)}. Adjusting high_risk down.`);
53645
53652
  high_risk = mid_risk;
53646
53653
  continue;
53647
53654
  }
@@ -53654,29 +53661,23 @@ function generateOptimumAppConfig(config2, payload, position2) {
53654
53661
  existingOrders: []
53655
53662
  });
53656
53663
  if (trades.length === 0) {
53657
- console.log(` -> No trades met quantity requirement after filtering at Risk=${mid_risk.toFixed(2)}. Adjusting low_risk up.`);
53658
53664
  low_risk = mid_risk;
53659
53665
  continue;
53660
53666
  }
53661
53667
  const last_trade = trades[trades.length - 1];
53662
53668
  const last_entry = last_trade.entry;
53663
- console.log(` -> Last Trade Entry (Filtered): ${last_entry.toFixed(4)}`);
53664
53669
  if (position2.kind === "long") {
53665
53670
  if (last_entry > position2.entry) {
53666
- console.log(` -> Constraint VIOLATED (Long): last_entry (${last_entry.toFixed(4)}) > position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
53667
53671
  high_risk = mid_risk;
53668
53672
  } else {
53669
- console.log(` -> Constraint MET (Long): last_entry (${last_entry.toFixed(4)}) <= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
53670
53673
  best_risk = mid_risk;
53671
53674
  best_app_config = current_app_config;
53672
53675
  low_risk = mid_risk;
53673
53676
  }
53674
53677
  } else {
53675
53678
  if (last_entry < position2.entry) {
53676
- console.log(` -> Constraint VIOLATED (Short): last_entry (${last_entry.toFixed(4)}) < position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
53677
53679
  high_risk = mid_risk;
53678
53680
  } else {
53679
- console.log(` -> Constraint MET (Short): last_entry (${last_entry.toFixed(4)}) >= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
53680
53681
  best_risk = mid_risk;
53681
53682
  best_app_config = current_app_config;
53682
53683
  low_risk = mid_risk;
@@ -53914,7 +53915,6 @@ class Strategy {
53914
53915
  quantity,
53915
53916
  kind
53916
53917
  };
53917
- console.log({ second_payload, third_payload });
53918
53918
  const app_config = generateOptimumAppConfig(this.config.global_config, second_payload, third_payload);
53919
53919
  let entries = [];
53920
53920
  let risk_per_trade = this.config.budget;
@@ -53927,6 +53927,7 @@ class Strategy {
53927
53927
  if (ignore_entries) {
53928
53928
  entries = [];
53929
53929
  }
53930
+ console.log({ app_config });
53930
53931
  }
53931
53932
  const risk = this.to_f(risk_per_trade);
53932
53933
  let below_reverse_entries = kind === "long" ? entries.filter((u) => {
@@ -58001,8 +58002,42 @@ class ExchangeAccount {
58001
58002
  }
58002
58003
  return 0;
58003
58004
  }
58005
+ async placeOppositeTradeAction(payload) {
58006
+ const { symbol, kind, data } = payload;
58007
+ const position2 = await this.syncAccount({
58008
+ symbol,
58009
+ kind,
58010
+ as_view: true
58011
+ });
58012
+ if (position2.avg_qty !== data.avg.quantity) {
58013
+ const config2 = await this.getPositionConfig({
58014
+ symbol,
58015
+ kind
58016
+ });
58017
+ await this.app_db.updateScheduledTrade(config2.id, {
58018
+ entry: data.entry,
58019
+ stop: data.stop,
58020
+ risk: data.risk_per_trade,
58021
+ profit_percent: data.profit_percent,
58022
+ risk_reward: data.risk_reward
58023
+ });
58024
+ await this.placeTrade({
58025
+ symbol,
58026
+ kind,
58027
+ place: true,
58028
+ ignore_config: true
58029
+ });
58030
+ await this.placeTrade({
58031
+ symbol,
58032
+ kind,
58033
+ place: true,
58034
+ stop: true,
58035
+ ignore_config: true
58036
+ });
58037
+ }
58038
+ }
58004
58039
  async buildOppositeTrades(payload) {
58005
- const { symbol, kind, place } = payload;
58040
+ const { symbol, kind, place, place_symbol } = payload;
58006
58041
  const strategy = await this.runSimulation({
58007
58042
  symbol,
58008
58043
  kind,
@@ -58013,37 +58048,12 @@ class ExchangeAccount {
58013
58048
  avg_entry: strategy.position[kind].avg_price
58014
58049
  });
58015
58050
  if (place && result?.kind) {
58016
- const position2 = await this.syncAccount({
58017
- symbol,
58051
+ const _symbol = place_symbol || symbol;
58052
+ await this.placeOppositeTradeAction({
58053
+ symbol: _symbol,
58018
58054
  kind: result.kind,
58019
- as_view: true
58055
+ data: result
58020
58056
  });
58021
- if (position2.avg_qty !== result.avg.quantity) {
58022
- const config2 = await this.getPositionConfig({
58023
- symbol,
58024
- kind: result.kind
58025
- });
58026
- await this.app_db.updateScheduledTrade(config2.id, {
58027
- entry: result.entry,
58028
- stop: result.stop,
58029
- risk: result.risk_per_trade,
58030
- profit_percent: result.profit_percent,
58031
- risk_reward: result.risk_reward
58032
- });
58033
- await this.placeTrade({
58034
- symbol,
58035
- kind: result.kind,
58036
- place: true,
58037
- ignore_config: true
58038
- });
58039
- await this.placeTrade({
58040
- symbol,
58041
- kind: result.kind,
58042
- place: true,
58043
- stop: true,
58044
- ignore_config: true
58045
- });
58046
- }
58047
58057
  }
58048
58058
  return result;
58049
58059
  }
@@ -58651,6 +58661,7 @@ class ExchangeAccount {
58651
58661
  kind,
58652
58662
  place,
58653
58663
  tp,
58664
+ limit = true,
58654
58665
  raw: _raw,
58655
58666
  cancel,
58656
58667
  stop,
@@ -58663,13 +58674,14 @@ class ExchangeAccount {
58663
58674
  kind
58664
58675
  });
58665
58676
  }
58666
- if (place) {
58677
+ if (limit) {
58667
58678
  return await this.triggerTradeFromConfig({
58668
58679
  symbol,
58669
58680
  kind,
58670
58681
  raw: payload.raw,
58671
58682
  stop,
58672
- ignore_config
58683
+ ignore_config,
58684
+ place
58673
58685
  });
58674
58686
  }
58675
58687
  await this.syncAccount({
@@ -58996,7 +59008,7 @@ class App {
58996
59008
  return await this.app_db.getMoverExchangeInstances();
58997
59009
  }
58998
59010
  async updateTpOnAllMarkets() {
58999
- const move_instances = await this.getMoverExchangeInstances();
59011
+ const move_instances = await this.app_db.getAccountWithActivePositions();
59000
59012
  for (const instance of move_instances) {
59001
59013
  const params = {
59002
59014
  account: {
package/dist/index.d.ts CHANGED
@@ -340,6 +340,7 @@ export declare class AppDatabase {
340
340
  api_key: string;
341
341
  api_secret: string;
342
342
  }): Promise<void>;
343
+ getAccountWithActivePositions(): Promise<import("pocketbase").RecordModel[]>;
343
344
  getAllSymbolsFromPositions(options?: {
344
345
  no_position?: boolean;
345
346
  kind?: "long" | "short";
@@ -1494,10 +1495,26 @@ declare class ExchangeAccount$1 {
1494
1495
  symbol: string;
1495
1496
  kind: "long" | "short";
1496
1497
  }): Promise<number>;
1498
+ placeOppositeTradeAction(payload: {
1499
+ symbol: string;
1500
+ kind: "long" | "short";
1501
+ data: {
1502
+ avg: {
1503
+ quantity: number;
1504
+ price: number;
1505
+ };
1506
+ entry: number;
1507
+ stop: number;
1508
+ risk_per_trade: number;
1509
+ profit_percent: number;
1510
+ risk_reward: number;
1511
+ };
1512
+ }): Promise<void>;
1497
1513
  buildOppositeTrades(payload: {
1498
1514
  symbol: string;
1499
1515
  kind: "long" | "short";
1500
1516
  place?: boolean;
1517
+ place_symbol?: string;
1501
1518
  }): Promise<{
1502
1519
  avg: {
1503
1520
  entry: number;
@@ -1635,6 +1652,7 @@ declare class ExchangeAccount$1 {
1635
1652
  symbol: string;
1636
1653
  kind: "long" | "short";
1637
1654
  place?: boolean;
1655
+ limit?: boolean;
1638
1656
  tp?: boolean;
1639
1657
  stop?: boolean;
1640
1658
  raw?: boolean;
package/dist/index.js CHANGED
@@ -51464,6 +51464,18 @@ class AppDatabase {
51464
51464
  await this.saveCredentials(password, credentials);
51465
51465
  }
51466
51466
  }
51467
+ async getAccountWithActivePositions() {
51468
+ const positions = await this.pb.collection("positions").getFullList({
51469
+ filter: `quantity > 0`,
51470
+ fields: `account`
51471
+ });
51472
+ const accounts = Array.from(new Set(positions.map((p) => p.account)));
51473
+ const filter = accounts.map((a) => `id = "${a}"`).join(" || ");
51474
+ const account_instances = await this.pb.collection("exchange_accounts").getFullList({
51475
+ filter
51476
+ });
51477
+ return account_instances;
51478
+ }
51467
51479
  async getAllSymbolsFromPositions(options) {
51468
51480
  const { no_position = false, kind = "long", custom_filter } = options || {};
51469
51481
  let filter = custom_filter || (no_position ? `quantity = 0` : undefined);
@@ -53217,9 +53229,7 @@ function buildConfig(app_config, {
53217
53229
  }
53218
53230
  const condition = (kind === "long" ? entry > app_config.support : entry >= app_config.support) && stop >= app_config.support * 0.999;
53219
53231
  if (kind === "short") {
53220
- console.log("condition", condition, entry === stop);
53221
53232
  }
53222
- console.log({ entry, support: app_config.support, stop });
53223
53233
  const result = entry === stop ? [] : condition ? instance.build_entry({
53224
53234
  current_price: entry,
53225
53235
  stop_loss: stop,
@@ -53548,7 +53558,6 @@ function generateOptimumAppConfig(config2, payload, position2) {
53548
53558
  const tolerance = 0.1;
53549
53559
  const max_iterations = 150;
53550
53560
  let iterations = 0;
53551
- console.log(`Starting risk search for ${position2.kind} position. Target Entry: ${position2.entry}, Initial Risk Range: [${low_risk}, ${high_risk}]`);
53552
53561
  while (high_risk - low_risk > tolerance && iterations < max_iterations) {
53553
53562
  iterations++;
53554
53563
  const mid_risk = (low_risk + high_risk) / 2;
@@ -53594,9 +53603,7 @@ function generateOptimumAppConfig(config2, payload, position2) {
53594
53603
  price_places: current_app_config.price_places,
53595
53604
  decimal_places: current_app_config.decimal_places
53596
53605
  });
53597
- console.log(`Iteration ${iterations}: Risk=${mid_risk.toFixed(2)}, Low=${low_risk.toFixed(2)}, High=${high_risk.toFixed(2)}`);
53598
53606
  if (full_trades.length === 0) {
53599
- console.log(` -> No trades generated by sortedBuildConfig at Risk=${mid_risk.toFixed(2)}. Adjusting high_risk down.`);
53600
53607
  high_risk = mid_risk;
53601
53608
  continue;
53602
53609
  }
@@ -53609,29 +53616,23 @@ function generateOptimumAppConfig(config2, payload, position2) {
53609
53616
  existingOrders: []
53610
53617
  });
53611
53618
  if (trades.length === 0) {
53612
- console.log(` -> No trades met quantity requirement after filtering at Risk=${mid_risk.toFixed(2)}. Adjusting low_risk up.`);
53613
53619
  low_risk = mid_risk;
53614
53620
  continue;
53615
53621
  }
53616
53622
  const last_trade = trades[trades.length - 1];
53617
53623
  const last_entry = last_trade.entry;
53618
- console.log(` -> Last Trade Entry (Filtered): ${last_entry.toFixed(4)}`);
53619
53624
  if (position2.kind === "long") {
53620
53625
  if (last_entry > position2.entry) {
53621
- console.log(` -> Constraint VIOLATED (Long): last_entry (${last_entry.toFixed(4)}) > position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
53622
53626
  high_risk = mid_risk;
53623
53627
  } else {
53624
- console.log(` -> Constraint MET (Long): last_entry (${last_entry.toFixed(4)}) <= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
53625
53628
  best_risk = mid_risk;
53626
53629
  best_app_config = current_app_config;
53627
53630
  low_risk = mid_risk;
53628
53631
  }
53629
53632
  } else {
53630
53633
  if (last_entry < position2.entry) {
53631
- console.log(` -> Constraint VIOLATED (Short): last_entry (${last_entry.toFixed(4)}) < position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
53632
53634
  high_risk = mid_risk;
53633
53635
  } else {
53634
- console.log(` -> Constraint MET (Short): last_entry (${last_entry.toFixed(4)}) >= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
53635
53636
  best_risk = mid_risk;
53636
53637
  best_app_config = current_app_config;
53637
53638
  low_risk = mid_risk;
@@ -53869,7 +53870,6 @@ class Strategy {
53869
53870
  quantity,
53870
53871
  kind
53871
53872
  };
53872
- console.log({ second_payload, third_payload });
53873
53873
  const app_config = generateOptimumAppConfig(this.config.global_config, second_payload, third_payload);
53874
53874
  let entries = [];
53875
53875
  let risk_per_trade = this.config.budget;
@@ -53882,6 +53882,7 @@ class Strategy {
53882
53882
  if (ignore_entries) {
53883
53883
  entries = [];
53884
53884
  }
53885
+ console.log({ app_config });
53885
53886
  }
53886
53887
  const risk = this.to_f(risk_per_trade);
53887
53888
  let below_reverse_entries = kind === "long" ? entries.filter((u) => {
@@ -57956,8 +57957,42 @@ class ExchangeAccount {
57956
57957
  }
57957
57958
  return 0;
57958
57959
  }
57960
+ async placeOppositeTradeAction(payload) {
57961
+ const { symbol, kind, data } = payload;
57962
+ const position2 = await this.syncAccount({
57963
+ symbol,
57964
+ kind,
57965
+ as_view: true
57966
+ });
57967
+ if (position2.avg_qty !== data.avg.quantity) {
57968
+ const config2 = await this.getPositionConfig({
57969
+ symbol,
57970
+ kind
57971
+ });
57972
+ await this.app_db.updateScheduledTrade(config2.id, {
57973
+ entry: data.entry,
57974
+ stop: data.stop,
57975
+ risk: data.risk_per_trade,
57976
+ profit_percent: data.profit_percent,
57977
+ risk_reward: data.risk_reward
57978
+ });
57979
+ await this.placeTrade({
57980
+ symbol,
57981
+ kind,
57982
+ place: true,
57983
+ ignore_config: true
57984
+ });
57985
+ await this.placeTrade({
57986
+ symbol,
57987
+ kind,
57988
+ place: true,
57989
+ stop: true,
57990
+ ignore_config: true
57991
+ });
57992
+ }
57993
+ }
57959
57994
  async buildOppositeTrades(payload) {
57960
- const { symbol, kind, place } = payload;
57995
+ const { symbol, kind, place, place_symbol } = payload;
57961
57996
  const strategy = await this.runSimulation({
57962
57997
  symbol,
57963
57998
  kind,
@@ -57968,37 +58003,12 @@ class ExchangeAccount {
57968
58003
  avg_entry: strategy.position[kind].avg_price
57969
58004
  });
57970
58005
  if (place && result?.kind) {
57971
- const position2 = await this.syncAccount({
57972
- symbol,
58006
+ const _symbol = place_symbol || symbol;
58007
+ await this.placeOppositeTradeAction({
58008
+ symbol: _symbol,
57973
58009
  kind: result.kind,
57974
- as_view: true
58010
+ data: result
57975
58011
  });
57976
- if (position2.avg_qty !== result.avg.quantity) {
57977
- const config2 = await this.getPositionConfig({
57978
- symbol,
57979
- kind: result.kind
57980
- });
57981
- await this.app_db.updateScheduledTrade(config2.id, {
57982
- entry: result.entry,
57983
- stop: result.stop,
57984
- risk: result.risk_per_trade,
57985
- profit_percent: result.profit_percent,
57986
- risk_reward: result.risk_reward
57987
- });
57988
- await this.placeTrade({
57989
- symbol,
57990
- kind: result.kind,
57991
- place: true,
57992
- ignore_config: true
57993
- });
57994
- await this.placeTrade({
57995
- symbol,
57996
- kind: result.kind,
57997
- place: true,
57998
- stop: true,
57999
- ignore_config: true
58000
- });
58001
- }
58002
58012
  }
58003
58013
  return result;
58004
58014
  }
@@ -58606,6 +58616,7 @@ class ExchangeAccount {
58606
58616
  kind,
58607
58617
  place,
58608
58618
  tp,
58619
+ limit = true,
58609
58620
  raw: _raw,
58610
58621
  cancel,
58611
58622
  stop,
@@ -58618,13 +58629,14 @@ class ExchangeAccount {
58618
58629
  kind
58619
58630
  });
58620
58631
  }
58621
- if (place) {
58632
+ if (limit) {
58622
58633
  return await this.triggerTradeFromConfig({
58623
58634
  symbol,
58624
58635
  kind,
58625
58636
  raw: payload.raw,
58626
58637
  stop,
58627
- ignore_config
58638
+ ignore_config,
58639
+ place
58628
58640
  });
58629
58641
  }
58630
58642
  await this.syncAccount({
@@ -58951,7 +58963,7 @@ class App {
58951
58963
  return await this.app_db.getMoverExchangeInstances();
58952
58964
  }
58953
58965
  async updateTpOnAllMarkets() {
58954
- const move_instances = await this.getMoverExchangeInstances();
58966
+ const move_instances = await this.app_db.getAccountWithActivePositions();
58955
58967
  for (const instance of move_instances) {
58956
58968
  const params = {
58957
58969
  account: {
@@ -58220,6 +58220,18 @@ class AppDatabase {
58220
58220
  await this.saveCredentials(password, credentials);
58221
58221
  }
58222
58222
  }
58223
+ async getAccountWithActivePositions() {
58224
+ const positions = await this.pb.collection("positions").getFullList({
58225
+ filter: `quantity > 0`,
58226
+ fields: `account`
58227
+ });
58228
+ const accounts = Array.from(new Set(positions.map((p) => p.account)));
58229
+ const filter = accounts.map((a) => `id = "${a}"`).join(" || ");
58230
+ const account_instances = await this.pb.collection("exchange_accounts").getFullList({
58231
+ filter
58232
+ });
58233
+ return account_instances;
58234
+ }
58223
58235
  async getAllSymbolsFromPositions(options) {
58224
58236
  const { no_position = false, kind = "long", custom_filter } = options || {};
58225
58237
  let filter = custom_filter || (no_position ? `quantity = 0` : undefined);
@@ -59966,9 +59978,7 @@ function buildConfig(app_config, {
59966
59978
  }
59967
59979
  const condition = (kind === "long" ? entry > app_config.support : entry >= app_config.support) && stop >= app_config.support * 0.999;
59968
59980
  if (kind === "short") {
59969
- console.log("condition", condition, entry === stop);
59970
59981
  }
59971
- console.log({ entry, support: app_config.support, stop });
59972
59982
  const result = entry === stop ? [] : condition ? instance.build_entry({
59973
59983
  current_price: entry,
59974
59984
  stop_loss: stop,
@@ -60284,7 +60294,6 @@ function generateOptimumAppConfig(config2, payload, position2) {
60284
60294
  const tolerance = 0.1;
60285
60295
  const max_iterations = 150;
60286
60296
  let iterations = 0;
60287
- console.log(`Starting risk search for ${position2.kind} position. Target Entry: ${position2.entry}, Initial Risk Range: [${low_risk}, ${high_risk}]`);
60288
60297
  while (high_risk - low_risk > tolerance && iterations < max_iterations) {
60289
60298
  iterations++;
60290
60299
  const mid_risk = (low_risk + high_risk) / 2;
@@ -60330,9 +60339,7 @@ function generateOptimumAppConfig(config2, payload, position2) {
60330
60339
  price_places: current_app_config.price_places,
60331
60340
  decimal_places: current_app_config.decimal_places
60332
60341
  });
60333
- console.log(`Iteration ${iterations}: Risk=${mid_risk.toFixed(2)}, Low=${low_risk.toFixed(2)}, High=${high_risk.toFixed(2)}`);
60334
60342
  if (full_trades.length === 0) {
60335
- console.log(` -> No trades generated by sortedBuildConfig at Risk=${mid_risk.toFixed(2)}. Adjusting high_risk down.`);
60336
60343
  high_risk = mid_risk;
60337
60344
  continue;
60338
60345
  }
@@ -60345,29 +60352,23 @@ function generateOptimumAppConfig(config2, payload, position2) {
60345
60352
  existingOrders: []
60346
60353
  });
60347
60354
  if (trades.length === 0) {
60348
- console.log(` -> No trades met quantity requirement after filtering at Risk=${mid_risk.toFixed(2)}. Adjusting low_risk up.`);
60349
60355
  low_risk = mid_risk;
60350
60356
  continue;
60351
60357
  }
60352
60358
  const last_trade = trades[trades.length - 1];
60353
60359
  const last_entry = last_trade.entry;
60354
- console.log(` -> Last Trade Entry (Filtered): ${last_entry.toFixed(4)}`);
60355
60360
  if (position2.kind === "long") {
60356
60361
  if (last_entry > position2.entry) {
60357
- console.log(` -> Constraint VIOLATED (Long): last_entry (${last_entry.toFixed(4)}) > position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
60358
60362
  high_risk = mid_risk;
60359
60363
  } else {
60360
- console.log(` -> Constraint MET (Long): last_entry (${last_entry.toFixed(4)}) <= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
60361
60364
  best_risk = mid_risk;
60362
60365
  best_app_config = current_app_config;
60363
60366
  low_risk = mid_risk;
60364
60367
  }
60365
60368
  } else {
60366
60369
  if (last_entry < position2.entry) {
60367
- console.log(` -> Constraint VIOLATED (Short): last_entry (${last_entry.toFixed(4)}) < position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
60368
60370
  high_risk = mid_risk;
60369
60371
  } else {
60370
- console.log(` -> Constraint MET (Short): last_entry (${last_entry.toFixed(4)}) >= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
60371
60372
  best_risk = mid_risk;
60372
60373
  best_app_config = current_app_config;
60373
60374
  low_risk = mid_risk;
@@ -60605,7 +60606,6 @@ class Strategy {
60605
60606
  quantity,
60606
60607
  kind
60607
60608
  };
60608
- console.log({ second_payload, third_payload });
60609
60609
  const app_config = generateOptimumAppConfig(this.config.global_config, second_payload, third_payload);
60610
60610
  let entries = [];
60611
60611
  let risk_per_trade = this.config.budget;
@@ -60618,6 +60618,7 @@ class Strategy {
60618
60618
  if (ignore_entries) {
60619
60619
  entries = [];
60620
60620
  }
60621
+ console.log({ app_config });
60621
60622
  }
60622
60623
  const risk = this.to_f(risk_per_trade);
60623
60624
  let below_reverse_entries = kind === "long" ? entries.filter((u) => {
@@ -64692,8 +64693,42 @@ class ExchangeAccount {
64692
64693
  }
64693
64694
  return 0;
64694
64695
  }
64696
+ async placeOppositeTradeAction(payload) {
64697
+ const { symbol, kind, data } = payload;
64698
+ const position2 = await this.syncAccount({
64699
+ symbol,
64700
+ kind,
64701
+ as_view: true
64702
+ });
64703
+ if (position2.avg_qty !== data.avg.quantity) {
64704
+ const config2 = await this.getPositionConfig({
64705
+ symbol,
64706
+ kind
64707
+ });
64708
+ await this.app_db.updateScheduledTrade(config2.id, {
64709
+ entry: data.entry,
64710
+ stop: data.stop,
64711
+ risk: data.risk_per_trade,
64712
+ profit_percent: data.profit_percent,
64713
+ risk_reward: data.risk_reward
64714
+ });
64715
+ await this.placeTrade({
64716
+ symbol,
64717
+ kind,
64718
+ place: true,
64719
+ ignore_config: true
64720
+ });
64721
+ await this.placeTrade({
64722
+ symbol,
64723
+ kind,
64724
+ place: true,
64725
+ stop: true,
64726
+ ignore_config: true
64727
+ });
64728
+ }
64729
+ }
64695
64730
  async buildOppositeTrades(payload) {
64696
- const { symbol, kind, place } = payload;
64731
+ const { symbol, kind, place, place_symbol } = payload;
64697
64732
  const strategy = await this.runSimulation({
64698
64733
  symbol,
64699
64734
  kind,
@@ -64704,37 +64739,12 @@ class ExchangeAccount {
64704
64739
  avg_entry: strategy.position[kind].avg_price
64705
64740
  });
64706
64741
  if (place && result?.kind) {
64707
- const position2 = await this.syncAccount({
64708
- symbol,
64742
+ const _symbol = place_symbol || symbol;
64743
+ await this.placeOppositeTradeAction({
64744
+ symbol: _symbol,
64709
64745
  kind: result.kind,
64710
- as_view: true
64746
+ data: result
64711
64747
  });
64712
- if (position2.avg_qty !== result.avg.quantity) {
64713
- const config2 = await this.getPositionConfig({
64714
- symbol,
64715
- kind: result.kind
64716
- });
64717
- await this.app_db.updateScheduledTrade(config2.id, {
64718
- entry: result.entry,
64719
- stop: result.stop,
64720
- risk: result.risk_per_trade,
64721
- profit_percent: result.profit_percent,
64722
- risk_reward: result.risk_reward
64723
- });
64724
- await this.placeTrade({
64725
- symbol,
64726
- kind: result.kind,
64727
- place: true,
64728
- ignore_config: true
64729
- });
64730
- await this.placeTrade({
64731
- symbol,
64732
- kind: result.kind,
64733
- place: true,
64734
- stop: true,
64735
- ignore_config: true
64736
- });
64737
- }
64738
64748
  }
64739
64749
  return result;
64740
64750
  }
@@ -65342,6 +65352,7 @@ class ExchangeAccount {
65342
65352
  kind,
65343
65353
  place,
65344
65354
  tp,
65355
+ limit = true,
65345
65356
  raw: _raw,
65346
65357
  cancel,
65347
65358
  stop,
@@ -65354,13 +65365,14 @@ class ExchangeAccount {
65354
65365
  kind
65355
65366
  });
65356
65367
  }
65357
- if (place) {
65368
+ if (limit) {
65358
65369
  return await this.triggerTradeFromConfig({
65359
65370
  symbol,
65360
65371
  kind,
65361
65372
  raw: payload.raw,
65362
65373
  stop,
65363
- ignore_config
65374
+ ignore_config,
65375
+ place
65364
65376
  });
65365
65377
  }
65366
65378
  await this.syncAccount({
@@ -65687,7 +65699,7 @@ class App {
65687
65699
  return await this.app_db.getMoverExchangeInstances();
65688
65700
  }
65689
65701
  async updateTpOnAllMarkets() {
65690
- const move_instances = await this.getMoverExchangeInstances();
65702
+ const move_instances = await this.app_db.getAccountWithActivePositions();
65691
65703
  for (const instance of move_instances) {
65692
65704
  const params = {
65693
65705
  account: {
@@ -58197,6 +58197,18 @@ class AppDatabase {
58197
58197
  await this.saveCredentials(password, credentials);
58198
58198
  }
58199
58199
  }
58200
+ async getAccountWithActivePositions() {
58201
+ const positions = await this.pb.collection("positions").getFullList({
58202
+ filter: `quantity > 0`,
58203
+ fields: `account`
58204
+ });
58205
+ const accounts = Array.from(new Set(positions.map((p) => p.account)));
58206
+ const filter = accounts.map((a) => `id = "${a}"`).join(" || ");
58207
+ const account_instances = await this.pb.collection("exchange_accounts").getFullList({
58208
+ filter
58209
+ });
58210
+ return account_instances;
58211
+ }
58200
58212
  async getAllSymbolsFromPositions(options) {
58201
58213
  const { no_position = false, kind = "long", custom_filter } = options || {};
58202
58214
  let filter = custom_filter || (no_position ? `quantity = 0` : undefined);
@@ -59943,9 +59955,7 @@ function buildConfig(app_config, {
59943
59955
  }
59944
59956
  const condition = (kind === "long" ? entry > app_config.support : entry >= app_config.support) && stop >= app_config.support * 0.999;
59945
59957
  if (kind === "short") {
59946
- console.log("condition", condition, entry === stop);
59947
59958
  }
59948
- console.log({ entry, support: app_config.support, stop });
59949
59959
  const result = entry === stop ? [] : condition ? instance.build_entry({
59950
59960
  current_price: entry,
59951
59961
  stop_loss: stop,
@@ -60261,7 +60271,6 @@ function generateOptimumAppConfig(config2, payload, position2) {
60261
60271
  const tolerance = 0.1;
60262
60272
  const max_iterations = 150;
60263
60273
  let iterations = 0;
60264
- console.log(`Starting risk search for ${position2.kind} position. Target Entry: ${position2.entry}, Initial Risk Range: [${low_risk}, ${high_risk}]`);
60265
60274
  while (high_risk - low_risk > tolerance && iterations < max_iterations) {
60266
60275
  iterations++;
60267
60276
  const mid_risk = (low_risk + high_risk) / 2;
@@ -60307,9 +60316,7 @@ function generateOptimumAppConfig(config2, payload, position2) {
60307
60316
  price_places: current_app_config.price_places,
60308
60317
  decimal_places: current_app_config.decimal_places
60309
60318
  });
60310
- console.log(`Iteration ${iterations}: Risk=${mid_risk.toFixed(2)}, Low=${low_risk.toFixed(2)}, High=${high_risk.toFixed(2)}`);
60311
60319
  if (full_trades.length === 0) {
60312
- console.log(` -> No trades generated by sortedBuildConfig at Risk=${mid_risk.toFixed(2)}. Adjusting high_risk down.`);
60313
60320
  high_risk = mid_risk;
60314
60321
  continue;
60315
60322
  }
@@ -60322,29 +60329,23 @@ function generateOptimumAppConfig(config2, payload, position2) {
60322
60329
  existingOrders: []
60323
60330
  });
60324
60331
  if (trades.length === 0) {
60325
- console.log(` -> No trades met quantity requirement after filtering at Risk=${mid_risk.toFixed(2)}. Adjusting low_risk up.`);
60326
60332
  low_risk = mid_risk;
60327
60333
  continue;
60328
60334
  }
60329
60335
  const last_trade = trades[trades.length - 1];
60330
60336
  const last_entry = last_trade.entry;
60331
- console.log(` -> Last Trade Entry (Filtered): ${last_entry.toFixed(4)}`);
60332
60337
  if (position2.kind === "long") {
60333
60338
  if (last_entry > position2.entry) {
60334
- console.log(` -> Constraint VIOLATED (Long): last_entry (${last_entry.toFixed(4)}) > position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
60335
60339
  high_risk = mid_risk;
60336
60340
  } else {
60337
- console.log(` -> Constraint MET (Long): last_entry (${last_entry.toFixed(4)}) <= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
60338
60341
  best_risk = mid_risk;
60339
60342
  best_app_config = current_app_config;
60340
60343
  low_risk = mid_risk;
60341
60344
  }
60342
60345
  } else {
60343
60346
  if (last_entry < position2.entry) {
60344
- console.log(` -> Constraint VIOLATED (Short): last_entry (${last_entry.toFixed(4)}) < position.entry (${position2.entry.toFixed(4)}). Reducing high_risk.`);
60345
60347
  high_risk = mid_risk;
60346
60348
  } else {
60347
- console.log(` -> Constraint MET (Short): last_entry (${last_entry.toFixed(4)}) >= position.entry (${position2.entry.toFixed(4)}). Storing as best, increasing low_risk.`);
60348
60349
  best_risk = mid_risk;
60349
60350
  best_app_config = current_app_config;
60350
60351
  low_risk = mid_risk;
@@ -60582,7 +60583,6 @@ class Strategy {
60582
60583
  quantity,
60583
60584
  kind
60584
60585
  };
60585
- console.log({ second_payload, third_payload });
60586
60586
  const app_config = generateOptimumAppConfig(this.config.global_config, second_payload, third_payload);
60587
60587
  let entries = [];
60588
60588
  let risk_per_trade = this.config.budget;
@@ -60595,6 +60595,7 @@ class Strategy {
60595
60595
  if (ignore_entries) {
60596
60596
  entries = [];
60597
60597
  }
60598
+ console.log({ app_config });
60598
60599
  }
60599
60600
  const risk = this.to_f(risk_per_trade);
60600
60601
  let below_reverse_entries = kind === "long" ? entries.filter((u) => {
@@ -64669,8 +64670,42 @@ class ExchangeAccount {
64669
64670
  }
64670
64671
  return 0;
64671
64672
  }
64673
+ async placeOppositeTradeAction(payload) {
64674
+ const { symbol, kind, data } = payload;
64675
+ const position2 = await this.syncAccount({
64676
+ symbol,
64677
+ kind,
64678
+ as_view: true
64679
+ });
64680
+ if (position2.avg_qty !== data.avg.quantity) {
64681
+ const config2 = await this.getPositionConfig({
64682
+ symbol,
64683
+ kind
64684
+ });
64685
+ await this.app_db.updateScheduledTrade(config2.id, {
64686
+ entry: data.entry,
64687
+ stop: data.stop,
64688
+ risk: data.risk_per_trade,
64689
+ profit_percent: data.profit_percent,
64690
+ risk_reward: data.risk_reward
64691
+ });
64692
+ await this.placeTrade({
64693
+ symbol,
64694
+ kind,
64695
+ place: true,
64696
+ ignore_config: true
64697
+ });
64698
+ await this.placeTrade({
64699
+ symbol,
64700
+ kind,
64701
+ place: true,
64702
+ stop: true,
64703
+ ignore_config: true
64704
+ });
64705
+ }
64706
+ }
64672
64707
  async buildOppositeTrades(payload) {
64673
- const { symbol, kind, place } = payload;
64708
+ const { symbol, kind, place, place_symbol } = payload;
64674
64709
  const strategy = await this.runSimulation({
64675
64710
  symbol,
64676
64711
  kind,
@@ -64681,37 +64716,12 @@ class ExchangeAccount {
64681
64716
  avg_entry: strategy.position[kind].avg_price
64682
64717
  });
64683
64718
  if (place && result?.kind) {
64684
- const position2 = await this.syncAccount({
64685
- symbol,
64719
+ const _symbol = place_symbol || symbol;
64720
+ await this.placeOppositeTradeAction({
64721
+ symbol: _symbol,
64686
64722
  kind: result.kind,
64687
- as_view: true
64723
+ data: result
64688
64724
  });
64689
- if (position2.avg_qty !== result.avg.quantity) {
64690
- const config2 = await this.getPositionConfig({
64691
- symbol,
64692
- kind: result.kind
64693
- });
64694
- await this.app_db.updateScheduledTrade(config2.id, {
64695
- entry: result.entry,
64696
- stop: result.stop,
64697
- risk: result.risk_per_trade,
64698
- profit_percent: result.profit_percent,
64699
- risk_reward: result.risk_reward
64700
- });
64701
- await this.placeTrade({
64702
- symbol,
64703
- kind: result.kind,
64704
- place: true,
64705
- ignore_config: true
64706
- });
64707
- await this.placeTrade({
64708
- symbol,
64709
- kind: result.kind,
64710
- place: true,
64711
- stop: true,
64712
- ignore_config: true
64713
- });
64714
- }
64715
64725
  }
64716
64726
  return result;
64717
64727
  }
@@ -65319,6 +65329,7 @@ class ExchangeAccount {
65319
65329
  kind,
65320
65330
  place,
65321
65331
  tp,
65332
+ limit = true,
65322
65333
  raw: _raw,
65323
65334
  cancel,
65324
65335
  stop,
@@ -65331,13 +65342,14 @@ class ExchangeAccount {
65331
65342
  kind
65332
65343
  });
65333
65344
  }
65334
- if (place) {
65345
+ if (limit) {
65335
65346
  return await this.triggerTradeFromConfig({
65336
65347
  symbol,
65337
65348
  kind,
65338
65349
  raw: payload.raw,
65339
65350
  stop,
65340
- ignore_config
65351
+ ignore_config,
65352
+ place
65341
65353
  });
65342
65354
  }
65343
65355
  await this.syncAccount({
@@ -65664,7 +65676,7 @@ class App {
65664
65676
  return await this.app_db.getMoverExchangeInstances();
65665
65677
  }
65666
65678
  async updateTpOnAllMarkets() {
65667
- const move_instances = await this.getMoverExchangeInstances();
65679
+ const move_instances = await this.app_db.getAccountWithActivePositions();
65668
65680
  for (const instance of move_instances) {
65669
65681
  const params = {
65670
65682
  account: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-77",
4
+ "version": "0.0.2-79",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",