8#define ATL_BROKER_ADAPTER_PRESENT 1
10#include "./client.hpp"
12#include "../../core/broker_session.hpp"
13#include "../../core/broker_traits.hpp"
172 return Venue::UNSPECIFIED;
181 return alpaca::detail::validate_bar_query(query);
191 if (country != Country::US) {
192 return alpaca::detail::refusal(
"market_unsupported",
"Alpaca's calendar is the US market's");
195 return alpaca::detail::refusal(
"invalid_date",
"the calendar needs a valid date");
207 return alpaca::detail::validate_order(order);
223 placed.
ref_.primary_ = raw.
value().order_id_;
238 for (
auto& open : raw.
value().orders_) {
240 working.
ref_.primary_ = open.order_id_;
242 working.
order_ = std::move(open);
243 orders.
orders_.push_back(std::move(working));
255 alpaca::detail::refusal(
"replace_unsupported",
"the Alpaca adapter does not replace orders; cancel and place")
268 alpaca::detail::refusal(
"partial_cancel_unsupported",
"Alpaca cancels the whole order only")
271 if (!alpaca::detail::valid_order_id(ref.
primary_)) {
284 return Result<HistoryQuery>(alpaca::detail::refusal(
"invalid_limit",
"Alpaca's order list takes 1 to 500 rows"));
287 return Result<HistoryQuery>(alpaca::detail::refusal(
"invalid_date",
"the history range needs valid dates"));
343 return client.
bars(std::move(query));
364 return client.
calendar(country, from);
394 return client.
place_order(account, std::move(order));
403 alpaca::detail::refusal(
"replace_unsupported",
"the Alpaca adapter does not replace orders; cancel and place")
415 return client.
cancel_order(account, std::move(amendment));
Either a value or a BrokerError.
Result(T value)
Constructs a successful result.
BrokerError & error()
Returns the error.
T & value()
Returns the value.
A blocking client for Alpaca's Trading API and Market Data API: exchange-listed US equities,...
const network::RateLimitStats * rate_limit_stats() const
Returns the trading connection's rate-limit statistics; read them between calls.
Result< Instrument > instrument(InstrumentId instrument)
Returns one instrument's reference data (/v2/assets/{symbol}), with lot 1, dollars and us::equity_pri...
Result< HoldingsSnapshot > holdings(AlpacaAccount account={})
Returns the US equity positions (/v2/positions), in dollars.
Result< OrderPage > order_history(AlpacaAccount account, OrderHistoryQuery query)
Returns US equity orders submitted from from_ to to_ (New York dates), newest first,...
Result< BarSeries > bars(BarQuery query)
Returns closed bars, oldest first: the newest count_ (1 to 9,999) at or before before_ (or now),...
Result< AlpacaOrderAck > cancel_order(AlpacaAccount account, AlpacaAmendment amendment)
Cancels a whole order (DELETE /v2/orders/{id}; 204, also for an order already cancelled)....
Result< QuoteSnapshot > quote(InstrumentId instrument)
Returns the last trade on the IEX feed, in dollars.
Result< TradingCalendar > calendar(Country country, std::optional< Date > from=std::nullopt)
Returns the New York Stock Exchange's trading calendar with each open day's session hours,...
Result< AlpacaOrderAck > place_order(AlpacaAccount account, OrderRequest order)
Places an order (POST /v2/orders). The outcome can be unknown after an error.
Result< AlpacaOpenOrders > working_orders(AlpacaAccount account={})
Returns the open US equity orders (/v2/orders?status=open, up to 499).
Result< OrderSnapshot > order_by_id(AlpacaAccount account, const std::string &id)
Returns one order by its id (/v2/orders/{id}); an order that is not a US equity is a PROTOCOL error.
Result< std::vector< AlpacaAccountInfo > > accounts()
Returns the one account the keys drive (/v2/account).
The Alpaca Trading API and Market Data API client: exchange-listed US equities, paper by default.
The library: broker-neutral types (core), and the modules below.
SellableSource
Where the library gets a broker's sellable quantity.
@ HOLDINGS
On the holdings snapshot, at no extra request.
Country
A country (market) the library supports.
std::optional< Decimal64 > decimal_subtract(const Decimal64 &lhs, const Decimal64 &rhs)
Subtracts two decimals exactly.
Venue
A trading venue: an exchange, or the combined view across exchanges.
BarInterval
The period one bar covers.
@ FIVE_MINUTES
Five minutes.
@ THIRTY_MINUTES
Thirty minutes.
@ FIFTEEN_MINUTES
Fifteen minutes.
bool is_valid_date(const Date &date)
Tests whether a date exists in the Gregorian calendar, in years 1 to 9999.
A modification or cancellation of a working order.
bool all_remaining_
Whether the whole remaining quantity is affected, rather than quantity_.
static constexpr bool has_price_limits
false: no daily price band is published for US equities.
static Result< OrderSnapshot > order_by_id(Client &client, const Account &account, const std::string &id)
Returns one order by its id.
static Result< WorkingOrders< alpaca::Broker > > normalize_working_orders(Result< WorkingOrdersResult > raw)
Converts the open orders into working orders, each amendable for its unfilled quantity.
static constexpr SellableSource sellable_source
HOLDINGS: each position's qty_available, with the basis AlpacaBlockingClient::holdings states.
static constexpr bool has_working_orders
true: /v2/orders?status=open.
static Result< QuoteSnapshot > quote(Client &client, InstrumentId instrument)
Returns a quote.
static Account selector_of(const AccountListItem &)
Returns the account selector a session binds, which is empty. The argument is not used.
static std::optional< BrokerError > validate_bars(const BarQuery &query)
Refuses a bar request this adapter cannot answer as asked, before any I/O.
static constexpr Venue effective_route(Venue) noexcept
Returns where an order goes: Alpaca routes it, and no venue can be asked for.
static Result< std::vector< AccountListItem > > accounts(Client &client)
Returns the one account the keys drive.
static constexpr bool has_trade_prints
false: served by Alpaca, not adapted.
alpaca::Broker BrokerTag
The broker tag.
static Result< OrderPage > order_history(Client &client, const Account &account, HistoryQuery query)
Returns a page of order history.
static constexpr bool has_order_by_id
true: /v2/orders/{id}.
static Result< WorkingOrdersResult > working_orders(Client &client, const Account &account)
Returns the open orders.
static constexpr bool has_history_limit
true: up to history_limit rows a page.
static constexpr std::string_view name
The broker's name.
static constexpr bool has_calendar
true: /v3/calendar/XNYS.
static std::optional< BrokerError > validate_place(const OrderRequest &order)
Refuses an order this adapter does not send as described, before any I/O; the same check the client's...
static Result< Instrument > instrument(Client &client, InstrumentId subject)
Returns reference data for one instrument.
static constexpr bool history_requires_range
false: a range is optional.
static constexpr bool has_history_cursor
true: a full page carries its last row's submission time, sent back as Alpaca's until.
static constexpr bool has_trigger_price
false: this adapter sends no stop orders.
static constexpr bool has_amendable_quantity
true: each order's qty less its filled_qty.
static constexpr int history_limit
The most rows one history page may ask for.
alpaca::AlpacaOrderAck PlaceAck
Alpaca's acknowledgement of a placement or cancellation.
static constexpr bool has_history_symbol_filter
true: Alpaca filters by symbols=.
static constexpr bool has_buying_power
false: no Alpaca figure is cash net of resting buys without credit (buying_power counts margin),...
static Result< BarSeries > bars(Client &client, BarQuery query)
Returns bars.
static std::string display_of(const AccountListItem &account)
Returns the account number as Alpaca reports it.
static constexpr bool has_buying_power_quantity
false: there is no buying power to express in shares.
alpaca::AlpacaAccount Account
The account selector, which carries nothing.
static constexpr bool has_calendar_range
true: one request covers the whole range.
OrderHistoryQuery HistoryQuery
The history query, the library's own.
static constexpr bool has_batch_reference_data
false: one instrument a request.
static constexpr bool has_calendar_sessions
true: the adapter fills each open day's regular session and, where stated, its extended span,...
alpaca::AlpacaBlockingClient Client
The client type.
static Result< Amendment > to_replacement(const OrderRef< alpaca::Broker > &, const AmendInstruction &)
Refuses every replacement: Alpaca's replacement is a new order that drops the client_order_id,...
alpaca::AlpacaOpenOrders WorkingOrdersResult
The open orders, before normalizing.
static Result< PlaceAck > replace_order(Client &, const Account &, Amendment)
Refuses a replacement; to_replacement refuses first, so a session never calls it.
static Result< HoldingsSnapshot > holdings(Client &client, const Account &account)
Returns the holdings.
static constexpr bool has_partial_cancel
false: Alpaca cancels the whole order.
static Result< PlacedOrder< alpaca::Broker > > normalize_place(Result< PlaceAck > raw)
Normalizes Alpaca's acknowledgement; the reference is the order id.
static constexpr bool working_orders_state_terminal
false: the open listing shows open orders only.
static constexpr bool has_client_order_id
true: client_order_id, 1 to 128 characters, unique for the account.
static Result< TradingCalendar > calendar(Client &client, Country country, std::optional< Date > from)
Returns the trading calendar.
static constexpr bool has_depth
false: served by Alpaca, not adapted.
static constexpr bool data_only
A broker that takes orders, not a market-data source.
static std::optional< BrokerError > validate_calendar(Country country, std::optional< Date > from)
Refuses a calendar for any market but the US, or with an invalid date.
static constexpr bool supports_interval(BarInterval interval)
Tests whether this adapter serves bars at an interval.
static Result< Amendment > to_cancellation(const OrderRef< alpaca::Broker > &ref, const AmendInstruction &instruction)
Converts a cancellation of the whole order; a stated quantity is refused.
static Result< PlaceAck > place_order(Client &client, const Account &account, OrderRequest order)
Places an order.
static Result< PlaceAck > cancel_order(Client &client, const Account &account, Amendment amendment)
Cancels an order.
static std::string type_of(const AccountListItem &)
Returns the account type, which this adapter does not read (the argument is not used).
static Result< HistoryQuery > to_history_query(const Account &, const OrderHistoryQuery &query)
Admits a history query with a limit of 1 to 500 and valid dates.
static constexpr bool has_reference_data
true: /v2/assets/{symbol}.
alpaca::AlpacaAmendment Amendment
A cancellation of one whole order.
static constexpr bool blocking
true: requests return their result directly.
alpaca::AlpacaConfig Config
The keys and environment.
static const network::RateLimitStats * rate_limit_stats(const Client &client)
Returns the trading connection's rate-limit statistics.
static constexpr bool has_commission_schedule
false: commission-free, and the per-share FINRA fee on sells has no CommissionRate shape.
alpaca::AlpacaAccountInfo AccountListItem
Item type of the broker's own account listing.
static constexpr bool has_account_discovery
true: /v2/account reports the one account the keys drive.
static constexpr bool has_batch_prices
false: one instrument a request.
Describes one broker client to the library: its types, capabilities, call thunks and normalizers.
Identifies an instrument: its country, venue and symbol.
std::optional< std::string > client_order_id_
The idempotency key, if the broker echoed one.
std::chrono::system_clock::time_point accepted_at_
When the acknowledgement was received.
std::string order_id_
The order number the broker assigned.
The filter for an order-history inquiry.
std::optional< int > limit_
The most rows to return, where the broker supports a limit.
std::optional< Date > from_
The first date to include; required where BrokerCaps::history_requires_range.
std::optional< Date > to_
The last date to include.
A broker's reference to one order, used to amend or cancel it.
std::string primary_
The order number or id.
An order to place, in broker-neutral form.
An accepted order: the acknowledgement and the reference to address it with.
OrderRef< Broker > ref_
The reference to amend or cancel the order with.
OrderAck ack_
The acknowledgement.
An order still open to modification or cancellation.
OrderRef< Broker > ref_
The reference to amend or cancel the order with.
std::optional< Decimal64 > amendable_quantity_
How much the broker says may still be amended. Empty means it stated none, not zero.
OrderSnapshot order_
The order's state.
The orders open to modification or cancellation at one point in time.
std::vector< WorkingOrder< Broker > > orders_
The open orders.
std::chrono::system_clock::time_point received_at_
When the listing was received, on this machine's clock.
The one account Alpaca's /v2/account reports for the keys.
std::string account_number_
The account number as Alpaca reported it, for display.
The Alpaca account selector, which is empty: the keys name the account, and no call takes a number.
A cancellation of one whole order (Alpaca replacements are refused by this adapter).
Alpaca API keys (key id and secret key), paper or live account.
The open US equity orders, before normalizing.
Alpaca's acknowledgement of a placement or cancellation.
The Alpaca broker tag: order references are typed by it, so an Alpaca reference works with no other b...
static constexpr std::string_view name
The broker's name.
A rate limiter's counters per traffic class, its current budget, and the broker's refusals (from Rate...