14
14
# You should have received a copy of the GNU Lesser General Public
15
15
# License along with this library
16
16
import octobot_commons .enums as commons_enums
17
+ import octobot_commons .authentication as authentication
17
18
import octobot_commons .constants as commons_constants
18
19
import octobot_commons .databases as commons_databases
20
+ import octobot_commons .tree as commons_tree
19
21
20
22
import octobot_trading .storage .abstract_storage as abstract_storage
23
+ import octobot_trading .personal_data .portfolios .history as portfolio_history
21
24
22
25
23
26
class PortfolioStorage (abstract_storage .AbstractStorage ):
24
27
IS_LIVE_CONSUMER = False
25
28
IS_HISTORICAL = True
29
+ PRICE_INIT_TIMEOUT = 30
26
30
27
31
async def store_history (self ):
28
32
if not self .enabled :
@@ -32,13 +36,44 @@ async def store_history(self):
32
36
portfolio_manager .historical_portfolio_value_manager
33
37
metadata = hist_portfolio_values_manager .get_metadata ()
34
38
# replace the whole table to ensure consistency
39
+ history = hist_portfolio_values_manager .get_dict_historical_values ()
35
40
await portfolio_db .upsert (commons_enums .RunDatabases .METADATA .value , metadata , None , uuid = 1 )
36
41
await portfolio_db .replace_all (
37
42
commons_enums .RunDatabases .HISTORICAL_PORTFOLIO_VALUE .value ,
38
- hist_portfolio_values_manager . get_dict_historical_values () ,
43
+ history ,
39
44
cache = False
40
45
)
41
46
await portfolio_db .flush ()
47
+ await self .trigger_debounced_update_auth_data ()
48
+
49
+ async def _update_auth_data (self ):
50
+ hist_portfolio_values_manager = self .exchange_manager .exchange_personal_data . \
51
+ portfolio_manager .historical_portfolio_value_manager
52
+ authenticator = authentication .Authenticator .instance ()
53
+ history = hist_portfolio_values_manager .get_dict_historical_values ()
54
+ if history and authenticator .is_initialized ():
55
+ if hist_portfolio_values_manager .portfolio_manager .portfolio_value_holder .initializing_symbol_prices_pairs :
56
+ for symbol in hist_portfolio_values_manager .portfolio_manager .portfolio_value_holder .initializing_symbol_prices_pairs :
57
+ await commons_tree .EventProvider .instance ().wait_for_event (
58
+ self .exchange_manager .bot_id ,
59
+ commons_tree .get_exchange_path (
60
+ self .exchange_manager .exchange_name ,
61
+ commons_enums .InitializationEventExchangeTopics .PRICE .value ,
62
+ symbol = symbol ,
63
+ ),
64
+ self .PRICE_INIT_TIMEOUT
65
+ )
66
+ await authenticator .update_portfolio (
67
+ history [- 1 ][portfolio_history .HistoricalAssetValue .VALUES_KEY ],
68
+ history [0 ][portfolio_history .HistoricalAssetValue .VALUES_KEY ],
69
+ hist_portfolio_values_manager .portfolio_manager .reference_market ,
70
+ hist_portfolio_values_manager .ending_portfolio ,
71
+ {
72
+ history_val [portfolio_history .HistoricalAssetValue .TIMESTAMP_KEY ]: history_val [portfolio_history .HistoricalAssetValue .VALUES_KEY ]
73
+ for history_val in history
74
+ },
75
+ hist_portfolio_values_manager .portfolio_manager .portfolio_value_holder .current_crypto_currencies_values
76
+ )
42
77
43
78
def get_db (self ):
44
79
return commons_databases .RunDatabasesProvider .instance ().get_historical_portfolio_value_db (
0 commit comments