oandapyV20.contrib.requests

Support classes

The requests package contains several classes that can be used optional when creating Order Requests. When creating an order to create a position, it is possible to create dependant orders that will be triggered when the position gets filled. This goes typically for Take Profit and Stop Loss.

class oandapyV20.contrib.requests.TakeProfitDetails(price, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Bases: oandapyV20.contrib.requests.onfill.OnFill

Representation of the specification for a TakeProfitOrder.

It is typically used to specify ‘take profit details’ for the ‘takeProfitOnFill’ parameter of an OrderRequest. This way one can create the Take Profit Order as a dependency when an order gets filled.

The other way to create a TakeProfitOrder is to create it afterwards on an existing trade. In that case you use TakeProfitOrderRequest on the trade.

__init__(price, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Instantiate TakeProfitDetails.

Parameters:
  • price (float or string (required)) – the price to trigger take profit order
  • timeInForce (TimeInForce (required), default TimeInForce.GTC) – the time in force
  • gtdTime (DateTime (optional)) – gtdTime is required in case timeInForce == TimeInForce.GTD

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import (
>>>     MarketOrderRequest, TakeProfitDetails)
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> # at time of writing EUR_USD = 1.0740
>>> # let us take profit at 1.10, GoodTillCancel (default)
>>> takeProfitOnFillOrder = TakeProfitDetails(price=1.10)
>>> print(takeProfitOnFillOrder)
{
    "timeInForce": "GTC",
    "price": "1.10000"
}
>>> ordr = MarketOrderRequest(
>>>     instrument="EUR_USD",
>>>     units=10000,
>>>     takeProfitOnFill=takeProfitOnFillOrder.data
>>> )
>>> # or as shortcut ...
>>> #   takeProfitOnFill=TakeProfitDetails(price=1.10).data
>>> print(json.dumps(ordr.data, indent=4))
>>> r = orders.OrderCreate(accountID, data=ordr.data)
>>> rv = client.request(r)
>>> ...
class oandapyV20.contrib.requests.StopLossDetails(price, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Bases: oandapyV20.contrib.requests.onfill.OnFill

Representation of the specification for a StopLossOrder.

It is typically used to specify ‘stop loss details’ for the ‘stopLossOnFill’ parameter of an OrderRequest. This way one can create the Stop Loss Order as a dependency when an order gets filled.

The other way to create a StopLossOrder is to create it afterwards on an existing trade. In that case you use StopLossOrderRequest on the trade.

__init__(price, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Instantiate TakeProfitDetails.

Parameters:
  • price (float or string (required)) – the price to trigger take profit order
  • timeInForce (TimeInForce (required), default TimeInForce.GTC) – the time in force
  • gtdTime (DateTime (optional)) – gtdTime is required in case timeInForce == TimeInForce.GTD
  • clientExtensions (ClientExtensions (optional)) –

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import (
>>>     MarketOrderRequest, StopLossDetails)
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> # at time of writing EUR_USD = 1.0740
>>> # let us take profit at 1.10, GoodTillCancel (default)
>>> stopLossOnFill = StopLossDetails(price=1.06)
>>> print(stopLossOnFill)
{
    "timeInForce": "GTC",
    "price": "1.10000"
}
>>> ordr = MarketOrderRequest(
>>>     instrument="EUR_USD",
>>>     units=10000,
>>>     stopLossOnFill=stopLossOnFill.data
>>> )
>>> # or as shortcut ...
>>> #   stopLossOnFill=StopLossDetails(price=1.06).data
>>> print(json.dumps(ordr.data, indent=4))
>>> r = orders.OrderCreate(accountID, data=ordr.data)
>>> rv = client.request(r)
>>> ...
class oandapyV20.contrib.requests.TrailingStopLossDetails(distance, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Bases: oandapyV20.contrib.requests.onfill.OnFill

Representation of the specification for a TrailinStopLossOrder.

It is typically used to specify ‘trainling stop loss details’ for the ‘trailingStopLossOnFill’ parameter of an OrderRequest. This way one can create the Trailing Stop Loss Order as a dependency when an order gets filled.

The other way to create a TrailinStopLossOrder is to create it afterwards on an existing trade. In that case you use TrailingStopLossOrderRequest on the trade.

__init__(distance, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Instantiate TakeProfitDetails.

Parameters:
  • distance (float or string (required)) – the price to trigger take profit order
  • timeInForce (TimeInForce (required), default TimeInForce.GTC) – the time in force
  • gtdTime (DateTime (optional)) – gtdTime is required in case timeInForce == TimeInForce.GTD
  • clientExtensions (ClientExtensions (optional)) –

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import (
>>>     MarketOrderRequest, TrailingStopLossDetails)
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> # at time of writing EUR_USD = 1.0740
>>> # let us take profit at 1.10, GoodTillCancel (default)
>>> trailingStopLossOnFill = TrailingStopLossDetails(price=1.06)
>>> print(trailingStopLossOnFill)
{
    "timeInForce": "GTC",
    "price": "1.10000"
}
>>> ordr = MarketOrderRequest(
>>>     instrument="EUR_USD",
>>>     units=10000,
>>>     trailingStopLossOnFill=trailingStopLossOnFill.data
>>> )
>>> # or as shortcut ...
>>> #   ...OnFill=trailingStopLossDetails(price=1.06).data
>>> print(json.dumps(ordr.data, indent=4))
>>> r = orders.OrderCreate(accountID, data=ordr.data)
>>> rv = client.request(r)
>>> ...

Client Extensions

Client extensions can be used optionally on Order Requests. It allows a client to set a custom ID, Tag and/or Comment.

class oandapyV20.contrib.requests.ClientExtensions(clientID=None, clientTag=None, clientComment=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

Representation of the ClientExtensions.

__init__(clientID=None, clientTag=None, clientComment=None)

Instantiate ClientExtensions.

Parameters:
  • clientID (clientID (required)) – the clientID
  • clientTag (clientTag (required)) – the clientTag
  • clientComment (clientComment (required)) – the clientComment

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import (
...     MarketOrderRequest, TakeProfitDetails, ClientExtensions)
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> # at time of writing EUR_USD = 1.0740
>>> # let us take profit at 1.10, GoodTillCancel (default)
>>> # add clientExtensions to it also
>>> takeProfitOnFillOrder = TakeProfitDetails(
...     price=1.10,
...     clientExtensions=ClientExtensions(clientTag="mytag").data)
>>> print(takeProfitOnFillOrder.data)
{
    'timeInForce': 'GTC',
    'price": '1.10000',
    'clientExtensions': {'tag': 'mytag'}
}
>>> ordr = MarketOrderRequest(
...     instrument="EUR_USD",
...     units=10000,
...     takeProfitOnFill=takeProfitOnFillOrder.data
... )
>>> # or as shortcut ...
>>> #   takeProfitOnFill=TakeProfitDetails(price=1.10).data
>>> print(json.dumps(ordr.data, indent=4))
>>> r = orders.OrderCreate(accountID, data=ordr.data)
>>> rv = client.request(r)
>>> ...

Order classes

class oandapyV20.contrib.requests.MarketOrderRequest(instrument, units, priceBound=None, positionFill='DEFAULT', clientExtensions=None, takeProfitOnFill=None, timeInForce='FOK', stopLossOnFill=None, trailingStopLossOnFill=None, tradeClientExtensions=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a MarketOrderRequest.

MarketOrderRequest is used to build the body for a MarketOrder. The body can be used to pass to the OrderCreate endpoint.

__init__(instrument, units, priceBound=None, positionFill='DEFAULT', clientExtensions=None, takeProfitOnFill=None, timeInForce='FOK', stopLossOnFill=None, trailingStopLossOnFill=None, tradeClientExtensions=None)

Instantiate a MarketOrderRequest.

Parameters:
  • instrument (string (required)) – the instrument to create the order for
  • units (integer (required)) – the number of units. If positive the order results in a LONG order. If negative the order results in a SHORT order

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import MarketOrderRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> mo = MarketOrderRequest(instrument="EUR_USD", units=10000)
>>> print(json.dumps(mo.data, indent=4))
{
    "order": {
        "type": "MARKET",
        "positionFill": "DEFAULT",
        "instrument": "EUR_USD",
        "timeInForce": "FOK",
        "units": "10000"
    }
}
>>> # now we have the order specification, create the order request
>>> r = orders.OrderCreate(accountID, data=mo.data)
>>> # perform the request
>>> rv = client.request(r)
>>> print(rv)
>>> print(json.dumps(rv, indent=4))
{
    "orderFillTransaction": {
        "reason": "MARKET_ORDER",
        "pl": "0.0000",
        "accountBalance": "97864.8813",
        "units": "10000",
        "instrument": "EUR_USD",
        "accountID": "101-004-1435156-001",
        "time": "2016-11-11T19:59:43.253587917Z",
        "type": "ORDER_FILL",
        "id": "2504",
        "financing": "0.0000",
        "tradeOpened": {
            "tradeID": "2504",
            "units": "10000"
        },
        "orderID": "2503",
        "userID": 1435156,
        "batchID": "2503",
        "price": "1.08463"
    },
    "lastTransactionID": "2504",
    "relatedTransactionIDs": [
        "2503",
        "2504"
    ],
    "orderCreateTransaction": {
        "type": "MARKET_ORDER",
        "reason": "CLIENT_ORDER",
        "id": "2503",
        "timeInForce": "FOK",
        "units": "10000",
        "time": "2016-11-11T19:59:43.253587917Z",
        "positionFill": "DEFAULT",
        "accountID": "101-004-1435156-001",
        "instrument": "EUR_USD",
        "batchID": "2503",
        "userID": 1435156
    }
}
>>>
data

data property.

return the JSON body.

class oandapyV20.contrib.requests.LimitOrderRequest(instrument, units, price, positionFill='DEFAULT', clientExtensions=None, takeProfitOnFill=None, timeInForce='GTC', stopLossOnFill=None, trailingStopLossOnFill=None, tradeClientExtensions=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a LimitOrderRequest.

LimitOrderRequest is used to build the body for a LimitOrder. The body can be used to pass to the OrderCreate endpoint.

__init__(instrument, units, price, positionFill='DEFAULT', clientExtensions=None, takeProfitOnFill=None, timeInForce='GTC', stopLossOnFill=None, trailingStopLossOnFill=None, tradeClientExtensions=None)

Instantiate a LimitOrderRequest.

Parameters:
  • instrument (string (required)) – the instrument to create the order for
  • units (integer (required)) – the number of units. If positive the order results in a LONG order. If negative the order results in a SHORT order
  • price (float (required)) – the price indicating the limit.

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import LimitOrderRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> mo = LimitOrderRequest(instrument="EUR_USD",
>>>                        units=10000, price=1.08)
>>> print(json.dumps(mo.data, indent=4))
>>> ...
data

data property.

return the JSON order body

class oandapyV20.contrib.requests.MITOrderRequest(instrument, units, price, priceBound=None, positionFill='DEFAULT', timeInForce='GTC', gtdTime=None, clientExtensions=None, takeProfitOnFill=None, stopLossOnFill=None, trailingStopLossOnFill=None, tradeClientExtensions=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a MarketIfTouched OrderRequest.

MITOrderRequest is used to build the body for a MITOrder. The body can be used to pass to the OrderCreate endpoint.

__init__(instrument, units, price, priceBound=None, positionFill='DEFAULT', timeInForce='GTC', gtdTime=None, clientExtensions=None, takeProfitOnFill=None, stopLossOnFill=None, trailingStopLossOnFill=None, tradeClientExtensions=None)

Instantiate an MITOrderRequest.

Parameters:
  • instrument (string (required)) – the instrument to create the order for
  • units (integer (required)) – the number of units. If positive the order results in a LONG order. If negative the order results in a SHORT order
  • price (float (required)) – the price indicating the limit.

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import MITOrderRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> mo = MITOrderRequest(instrument="EUR_USD",
>>>                      units=10000, price=1.08)
>>> print(json.dumps(mo.data, indent=4))
>>> r = orders.OrderCreate(accountID, data=mo.data)
>>> rv = client.request(r)
>>> ...
data

data property.

return the JSON order body

class oandapyV20.contrib.requests.TakeProfitOrderRequest(tradeID, price, clientTradeID=None, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a TakeProfit OrderRequest.

TakeProfitOrderRequest is used to build the body for a TakeProfitOrder. The body can be used to pass to the OrderCreate endpoint.

__init__(tradeID, price, clientTradeID=None, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Instantiate a TakeProfitOrderRequest.

Parameters:
  • tradeID (string (required)) – the tradeID of an existing trade
  • price (float (required)) – the price indicating the target price to close the order.

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import TakeProfitOrderRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> ordr = TakeProfitOrderRequest(tradeID="1234",
>>>                               price=1.22)
>>> print(json.dumps(ordr.data, indent=4))
>>> r = orders.OrderCreate(accountID, data=ordr.data)
>>> rv = client.request(r)
>>> ...
data

data property.

return the JSON order body

class oandapyV20.contrib.requests.StopLossOrderRequest(tradeID, price, clientTradeID=None, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a StopLossOrderRequest.

StopLossOrderRequest is used to build the body for a MarketOrder. The body can be used to pass to the OrderCreate endpoint.

__init__(tradeID, price, clientTradeID=None, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Instantiate a StopLossOrderRequest.

Parameters:
  • tradeID (string (required)) – the tradeID of an existing trade
  • price (float (required)) – the treshold price indicating the price to close the order

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import StopLossOrderRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> ordr = StopLossOrderRequest(tradeID="1234", price=1.07)
>>> print(json.dumps(ordr.data, indent=4))
{
    "order": {
        "type": "STOP_LOSS",
        "tradeID": "1234",
        "price": "1.07000",
        "timeInForce": "GTC",
    }
}
>>> # now we have the order specification, create the order request
>>> r = orders.OrderCreate(accountID, data=ordr.data)
>>> # perform the request
>>> rv = client.request(r)
>>> print(rv)
>>> print(json.dumps(rv, indent=4))
>>> ...
data

data property.

return the JSON body.

class oandapyV20.contrib.requests.TrailingStopLossOrderRequest(tradeID, distance, clientTradeID=None, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a TrailingStopLossOrderRequest.

TrailingStopLossOrderRequest is used to build the body for a TrailingStopLossOrder. The body can be used to pass to the OrderCreate endpoint.

__init__(tradeID, distance, clientTradeID=None, timeInForce='GTC', gtdTime=None, clientExtensions=None)

Instantiate a TrailingStopLossOrderRequest.

Parameters:
  • tradeID (string (required)) – the tradeID of an existing trade
  • distance (float (required)) – the price distance

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import TrailingStopLossOrderRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> ordr = TrailingStopLossOrderRequest(tradeID="1234", distance=20)
>>> print(json.dumps(ordr.data, indent=4))
{
    "order": {
        "type": "TRAILING_STOP_LOSS",
        "tradeID": "1234",
        "timeInForce": "GTC",
        "distance": "20.00000"
    }
}
>>> # now we have the order specification, create the order request
>>> r = orders.OrderCreate(accountID, data=ordr.data)
>>> # perform the request
>>> rv = client.request(r)
>>> print(rv)
>>> print(json.dumps(rv, indent=4))
>>> ...
data

data property.

return the JSON body.

class oandapyV20.contrib.requests.StopOrderRequest(instrument, units, price, priceBound=None, positionFill='DEFAULT', timeInForce='GTC', gtdTime=None, clientExtensions=None, takeProfitOnFill=None, stopLossOnFill=None, trailingStopLossOnFill=None, tradeClientExtensions=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a StopOrderRequest.

StopOrderRequest is used to build the body for an StopOrder. The body can be used to pass to the OrderCreate endpoint.

__init__(instrument, units, price, priceBound=None, positionFill='DEFAULT', timeInForce='GTC', gtdTime=None, clientExtensions=None, takeProfitOnFill=None, stopLossOnFill=None, trailingStopLossOnFill=None, tradeClientExtensions=None)

Instantiate a StopOrderRequest.

Parameters:
  • instrument (string (required)) – the instrument to create the order for
  • units (integer (required)) – the number of units. If positive the order results in a LONG order. If negative the order results in a SHORT order
  • price (float (required)) – the treshold price indicating the price to activate the order

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.orders as orders
>>> from oandapyV20.contrib.requests import StopOrderRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> ordr = StopOrderRequest(instrument="EUR_USD",
>>>                         units=10000, price=1.07)
>>> print(json.dumps(ordr.data, indent=4))
{
    "order": {
        "type": "STOP",
        "price": "1.07000",
        "positionFill": "DEFAULT",
        "instrument": "EUR_USD",
        "timeInForce": "GTC",
        "units": "10000"
    }
}
>>> # now we have the order specification, create the order request
>>> r = orders.OrderCreate(accountID, data=ordr.data)
>>> # perform the request
>>> rv = client.request(r)
>>> print(rv)
>>> print(json.dumps(rv, indent=4))
>>> ...
data

data property.

return the JSON body.

class oandapyV20.contrib.requests.PositionCloseRequest(longUnits=None, longClientExtensions=None, shortUnits=None, shortClientExtensions=None)

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a PositionCloseRequest.

PositionCloseRequest is used to build the body to close a position. The body can be used to pass to the PositionClose endpoint.

__init__(longUnits=None, longClientExtensions=None, shortUnits=None, shortClientExtensions=None)

Instantiate a PositionCloseRequest.

Parameters:
  • longUnits (integer (optional)) – the number of long units to close
  • longClientExtensions (dict (optional)) – dict representing longClientExtensions
  • shortUnits (integer (optional)) – the number of short units to close
  • shortClientExtensions (dict (optional)) – dict representing shortClientExtensions
  • of the parameters or both must be supplied. (One) –

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.positions as positions
>>> from oandapyV20.contrib.requests import PositionCloseRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> ordr = PositionCloseRequest(longUnits=10000)
>>> print(json.dumps(ordr.data, indent=4))
{
   "longUnits": "10000"
}
>>> # now we have the order specification, create the order request
>>> r = position.PositionClose(accountID,
>>>                            instrument="EUR_USD", data=ordr.data)
>>> # perform the request
>>> rv = client.request(r)
>>> print(rv)
>>> ...
class oandapyV20.contrib.requests.TradeCloseRequest(units='ALL')

Bases: oandapyV20.contrib.requests.baserequest.BaseRequest

create a TradeCloseRequest.

TradeCloseRequest is used to build the body to close a trade. The body can be used to pass to the TradeClose endpoint.

__init__(units='ALL')

Instantiate a TradeCloseRequest.

Parameters:units (integer (optional)) – the number of units to close. Default it is set to “ALL”.

Example

>>> import json
>>> from oandapyV20 import API
>>> import oandapyV20.endpoints.trades as trades
>>> from oandapyV20.contrib.requests import TradeCloseRequest
>>>
>>> accountID = "..."
>>> client = API(access_token=...)
>>> ordr = TradeCloseRequest(units=10000)
>>> print(json.dumps(ordr.data, indent=4))
{
   "units": "10000"
}
>>> # now we have the order specification, create the order request
>>> r = trades.TradeClose(accountID, tradeID=1234,
>>>                       data=ordr.data)
>>> # perform the request
>>> rv = client.request(r)
>>> print(rv)
>>> ...