quantum-encoding/zig-alpaca-trading
Production-grade Alpaca Markets API client for Zig. Build algorithmic trading systems with deterministic performance and zero GC pauses.
A high-performance, thread-safe Alpaca Markets API client for Zig 0.16.0, designed for algorithmic trading systems.
Add this library to your build.zig.zon
:
.dependencies = .{
.quantum_alpaca = .{
.url = "https://github.com/YOUR_USERNAME/quantum-alpaca-zig/archive/refs/tags/v1.0.0.tar.gz",
.hash = "YOUR_HASH_HERE",
},
},
Then in your build.zig
:
const quantum_alpaca = b.dependency("quantum_alpaca", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("quantum-alpaca", quantum_alpaca.module("quantum-alpaca"));
const std = @import("std");
const AlpacaClient = @import("quantum-alpaca").AlpacaClient;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Initialize client
var client = AlpacaClient.init(
allocator,
"YOUR_API_KEY",
"YOUR_SECRET_KEY",
.paper, // or .live for production
);
defer client.deinit();
// Get account info
var account = try client.getAccount();
defer account.deinit();
// Submit an order
const order = AlpacaClient.OrderRequest{
.symbol = "AAPL",
.qty = 10,
.side = "buy",
.type = "limit",
.time_in_force = "day",
.limit_price = 150.00,
};
var response = try client.submitOrder(order);
defer response.deinit();
}
var client = AlpacaClient.init(allocator, api_key, secret_key, environment);
defer client.deinit();
Environments:
.paper
- Paper trading (testing).live
- Live trading (production)// Get account information
var response = try client.getAccount();
// Get portfolio history
var response = try client.getPortfolioHistory("1M", "1D");
// Get all positions
var response = try client.getPositions();
// Get specific position
var response = try client.getPosition("AAPL");
// Close position
var response = try client.closePosition("AAPL");
// Close all positions
var response = try client.closeAllPositions();
// Submit order
const order = AlpacaClient.OrderRequest{
.symbol = "AAPL",
.qty = 10,
.side = "buy",
.type = "limit",
.time_in_force = "day",
.limit_price = 150.00,
};
var response = try client.submitOrder(order);
// Get all orders
var response = try client.getOrders();
// Get specific order
var response = try client.getOrder(order_id);
// Cancel order
var response = try client.cancelOrder(order_id);
// Cancel all orders
var response = try client.cancelAllOrders();
// Get latest quote
var response = try client.getLatestQuote("AAPL");
// Get latest trade
var response = try client.getLatestTrade("AAPL");
// Get bars (candlestick data)
var response = try client.getBars("AAPL", "1Day", "2024-01-01", "2024-01-31", 30);
// Get market clock
var response = try client.getClock();
// Get market calendar
var response = try client.getCalendar();
const order = AlpacaClient.OrderRequest{
.symbol = "AAPL",
.qty = 10,
.side = "buy",
.type = "market",
.time_in_force = "day",
};
const order = AlpacaClient.OrderRequest{
.symbol = "AAPL",
.qty = 10,
.side = "buy",
.type = "limit",
.time_in_force = "day",
.limit_price = 150.00,
};
const order = AlpacaClient.OrderRequest{
.symbol = "AAPL",
.qty = 10,
.side = "sell",
.type = "stop",
.time_in_force = "day",
.stop_price = 145.00,
};
const order = AlpacaClient.OrderRequest{
.symbol = "AAPL",
.qty = 10,
.side = "buy",
.type = "limit",
.time_in_force = "day",
.limit_price = 150.00,
.order_class = "bracket",
.take_profit = .{ .limit_price = 155.00 },
.stop_loss = .{ .stop_price = 145.00 },
};
Each thread should create its own AlpacaClient
instance:
fn tradingThread(allocator: std.mem.Allocator, api_key: []const u8, secret: []const u8) void {
var client = AlpacaClient.init(allocator, api_key, secret, .paper);
defer client.deinit();
// Trading logic here...
}
Set your API credentials as environment variables:
export ALPACA_API_KEY="your_api_key"
export ALPACA_SECRET_KEY="your_secret_key"
Then in your code:
const api_key = try std.process.getEnvVarOwned(allocator, "ALPACA_API_KEY");
const secret = try std.process.getEnvVarOwned(allocator, "ALPACA_SECRET_KEY");
All methods return a Response
that includes the HTTP status code:
var response = try client.getAccount();
defer response.deinit();
if (response.status == .ok) {
// Success
const account = try response.json(AlpacaClient.Account);
defer account.deinit();
} else {
// Handle error
std.debug.print("Error: {}\n", .{response.status});
}
Alpaca has rate limits. This client does not implement automatic retry logic, allowing you to implement your own strategy:
Run tests:
zig build test
Run examples (requires API keys):
export ALPACA_API_KEY="your_key"
export ALPACA_SECRET_KEY="your_secret"
zig build example
This client is optimized for:
MIT License - See LICENSE file for details
This software is for educational and research purposes. Always test thoroughly with paper trading before using in production. The authors are not responsible for any financial losses incurred through the use of this software.
Built for high-frequency trading systems where every microsecond counts.