Order management, trade operations, and position handling.
# Create
class OrderCreate:
def __init__(self, accountID: str, data: dict): ...
# POST /v3/accounts/{accountID}/orders
# List
class OrderList:
def __init__(self, accountID: str, params: dict = None): ...
# params: ids, state (PENDING|FILLED|TRIGGERED|CANCELLED|ALL), instrument, count, beforeID
class OrdersPending:
def __init__(self, accountID: str): ...
# Details
class OrderDetails:
def __init__(self, accountID: str, orderID: str): ...
# Modify
class OrderReplace:
def __init__(self, accountID: str, orderID: str, data: dict): ...
class OrderCancel:
def __init__(self, accountID: str, orderID: str): ...
class OrderClientExtensions:
def __init__(self, accountID: str, orderID: str, data: dict): ...from oandapyV20.endpoints.orders import OrderCreate
from oandapyV20.contrib.requests import (
MarketOrderRequest, LimitOrderRequest, StopOrderRequest,
TakeProfitDetails, StopLossDetails, TrailingStopLossDetails
)
# Market order
order = MarketOrderRequest(instrument="EUR_USD", units=1000).data
api.request(OrderCreate(accountID=account_id, data=order))
# Market with TP/SL
order = MarketOrderRequest(
instrument="EUR_USD",
units=1000,
takeProfitOnFill=TakeProfitDetails(price=1.1200).data,
stopLossOnFill=StopLossDetails(price=1.0900).data
).data
# Limit order
order = LimitOrderRequest(
instrument="EUR_USD",
units=1000,
price=1.1000,
timeInForce="GTC"
).data
# Stop order
order = StopOrderRequest(
instrument="EUR_USD",
units=1000,
price=1.1200
).data
# With trailing stop loss
order = LimitOrderRequest(
instrument="EUR_USD",
units=1000,
price=1.1000,
trailingStopLossOnFill=TrailingStopLossDetails(distance=0.0050).data
).data# List
class TradesList:
def __init__(self, accountID: str, params: dict = None): ...
# params: ids, state (OPEN|CLOSED|CLOSE_WHEN_TRADEABLE|ALL), instrument, count, beforeID
class OpenTrades:
def __init__(self, accountID: str): ...
# Details
class TradeDetails:
def __init__(self, accountID: str, tradeID: str): ...
# Close
class TradeClose:
def __init__(self, accountID: str, tradeID: str, data: dict = None): ...
# data: {"units": "ALL" | number}
# Modify
class TradeClientExtensions:
def __init__(self, accountID: str, tradeID: str, data: dict = None): ...
class TradeCRCDO:
"""Create/Replace/Cancel Dependent Orders on trade."""
def __init__(self, accountID: str, tradeID: str, data: dict): ...
# data: {"takeProfit": {...}, "stopLoss": {...}, "trailingStopLoss": {...}}from oandapyV20.endpoints.trades import OpenTrades, TradeClose, TradeCRCDO
from oandapyV20.contrib.requests import TradeCloseRequest, TakeProfitDetails, StopLossDetails
# List open trades
trades = api.request(OpenTrades(accountID=account_id))['trades']
# Close trade fully
close_data = TradeCloseRequest(units="ALL").data
api.request(TradeClose(accountID=account_id, tradeID=trade_id, data=close_data))
# Close partially
close_data = TradeCloseRequest(units="500").data
api.request(TradeClose(accountID=account_id, tradeID=trade_id, data=close_data))
# Add TP/SL to existing trade
dependent_orders = {
"takeProfit": TakeProfitDetails(price=1.1200).data,
"stopLoss": StopLossDetails(price=1.0900).data
}
api.request(TradeCRCDO(accountID=account_id, tradeID=trade_id, data=dependent_orders))# List
class PositionList:
def __init__(self, accountID: str): ...
class OpenPositions:
def __init__(self, accountID: str): ...
# Details
class PositionDetails:
def __init__(self, accountID: str, instrument: str): ...
# Close
class PositionClose:
def __init__(self, accountID: str, instrument: str, data: dict): ...
# data: {"longUnits": "ALL" | number, "shortUnits": "ALL" | number}from oandapyV20.endpoints.positions import OpenPositions, PositionDetails, PositionClose
from oandapyV20.contrib.requests import PositionCloseRequest
# List positions
positions = api.request(OpenPositions(accountID=account_id))['positions']
for pos in positions:
print(f"{pos['instrument']}: Long={pos['long']['units']}, Short={pos['short']['units']}, P/L={pos['unrealizedPL']}")
# Get specific position
position = api.request(PositionDetails(accountID=account_id, instrument="EUR_USD"))['position']
# Close long side
close_data = PositionCloseRequest(longUnits="ALL").data
api.request(PositionClose(accountID=account_id, instrument="EUR_USD", data=close_data))
# Close both sides
close_data = PositionCloseRequest(longUnits="ALL", shortUnits="ALL").data
api.request(PositionClose(accountID=account_id, instrument="EUR_USD", data=close_data))from oandapyV20.contrib.requests import ClientExtensions
extensions = ClientExtensions(
clientID="trade-001",
clientTag="momentum-strategy",
clientComment="Breakout entry"
).data
# Use in order
order = MarketOrderRequest(
instrument="EUR_USD",
units=1000,
clientExtensions=extensions
).datafrom oandapyV20.endpoints.trades import OpenTrades, TradeClose
from oandapyV20.contrib.requests import TradeCloseRequest
trades = api.request(OpenTrades(accountID=account_id))['trades']
for trade in trades:
close_data = TradeCloseRequest(units="ALL").data
api.request(TradeClose(accountID=account_id, tradeID=trade['id'], data=close_data))from oandapyV20.endpoints.positions import OpenPositions, PositionClose
from oandapyV20.contrib.requests import PositionCloseRequest
positions = api.request(OpenPositions(accountID=account_id))['positions']
for pos in positions:
long_units = "ALL" if float(pos['long']['units']) != 0 else None
short_units = "ALL" if float(pos['short']['units']) != 0 else None
close_data = PositionCloseRequest(longUnits=long_units, shortUnits=short_units).data
api.request(PositionClose(accountID=account_id, instrument=pos['instrument'], data=close_data))from oandapyV20.endpoints.orders import OrdersPending, OrderCancel
orders = api.request(OrdersPending(accountID=account_id))['orders']
for order in orders:
api.request(OrderCancel(accountID=account_id, orderID=order['id']))