API functions: Strategy
API wrapper functions for strategy endpoints
Strategy Functions | API Credits | |||
---|---|---|---|---|
strategy | 1 | This operation returns the data from the Summary, Current Holdings and Statistics tabs of a strategy or book. | ||
strategy_holdings | 1 | Get strategy holdings on a particular date (defaults to today). | ||
strategy_rebalance | 1 | Get rebalance recommendations. | ||
strategy_rebalance_commit | 1 | Commit rebalance transactions. | ||
strategy_transactions | 1 | Get transaction history for a date or date range | ||
strategy_transaction_import | 1 | Import transactions. | ||
strategy_transaction_delete | 1 | Delete strategy transactions. |
strategy()
Wrapper for GET /strategy/{id}
This operation returns the data from the following pages:
Summary
Holdings
Statistics
Performance
Risk Measurements
Trading (except for simulated books)
p123api.Client().strategy(
strategy_id=123
)
strategy_holdings()
Wrapper for GET /strategy/{id}/holdings
Get the holdings for this strategy as of a single date (defaults to today).
p123api.Client().strategy_holdings(
strategy_id = 123,
date = None, # "YYYY-MM-DD"
to_pandas = False
)
strategy_rebalance()
Wrapper for GET /strategy/{id}/rebalance
Get rebalance recommendations for a strategy. To reject recommendation and get replacements, call the method again with the P123 ids to reject in the "reject" parameter.
p123api.Client().strategy_rebalance(
strategy_id=123,
# optional parameters
params={
"pitMethod": "Prelim" | "Complete",
"reject": [ int, int, ... ], # list of P123ID
"figi": "Share Class" | "Country Composite"
# Required for Dynamic Weight
"op": "Rebal" | "Recon" | "ReconRebal",
}
)
strategy_rebalance_commit()
Wrapper for GET /strategy/{id}/rebalance/commit
Commit rebalance transactions for a strategy. To get recommendations, call the strategy_rebalance() method.
p123api.Client().strategy_rebalance_commit(
strategy_id=123,
params={
"op": <op from strategy_rebalance()>,
"ranks": <ranks from strategy_rebalance()>,
"trans": [
{
"p123Uid": 1073741824, "action": "BUY", "price": 0.1, "shares": 0.1,
"comm": 0.1, "slip": 0.1, "note": "string"
},
{...}
]
}
)
IMPORTANT:
Edit "trans" with actual trading information. For example exclude transactions that were not filled or change the price, shares, commission and slippage.
To update the holdings ranks you must call this function even if there are no transactions
strategy_transactions()
Wrapper for GET /strategy/{id}/transactions
Get transaction history from a strategy for a date range.
p123api.Client().strategy_transactions(
strategy_id=123,
start="YYYY-MM-DD",
end="YYYY-MM-DD",
to_pandas=False
)
strategy_transactions_import()
Wrapper for POST /strategy/{id}/transactions
Import transactions into a strategy. When a transaction is imported in the past the dividends and splits are automatically computed unless they are provide in the list.
p123api.Client().strategy_transaction_import(
strategy_id=123,
data: str | IO[str],
content_type: "text/csv" | "text/tsv",
update_existing=False,
make_rebal_dt_curr=False
)
Data:
The transaction data parameter can be either passed in directly of via a file-like object (e.g. obtained via open
).
Supported formats: CSV and TSV
Preferred Country will be used to resolve tickers that do not have a country suffix. If the ticker does not exist in the Preferred Country, then we will look for the best match (Primary first followed by Foreign Primary).
Type must be one of: BUY, SELL, COVER, SHORT, DIV, SPLIT, CASH
Price and commission are assumed to be in the same currency as the strategy.
Notes is optional.
Columns must be in this order (notes is optional): date, ticker, type, shares, price, commission, notes
Transaction Data Example
2025-04-28,IBM,BUY,100,123.45,1.0,Optional Note
2025-04-28,MSFT,SELL,200,123.45,1.0,Optional Note
2025-04-27,,CASH,,1000.0,,Starting Cash
strategy_transaction_delete()
Wrapper for DELETE /strategy/{id}/transactions
Delete strategy transactions by transaction IDs.
p123api.Client().strategy_transaction_delete(
strategy_id=id,
items=[tranId,...]
)
Go to:
All p123api functions
Technical API reference
Did this answer your question?