Trading Lab

Build, test, and deploy algorithmic strategies

Beta

Trading SDK

API documentation for building algorithmic trading strategies

v1.0.0Async/Await
Quick Start
Get started with the Python SDK
from trading_sdk import TradingClient
# Create a client instance
client = TradingClient()
# Get account info
account = await client.get_account()
print(f"Cash Balance: ${account.cash_balance}")
# Get a stock quote
quote = await client.get_quote('AAPL')
print(f"AAPL: ${quote.price}")
# Buy a call option
result = await client.buy_call('AAPL', 200, '2024-03-15', 5)
print(result.message)
# Close a position (50%)
await client.close_position(position_id, percent=50)

Account & Portfolio

Methods for accessing account information and portfolio data.

get_account()
async
Get current account information including balances and P&L.

Returns

Account

Example

account = await client.get_account()
# Account object contains:
# - cash_balance: float
# - portfolio_value: float
# - total_pnl: float
# - total_pnl_percent: float
# - buying_power: float
print(f"Portfolio: ${account.portfolio_value}")
print(f"P&L: ${account.total_pnl} ({account.total_pnl_percent}%)")
get_cash_balance()
async
Get available cash balance.

Returns

float

Example

cash = await client.get_cash_balance()
print(f"Available: ${cash}")
get_portfolio_value()
async
Get total portfolio value.

Returns

float

Example

value = await client.get_portfolio_value()
print(f"Total Value: ${value}")
Example Strategy
A simple momentum-based options strategy using the Python SDK
from trading_sdk import TradingClient
from datetime import datetime, timedelta
client = TradingClient(debug=True)
def get_next_friday():
today = datetime.now()
days_ahead = 4 - today.weekday() # Friday is 4
if days_ahead <= 0:
days_ahead += 7
return (today + timedelta(days=days_ahead)).strftime('%Y-%m-%d')
async def momentum_strategy():
# Get account and check buying power
account = await client.get_account()
if account.cash_balance < 10000:
print('Insufficient funds')
return
# Get AAPL quote
quote = await client.get_quote('AAPL')
price_change = quote.change_percent
# Check if we already have a position
if await client.has_position('AAPL'):
positions = await client.get_positions_by_symbol('AAPL')
# Close if profit > 20%
for pos in positions:
if pos.pnl_percent > 20:
await client.close_position(pos.id)
print(f"Closed {pos.symbol} with {pos.pnl_percent}% profit")
return
# Strong momentum: buy calls
if price_change > 2:
strike = ((quote.price // 5) + 1) * 5 # Round to nearest $5
expiry = get_next_friday()
await client.buy_call('AAPL', strike, expiry, 5,
stop_loss=1.00, # Stop at $1
take_profit=5.00 # Take profit at $5
)
print(f"Bought AAPL ${strike} calls")
# Strong negative momentum: buy puts
elif price_change < -2:
strike = (quote.price // 5) * 5
expiry = get_next_friday()
await client.buy_put('AAPL', strike, expiry, 5)
print(f"Bought AAPL ${strike} puts")
# Run the strategy
import asyncio
asyncio.run(momentum_strategy())
Python Types
Type hints and dataclasses available in the SDK
from trading_sdk import (
# Client
TradingClient,
# Account types
Account,
Portfolio,
# Position types
Position,
PositionSummary,
AssetClass, # Literal['option', 'stock', 'crypto', 'future', 'cfd']
OptionType, # Literal['call', 'put']
PositionSide, # Literal['long', 'short']
OrderSide, # Literal['buy', 'sell']
# Market data types
Quote,
OptionChain,
OptionContract,
CryptoQuote,
FuturesContract,
CFDInstrument,
# Order types
OrderResult,
OptionOrderParams,
CryptoOrderParams,
# Position management
ClosePositionParams,
ClosePositionResult,
# Trade history
Trade,
TradeHistory,
# Errors
TradingError,
)