Reputation: 1186
Hi I'm trying to implement a forex trading algorithm using the OANDA api. I created an account in OANDA and the key was generated.
When following code was implemented, I didn't recive any error, but seems that the data are not getting fetched.
"""
The main file that will evolve into our trading library
"""
from datetime import datetime, timedelta
import v20
OANDA_ACCESS_TOKEN = ""
OANDA_ACCOUNT_ID = '' #put your access id here
def main():
print("------ System online -------", datetime.now())
latest_price_time = (datetime.utcnow() - timedelta(seconds=15)).isoformat('T')+'Z'
api = v20.Context(
'api-fxpractice.oanda.com',
'443',
token=OANDA_ACCESS_TOKEN)
response = api.pricing.get(
OANDA_ACCOUNT_ID,
instruments='EUR_USD',
since=latest_price_time,
includeUnitsAvailable=False)
print(response)
prices = response.get("prices", 200)
print (prices)
if len(prices):
buy_price = prices[0].bids[0].price
print("Buy at " + str(buy_price))
response = api.order.market(
OANDA_ACCOUNT_ID,
instrument='EUR_USD',
units=5000)
print("Trading id: " + str(response.get('orderFillTransaction').id))
print("Account Balance: " + str(response.get('orderFillTransaction').accountBalance))
print("Price: " + str(response.get('orderFillTransaction').price))
else:
print(response.reason)
if __name__ == "__main__":
main()
Here is the output that I received.
------ System online ------- 2019-04-27 13:08:32.531057 Method = GET Path = https://api-fxpractice.oanda.com:443/v3/accounts/101-011-11099167-001/pricing?instruments=EUR_USD&since=2019-04-27T07%3A38%3A17.531057Z&includeUnitsAvailable=False Status = 200 Reason = OK Content-Type = application/json
[] OK
But when the above path was accessed I'm receiving the following error.
Upvotes: 0
Views: 239
Reputation: 350
The link in the error message needs the authentication header, that is not a public link only you with the proper token can see it. Try it with postman and add the authentication to the request header.
prices = ctx.pricing.get('ACCOUNT_ID', instruments='EUR_USD').get('prices')
print(prices[0])
and the result is something like this:
type: PRICE
instrument: EUR_USD
time: '2021-12-03T22:00:00.103275355Z'
status: non-tradeable
tradeable: false
bids:
- price: 1.13145
liquidity: 10000000
asks:
- price: 1.13245
liquidity: 10000000
closeoutBid: 1.13145
closeoutAsk: 1.13245
quoteHomeConversionFactors:
positiveUnits: 1.0
negativeUnits: 1.0
On the other side, you dont really need the latest price if you buy on market.
Upvotes: 1