mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-05 15:33:45 +08:00
BLD: saving the cumulative_performance only which seems sufficient to keep the algo state. more testing required.
This commit is contained in:
@@ -44,9 +44,9 @@ from catalyst.exchange.stats_utils import get_pretty_stats, stats_to_s3, \
|
||||
stats_to_algo_folder
|
||||
from catalyst.exchange.utils.serialization import portfolio_to_dict
|
||||
from catalyst.finance.execution import MarketOrder
|
||||
from catalyst.finance.performance import PerformanceTracker
|
||||
from catalyst.finance.performance import PerformanceTracker, Position
|
||||
from catalyst.finance.performance.period import calc_period_stats
|
||||
from catalyst.protocol import Positions, Position
|
||||
from catalyst.finance.performance.position import positiondict
|
||||
from catalyst.gens.tradesimulation import AlgorithmSimulator
|
||||
from catalyst.utils.api_support import api_method
|
||||
from catalyst.utils.input_validation import error_keywords, ensure_upper_case
|
||||
@@ -353,7 +353,7 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase):
|
||||
self.retry_order = 2
|
||||
self.retry_delay = 5
|
||||
|
||||
self.stats_minutes = 10
|
||||
self.stats_minutes = 1
|
||||
|
||||
super(ExchangeTradingAlgorithmLive, self).__init__(*args, **kwargs)
|
||||
|
||||
@@ -446,39 +446,10 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase):
|
||||
# Unpacking the perf_tracker and positions if available
|
||||
perf = get_algo_object(
|
||||
algo_name=self.algo_namespace,
|
||||
key='perf_tracker',
|
||||
key='cumulative_performance',
|
||||
)
|
||||
if perf is not None:
|
||||
# Unpack the position and converting dict or object
|
||||
p = get_algo_object(
|
||||
algo_name=self.algo_namespace,
|
||||
key='portfolio',
|
||||
how='json',
|
||||
)
|
||||
portfolio = Portfolio()
|
||||
portfolio.capital_used = p['capital_used']
|
||||
portfolio.starting_cash = p['starting_cash']
|
||||
portfolio.portfolio_value = p['portfolio_value']
|
||||
portfolio.pnl = p['pnl']
|
||||
portfolio.returns = p['returns']
|
||||
portfolio.cash = p['cash']
|
||||
portfolio.start_date = p['start_date']
|
||||
portfolio.positions_value = p['positions_value']
|
||||
|
||||
portfolio.positions = positions = Positions()
|
||||
for p in p['positions']:
|
||||
exchange = self.exchanges[p['exchange']]
|
||||
asset = exchange.get_asset(p['symbol'])
|
||||
positions[asset] = Position(
|
||||
asset=asset,
|
||||
amount=p['amount'],
|
||||
cost_basis=p['cost_basis'],
|
||||
last_sale_price=p['last_sale_price'],
|
||||
last_sale_date=None,
|
||||
)
|
||||
|
||||
tracker.period_start = perf['period_start']
|
||||
tracker.position_tracker.positions = portfolio.positions
|
||||
tracker.cumulative_performance = perf
|
||||
|
||||
# Call the simulation trading algorithm for side-effects:
|
||||
# it creates the perf tracker
|
||||
@@ -703,15 +674,8 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase):
|
||||
|
||||
save_algo_object(
|
||||
algo_name=self.algo_namespace,
|
||||
key='perf_tracker',
|
||||
obj=self.perf_tracker.to_dict(emission_type=self.data_frequency),
|
||||
)
|
||||
portfolio = portfolio_to_dict(self.portfolio)
|
||||
save_algo_object(
|
||||
algo_name=self.algo_namespace,
|
||||
key='portfolio',
|
||||
obj=portfolio,
|
||||
how='json',
|
||||
key='cumulative_performance',
|
||||
obj=self.perf_tracker.cumulative_performance,
|
||||
)
|
||||
|
||||
self.current_day = data.current_dt.floor('1D')
|
||||
@@ -747,7 +711,7 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase):
|
||||
stats=get_pretty_stats(
|
||||
stats=self.frame_stats,
|
||||
recorded_cols=recorded_cols,
|
||||
num_rows=self.stats_minutes
|
||||
num_rows=self.stats_minutes,
|
||||
)
|
||||
))
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import pandas as pd
|
||||
from catalyst.assets._assets import TradingPair
|
||||
|
||||
from catalyst.exchange.exchange_utils import get_algo_folder
|
||||
from catalyst.utils.paths import data_root
|
||||
from catalyst.utils.paths import data_root, ensure_directory
|
||||
|
||||
s3_conn = []
|
||||
mailgun = []
|
||||
@@ -403,7 +403,10 @@ def stats_to_algo_folder(stats, algo_namespace, recorded_cols=None):
|
||||
timestr = time.strftime('%Y%m%d')
|
||||
folder = get_algo_folder(algo_namespace)
|
||||
|
||||
filename = os.path.join(folder, '{}-{}.csv'.format(timestr, 'frames'))
|
||||
stats_folder = os.path.join(folder, 'stats')
|
||||
ensure_directory(stats_folder)
|
||||
|
||||
filename = os.path.join(stats_folder, '{}.csv'.format(timestr))
|
||||
|
||||
with open(filename, 'wb') as handle:
|
||||
handle.write(bytes_to_write)
|
||||
|
||||
@@ -46,20 +46,23 @@ class ExchangeJSONDecoder(json.JSONDecoder):
|
||||
|
||||
|
||||
def portfolio_to_dict(portfolio):
|
||||
positions = portfolio.positions
|
||||
positions = []
|
||||
for asset in portfolio.positions:
|
||||
position = portfolio.positions[asset].to_dict()
|
||||
|
||||
position['symbol'] = asset.symbol
|
||||
position['exchange'] = asset.exchange
|
||||
del position['sid']
|
||||
p = portfolio.positions[asset] # Type: Position
|
||||
|
||||
position = dict(
|
||||
symbol=asset.symbol,
|
||||
exchange=asset.exchange,
|
||||
amount=p.amount,
|
||||
cost_basis=p.cost_basis,
|
||||
last_sale_price=p.last_sale_price,
|
||||
last_sale_date=p.last_sale_date,
|
||||
)
|
||||
positions.append(position)
|
||||
|
||||
portfolio_dict = vars(portfolio)
|
||||
del portfolio_dict['positions']
|
||||
|
||||
portfolio_dict['positions'] = positions
|
||||
|
||||
return portfolio_dict
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user