This notebook provides an example of
In [9]:
import json
import oandapyV20
import oandapyV20.endpoints.orders as orders
from exampleauth import exampleauth
accountID, access_token = exampleauth.exampleAuth()
client = oandapyV20.API(access_token=access_token)
# create a market order to enter a LONG position 10000 EUR_USD, stopLoss @1.07 takeProfit @1.10 ( current: 1.055)
# according to the docs at developer.oanda.com the requestbody looks like:
mktOrder = {
"order": {
"timeInForce": "FOK", # Fill-or-kill
"instrument": "EUR_USD",
"positionFill": "DEFAULT",
"type": "MARKET",
"units": 10000, # as integer
"takeProfitOnFill": {
"timeInForce": "GTC", # Good-till-cancelled
"price": 1.10 # as float
},
"stopLossOnFill": {
"timeInForce": "GTC",
"price": "1.07" # as string
}
}
}
r = orders.OrderCreate(accountID=accountID, data=mktOrder)
print("Request: ", r)
print("MarketOrder specs: ", json.dumps(mktOrder, indent=2))
Well that looks fine, but constructing orderbodies that way is not really what we want. Types are not checked for instance and all the defaults need to be supplied.
This kind of datastructures can become complex, are not easy to read or construct and are prone to errors.
Oanda uses several types and definitions througout their documentation. These types are covered by the oandapyV20.types package and the definitions by the oandapyV20.definitions package.
The oandapyV20.contrib.requests package offers classes providing an easy way to construct the data for the data parameter of the OrderCreate endpoint or the TradeCRCDO (Create/Replace/Cancel Dependent Orders). The oandapyV20.contrib.requests package makes use of the oandapyV20.types and oandapyV20.definitions.
Let's improve the previous example by making use of oandapyV20.contrib.requests:
In [1]:
import json
import oandapyV20
import oandapyV20.endpoints.orders as orders
from oandapyV20.contrib.requests import (
MarketOrderRequest,
TakeProfitDetails,
StopLossDetails)
from exampleauth import exampleauth
accountID, access_token = exampleauth.exampleAuth()
client = oandapyV20.API(access_token=access_token)
# create a market order to enter a LONG position 10000 EUR_USD
mktOrder = MarketOrderRequest(instrument="EUR_USD",
units=10000,
takeProfitOnFill=TakeProfitDetails(price=1.10).data,
stopLossOnFill=StopLossDetails(price=1.07).data
).data
r = orders.OrderCreate(accountID=accountID, data=mktOrder)
print("Request: ", r)
print("MarketOrder specs: ", json.dumps(mktOrder, indent=2))
As you can see, the specs contain price values that were converted to strings and the defaults positionFill and timeInForce were added. Using contrib.requests makes it very easy to construct the orderdata body for order requests. Parameters for those requests are also validated.
Next step, place the order:
In [2]:
rv = client.request(r)
print("Response: {}\n{}".format(r.status_code, json.dumps(rv, indent=2)))
Lets analyze that. We see an orderCancelTransaction and reason STOP_LOSS_ON_FILL_LOSS. So the order was not placed ? Well it was placed and cancelled right away. The marketprice of EUR_USD is at the moment of this writing 1.058. So the stopLoss order at 1.07 makes no sense. The status_code of 201 is as the specs say: http://developer.oanda.com/rest-live-v20/order-ep/ .
Lets change the stopLoss level below the current price and place the order once again.
In [3]:
mktOrder = MarketOrderRequest(instrument="EUR_USD",
units=10000,
takeProfitOnFill=TakeProfitDetails(price=1.10).data,
stopLossOnFill=StopLossDetails(price=1.05).data
).data
r = orders.OrderCreate(accountID=accountID, data=mktOrder)
rv = client.request(r)
print("Response: {}\n{}".format(r.status_code, json.dumps(rv, indent=2)))
We now see an orderFillTransaction for 10000 units EUR_USD with reason MARKET_ORDER.
Lets retrieve the orders. We should see the stopLoss and takeProfit orders as pending:
In [4]:
r = orders.OrdersPending(accountID=accountID)
rv = client.request(r)
print("Response:\n", json.dumps(rv, indent=2))
Depending on the state of your account you should see at least the orders associated with the previously executed marketorder. The relatedTransactionIDs should be in the orders output of OrdersPending().
Now lets cancel all pending TAKE_PROFIT orders:
In [5]:
r = orders.OrdersPending(accountID=accountID)
rv = client.request(r)
idsToCancel = [order.get('id') for order in rv['orders'] if order.get('type') == "TAKE_PROFIT"]
for orderID in idsToCancel:
r = orders.OrderCancel(accountID=accountID, orderID=orderID)
rv = client.request(r)
print("Request: {} ... response: {}".format(r, json.dumps(rv, indent=2)))
In [12]:
from oandapyV20.contrib.requests import LimitOrderRequest
# make sure GTD_TIME is in the future
# also make sure the price condition is not met
# and specify GTD_TIME as UTC or local
# GTD_TIME="2018-07-02T00:00:00Z" # UTC
GTD_TIME="2018-07-02T00:00:00"
ordr = LimitOrderRequest(instrument="EUR_USD",
units=10000,
timeInForce="GTD",
gtdTime=GTD_TIME,
price=1.08)
print(json.dumps(ordr.data, indent=4))
r = orders.OrderCreate(accountID=accountID, data=ordr.data)
rv = client.request(r)
print(json.dumps(rv, indent=2))
In [13]:
r = orders.OrdersPending(accountID=accountID)
rv = client.request(r)
print(json.dumps(rv, indent=2))
In [14]:
r = orders.OrderCancel(accountID=accountID, orderID=8923)
rv = client.request(r)
print(json.dumps(rv, indent=2))
Request pendig orders once again ... the 8923 should be gone
In [15]:
r = orders.OrdersPending(accountID=accountID)
rv = client.request(r)
print(json.dumps(rv, indent=2))