{"action":"created","comment":{"url":"https://api.github.com/repos/quantopian/zipline/pulls/comments/121786628","pull_request_review_id":43837579,"id":121786628,"diff_hunk":"@@ -194,6 +200,87 @@ def current_portfolio_weights(self):\n         return position_values / self.portfolio_value\n \n \n+class AlgorithmPortfolio(Portfolio):\n+\n+    def __init__(self, data_portal, benchmark=None):\n+        super(AlgorithmPortfolio, self).__init__()\n+        self.data_portal = data_portal\n+        self.benchmark = benchmark\n+        self.current_date = None\n+\n+    @property\n+    def expected_shortfall(self):\n+        \"\"\"\n+        Function for computing expected shortfall (also known as CVaR, or\n+        Conditional Value at Risk) for the portfolio according to the assets\n+        currently held and their respective weight in the portfolio.\n+\n+        This function requires a data portal in order to retrieve price\n+        histories of the assets in the portfolio.\n+        \"\"\"\n+        data_portal = self.data_portal\n+        current_date = self.current_date\n+\n+        # If we do not even have a year's worth of data to look back on\n+        # then the expected shortfall calculation will not be reliable, so\n+        # just return NaN. If we only have between one and two years of\n+        # data just use what is available.\n+        num_days_of_data = data_portal.trading_calendar.session_distance(\n+            self.start_date, current_date,\n+        )\n+        if num_days_of_data < TRADING_DAYS_PER_YEAR:\n+            return np.NaN\n+        elif num_days_of_data < CVAR_LOOKBACK_DAYS:\n+            num_lookback_days = num_days_of_data\n+        else:\n+            num_lookback_days = CVAR_LOOKBACK_DAYS\n+\n+        # Series mapping each asset to its portfolio weight.\n+        weights = self.current_portfolio_weights","path":"zipline/protocol.py","position":64,"original_position":64,"commit_id":"f89df28a2e26d90cbf750b2edcbae815a8f86e26","original_commit_id":"f89df28a2e26d90cbf750b2edcbae815a8f86e26","user":{"login":"ssanderson","id":1588357,"avatar_url":"https://avatars3.githubusercontent.com/u/1588357?v=3","gravatar_id":"","url":"https://api.github.com/users/ssanderson","html_url":"https://github.com/ssanderson","followers_url":"https://api.github.com/users/ssanderson/followers","following_url":"https://api.github.com/users/ssanderson/following{/other_user}","gists_url":"https://api.github.com/users/ssanderson/gists{/gist_id}","starred_url":"https://api.github.com/users/ssanderson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ssanderson/subscriptions","organizations_url":"https://api.github.com/users/ssanderson/orgs","repos_url":"https://api.github.com/users/ssanderson/repos","events_url":"https://api.github.com/users/ssanderson/events{/privacy}","received_events_url":"https://api.github.com/users/ssanderson/received_events","type":"User","site_admin":false},"body":"Where does current_portfolio_weights come from?","created_at":"2017-06-13T20:23:25Z","updated_at":"2017-06-13T21:04:48Z","html_url":"https://github.com/quantopian/zipline/pull/1839#discussion_r121786628","pull_request_url":"https://api.github.com/repos/quantopian/zipline/pulls/1839","_links":{"self":{"href":"https://api.github.com/repos/quantopian/zipline/pulls/comments/121786628"},"html":{"href":"https://github.com/quantopian/zipline/pull/1839#discussion_r121786628"},"pull_request":{"href":"https://api.github.com/repos/quantopian/zipline/pulls/1839"}}},"pull_request":{"url":"https://api.github.com/repos/quantopian/zipline/pulls/1839","id":124929671,"html_url":"https://github.com/quantopian/zipline/pull/1839","diff_url":"https://github.com/quantopian/zipline/pull/1839.diff","patch_url":"https://github.com/quantopian/zipline/pull/1839.patch","issue_url":"https://api.github.com/repos/quantopian/zipline/issues/1839","number":1839,"state":"open","locked":false,"title":"Add `expected_shortfall` method to portfolio object","user":{"login":"dmichalowicz","id":13137798,"avatar_url":"https://avatars0.githubusercontent.com/u/13137798?v=3","gravatar_id":"","url":"https://api.github.com/users/dmichalowicz","html_url":"https://github.com/dmichalowicz","followers_url":"https://api.github.com/users/dmichalowicz/followers","following_url":"https://api.github.com/users/dmichalowicz/following{/other_user}","gists_url":"https://api.github.com/users/dmichalowicz/gists{/gist_id}","starred_url":"https://api.github.com/users/dmichalowicz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmichalowicz/subscriptions","organizations_url":"https://api.github.com/users/dmichalowicz/orgs","repos_url":"https://api.github.com/users/dmichalowicz/repos","events_url":"https://api.github.com/users/dmichalowicz/events{/privacy}","received_events_url":"https://api.github.com/users/dmichalowicz/received_events","type":"User","site_admin":false},"body":"- Create a method called `calculate_expected_shortfall` on `context.portfolio` which computes the expected shortfall of the portfolio according the 2-year returns history of the assets currently being held, taking into account the weights of each holding in the portfolio.\r\n- Add expected shortfall (aka CVaR) as a metric to algorithm risk results.","created_at":"2017-06-09T18:56:45Z","updated_at":"2017-06-13T21:04:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6cab6011d0d914388dd49ce4fc7bc64acdc973ef","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/quantopian/zipline/pulls/1839/commits","review_comments_url":"https://api.github.com/repos/quantopian/zipline/pulls/1839/comments","review_comment_url":"https://api.github.com/repos/quantopian/zipline/pulls/comments{/number}","comments_url":"https://api.github.com/repos/quantopian/zipline/issues/1839/comments","statuses_url":"https://api.github.com/repos/quantopian/zipline/statuses/f89df28a2e26d90cbf750b2edcbae815a8f86e26","head":{"label":"quantopian:var-and-cvar","ref":"var-and-cvar","sha":"f89df28a2e26d90cbf750b2edcbae815a8f86e26","user":{"login":"quantopian","id":1393215,"avatar_url":"https://avatars3.githubusercontent.com/u/1393215?v=3","gravatar_id":"","url":"https://api.github.com/users/quantopian","html_url":"https://github.com/quantopian","followers_url":"https://api.github.com/users/quantopian/followers","following_url":"https://api.github.com/users/quantopian/following{/other_user}","gists_url":"https://api.github.com/users/quantopian/gists{/gist_id}","starred_url":"https://api.github.com/users/quantopian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/quantopian/subscriptions","organizations_url":"https://api.github.com/users/quantopian/orgs","repos_url":"https://api.github.com/users/quantopian/repos","events_url":"https://api.github.com/users/quantopian/events{/privacy}","received_events_url":"https://api.github.com/users/quantopian/received_events","type":"Organization","site_admin":false},"repo":{"id":6297520,"name":"zipline","full_name":"quantopian/zipline","owner":{"login":"quantopian","id":1393215,"avatar_url":"https://avatars3.githubusercontent.com/u/1393215?v=3","gravatar_id":"","url":"https://api.github.com/users/quantopian","html_url":"https://github.com/quantopian","followers_url":"https://api.github.com/users/quantopian/followers","following_url":"https://api.github.com/users/quantopian/following{/other_user}","gists_url":"https://api.github.com/users/quantopian/gists{/gist_id}","starred_url":"https://api.github.com/users/quantopian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/quantopian/subscriptions","organizations_url":"https://api.github.com/users/quantopian/orgs","repos_url":"https://api.github.com/users/quantopian/repos","events_url":"https://api.github.com/users/quantopian/events{/privacy}","received_events_url":"https://api.github.com/users/quantopian/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/quantopian/zipline","description":"Zipline, a Pythonic Algorithmic Trading Library","fork":false,"url":"https://api.github.com/repos/quantopian/zipline","forks_url":"https://api.github.com/repos/quantopian/zipline/forks","keys_url":"https://api.github.com/repos/quantopian/zipline/keys{/key_id}","collaborators_url":"https://api.github.com/repos/quantopian/zipline/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/quantopian/zipline/teams","hooks_url":"https://api.github.com/repos/quantopian/zipline/hooks","issue_events_url":"https://api.github.com/repos/quantopian/zipline/issues/events{/number}","events_url":"https://api.github.com/repos/quantopian/zipline/events","assignees_url":"https://api.github.com/repos/quantopian/zipline/assignees{/user}","branches_url":"https://api.github.com/repos/quantopian/zipline/branches{/branch}","tags_url":"https://api.github.com/repos/quantopian/zipline/tags","blobs_url":"https://api.github.com/repos/quantopian/zipline/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/quantopian/zipline/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/quantopian/zipline/git/refs{/sha}","trees_url":"https://api.github.com/repos/quantopian/zipline/git/trees{/sha}","statuses_url":"https://api.github.com/repos/quantopian/zipline/statuses/{sha}","languages_url":"https://api.github.com/repos/quantopian/zipline/languages","stargazers_url":"https://api.github.com/repos/quantopian/zipline/stargazers","contributors_url":"https://api.github.com/repos/quantopian/zipline/contributors","subscribers_url":"https://api.github.com/repos/quantopian/zipline/subscribers","subscription_url":"https://api.github.com/repos/quantopian/zipline/subscription","commits_url":"https://api.github.com/repos/quantopian/zipline/commits{/sha}","git_commits_url":"https://api.github.com/repos/quantopian/zipline/git/commits{/sha}","comments_url":"https://api.github.com/repos/quantopian/zipline/comments{/number}","issue_comment_url":"https://api.github.com/repos/quantopian/zipline/issues/comments{/number}","contents_url":"https://api.github.com/repos/quantopian/zipline/contents/{+path}","compare_url":"https://api.github.com/repos/quantopian/zipline/compare/{base}...{head}","merges_url":"https://api.github.com/repos/quantopian/zipline/merges","archive_url":"https://api.github.com/repos/quantopian/zipline/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/quantopian/zipline/downloads","issues_url":"https://api.github.com/repos/quantopian/zipline/issues{/number}","pulls_url":"https://api.github.com/repos/quantopian/zipline/pulls{/number}","milestones_url":"https://api.github.com/repos/quantopian/zipline/milestones{/number}","notifications_url":"https://api.github.com/repos/quantopian/zipline/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/quantopian/zipline/labels{/name}","releases_url":"https://api.github.com/repos/quantopian/zipline/releases{/id}","deployments_url":"https://api.github.com/repos/quantopian/zipline/deployments","created_at":"2012-10-19T15:50:29Z","updated_at":"2017-06-13T11:23:19Z","pushed_at":"2017-06-13T13:28:49Z","git_url":"git://github.com/quantopian/zipline.git","ssh_url":"git@github.com:quantopian/zipline.git","clone_url":"https://github.com/quantopian/zipline.git","svn_url":"https://github.com/quantopian/zipline","homepage":"http://www.zipline.io/","size":140952,"stargazers_count":5038,"watchers_count":5038,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1628,"mirror_url":null,"open_issues_count":196,"forks":1628,"open_issues":196,"watchers":5038,"default_branch":"master"}},"base":{"label":"quantopian:master","ref":"master","sha":"0840ea1bef84a7af0fd18812831de9e5b8c16130","user":{"login":"quantopian","id":1393215,"avatar_url":"https://avatars3.githubusercontent.com/u/1393215?v=3","gravatar_id":"","url":"https://api.github.com/users/quantopian","html_url":"https://github.com/quantopian","followers_url":"https://api.github.com/users/quantopian/followers","following_url":"https://api.github.com/users/quantopian/following{/other_user}","gists_url":"https://api.github.com/users/quantopian/gists{/gist_id}","starred_url":"https://api.github.com/users/quantopian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/quantopian/subscriptions","organizations_url":"https://api.github.com/users/quantopian/orgs","repos_url":"https://api.github.com/users/quantopian/repos","events_url":"https://api.github.com/users/quantopian/events{/privacy}","received_events_url":"https://api.github.com/users/quantopian/received_events","type":"Organization","site_admin":false},"repo":{"id":6297520,"name":"zipline","full_name":"quantopian/zipline","owner":{"login":"quantopian","id":1393215,"avatar_url":"https://avatars3.githubusercontent.com/u/1393215?v=3","gravatar_id":"","url":"https://api.github.com/users/quantopian","html_url":"https://github.com/quantopian","followers_url":"https://api.github.com/users/quantopian/followers","following_url":"https://api.github.com/users/quantopian/following{/other_user}","gists_url":"https://api.github.com/users/quantopian/gists{/gist_id}","starred_url":"https://api.github.com/users/quantopian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/quantopian/subscriptions","organizations_url":"https://api.github.com/users/quantopian/orgs","repos_url":"https://api.github.com/users/quantopian/repos","events_url":"https://api.github.com/users/quantopian/events{/privacy}","received_events_url":"https://api.github.com/users/quantopian/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/quantopian/zipline","description":"Zipline, a Pythonic Algorithmic Trading Library","fork":false,"url":"https://api.github.com/repos/quantopian/zipline","forks_url":"https://api.github.com/repos/quantopian/zipline/forks","keys_url":"https://api.github.com/repos/quantopian/zipline/keys{/key_id}","collaborators_url":"https://api.github.com/repos/quantopian/zipline/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/quantopian/zipline/teams","hooks_url":"https://api.github.com/repos/quantopian/zipline/hooks","issue_events_url":"https://api.github.com/repos/quantopian/zipline/issues/events{/number}","events_url":"https://api.github.com/repos/quantopian/zipline/events","assignees_url":"https://api.github.com/repos/quantopian/zipline/assignees{/user}","branches_url":"https://api.github.com/repos/quantopian/zipline/branches{/branch}","tags_url":"https://api.github.com/repos/quantopian/zipline/tags","blobs_url":"https://api.github.com/repos/quantopian/zipline/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/quantopian/zipline/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/quantopian/zipline/git/refs{/sha}","trees_url":"https://api.github.com/repos/quantopian/zipline/git/trees{/sha}","statuses_url":"https://api.github.com/repos/quantopian/zipline/statuses/{sha}","languages_url":"https://api.github.com/repos/quantopian/zipline/languages","stargazers_url":"https://api.github.com/repos/quantopian/zipline/stargazers","contributors_url":"https://api.github.com/repos/quantopian/zipline/contributors","subscribers_url":"https://api.github.com/repos/quantopian/zipline/subscribers","subscription_url":"https://api.github.com/repos/quantopian/zipline/subscription","commits_url":"https://api.github.com/repos/quantopian/zipline/commits{/sha}","git_commits_url":"https://api.github.com/repos/quantopian/zipline/git/commits{/sha}","comments_url":"https://api.github.com/repos/quantopian/zipline/comments{/number}","issue_comment_url":"https://api.github.com/repos/quantopian/zipline/issues/comments{/number}","contents_url":"https://api.github.com/repos/quantopian/zipline/contents/{+path}","compare_url":"https://api.github.com/repos/quantopian/zipline/compare/{base}...{head}","merges_url":"https://api.github.com/repos/quantopian/zipline/merges","archive_url":"https://api.github.com/repos/quantopian/zipline/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/quantopian/zipline/downloads","issues_url":"https://api.github.com/repos/quantopian/zipline/issues{/number}","pulls_url":"https://api.github.com/repos/quantopian/zipline/pulls{/number}","milestones_url":"https://api.github.com/repos/quantopian/zipline/milestones{/number}","notifications_url":"https://api.github.com/repos/quantopian/zipline/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/quantopian/zipline/labels{/name}","releases_url":"https://api.github.com/repos/quantopian/zipline/releases{/id}","deployments_url":"https://api.github.com/repos/quantopian/zipline/deployments","created_at":"2012-10-19T15:50:29Z","updated_at":"2017-06-13T11:23:19Z","pushed_at":"2017-06-13T13:28:49Z","git_url":"git://github.com/quantopian/zipline.git","ssh_url":"git@github.com:quantopian/zipline.git","clone_url":"https://github.com/quantopian/zipline.git","svn_url":"https://github.com/quantopian/zipline","homepage":"http://www.zipline.io/","size":140952,"stargazers_count":5038,"watchers_count":5038,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1628,"mirror_url":null,"open_issues_count":196,"forks":1628,"open_issues":196,"watchers":5038,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/quantopian/zipline/pulls/1839"},"html":{"href":"https://github.com/quantopian/zipline/pull/1839"},"issue":{"href":"https://api.github.com/repos/quantopian/zipline/issues/1839"},"comments":{"href":"https://api.github.com/repos/quantopian/zipline/issues/1839/comments"},"review_comments":{"href":"https://api.github.com/repos/quantopian/zipline/pulls/1839/comments"},"review_comment":{"href":"https://api.github.com/repos/quantopian/zipline/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/quantopian/zipline/pulls/1839/commits"},"statuses":{"href":"https://api.github.com/repos/quantopian/zipline/statuses/f89df28a2e26d90cbf750b2edcbae815a8f86e26"}}}}
{"actor":{"display_login":"ssanderson"}}