hootnot / oanda-api-v20

OANDA REST-V20 API wrapper. Easy access to OANDA's REST v20 API with oandapyV20 package. Checkout the Jupyter notebooks!
MIT License
403 stars 107 forks source link

order placing example #17

Closed jorfermo closed 8 years ago

jorfermo commented 8 years ago

Can you post an example on how to place a new order?

hootnot commented 8 years ago

... yup, I can:

import json
import sys
import requests
from oandapyV20 import API
import oandapyV20.endpoints.orders as orders
from oandapyV20.exceptions import V20Error
from exampleauth import exampleAuth

accountID, token = exampleAuth()

oConf = {
         "order": {
            "units": "100",
            "instrument": "EUR_USD",
            "timeInForce": "FOK",
            "type": "MARKET",
            "positionFill": "DEFAULT"
          }
        }

# client
api = API(access_token=token, headers={"Content-Type": "application/json"})

# order create request
r = orders.OrderCreate(accountID=accountID, data=oConf)
response = api.request(r)
print("{}".format(json.dumps(response, indent=2)))

# but it is also in the request: r._response 

Will give:

```json
{
  "orderFillTransaction": {
    "orderID": "2215", 
    "financing": "0.0000", 
    "instrument": "EUR_USD", 
    "price": "1.09673", 
    "userID": 1435156, 
    "batchID": "2215", 
    "accountBalance": "35126.0096", 
    "reason": "MARKET_ORDER", 
    "tradeOpened": {
      "units": "100", 
      "tradeID": "2216"
    }, 
    "time": "2016-10-19T17:13:20.236527425Z", 
    "units": "100", 
    "type": "ORDER_FILL", 
    "id": "2216", 
    "pl": "0.0000", 
    "accountID": "101-004-1435156-001"
  }, 
  "orderCreateTransaction": {
    "timeInForce": "FOK", 
    "positionFill": "DEFAULT", 
    "userID": 1435156, 
    "batchID": "2215", 
    "instrument": "EUR_USD", 
    "reason": "CLIENT_ORDER", 
    "time": "2016-10-19T17:13:20.236527425Z", 
    "units": "100", 
    "type": "MARKET_ORDER", 
    "id": "2215", 
    "accountID": "101-004-1435156-001"
  }, 
  "relatedTransactionIDs": [
    "2215", 
    "2216"
  ], 
  "lastTransactionID": "2216"
}
jorfermo commented 8 years ago

Thanks!

I didn't add the headers={"Content-Type": "application/json"} in the constructor and it was complaining about wrong JSON :)

hootnot commented 8 years ago

All API calls that send data need to have that header. I will see to make some changes that the header is either set automatically when such calls are made, or that some exception is raised.

jorfermo commented 8 years ago

If it's mandatory it would be great to have it automatically set

hootnot commented 8 years ago

Done. See #18.