Models for Primary API (REST / WS) messages
API Documentation: https://apihub.primary.com.ar/
Special use case with pyRofex (https://github.com/matbarofex/pyRofex)
pip install primary-api-models
- Segments
import pyRofex
from primary_api_models import SegmentsResponse
pyRofex.initialize(...)
# Get all segments
response = pyRofex.get_segments()
data = SegmentsResponse.model_validate_json(response)
print(data.segments)- All Instruments
import pyRofex
from primary_api_models import AllInstrumentsResponse
pyRofex.initialize(...)
# Get available instrument list
response = pyRofex.get_all_instruments()
data = AllInstrumentsResponse.model_validate_json(response)
print(data.instruments)- Instruments by Segments/Cficode
import pyRofex
from primary_api_models import InstrumentsByResponse
pyRofex.initialize(...)
# Get instrument list by given cfi code or segment:
response = pyRofex.get_instruments('by_cfi', ...)
response = pyRofex.get_instruments('by_segments', ...)
data = InstrumentsByResponse.model_validate_json(response)
print(data.instruments)- Detailed Instruments List
import pyRofex
from primary_api_models import DetailedInstrumentsResponse
pyRofex.initialize(...)
# Get detailed instrument list
response = pyRofex.get_detailed_instruments()
data = DetailedInstrumentsResponse.model_validate_json(response)
print(data.instruments)- Instrument Details
import pyRofex
from primary_api_models import InstrumentDetailsResponse
pyRofex.initialize(...)
# Get instrument details
response = pyRofex.get_instrument_details(ticker="MERV - XMEV - AAPL - 24hs")
data = InstrumentDetailsResponse.model_validate_json(response)
print(data.instrument)- Order Status
import pyRofex
from primary_api_models import OrderStatusResponse
pyRofex.initialize(...)
# Get all order reports for the configured account
response = pyRofex.get_all_orders_status()
data = OrderStatusResponse.model_validate_json(response)
print(data.orders)
# Get order status
response = pyRofex.get_order_status(...)
data = OrderStatusResponse.model_validate_json(response)
print(data.order)- Market Data
import pyRofex
from primary_api_models import StatusResponse
from primary_api_models import MarketDataResponse, MarketData
pyRofex.initialize(...)
# Makes a request to the Rest API and get bids, offers and last price
# Use the MarketDataEntry enum to specify the data
entries = [pyRofex.MarketDataEntry.BIDS,
pyRofex.MarketDataEntry.OFFERS,
pyRofex.MarketDataEntry.LAST]
response = pyRofex.get_market_data(ticker="MERV - XMEV - AAPL - 24hs", entries=entries)
data = MarketDataResponse.model_validate_json(response)
if data.status == StatusResponse.OK:
md = data.market_data
bids = md.BI
asks = md.OF
last_trade = md.LA
print(last_trade)- Send Order
import pyRofex
from primary_api_models import StatusResponse
from primary_api_models import Order, OrderConfirmationResponse
pyRofex.initialize(...)
new_order = Order(
ticker="MERV - XMEV - AAPL - 24hs",
side=pyRofex.Side.BUY,
price=1000,
size=5,
order_type=pyRofex.OrderType.LIMIT,
)
response = pyRofex.send_order(**new_order.model_dump())
data = OrderConfirmationResponse.model_validate_json(response)
if data.status == StatusResponse.OK:
print(data.client_id)- Account Report
import pyRofex
from primary_api_models import AccountReportResponse
pyRofex.initialize(...)
# Get account report
response = pyRofex.get_account_report(...)
data = AccountReportResponse.model_validate_json(response)
print(data.account_data)- Account Positions
import pyRofex
from primary_api_models import PositionsResponse
pyRofex.initialize(...)
# Get all positions for an account
response = pyRofex.get_account_report(...)
data = PositionsResponse.model_validate_json(response)
print(data.positions)- Detailed Positions
import pyRofex
from primary_api_models import DetailedPositionsResponse
pyRofex.initialize(...)
# Get detailed positions for an account
response = pyRofex.get_detailed_position(...)
data = DetailedPositionsResponse.model_validate_json(response)
print(data.detailed_position)
[ // Enums "StatusResponse", "MessageType", "MarketId", "Side", "OrderType", "OrderStatus", "TimeInForce", "Currency", "CurrencyIndex", "SettlementIndex", "MarketDataEntry", "CFICode", "MarketSegment", "ContractType", // Models "AccountReportResponse", "AccountData", "CommonResponse", "PriceSize", "PriceSizeDate", "InstrumentId", "Instrument", "DetailedInstrumentsResponse", "DetailedPositionsResponse", "DetailedPositionData", "DetailedPosition", "EnhancedOrderReport", "InstrumentDetailsResponse", "InstrumentDetails", "AllInstrumentsResponse", "InstrumentsByResponse", "MarketDataResponse", "MarketDataMessage", "MarketData", "Order", "OrderReportMessage", "OrderReport", "OrderStatusResponse", "OrderConfirmationResponse", "PositionsResponse", "Position", "SegmentsResponse" ]