Getting Your Portfolio History From Alpaca using Python

Decentral Farm
4 min readOct 6, 2023

Check and chart your Portfolio History programmatically

I was playing with the Node JS Alpaca API Library and discovered that actually you can get the history of your portfolio very suitable for being use in a chart, and I suspect is the way Alpaca graph the daily, monthly, yearly and all the value of your equity.

Alpaca Paper trading account Portfolio Chart as appears on the Home page.

I turned to the Python library to retrieve and plot the Portfolio History, but after digging for a while I found that the Python library have not yet implemented that function.

Extending the TradingClient class

The solution I found suit best my needs was extending the TradingClient class.

from alpaca.trading.requests import GetPortfolioHistoryRequest
from alpaca.trading.client import TradingClient
from alpaca.common.enums import BaseURL
from typing import Optional, List, Union
from alpaca.trading.models import PortfolioHistory
from alpaca.common import RawData
import config

class TradingClientPortfolioAble(TradingClient):
def __init__(
self,
api_key: Optional[str] = None,
secret_key: Optional[str] = None,
paper…

--

--

Decentral Farm

Writing about programming, hustles, investing, money and cryptos! Glad to help! Tips ⚡decentralfarm@getalby.com⚡| https://www.buymeacoffee.com/decentralfarm