@gbozee/ultimate 0.0.2-next.63 → 0.0.2-next.65

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.cjs CHANGED
@@ -75800,7 +75800,8 @@ class ExchangePosition {
75800
75800
  await this.exchange_account.placeProfitAndStop({
75801
75801
  symbol: this.symbol,
75802
75802
  trigger: true,
75803
- target_pnl
75803
+ target_pnl,
75804
+ kind: this.kind
75804
75805
  });
75805
75806
  }
75806
75807
  return [];
@@ -76792,8 +76793,7 @@ async function savePaperDetails(client, payload, new_payload) {
76792
76793
  });
76793
76794
  return await updateDbDetails(client, payload, current_account_info, new_payload);
76794
76795
  }
76795
- async function fetchDBExchangeData(client, symbol) {
76796
- const current_price = await getCurrentPrice3(client.client, symbol);
76796
+ async function fetchDBExchangeData(client, symbol, options) {
76797
76797
  let orders = [];
76798
76798
  let trades = { long: [], short: [] };
76799
76799
  let positions = {
@@ -76804,6 +76804,8 @@ async function fetchDBExchangeData(client, symbol) {
76804
76804
  long: null,
76805
76805
  short: null
76806
76806
  };
76807
+ let drift = { long: false, short: false };
76808
+ let priceChanged = false;
76807
76809
  const app_db = await client.initializeAppDb();
76808
76810
  const live_exchange_details = await app_db.getLiveExchangeInstance({
76809
76811
  account: {
@@ -76813,8 +76815,12 @@ async function fetchDBExchangeData(client, symbol) {
76813
76815
  symbol
76814
76816
  });
76815
76817
  const symbol_config = await app_db.getSymbolConfigFromDB(symbol);
76818
+ const dbPrice = live_exchange_details?.data?.current_price;
76819
+ const current_price = options?.current_price ?? dbPrice ?? await getCurrentPrice3(client.client, symbol);
76820
+ priceChanged = dbPrice !== undefined && dbPrice !== current_price;
76816
76821
  if (live_exchange_details) {
76817
76822
  const raw_data = live_exchange_details.data;
76823
+ raw_data.current_price = current_price;
76818
76824
  orders = raw_data.config.trades.exchange_info.open_orders || [];
76819
76825
  positions = raw_data.config.trades.exchange_info.positions;
76820
76826
  const long_result = updatePositionBasedOffTpOrSl({
@@ -76834,6 +76840,20 @@ async function fetchDBExchangeData(client, symbol) {
76834
76840
  positions.short = short_result.position;
76835
76841
  live_exchange_details.data.config.trades.exchange_info.open_orders = orders;
76836
76842
  live_exchange_details.data.config.trades.exchange_info.positions = positions;
76843
+ const prevLong = live_exchange_details.data.long_position;
76844
+ const prevShort = live_exchange_details.data.short_position;
76845
+ drift = {
76846
+ long: prevLong?.entry !== positions.long?.entry || prevLong?.quantity !== positions.long?.quantity,
76847
+ short: prevShort?.entry !== positions.short?.entry || prevShort?.quantity !== positions.short?.quantity
76848
+ };
76849
+ live_exchange_details.data.long_position = {
76850
+ ...prevLong,
76851
+ ...positions.long
76852
+ };
76853
+ live_exchange_details.data.short_position = {
76854
+ ...prevShort,
76855
+ ...positions.short
76856
+ };
76837
76857
  trades = {
76838
76858
  long: long_result.trades,
76839
76859
  short: short_result.trades
@@ -76846,11 +76866,13 @@ async function fetchDBExchangeData(client, symbol) {
76846
76866
  current_price,
76847
76867
  config: config2,
76848
76868
  trades,
76869
+ drift,
76870
+ priceChanged,
76849
76871
  raw_data: live_exchange_details?.data
76850
76872
  };
76851
76873
  }
76852
- async function getPositionInfo3(client, symbol) {
76853
- const { positions, symbol_config, current_price, raw_data, trades } = await fetchDBExchangeData(client, symbol);
76874
+ async function reconcileEngineState(client, symbol, options) {
76875
+ const { positions, symbol_config, current_price, raw_data, trades, drift, priceChanged } = await fetchDBExchangeData(client, symbol, options);
76854
76876
  const long_generator = new TradeEngine({
76855
76877
  position: {
76856
76878
  kind: "long",
@@ -76875,65 +76897,44 @@ async function getPositionInfo3(client, symbol) {
76875
76897
  trades: trades.short,
76876
76898
  price: current_price
76877
76899
  });
76900
+ let changed = false;
76878
76901
  if (raw_data) {
76879
76902
  const long_state_trades = long_state.newTrades;
76880
76903
  const short_state_trades = short_state.newTrades;
76881
- if (trades.long.length !== long_state_trades.length && positions.long.quantity !== long_state.position.quantity) {
76882
- console.log({
76883
- old: trades.long.length,
76884
- new: long_state_trades.length,
76885
- position: long_state.position
76886
- });
76887
- let _position = {
76888
- entry: long_state.position.entry,
76889
- quantity: long_state.position.quantity
76890
- };
76891
- let tp = undefined;
76892
- if (long_state_trades.length === 0 && positions.long.quantity === 0) {
76893
- _position.entry = 0;
76894
- _position.quantity = 0;
76895
- long_state.position.entry = 0;
76896
- long_state.position.quantity = 0;
76897
- tp = {
76898
- price: 0,
76899
- quantity: 0
76904
+ for (const kind of ["long", "short"]) {
76905
+ const state = kind === "long" ? long_state : short_state;
76906
+ const state_trades = kind === "long" ? long_state_trades : short_state_trades;
76907
+ if (trades[kind].length !== state_trades.length && positions[kind].quantity !== state.position.quantity || drift[kind]) {
76908
+ let _position = {
76909
+ entry: state.position.entry,
76910
+ quantity: state.position.quantity
76900
76911
  };
76912
+ let tp = undefined;
76913
+ if (state_trades.length === 0 && positions[kind].quantity === 0) {
76914
+ _position.entry = 0;
76915
+ _position.quantity = 0;
76916
+ state.position.entry = 0;
76917
+ state.position.quantity = 0;
76918
+ tp = { price: 0, quantity: 0 };
76919
+ }
76920
+ await updateDbDetails(client, { kind, symbol }, raw_data, { take_profit: tp, position: _position, orders: state_trades });
76921
+ changed = true;
76901
76922
  }
76902
- await updateDbDetails(client, {
76903
- kind: "long",
76904
- symbol
76905
- }, raw_data, {
76906
- take_profit: tp,
76907
- position: _position,
76908
- orders: long_state_trades
76909
- });
76910
76923
  }
76911
- if (trades.short.length !== short_state_trades.length && positions.short.quantity !== short_state.position.quantity) {
76912
- let _position = {
76913
- entry: short_state.position.entry,
76914
- quantity: short_state.position.quantity
76915
- };
76916
- let tp = undefined;
76917
- if (short_state_trades.length === 0 && positions.short.quantity === 0) {
76918
- _position.entry = 0;
76919
- _position.quantity = 0;
76920
- short_state.position.entry = 0;
76921
- short_state.position.quantity = 0;
76922
- tp = {
76923
- price: 0,
76924
- quantity: 0
76925
- };
76926
- }
76927
- await updateDbDetails(client, {
76928
- kind: "short",
76929
- symbol
76930
- }, raw_data, {
76931
- position: _position,
76932
- orders: short_state_trades,
76933
- take_profit: tp
76924
+ if (!changed && priceChanged) {
76925
+ const app_db = await client.initializeAppDb();
76926
+ await app_db.createOrUpdateLiveExchangeInstance({
76927
+ account: { owner: client.owner, exchange: "paper" },
76928
+ symbol,
76929
+ data: raw_data
76934
76930
  });
76931
+ changed = true;
76935
76932
  }
76936
76933
  }
76934
+ return { long_state, short_state, changed };
76935
+ }
76936
+ async function getPositionInfo3(client, symbol) {
76937
+ const { long_state, short_state } = await reconcileEngineState(client, symbol);
76937
76938
  const long_position = long_state.position;
76938
76939
  const short_position = short_state.position;
76939
76940
  const long_quantity = long_position.quantity;
package/dist/index.js CHANGED
@@ -75712,7 +75712,8 @@ class ExchangePosition {
75712
75712
  await this.exchange_account.placeProfitAndStop({
75713
75713
  symbol: this.symbol,
75714
75714
  trigger: true,
75715
- target_pnl
75715
+ target_pnl,
75716
+ kind: this.kind
75716
75717
  });
75717
75718
  }
75718
75719
  return [];
@@ -76704,8 +76705,7 @@ async function savePaperDetails(client, payload, new_payload) {
76704
76705
  });
76705
76706
  return await updateDbDetails(client, payload, current_account_info, new_payload);
76706
76707
  }
76707
- async function fetchDBExchangeData(client, symbol) {
76708
- const current_price = await getCurrentPrice3(client.client, symbol);
76708
+ async function fetchDBExchangeData(client, symbol, options) {
76709
76709
  let orders = [];
76710
76710
  let trades = { long: [], short: [] };
76711
76711
  let positions = {
@@ -76716,6 +76716,8 @@ async function fetchDBExchangeData(client, symbol) {
76716
76716
  long: null,
76717
76717
  short: null
76718
76718
  };
76719
+ let drift = { long: false, short: false };
76720
+ let priceChanged = false;
76719
76721
  const app_db = await client.initializeAppDb();
76720
76722
  const live_exchange_details = await app_db.getLiveExchangeInstance({
76721
76723
  account: {
@@ -76725,8 +76727,12 @@ async function fetchDBExchangeData(client, symbol) {
76725
76727
  symbol
76726
76728
  });
76727
76729
  const symbol_config = await app_db.getSymbolConfigFromDB(symbol);
76730
+ const dbPrice = live_exchange_details?.data?.current_price;
76731
+ const current_price = options?.current_price ?? dbPrice ?? await getCurrentPrice3(client.client, symbol);
76732
+ priceChanged = dbPrice !== undefined && dbPrice !== current_price;
76728
76733
  if (live_exchange_details) {
76729
76734
  const raw_data = live_exchange_details.data;
76735
+ raw_data.current_price = current_price;
76730
76736
  orders = raw_data.config.trades.exchange_info.open_orders || [];
76731
76737
  positions = raw_data.config.trades.exchange_info.positions;
76732
76738
  const long_result = updatePositionBasedOffTpOrSl({
@@ -76746,6 +76752,20 @@ async function fetchDBExchangeData(client, symbol) {
76746
76752
  positions.short = short_result.position;
76747
76753
  live_exchange_details.data.config.trades.exchange_info.open_orders = orders;
76748
76754
  live_exchange_details.data.config.trades.exchange_info.positions = positions;
76755
+ const prevLong = live_exchange_details.data.long_position;
76756
+ const prevShort = live_exchange_details.data.short_position;
76757
+ drift = {
76758
+ long: prevLong?.entry !== positions.long?.entry || prevLong?.quantity !== positions.long?.quantity,
76759
+ short: prevShort?.entry !== positions.short?.entry || prevShort?.quantity !== positions.short?.quantity
76760
+ };
76761
+ live_exchange_details.data.long_position = {
76762
+ ...prevLong,
76763
+ ...positions.long
76764
+ };
76765
+ live_exchange_details.data.short_position = {
76766
+ ...prevShort,
76767
+ ...positions.short
76768
+ };
76749
76769
  trades = {
76750
76770
  long: long_result.trades,
76751
76771
  short: short_result.trades
@@ -76758,11 +76778,13 @@ async function fetchDBExchangeData(client, symbol) {
76758
76778
  current_price,
76759
76779
  config: config2,
76760
76780
  trades,
76781
+ drift,
76782
+ priceChanged,
76761
76783
  raw_data: live_exchange_details?.data
76762
76784
  };
76763
76785
  }
76764
- async function getPositionInfo3(client, symbol) {
76765
- const { positions, symbol_config, current_price, raw_data, trades } = await fetchDBExchangeData(client, symbol);
76786
+ async function reconcileEngineState(client, symbol, options) {
76787
+ const { positions, symbol_config, current_price, raw_data, trades, drift, priceChanged } = await fetchDBExchangeData(client, symbol, options);
76766
76788
  const long_generator = new TradeEngine({
76767
76789
  position: {
76768
76790
  kind: "long",
@@ -76787,65 +76809,44 @@ async function getPositionInfo3(client, symbol) {
76787
76809
  trades: trades.short,
76788
76810
  price: current_price
76789
76811
  });
76812
+ let changed = false;
76790
76813
  if (raw_data) {
76791
76814
  const long_state_trades = long_state.newTrades;
76792
76815
  const short_state_trades = short_state.newTrades;
76793
- if (trades.long.length !== long_state_trades.length && positions.long.quantity !== long_state.position.quantity) {
76794
- console.log({
76795
- old: trades.long.length,
76796
- new: long_state_trades.length,
76797
- position: long_state.position
76798
- });
76799
- let _position = {
76800
- entry: long_state.position.entry,
76801
- quantity: long_state.position.quantity
76802
- };
76803
- let tp = undefined;
76804
- if (long_state_trades.length === 0 && positions.long.quantity === 0) {
76805
- _position.entry = 0;
76806
- _position.quantity = 0;
76807
- long_state.position.entry = 0;
76808
- long_state.position.quantity = 0;
76809
- tp = {
76810
- price: 0,
76811
- quantity: 0
76816
+ for (const kind of ["long", "short"]) {
76817
+ const state = kind === "long" ? long_state : short_state;
76818
+ const state_trades = kind === "long" ? long_state_trades : short_state_trades;
76819
+ if (trades[kind].length !== state_trades.length && positions[kind].quantity !== state.position.quantity || drift[kind]) {
76820
+ let _position = {
76821
+ entry: state.position.entry,
76822
+ quantity: state.position.quantity
76812
76823
  };
76824
+ let tp = undefined;
76825
+ if (state_trades.length === 0 && positions[kind].quantity === 0) {
76826
+ _position.entry = 0;
76827
+ _position.quantity = 0;
76828
+ state.position.entry = 0;
76829
+ state.position.quantity = 0;
76830
+ tp = { price: 0, quantity: 0 };
76831
+ }
76832
+ await updateDbDetails(client, { kind, symbol }, raw_data, { take_profit: tp, position: _position, orders: state_trades });
76833
+ changed = true;
76813
76834
  }
76814
- await updateDbDetails(client, {
76815
- kind: "long",
76816
- symbol
76817
- }, raw_data, {
76818
- take_profit: tp,
76819
- position: _position,
76820
- orders: long_state_trades
76821
- });
76822
76835
  }
76823
- if (trades.short.length !== short_state_trades.length && positions.short.quantity !== short_state.position.quantity) {
76824
- let _position = {
76825
- entry: short_state.position.entry,
76826
- quantity: short_state.position.quantity
76827
- };
76828
- let tp = undefined;
76829
- if (short_state_trades.length === 0 && positions.short.quantity === 0) {
76830
- _position.entry = 0;
76831
- _position.quantity = 0;
76832
- short_state.position.entry = 0;
76833
- short_state.position.quantity = 0;
76834
- tp = {
76835
- price: 0,
76836
- quantity: 0
76837
- };
76838
- }
76839
- await updateDbDetails(client, {
76840
- kind: "short",
76841
- symbol
76842
- }, raw_data, {
76843
- position: _position,
76844
- orders: short_state_trades,
76845
- take_profit: tp
76836
+ if (!changed && priceChanged) {
76837
+ const app_db = await client.initializeAppDb();
76838
+ await app_db.createOrUpdateLiveExchangeInstance({
76839
+ account: { owner: client.owner, exchange: "paper" },
76840
+ symbol,
76841
+ data: raw_data
76846
76842
  });
76843
+ changed = true;
76847
76844
  }
76848
76845
  }
76846
+ return { long_state, short_state, changed };
76847
+ }
76848
+ async function getPositionInfo3(client, symbol) {
76849
+ const { long_state, short_state } = await reconcileEngineState(client, symbol);
76849
76850
  const long_position = long_state.position;
76850
76851
  const short_position = short_state.position;
76851
76852
  const long_quantity = long_position.quantity;
@@ -79522,7 +79522,8 @@ class ExchangePosition {
79522
79522
  await this.exchange_account.placeProfitAndStop({
79523
79523
  symbol: this.symbol,
79524
79524
  trigger: true,
79525
- target_pnl
79525
+ target_pnl,
79526
+ kind: this.kind
79526
79527
  });
79527
79528
  }
79528
79529
  return [];
@@ -80514,8 +80515,7 @@ async function savePaperDetails(client, payload, new_payload) {
80514
80515
  });
80515
80516
  return await updateDbDetails(client, payload, current_account_info, new_payload);
80516
80517
  }
80517
- async function fetchDBExchangeData(client, symbol) {
80518
- const current_price = await getCurrentPrice3(client.client, symbol);
80518
+ async function fetchDBExchangeData(client, symbol, options) {
80519
80519
  let orders = [];
80520
80520
  let trades = { long: [], short: [] };
80521
80521
  let positions = {
@@ -80526,6 +80526,8 @@ async function fetchDBExchangeData(client, symbol) {
80526
80526
  long: null,
80527
80527
  short: null
80528
80528
  };
80529
+ let drift = { long: false, short: false };
80530
+ let priceChanged = false;
80529
80531
  const app_db = await client.initializeAppDb();
80530
80532
  const live_exchange_details = await app_db.getLiveExchangeInstance({
80531
80533
  account: {
@@ -80535,8 +80537,12 @@ async function fetchDBExchangeData(client, symbol) {
80535
80537
  symbol
80536
80538
  });
80537
80539
  const symbol_config = await app_db.getSymbolConfigFromDB(symbol);
80540
+ const dbPrice = live_exchange_details?.data?.current_price;
80541
+ const current_price = options?.current_price ?? dbPrice ?? await getCurrentPrice3(client.client, symbol);
80542
+ priceChanged = dbPrice !== undefined && dbPrice !== current_price;
80538
80543
  if (live_exchange_details) {
80539
80544
  const raw_data = live_exchange_details.data;
80545
+ raw_data.current_price = current_price;
80540
80546
  orders = raw_data.config.trades.exchange_info.open_orders || [];
80541
80547
  positions = raw_data.config.trades.exchange_info.positions;
80542
80548
  const long_result = updatePositionBasedOffTpOrSl({
@@ -80556,6 +80562,20 @@ async function fetchDBExchangeData(client, symbol) {
80556
80562
  positions.short = short_result.position;
80557
80563
  live_exchange_details.data.config.trades.exchange_info.open_orders = orders;
80558
80564
  live_exchange_details.data.config.trades.exchange_info.positions = positions;
80565
+ const prevLong = live_exchange_details.data.long_position;
80566
+ const prevShort = live_exchange_details.data.short_position;
80567
+ drift = {
80568
+ long: prevLong?.entry !== positions.long?.entry || prevLong?.quantity !== positions.long?.quantity,
80569
+ short: prevShort?.entry !== positions.short?.entry || prevShort?.quantity !== positions.short?.quantity
80570
+ };
80571
+ live_exchange_details.data.long_position = {
80572
+ ...prevLong,
80573
+ ...positions.long
80574
+ };
80575
+ live_exchange_details.data.short_position = {
80576
+ ...prevShort,
80577
+ ...positions.short
80578
+ };
80559
80579
  trades = {
80560
80580
  long: long_result.trades,
80561
80581
  short: short_result.trades
@@ -80568,11 +80588,13 @@ async function fetchDBExchangeData(client, symbol) {
80568
80588
  current_price,
80569
80589
  config: config2,
80570
80590
  trades,
80591
+ drift,
80592
+ priceChanged,
80571
80593
  raw_data: live_exchange_details?.data
80572
80594
  };
80573
80595
  }
80574
- async function getPositionInfo3(client, symbol) {
80575
- const { positions, symbol_config, current_price, raw_data, trades } = await fetchDBExchangeData(client, symbol);
80596
+ async function reconcileEngineState(client, symbol, options) {
80597
+ const { positions, symbol_config, current_price, raw_data, trades, drift, priceChanged } = await fetchDBExchangeData(client, symbol, options);
80576
80598
  const long_generator = new TradeEngine({
80577
80599
  position: {
80578
80600
  kind: "long",
@@ -80597,65 +80619,44 @@ async function getPositionInfo3(client, symbol) {
80597
80619
  trades: trades.short,
80598
80620
  price: current_price
80599
80621
  });
80622
+ let changed = false;
80600
80623
  if (raw_data) {
80601
80624
  const long_state_trades = long_state.newTrades;
80602
80625
  const short_state_trades = short_state.newTrades;
80603
- if (trades.long.length !== long_state_trades.length && positions.long.quantity !== long_state.position.quantity) {
80604
- console.log({
80605
- old: trades.long.length,
80606
- new: long_state_trades.length,
80607
- position: long_state.position
80608
- });
80609
- let _position = {
80610
- entry: long_state.position.entry,
80611
- quantity: long_state.position.quantity
80612
- };
80613
- let tp = undefined;
80614
- if (long_state_trades.length === 0 && positions.long.quantity === 0) {
80615
- _position.entry = 0;
80616
- _position.quantity = 0;
80617
- long_state.position.entry = 0;
80618
- long_state.position.quantity = 0;
80619
- tp = {
80620
- price: 0,
80621
- quantity: 0
80626
+ for (const kind of ["long", "short"]) {
80627
+ const state = kind === "long" ? long_state : short_state;
80628
+ const state_trades = kind === "long" ? long_state_trades : short_state_trades;
80629
+ if (trades[kind].length !== state_trades.length && positions[kind].quantity !== state.position.quantity || drift[kind]) {
80630
+ let _position = {
80631
+ entry: state.position.entry,
80632
+ quantity: state.position.quantity
80622
80633
  };
80634
+ let tp = undefined;
80635
+ if (state_trades.length === 0 && positions[kind].quantity === 0) {
80636
+ _position.entry = 0;
80637
+ _position.quantity = 0;
80638
+ state.position.entry = 0;
80639
+ state.position.quantity = 0;
80640
+ tp = { price: 0, quantity: 0 };
80641
+ }
80642
+ await updateDbDetails(client, { kind, symbol }, raw_data, { take_profit: tp, position: _position, orders: state_trades });
80643
+ changed = true;
80623
80644
  }
80624
- await updateDbDetails(client, {
80625
- kind: "long",
80626
- symbol
80627
- }, raw_data, {
80628
- take_profit: tp,
80629
- position: _position,
80630
- orders: long_state_trades
80631
- });
80632
80645
  }
80633
- if (trades.short.length !== short_state_trades.length && positions.short.quantity !== short_state.position.quantity) {
80634
- let _position = {
80635
- entry: short_state.position.entry,
80636
- quantity: short_state.position.quantity
80637
- };
80638
- let tp = undefined;
80639
- if (short_state_trades.length === 0 && positions.short.quantity === 0) {
80640
- _position.entry = 0;
80641
- _position.quantity = 0;
80642
- short_state.position.entry = 0;
80643
- short_state.position.quantity = 0;
80644
- tp = {
80645
- price: 0,
80646
- quantity: 0
80647
- };
80648
- }
80649
- await updateDbDetails(client, {
80650
- kind: "short",
80651
- symbol
80652
- }, raw_data, {
80653
- position: _position,
80654
- orders: short_state_trades,
80655
- take_profit: tp
80646
+ if (!changed && priceChanged) {
80647
+ const app_db = await client.initializeAppDb();
80648
+ await app_db.createOrUpdateLiveExchangeInstance({
80649
+ account: { owner: client.owner, exchange: "paper" },
80650
+ symbol,
80651
+ data: raw_data
80656
80652
  });
80653
+ changed = true;
80657
80654
  }
80658
80655
  }
80656
+ return { long_state, short_state, changed };
80657
+ }
80658
+ async function getPositionInfo3(client, symbol) {
80659
+ const { long_state, short_state } = await reconcileEngineState(client, symbol);
80659
80660
  const long_position = long_state.position;
80660
80661
  const short_position = short_state.position;
80661
80662
  const long_quantity = long_position.quantity;
@@ -79495,7 +79495,8 @@ class ExchangePosition {
79495
79495
  await this.exchange_account.placeProfitAndStop({
79496
79496
  symbol: this.symbol,
79497
79497
  trigger: true,
79498
- target_pnl
79498
+ target_pnl,
79499
+ kind: this.kind
79499
79500
  });
79500
79501
  }
79501
79502
  return [];
@@ -80487,8 +80488,7 @@ async function savePaperDetails(client, payload, new_payload) {
80487
80488
  });
80488
80489
  return await updateDbDetails(client, payload, current_account_info, new_payload);
80489
80490
  }
80490
- async function fetchDBExchangeData(client, symbol) {
80491
- const current_price = await getCurrentPrice3(client.client, symbol);
80491
+ async function fetchDBExchangeData(client, symbol, options) {
80492
80492
  let orders = [];
80493
80493
  let trades = { long: [], short: [] };
80494
80494
  let positions = {
@@ -80499,6 +80499,8 @@ async function fetchDBExchangeData(client, symbol) {
80499
80499
  long: null,
80500
80500
  short: null
80501
80501
  };
80502
+ let drift = { long: false, short: false };
80503
+ let priceChanged = false;
80502
80504
  const app_db = await client.initializeAppDb();
80503
80505
  const live_exchange_details = await app_db.getLiveExchangeInstance({
80504
80506
  account: {
@@ -80508,8 +80510,12 @@ async function fetchDBExchangeData(client, symbol) {
80508
80510
  symbol
80509
80511
  });
80510
80512
  const symbol_config = await app_db.getSymbolConfigFromDB(symbol);
80513
+ const dbPrice = live_exchange_details?.data?.current_price;
80514
+ const current_price = options?.current_price ?? dbPrice ?? await getCurrentPrice3(client.client, symbol);
80515
+ priceChanged = dbPrice !== undefined && dbPrice !== current_price;
80511
80516
  if (live_exchange_details) {
80512
80517
  const raw_data = live_exchange_details.data;
80518
+ raw_data.current_price = current_price;
80513
80519
  orders = raw_data.config.trades.exchange_info.open_orders || [];
80514
80520
  positions = raw_data.config.trades.exchange_info.positions;
80515
80521
  const long_result = updatePositionBasedOffTpOrSl({
@@ -80529,6 +80535,20 @@ async function fetchDBExchangeData(client, symbol) {
80529
80535
  positions.short = short_result.position;
80530
80536
  live_exchange_details.data.config.trades.exchange_info.open_orders = orders;
80531
80537
  live_exchange_details.data.config.trades.exchange_info.positions = positions;
80538
+ const prevLong = live_exchange_details.data.long_position;
80539
+ const prevShort = live_exchange_details.data.short_position;
80540
+ drift = {
80541
+ long: prevLong?.entry !== positions.long?.entry || prevLong?.quantity !== positions.long?.quantity,
80542
+ short: prevShort?.entry !== positions.short?.entry || prevShort?.quantity !== positions.short?.quantity
80543
+ };
80544
+ live_exchange_details.data.long_position = {
80545
+ ...prevLong,
80546
+ ...positions.long
80547
+ };
80548
+ live_exchange_details.data.short_position = {
80549
+ ...prevShort,
80550
+ ...positions.short
80551
+ };
80532
80552
  trades = {
80533
80553
  long: long_result.trades,
80534
80554
  short: short_result.trades
@@ -80541,11 +80561,13 @@ async function fetchDBExchangeData(client, symbol) {
80541
80561
  current_price,
80542
80562
  config: config2,
80543
80563
  trades,
80564
+ drift,
80565
+ priceChanged,
80544
80566
  raw_data: live_exchange_details?.data
80545
80567
  };
80546
80568
  }
80547
- async function getPositionInfo3(client, symbol) {
80548
- const { positions, symbol_config, current_price, raw_data, trades } = await fetchDBExchangeData(client, symbol);
80569
+ async function reconcileEngineState(client, symbol, options) {
80570
+ const { positions, symbol_config, current_price, raw_data, trades, drift, priceChanged } = await fetchDBExchangeData(client, symbol, options);
80549
80571
  const long_generator = new TradeEngine({
80550
80572
  position: {
80551
80573
  kind: "long",
@@ -80570,65 +80592,44 @@ async function getPositionInfo3(client, symbol) {
80570
80592
  trades: trades.short,
80571
80593
  price: current_price
80572
80594
  });
80595
+ let changed = false;
80573
80596
  if (raw_data) {
80574
80597
  const long_state_trades = long_state.newTrades;
80575
80598
  const short_state_trades = short_state.newTrades;
80576
- if (trades.long.length !== long_state_trades.length && positions.long.quantity !== long_state.position.quantity) {
80577
- console.log({
80578
- old: trades.long.length,
80579
- new: long_state_trades.length,
80580
- position: long_state.position
80581
- });
80582
- let _position = {
80583
- entry: long_state.position.entry,
80584
- quantity: long_state.position.quantity
80585
- };
80586
- let tp = undefined;
80587
- if (long_state_trades.length === 0 && positions.long.quantity === 0) {
80588
- _position.entry = 0;
80589
- _position.quantity = 0;
80590
- long_state.position.entry = 0;
80591
- long_state.position.quantity = 0;
80592
- tp = {
80593
- price: 0,
80594
- quantity: 0
80599
+ for (const kind of ["long", "short"]) {
80600
+ const state = kind === "long" ? long_state : short_state;
80601
+ const state_trades = kind === "long" ? long_state_trades : short_state_trades;
80602
+ if (trades[kind].length !== state_trades.length && positions[kind].quantity !== state.position.quantity || drift[kind]) {
80603
+ let _position = {
80604
+ entry: state.position.entry,
80605
+ quantity: state.position.quantity
80595
80606
  };
80607
+ let tp = undefined;
80608
+ if (state_trades.length === 0 && positions[kind].quantity === 0) {
80609
+ _position.entry = 0;
80610
+ _position.quantity = 0;
80611
+ state.position.entry = 0;
80612
+ state.position.quantity = 0;
80613
+ tp = { price: 0, quantity: 0 };
80614
+ }
80615
+ await updateDbDetails(client, { kind, symbol }, raw_data, { take_profit: tp, position: _position, orders: state_trades });
80616
+ changed = true;
80596
80617
  }
80597
- await updateDbDetails(client, {
80598
- kind: "long",
80599
- symbol
80600
- }, raw_data, {
80601
- take_profit: tp,
80602
- position: _position,
80603
- orders: long_state_trades
80604
- });
80605
80618
  }
80606
- if (trades.short.length !== short_state_trades.length && positions.short.quantity !== short_state.position.quantity) {
80607
- let _position = {
80608
- entry: short_state.position.entry,
80609
- quantity: short_state.position.quantity
80610
- };
80611
- let tp = undefined;
80612
- if (short_state_trades.length === 0 && positions.short.quantity === 0) {
80613
- _position.entry = 0;
80614
- _position.quantity = 0;
80615
- short_state.position.entry = 0;
80616
- short_state.position.quantity = 0;
80617
- tp = {
80618
- price: 0,
80619
- quantity: 0
80620
- };
80621
- }
80622
- await updateDbDetails(client, {
80623
- kind: "short",
80624
- symbol
80625
- }, raw_data, {
80626
- position: _position,
80627
- orders: short_state_trades,
80628
- take_profit: tp
80619
+ if (!changed && priceChanged) {
80620
+ const app_db = await client.initializeAppDb();
80621
+ await app_db.createOrUpdateLiveExchangeInstance({
80622
+ account: { owner: client.owner, exchange: "paper" },
80623
+ symbol,
80624
+ data: raw_data
80629
80625
  });
80626
+ changed = true;
80630
80627
  }
80631
80628
  }
80629
+ return { long_state, short_state, changed };
80630
+ }
80631
+ async function getPositionInfo3(client, symbol) {
80632
+ const { long_state, short_state } = await reconcileEngineState(client, symbol);
80632
80633
  const long_position = long_state.position;
80633
80634
  const short_position = short_state.position;
80634
80635
  const long_quantity = long_position.quantity;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-next.63",
4
+ "version": "0.0.2-next.65",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",