Get Accounts
Retrieve all accounts associated with your authenticated user.
public async Task<Account[]> GetAccounts()
Response
Returns an array of Account objects:
Numeric account identifier
Account name/number (e.g., “REM2046”)
Account status (true = active)
Example
using Primary;
using System;
using System.Threading.Tasks;
var api = new Api(Api.DemoEndpoint);
await api.Login(Api.DemoUsername, Api.DemoPassword);
// Get all accounts
var accounts = await api.GetAccounts();
foreach (var account in accounts)
{
Console.WriteLine($"Account: {account.Name}");
Console.WriteLine($" ID: {account.Id}");
Console.WriteLine($" Broker: {account.BrokerId}");
Console.WriteLine($" Active: {account.Status}");
Console.WriteLine();
}
API Endpoint
GET {BaseUri}/rest/accounts
Get Positions (by Account Array)
Retrieve positions for multiple accounts.
public async Task<Position[]> GetPositions(Account[] accounts)
Parameters
Array of account objects to get positions for
Example
var api = new Api(Api.DemoEndpoint);
await api.Login(Api.DemoUsername, Api.DemoPassword);
// Get all accounts
var accounts = await api.GetAccounts();
// Get positions for all accounts
var positions = await api.GetPositions(accounts);
foreach (var position in positions)
{
Console.WriteLine($"{position.Symbol}");
Console.WriteLine($" Buy: {position.BuySize} @ {position.BuyPrice}");
Console.WriteLine($" Sell: {position.SellSize} @ {position.SellPrice}");
Console.WriteLine($" P&L: {position.TotalDiff}");
}
Get Positions (by Account Name)
Retrieve positions for a specific account by name.
public async Task<Position[]> GetPositions(string accountName)
Parameters
Account name/number (e.g., “REM2046”)
Response
Returns an array of Position objects:
Instrument information:
SymbolReference: Reference symbol
SettlementType: Settlement type (CI, T24H, T48H)
Trading symbol identifier
Current long position size
Current short position size
Example
using Primary;
using System;
using System.Linq;
using System.Threading.Tasks;
var api = new Api(Api.DemoEndpoint);
await api.Login(Api.DemoUsername, Api.DemoPassword);
// Get positions for demo account
var positions = await api.GetPositions(Api.DemoAccount);
Console.WriteLine($"Total positions: {positions.Length}\n");
foreach (var position in positions)
{
Console.WriteLine($"Symbol: {position.Symbol}");
Console.WriteLine($" Reference: {position.Instrument.SymbolReference}");
Console.WriteLine($" Settlement: {position.Instrument.SettlementType}");
if (position.BuySize > 0)
{
Console.WriteLine($" Long: {position.BuySize} @ {position.BuyPrice:N2}");
}
if (position.SellSize > 0)
{
Console.WriteLine($" Short: {position.SellSize} @ {position.SellPrice:N2}");
}
Console.WriteLine($" Daily P&L: {position.TotalDailyDiff:N2}");
Console.WriteLine($" Total P&L: {position.TotalDiff:N2}");
Console.WriteLine();
}
Calculate Portfolio Value
var positions = await api.GetPositions(Api.DemoAccount);
decimal totalPnL = 0;
decimal totalDailyPnL = 0;
foreach (var position in positions)
{
totalPnL += position.TotalDiff;
totalDailyPnL += position.TotalDailyDiff;
}
Console.WriteLine($"Portfolio Statistics:");
Console.WriteLine($" Total P&L: {totalPnL:N2}");
Console.WriteLine($" Daily P&L: {totalDailyPnL:N2}");
Console.WriteLine($" Number of Positions: {positions.Length}");
Filter Positions
var positions = await api.GetPositions(Api.DemoAccount);
// Only long positions
var longPositions = positions.Where(p => p.BuySize > 0);
// Only short positions
var shortPositions = positions.Where(p => p.SellSize > 0);
// Profitable positions
var profitablePositions = positions.Where(p => p.TotalDiff > 0);
// Losing positions
var losingPositions = positions.Where(p => p.TotalDiff < 0);
Console.WriteLine($"Long: {longPositions.Count()}");
Console.WriteLine($"Short: {shortPositions.Count()}");
Console.WriteLine($"Profitable: {profitablePositions.Count()}");
Console.WriteLine($"Losing: {losingPositions.Count()}");
API Endpoint
GET {BaseUri}/rest/risk/position/getPositions/{accountName}
Settlement Types
Positions include settlement type information:
public enum SettlementType
{
CI, // Immediate settlement (value: 0)
T24H, // 24-hour settlement (value: 1)
T48H // 48-hour settlement (value: 2)
}
Example: Group by Settlement Type
var positions = await api.GetPositions(Api.DemoAccount);
var grouped = positions.GroupBy(p => p.Instrument.SettlementType);
foreach (var group in grouped)
{
Console.WriteLine($"\n{group.Key} Settlement:");
foreach (var position in group)
{
Console.WriteLine($" {position.Instrument.SymbolReference}: {position.TotalDiff:N2}");
}
}
Complete Portfolio Manager Example
using Primary;
using Primary.Data;
using System;
using System.Linq;
using System.Threading.Tasks;
class PortfolioManager
{
static async Task Main()
{
var api = new Api(Api.DemoEndpoint);
await api.Login(Api.DemoUsername, Api.DemoPassword);
// Get all accounts
var accounts = await api.GetAccounts();
Console.WriteLine($"Found {accounts.Length} account(s)\n");
foreach (var account in accounts)
{
Console.WriteLine($"\n{'='} Account: {account.Name} {'='}");
// Get positions for this account
var positions = await api.GetPositions(account.Name);
if (positions.Length == 0)
{
Console.WriteLine(" No positions");
continue;
}
// Calculate statistics
decimal totalValue = 0;
decimal totalPnL = 0;
decimal totalDailyPnL = 0;
int longCount = 0;
int shortCount = 0;
foreach (var position in positions)
{
decimal positionValue = (position.BuySize * position.BuyPrice) +
(position.SellSize * position.SellPrice);
totalValue += positionValue;
totalPnL += position.TotalDiff;
totalDailyPnL += position.TotalDailyDiff;
if (position.BuySize > 0) longCount++;
if (position.SellSize > 0) shortCount++;
}
// Display summary
Console.WriteLine($"\n Portfolio Summary:");
Console.WriteLine($" Total Positions: {positions.Length}");
Console.WriteLine($" Long: {longCount} | Short: {shortCount}");
Console.WriteLine($" Total Value: {totalValue:N2}");
Console.WriteLine($" Total P&L: {totalPnL:N2}");
Console.WriteLine($" Daily P&L: {totalDailyPnL:N2}");
Console.WriteLine($" Return: {(totalPnL / totalValue * 100):N2}%");
// Display top 5 positions by P&L
Console.WriteLine($"\n Top Positions:");
var topPositions = positions
.OrderByDescending(p => p.TotalDiff)
.Take(5);
foreach (var position in topPositions)
{
string direction = position.BuySize > 0 ? "LONG" : "SHORT";
decimal size = position.BuySize > 0 ? position.BuySize : position.SellSize;
decimal price = position.BuySize > 0 ? position.BuyPrice : position.SellPrice;
Console.WriteLine($" {position.Instrument.SymbolReference,-8} {direction,-6} {size,8:N0} @ {price,10:N2} P&L: {position.TotalDiff,10:N2}");
}
}
}
}
The positions endpoint returns JSON in this format:
{
"status": "OK",
"positions": [
{
"instrument": {
"symbolReference": "GGAL",
"settlType": 2
},
"symbol": "MERV - XMEV - GGAL - 48hs",
"buySize": 100.0,
"buyPrice": 150.50,
"sellSize": 0.0,
"sellPrice": 0.0,
"totalDailyDiff": 12.5,
"totalDiff": 250.75,
"tradingSymbol": "MERV - XMEV - GGAL - 48hs",
"originalBuyPrice": 150.50,
"originalSellPrice": 0.0,
"originalBuySize": 100,
"originalSellSize": 0
}
]
}