@gbozee/ultimate 0.0.2-next.64 → 0.0.2-next.66

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
@@ -74993,7 +74993,8 @@ function build_reduce_config(payload) {
74993
74993
  not_reduce = false,
74994
74994
  _positions,
74995
74995
  long_config,
74996
- short_config
74996
+ short_config,
74997
+ kind
74997
74998
  } = payload;
74998
74999
  const long_position = _positions.find((x) => x.kind === "long");
74999
75000
  const short_position = _positions.find((x) => x.kind === "short");
@@ -75008,8 +75009,8 @@ function build_reduce_config(payload) {
75008
75009
  short_target_pnl = Math.abs(short_position.entry - buy_price) * short_position.quantity;
75009
75010
  }
75010
75011
  return {
75011
- trigger_short: true,
75012
- trigger_long: true,
75012
+ trigger_short: kind ? kind === "short" : true,
75013
+ trigger_long: kind ? kind === "long" : true,
75013
75014
  symbol,
75014
75015
  short_minimum_pnl,
75015
75016
  long_minimum_pnl,
@@ -76793,8 +76794,7 @@ async function savePaperDetails(client, payload, new_payload) {
76793
76794
  });
76794
76795
  return await updateDbDetails(client, payload, current_account_info, new_payload);
76795
76796
  }
76796
- async function fetchDBExchangeData(client, symbol) {
76797
- const current_price = await getCurrentPrice3(client.client, symbol);
76797
+ async function fetchDBExchangeData(client, symbol, options) {
76798
76798
  let orders = [];
76799
76799
  let trades = { long: [], short: [] };
76800
76800
  let positions = {
@@ -76805,6 +76805,8 @@ async function fetchDBExchangeData(client, symbol) {
76805
76805
  long: null,
76806
76806
  short: null
76807
76807
  };
76808
+ let drift = { long: false, short: false };
76809
+ let priceChanged = false;
76808
76810
  const app_db = await client.initializeAppDb();
76809
76811
  const live_exchange_details = await app_db.getLiveExchangeInstance({
76810
76812
  account: {
@@ -76814,8 +76816,12 @@ async function fetchDBExchangeData(client, symbol) {
76814
76816
  symbol
76815
76817
  });
76816
76818
  const symbol_config = await app_db.getSymbolConfigFromDB(symbol);
76819
+ const dbPrice = live_exchange_details?.data?.current_price;
76820
+ const current_price = options?.current_price ?? dbPrice ?? await getCurrentPrice3(client.client, symbol);
76821
+ priceChanged = dbPrice !== undefined && dbPrice !== current_price;
76817
76822
  if (live_exchange_details) {
76818
76823
  const raw_data = live_exchange_details.data;
76824
+ raw_data.current_price = current_price;
76819
76825
  orders = raw_data.config.trades.exchange_info.open_orders || [];
76820
76826
  positions = raw_data.config.trades.exchange_info.positions;
76821
76827
  const long_result = updatePositionBasedOffTpOrSl({
@@ -76835,6 +76841,20 @@ async function fetchDBExchangeData(client, symbol) {
76835
76841
  positions.short = short_result.position;
76836
76842
  live_exchange_details.data.config.trades.exchange_info.open_orders = orders;
76837
76843
  live_exchange_details.data.config.trades.exchange_info.positions = positions;
76844
+ const prevLong = live_exchange_details.data.long_position;
76845
+ const prevShort = live_exchange_details.data.short_position;
76846
+ drift = {
76847
+ long: prevLong?.entry !== positions.long?.entry || prevLong?.quantity !== positions.long?.quantity,
76848
+ short: prevShort?.entry !== positions.short?.entry || prevShort?.quantity !== positions.short?.quantity
76849
+ };
76850
+ live_exchange_details.data.long_position = {
76851
+ ...prevLong,
76852
+ ...positions.long
76853
+ };
76854
+ live_exchange_details.data.short_position = {
76855
+ ...prevShort,
76856
+ ...positions.short
76857
+ };
76838
76858
  trades = {
76839
76859
  long: long_result.trades,
76840
76860
  short: short_result.trades
@@ -76847,11 +76867,13 @@ async function fetchDBExchangeData(client, symbol) {
76847
76867
  current_price,
76848
76868
  config: config2,
76849
76869
  trades,
76870
+ drift,
76871
+ priceChanged,
76850
76872
  raw_data: live_exchange_details?.data
76851
76873
  };
76852
76874
  }
76853
- async function getPositionInfo3(client, symbol) {
76854
- const { positions, symbol_config, current_price, raw_data, trades } = await fetchDBExchangeData(client, symbol);
76875
+ async function reconcileEngineState(client, symbol, options) {
76876
+ const { positions, symbol_config, current_price, raw_data, trades, drift, priceChanged } = await fetchDBExchangeData(client, symbol, options);
76855
76877
  const long_generator = new TradeEngine({
76856
76878
  position: {
76857
76879
  kind: "long",
@@ -76876,65 +76898,44 @@ async function getPositionInfo3(client, symbol) {
76876
76898
  trades: trades.short,
76877
76899
  price: current_price
76878
76900
  });
76901
+ let changed = false;
76879
76902
  if (raw_data) {
76880
76903
  const long_state_trades = long_state.newTrades;
76881
76904
  const short_state_trades = short_state.newTrades;
76882
- if (trades.long.length !== long_state_trades.length && positions.long.quantity !== long_state.position.quantity) {
76883
- console.log({
76884
- old: trades.long.length,
76885
- new: long_state_trades.length,
76886
- position: long_state.position
76887
- });
76888
- let _position = {
76889
- entry: long_state.position.entry,
76890
- quantity: long_state.position.quantity
76891
- };
76892
- let tp = undefined;
76893
- if (long_state_trades.length === 0 && positions.long.quantity === 0) {
76894
- _position.entry = 0;
76895
- _position.quantity = 0;
76896
- long_state.position.entry = 0;
76897
- long_state.position.quantity = 0;
76898
- tp = {
76899
- price: 0,
76900
- quantity: 0
76905
+ for (const kind of ["long", "short"]) {
76906
+ const state = kind === "long" ? long_state : short_state;
76907
+ const state_trades = kind === "long" ? long_state_trades : short_state_trades;
76908
+ if (trades[kind].length !== state_trades.length && positions[kind].quantity !== state.position.quantity || drift[kind]) {
76909
+ let _position = {
76910
+ entry: state.position.entry,
76911
+ quantity: state.position.quantity
76901
76912
  };
76913
+ let tp = undefined;
76914
+ if (state_trades.length === 0 && positions[kind].quantity === 0) {
76915
+ _position.entry = 0;
76916
+ _position.quantity = 0;
76917
+ state.position.entry = 0;
76918
+ state.position.quantity = 0;
76919
+ tp = { price: 0, quantity: 0 };
76920
+ }
76921
+ await updateDbDetails(client, { kind, symbol }, raw_data, { take_profit: tp, position: _position, orders: state_trades });
76922
+ changed = true;
76902
76923
  }
76903
- await updateDbDetails(client, {
76904
- kind: "long",
76905
- symbol
76906
- }, raw_data, {
76907
- take_profit: tp,
76908
- position: _position,
76909
- orders: long_state_trades
76910
- });
76911
76924
  }
76912
- if (trades.short.length !== short_state_trades.length && positions.short.quantity !== short_state.position.quantity) {
76913
- let _position = {
76914
- entry: short_state.position.entry,
76915
- quantity: short_state.position.quantity
76916
- };
76917
- let tp = undefined;
76918
- if (short_state_trades.length === 0 && positions.short.quantity === 0) {
76919
- _position.entry = 0;
76920
- _position.quantity = 0;
76921
- short_state.position.entry = 0;
76922
- short_state.position.quantity = 0;
76923
- tp = {
76924
- price: 0,
76925
- quantity: 0
76926
- };
76927
- }
76928
- await updateDbDetails(client, {
76929
- kind: "short",
76930
- symbol
76931
- }, raw_data, {
76932
- position: _position,
76933
- orders: short_state_trades,
76934
- take_profit: tp
76925
+ if (!changed && priceChanged) {
76926
+ const app_db = await client.initializeAppDb();
76927
+ await app_db.createOrUpdateLiveExchangeInstance({
76928
+ account: { owner: client.owner, exchange: "paper" },
76929
+ symbol,
76930
+ data: raw_data
76935
76931
  });
76932
+ changed = true;
76936
76933
  }
76937
76934
  }
76935
+ return { long_state, short_state, changed };
76936
+ }
76937
+ async function getPositionInfo3(client, symbol) {
76938
+ const { long_state, short_state } = await reconcileEngineState(client, symbol);
76938
76939
  const long_position = long_state.position;
76939
76940
  const short_position = short_state.position;
76940
76941
  const long_quantity = long_position.quantity;
@@ -77808,7 +77809,8 @@ class ExchangeAccount {
77808
77809
  short_position.getInstance()
77809
77810
  ],
77810
77811
  long_config,
77811
- short_config
77812
+ short_config,
77813
+ kind: payload.kind
77812
77814
  });
77813
77815
  console.log("config", config2);
77814
77816
  if (!long_config || !short_config) {
package/dist/index.js CHANGED
@@ -74905,7 +74905,8 @@ function build_reduce_config(payload) {
74905
74905
  not_reduce = false,
74906
74906
  _positions,
74907
74907
  long_config,
74908
- short_config
74908
+ short_config,
74909
+ kind
74909
74910
  } = payload;
74910
74911
  const long_position = _positions.find((x) => x.kind === "long");
74911
74912
  const short_position = _positions.find((x) => x.kind === "short");
@@ -74920,8 +74921,8 @@ function build_reduce_config(payload) {
74920
74921
  short_target_pnl = Math.abs(short_position.entry - buy_price) * short_position.quantity;
74921
74922
  }
74922
74923
  return {
74923
- trigger_short: true,
74924
- trigger_long: true,
74924
+ trigger_short: kind ? kind === "short" : true,
74925
+ trigger_long: kind ? kind === "long" : true,
74925
74926
  symbol,
74926
74927
  short_minimum_pnl,
74927
74928
  long_minimum_pnl,
@@ -76705,8 +76706,7 @@ async function savePaperDetails(client, payload, new_payload) {
76705
76706
  });
76706
76707
  return await updateDbDetails(client, payload, current_account_info, new_payload);
76707
76708
  }
76708
- async function fetchDBExchangeData(client, symbol) {
76709
- const current_price = await getCurrentPrice3(client.client, symbol);
76709
+ async function fetchDBExchangeData(client, symbol, options) {
76710
76710
  let orders = [];
76711
76711
  let trades = { long: [], short: [] };
76712
76712
  let positions = {
@@ -76717,6 +76717,8 @@ async function fetchDBExchangeData(client, symbol) {
76717
76717
  long: null,
76718
76718
  short: null
76719
76719
  };
76720
+ let drift = { long: false, short: false };
76721
+ let priceChanged = false;
76720
76722
  const app_db = await client.initializeAppDb();
76721
76723
  const live_exchange_details = await app_db.getLiveExchangeInstance({
76722
76724
  account: {
@@ -76726,8 +76728,12 @@ async function fetchDBExchangeData(client, symbol) {
76726
76728
  symbol
76727
76729
  });
76728
76730
  const symbol_config = await app_db.getSymbolConfigFromDB(symbol);
76731
+ const dbPrice = live_exchange_details?.data?.current_price;
76732
+ const current_price = options?.current_price ?? dbPrice ?? await getCurrentPrice3(client.client, symbol);
76733
+ priceChanged = dbPrice !== undefined && dbPrice !== current_price;
76729
76734
  if (live_exchange_details) {
76730
76735
  const raw_data = live_exchange_details.data;
76736
+ raw_data.current_price = current_price;
76731
76737
  orders = raw_data.config.trades.exchange_info.open_orders || [];
76732
76738
  positions = raw_data.config.trades.exchange_info.positions;
76733
76739
  const long_result = updatePositionBasedOffTpOrSl({
@@ -76747,6 +76753,20 @@ async function fetchDBExchangeData(client, symbol) {
76747
76753
  positions.short = short_result.position;
76748
76754
  live_exchange_details.data.config.trades.exchange_info.open_orders = orders;
76749
76755
  live_exchange_details.data.config.trades.exchange_info.positions = positions;
76756
+ const prevLong = live_exchange_details.data.long_position;
76757
+ const prevShort = live_exchange_details.data.short_position;
76758
+ drift = {
76759
+ long: prevLong?.entry !== positions.long?.entry || prevLong?.quantity !== positions.long?.quantity,
76760
+ short: prevShort?.entry !== positions.short?.entry || prevShort?.quantity !== positions.short?.quantity
76761
+ };
76762
+ live_exchange_details.data.long_position = {
76763
+ ...prevLong,
76764
+ ...positions.long
76765
+ };
76766
+ live_exchange_details.data.short_position = {
76767
+ ...prevShort,
76768
+ ...positions.short
76769
+ };
76750
76770
  trades = {
76751
76771
  long: long_result.trades,
76752
76772
  short: short_result.trades
@@ -76759,11 +76779,13 @@ async function fetchDBExchangeData(client, symbol) {
76759
76779
  current_price,
76760
76780
  config: config2,
76761
76781
  trades,
76782
+ drift,
76783
+ priceChanged,
76762
76784
  raw_data: live_exchange_details?.data
76763
76785
  };
76764
76786
  }
76765
- async function getPositionInfo3(client, symbol) {
76766
- const { positions, symbol_config, current_price, raw_data, trades } = await fetchDBExchangeData(client, symbol);
76787
+ async function reconcileEngineState(client, symbol, options) {
76788
+ const { positions, symbol_config, current_price, raw_data, trades, drift, priceChanged } = await fetchDBExchangeData(client, symbol, options);
76767
76789
  const long_generator = new TradeEngine({
76768
76790
  position: {
76769
76791
  kind: "long",
@@ -76788,65 +76810,44 @@ async function getPositionInfo3(client, symbol) {
76788
76810
  trades: trades.short,
76789
76811
  price: current_price
76790
76812
  });
76813
+ let changed = false;
76791
76814
  if (raw_data) {
76792
76815
  const long_state_trades = long_state.newTrades;
76793
76816
  const short_state_trades = short_state.newTrades;
76794
- if (trades.long.length !== long_state_trades.length && positions.long.quantity !== long_state.position.quantity) {
76795
- console.log({
76796
- old: trades.long.length,
76797
- new: long_state_trades.length,
76798
- position: long_state.position
76799
- });
76800
- let _position = {
76801
- entry: long_state.position.entry,
76802
- quantity: long_state.position.quantity
76803
- };
76804
- let tp = undefined;
76805
- if (long_state_trades.length === 0 && positions.long.quantity === 0) {
76806
- _position.entry = 0;
76807
- _position.quantity = 0;
76808
- long_state.position.entry = 0;
76809
- long_state.position.quantity = 0;
76810
- tp = {
76811
- price: 0,
76812
- quantity: 0
76817
+ for (const kind of ["long", "short"]) {
76818
+ const state = kind === "long" ? long_state : short_state;
76819
+ const state_trades = kind === "long" ? long_state_trades : short_state_trades;
76820
+ if (trades[kind].length !== state_trades.length && positions[kind].quantity !== state.position.quantity || drift[kind]) {
76821
+ let _position = {
76822
+ entry: state.position.entry,
76823
+ quantity: state.position.quantity
76813
76824
  };
76825
+ let tp = undefined;
76826
+ if (state_trades.length === 0 && positions[kind].quantity === 0) {
76827
+ _position.entry = 0;
76828
+ _position.quantity = 0;
76829
+ state.position.entry = 0;
76830
+ state.position.quantity = 0;
76831
+ tp = { price: 0, quantity: 0 };
76832
+ }
76833
+ await updateDbDetails(client, { kind, symbol }, raw_data, { take_profit: tp, position: _position, orders: state_trades });
76834
+ changed = true;
76814
76835
  }
76815
- await updateDbDetails(client, {
76816
- kind: "long",
76817
- symbol
76818
- }, raw_data, {
76819
- take_profit: tp,
76820
- position: _position,
76821
- orders: long_state_trades
76822
- });
76823
76836
  }
76824
- if (trades.short.length !== short_state_trades.length && positions.short.quantity !== short_state.position.quantity) {
76825
- let _position = {
76826
- entry: short_state.position.entry,
76827
- quantity: short_state.position.quantity
76828
- };
76829
- let tp = undefined;
76830
- if (short_state_trades.length === 0 && positions.short.quantity === 0) {
76831
- _position.entry = 0;
76832
- _position.quantity = 0;
76833
- short_state.position.entry = 0;
76834
- short_state.position.quantity = 0;
76835
- tp = {
76836
- price: 0,
76837
- quantity: 0
76838
- };
76839
- }
76840
- await updateDbDetails(client, {
76841
- kind: "short",
76842
- symbol
76843
- }, raw_data, {
76844
- position: _position,
76845
- orders: short_state_trades,
76846
- take_profit: tp
76837
+ if (!changed && priceChanged) {
76838
+ const app_db = await client.initializeAppDb();
76839
+ await app_db.createOrUpdateLiveExchangeInstance({
76840
+ account: { owner: client.owner, exchange: "paper" },
76841
+ symbol,
76842
+ data: raw_data
76847
76843
  });
76844
+ changed = true;
76848
76845
  }
76849
76846
  }
76847
+ return { long_state, short_state, changed };
76848
+ }
76849
+ async function getPositionInfo3(client, symbol) {
76850
+ const { long_state, short_state } = await reconcileEngineState(client, symbol);
76850
76851
  const long_position = long_state.position;
76851
76852
  const short_position = short_state.position;
76852
76853
  const long_quantity = long_position.quantity;
@@ -77720,7 +77721,8 @@ class ExchangeAccount {
77720
77721
  short_position.getInstance()
77721
77722
  ],
77722
77723
  long_config,
77723
- short_config
77724
+ short_config,
77725
+ kind: payload.kind
77724
77726
  });
77725
77727
  console.log("config", config2);
77726
77728
  if (!long_config || !short_config) {
@@ -78715,7 +78715,8 @@ function build_reduce_config(payload) {
78715
78715
  not_reduce = false,
78716
78716
  _positions,
78717
78717
  long_config,
78718
- short_config
78718
+ short_config,
78719
+ kind
78719
78720
  } = payload;
78720
78721
  const long_position = _positions.find((x) => x.kind === "long");
78721
78722
  const short_position = _positions.find((x) => x.kind === "short");
@@ -78730,8 +78731,8 @@ function build_reduce_config(payload) {
78730
78731
  short_target_pnl = Math.abs(short_position.entry - buy_price) * short_position.quantity;
78731
78732
  }
78732
78733
  return {
78733
- trigger_short: true,
78734
- trigger_long: true,
78734
+ trigger_short: kind ? kind === "short" : true,
78735
+ trigger_long: kind ? kind === "long" : true,
78735
78736
  symbol,
78736
78737
  short_minimum_pnl,
78737
78738
  long_minimum_pnl,
@@ -80515,8 +80516,7 @@ async function savePaperDetails(client, payload, new_payload) {
80515
80516
  });
80516
80517
  return await updateDbDetails(client, payload, current_account_info, new_payload);
80517
80518
  }
80518
- async function fetchDBExchangeData(client, symbol) {
80519
- const current_price = await getCurrentPrice3(client.client, symbol);
80519
+ async function fetchDBExchangeData(client, symbol, options) {
80520
80520
  let orders = [];
80521
80521
  let trades = { long: [], short: [] };
80522
80522
  let positions = {
@@ -80527,6 +80527,8 @@ async function fetchDBExchangeData(client, symbol) {
80527
80527
  long: null,
80528
80528
  short: null
80529
80529
  };
80530
+ let drift = { long: false, short: false };
80531
+ let priceChanged = false;
80530
80532
  const app_db = await client.initializeAppDb();
80531
80533
  const live_exchange_details = await app_db.getLiveExchangeInstance({
80532
80534
  account: {
@@ -80536,8 +80538,12 @@ async function fetchDBExchangeData(client, symbol) {
80536
80538
  symbol
80537
80539
  });
80538
80540
  const symbol_config = await app_db.getSymbolConfigFromDB(symbol);
80541
+ const dbPrice = live_exchange_details?.data?.current_price;
80542
+ const current_price = options?.current_price ?? dbPrice ?? await getCurrentPrice3(client.client, symbol);
80543
+ priceChanged = dbPrice !== undefined && dbPrice !== current_price;
80539
80544
  if (live_exchange_details) {
80540
80545
  const raw_data = live_exchange_details.data;
80546
+ raw_data.current_price = current_price;
80541
80547
  orders = raw_data.config.trades.exchange_info.open_orders || [];
80542
80548
  positions = raw_data.config.trades.exchange_info.positions;
80543
80549
  const long_result = updatePositionBasedOffTpOrSl({
@@ -80557,6 +80563,20 @@ async function fetchDBExchangeData(client, symbol) {
80557
80563
  positions.short = short_result.position;
80558
80564
  live_exchange_details.data.config.trades.exchange_info.open_orders = orders;
80559
80565
  live_exchange_details.data.config.trades.exchange_info.positions = positions;
80566
+ const prevLong = live_exchange_details.data.long_position;
80567
+ const prevShort = live_exchange_details.data.short_position;
80568
+ drift = {
80569
+ long: prevLong?.entry !== positions.long?.entry || prevLong?.quantity !== positions.long?.quantity,
80570
+ short: prevShort?.entry !== positions.short?.entry || prevShort?.quantity !== positions.short?.quantity
80571
+ };
80572
+ live_exchange_details.data.long_position = {
80573
+ ...prevLong,
80574
+ ...positions.long
80575
+ };
80576
+ live_exchange_details.data.short_position = {
80577
+ ...prevShort,
80578
+ ...positions.short
80579
+ };
80560
80580
  trades = {
80561
80581
  long: long_result.trades,
80562
80582
  short: short_result.trades
@@ -80569,11 +80589,13 @@ async function fetchDBExchangeData(client, symbol) {
80569
80589
  current_price,
80570
80590
  config: config2,
80571
80591
  trades,
80592
+ drift,
80593
+ priceChanged,
80572
80594
  raw_data: live_exchange_details?.data
80573
80595
  };
80574
80596
  }
80575
- async function getPositionInfo3(client, symbol) {
80576
- const { positions, symbol_config, current_price, raw_data, trades } = await fetchDBExchangeData(client, symbol);
80597
+ async function reconcileEngineState(client, symbol, options) {
80598
+ const { positions, symbol_config, current_price, raw_data, trades, drift, priceChanged } = await fetchDBExchangeData(client, symbol, options);
80577
80599
  const long_generator = new TradeEngine({
80578
80600
  position: {
80579
80601
  kind: "long",
@@ -80598,65 +80620,44 @@ async function getPositionInfo3(client, symbol) {
80598
80620
  trades: trades.short,
80599
80621
  price: current_price
80600
80622
  });
80623
+ let changed = false;
80601
80624
  if (raw_data) {
80602
80625
  const long_state_trades = long_state.newTrades;
80603
80626
  const short_state_trades = short_state.newTrades;
80604
- if (trades.long.length !== long_state_trades.length && positions.long.quantity !== long_state.position.quantity) {
80605
- console.log({
80606
- old: trades.long.length,
80607
- new: long_state_trades.length,
80608
- position: long_state.position
80609
- });
80610
- let _position = {
80611
- entry: long_state.position.entry,
80612
- quantity: long_state.position.quantity
80613
- };
80614
- let tp = undefined;
80615
- if (long_state_trades.length === 0 && positions.long.quantity === 0) {
80616
- _position.entry = 0;
80617
- _position.quantity = 0;
80618
- long_state.position.entry = 0;
80619
- long_state.position.quantity = 0;
80620
- tp = {
80621
- price: 0,
80622
- quantity: 0
80627
+ for (const kind of ["long", "short"]) {
80628
+ const state = kind === "long" ? long_state : short_state;
80629
+ const state_trades = kind === "long" ? long_state_trades : short_state_trades;
80630
+ if (trades[kind].length !== state_trades.length && positions[kind].quantity !== state.position.quantity || drift[kind]) {
80631
+ let _position = {
80632
+ entry: state.position.entry,
80633
+ quantity: state.position.quantity
80623
80634
  };
80635
+ let tp = undefined;
80636
+ if (state_trades.length === 0 && positions[kind].quantity === 0) {
80637
+ _position.entry = 0;
80638
+ _position.quantity = 0;
80639
+ state.position.entry = 0;
80640
+ state.position.quantity = 0;
80641
+ tp = { price: 0, quantity: 0 };
80642
+ }
80643
+ await updateDbDetails(client, { kind, symbol }, raw_data, { take_profit: tp, position: _position, orders: state_trades });
80644
+ changed = true;
80624
80645
  }
80625
- await updateDbDetails(client, {
80626
- kind: "long",
80627
- symbol
80628
- }, raw_data, {
80629
- take_profit: tp,
80630
- position: _position,
80631
- orders: long_state_trades
80632
- });
80633
80646
  }
80634
- if (trades.short.length !== short_state_trades.length && positions.short.quantity !== short_state.position.quantity) {
80635
- let _position = {
80636
- entry: short_state.position.entry,
80637
- quantity: short_state.position.quantity
80638
- };
80639
- let tp = undefined;
80640
- if (short_state_trades.length === 0 && positions.short.quantity === 0) {
80641
- _position.entry = 0;
80642
- _position.quantity = 0;
80643
- short_state.position.entry = 0;
80644
- short_state.position.quantity = 0;
80645
- tp = {
80646
- price: 0,
80647
- quantity: 0
80648
- };
80649
- }
80650
- await updateDbDetails(client, {
80651
- kind: "short",
80652
- symbol
80653
- }, raw_data, {
80654
- position: _position,
80655
- orders: short_state_trades,
80656
- take_profit: tp
80647
+ if (!changed && priceChanged) {
80648
+ const app_db = await client.initializeAppDb();
80649
+ await app_db.createOrUpdateLiveExchangeInstance({
80650
+ account: { owner: client.owner, exchange: "paper" },
80651
+ symbol,
80652
+ data: raw_data
80657
80653
  });
80654
+ changed = true;
80658
80655
  }
80659
80656
  }
80657
+ return { long_state, short_state, changed };
80658
+ }
80659
+ async function getPositionInfo3(client, symbol) {
80660
+ const { long_state, short_state } = await reconcileEngineState(client, symbol);
80660
80661
  const long_position = long_state.position;
80661
80662
  const short_position = short_state.position;
80662
80663
  const long_quantity = long_position.quantity;
@@ -81530,7 +81531,8 @@ class ExchangeAccount {
81530
81531
  short_position.getInstance()
81531
81532
  ],
81532
81533
  long_config,
81533
- short_config
81534
+ short_config,
81535
+ kind: payload.kind
81534
81536
  });
81535
81537
  console.log("config", config2);
81536
81538
  if (!long_config || !short_config) {
@@ -78688,7 +78688,8 @@ function build_reduce_config(payload) {
78688
78688
  not_reduce = false,
78689
78689
  _positions,
78690
78690
  long_config,
78691
- short_config
78691
+ short_config,
78692
+ kind
78692
78693
  } = payload;
78693
78694
  const long_position = _positions.find((x) => x.kind === "long");
78694
78695
  const short_position = _positions.find((x) => x.kind === "short");
@@ -78703,8 +78704,8 @@ function build_reduce_config(payload) {
78703
78704
  short_target_pnl = Math.abs(short_position.entry - buy_price) * short_position.quantity;
78704
78705
  }
78705
78706
  return {
78706
- trigger_short: true,
78707
- trigger_long: true,
78707
+ trigger_short: kind ? kind === "short" : true,
78708
+ trigger_long: kind ? kind === "long" : true,
78708
78709
  symbol,
78709
78710
  short_minimum_pnl,
78710
78711
  long_minimum_pnl,
@@ -80488,8 +80489,7 @@ async function savePaperDetails(client, payload, new_payload) {
80488
80489
  });
80489
80490
  return await updateDbDetails(client, payload, current_account_info, new_payload);
80490
80491
  }
80491
- async function fetchDBExchangeData(client, symbol) {
80492
- const current_price = await getCurrentPrice3(client.client, symbol);
80492
+ async function fetchDBExchangeData(client, symbol, options) {
80493
80493
  let orders = [];
80494
80494
  let trades = { long: [], short: [] };
80495
80495
  let positions = {
@@ -80500,6 +80500,8 @@ async function fetchDBExchangeData(client, symbol) {
80500
80500
  long: null,
80501
80501
  short: null
80502
80502
  };
80503
+ let drift = { long: false, short: false };
80504
+ let priceChanged = false;
80503
80505
  const app_db = await client.initializeAppDb();
80504
80506
  const live_exchange_details = await app_db.getLiveExchangeInstance({
80505
80507
  account: {
@@ -80509,8 +80511,12 @@ async function fetchDBExchangeData(client, symbol) {
80509
80511
  symbol
80510
80512
  });
80511
80513
  const symbol_config = await app_db.getSymbolConfigFromDB(symbol);
80514
+ const dbPrice = live_exchange_details?.data?.current_price;
80515
+ const current_price = options?.current_price ?? dbPrice ?? await getCurrentPrice3(client.client, symbol);
80516
+ priceChanged = dbPrice !== undefined && dbPrice !== current_price;
80512
80517
  if (live_exchange_details) {
80513
80518
  const raw_data = live_exchange_details.data;
80519
+ raw_data.current_price = current_price;
80514
80520
  orders = raw_data.config.trades.exchange_info.open_orders || [];
80515
80521
  positions = raw_data.config.trades.exchange_info.positions;
80516
80522
  const long_result = updatePositionBasedOffTpOrSl({
@@ -80530,6 +80536,20 @@ async function fetchDBExchangeData(client, symbol) {
80530
80536
  positions.short = short_result.position;
80531
80537
  live_exchange_details.data.config.trades.exchange_info.open_orders = orders;
80532
80538
  live_exchange_details.data.config.trades.exchange_info.positions = positions;
80539
+ const prevLong = live_exchange_details.data.long_position;
80540
+ const prevShort = live_exchange_details.data.short_position;
80541
+ drift = {
80542
+ long: prevLong?.entry !== positions.long?.entry || prevLong?.quantity !== positions.long?.quantity,
80543
+ short: prevShort?.entry !== positions.short?.entry || prevShort?.quantity !== positions.short?.quantity
80544
+ };
80545
+ live_exchange_details.data.long_position = {
80546
+ ...prevLong,
80547
+ ...positions.long
80548
+ };
80549
+ live_exchange_details.data.short_position = {
80550
+ ...prevShort,
80551
+ ...positions.short
80552
+ };
80533
80553
  trades = {
80534
80554
  long: long_result.trades,
80535
80555
  short: short_result.trades
@@ -80542,11 +80562,13 @@ async function fetchDBExchangeData(client, symbol) {
80542
80562
  current_price,
80543
80563
  config: config2,
80544
80564
  trades,
80565
+ drift,
80566
+ priceChanged,
80545
80567
  raw_data: live_exchange_details?.data
80546
80568
  };
80547
80569
  }
80548
- async function getPositionInfo3(client, symbol) {
80549
- const { positions, symbol_config, current_price, raw_data, trades } = await fetchDBExchangeData(client, symbol);
80570
+ async function reconcileEngineState(client, symbol, options) {
80571
+ const { positions, symbol_config, current_price, raw_data, trades, drift, priceChanged } = await fetchDBExchangeData(client, symbol, options);
80550
80572
  const long_generator = new TradeEngine({
80551
80573
  position: {
80552
80574
  kind: "long",
@@ -80571,65 +80593,44 @@ async function getPositionInfo3(client, symbol) {
80571
80593
  trades: trades.short,
80572
80594
  price: current_price
80573
80595
  });
80596
+ let changed = false;
80574
80597
  if (raw_data) {
80575
80598
  const long_state_trades = long_state.newTrades;
80576
80599
  const short_state_trades = short_state.newTrades;
80577
- if (trades.long.length !== long_state_trades.length && positions.long.quantity !== long_state.position.quantity) {
80578
- console.log({
80579
- old: trades.long.length,
80580
- new: long_state_trades.length,
80581
- position: long_state.position
80582
- });
80583
- let _position = {
80584
- entry: long_state.position.entry,
80585
- quantity: long_state.position.quantity
80586
- };
80587
- let tp = undefined;
80588
- if (long_state_trades.length === 0 && positions.long.quantity === 0) {
80589
- _position.entry = 0;
80590
- _position.quantity = 0;
80591
- long_state.position.entry = 0;
80592
- long_state.position.quantity = 0;
80593
- tp = {
80594
- price: 0,
80595
- quantity: 0
80600
+ for (const kind of ["long", "short"]) {
80601
+ const state = kind === "long" ? long_state : short_state;
80602
+ const state_trades = kind === "long" ? long_state_trades : short_state_trades;
80603
+ if (trades[kind].length !== state_trades.length && positions[kind].quantity !== state.position.quantity || drift[kind]) {
80604
+ let _position = {
80605
+ entry: state.position.entry,
80606
+ quantity: state.position.quantity
80596
80607
  };
80608
+ let tp = undefined;
80609
+ if (state_trades.length === 0 && positions[kind].quantity === 0) {
80610
+ _position.entry = 0;
80611
+ _position.quantity = 0;
80612
+ state.position.entry = 0;
80613
+ state.position.quantity = 0;
80614
+ tp = { price: 0, quantity: 0 };
80615
+ }
80616
+ await updateDbDetails(client, { kind, symbol }, raw_data, { take_profit: tp, position: _position, orders: state_trades });
80617
+ changed = true;
80597
80618
  }
80598
- await updateDbDetails(client, {
80599
- kind: "long",
80600
- symbol
80601
- }, raw_data, {
80602
- take_profit: tp,
80603
- position: _position,
80604
- orders: long_state_trades
80605
- });
80606
80619
  }
80607
- if (trades.short.length !== short_state_trades.length && positions.short.quantity !== short_state.position.quantity) {
80608
- let _position = {
80609
- entry: short_state.position.entry,
80610
- quantity: short_state.position.quantity
80611
- };
80612
- let tp = undefined;
80613
- if (short_state_trades.length === 0 && positions.short.quantity === 0) {
80614
- _position.entry = 0;
80615
- _position.quantity = 0;
80616
- short_state.position.entry = 0;
80617
- short_state.position.quantity = 0;
80618
- tp = {
80619
- price: 0,
80620
- quantity: 0
80621
- };
80622
- }
80623
- await updateDbDetails(client, {
80624
- kind: "short",
80625
- symbol
80626
- }, raw_data, {
80627
- position: _position,
80628
- orders: short_state_trades,
80629
- take_profit: tp
80620
+ if (!changed && priceChanged) {
80621
+ const app_db = await client.initializeAppDb();
80622
+ await app_db.createOrUpdateLiveExchangeInstance({
80623
+ account: { owner: client.owner, exchange: "paper" },
80624
+ symbol,
80625
+ data: raw_data
80630
80626
  });
80627
+ changed = true;
80631
80628
  }
80632
80629
  }
80630
+ return { long_state, short_state, changed };
80631
+ }
80632
+ async function getPositionInfo3(client, symbol) {
80633
+ const { long_state, short_state } = await reconcileEngineState(client, symbol);
80633
80634
  const long_position = long_state.position;
80634
80635
  const short_position = short_state.position;
80635
80636
  const long_quantity = long_position.quantity;
@@ -81503,7 +81504,8 @@ class ExchangeAccount {
81503
81504
  short_position.getInstance()
81504
81505
  ],
81505
81506
  long_config,
81506
- short_config
81507
+ short_config,
81508
+ kind: payload.kind
81507
81509
  });
81508
81510
  console.log("config", config2);
81509
81511
  if (!long_config || !short_config) {
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.64",
4
+ "version": "0.0.2-next.66",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",