-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oanda1
executable file
·103 lines (80 loc) · 3.17 KB
/
oanda1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# system imports
import sys
import os
import json
# OANDA library functions
import oandapyV20
import oandapyV20.endpoints
import oandapyV20.endpoints.accounts as v20Accounts
import oandapyV20.endpoints.instruments as v20Instruments
import oandapyV20.endpoints.pricing as v20Pricing
import oandapyV20.endpoints.orders as v20Orders
import oandapyV20.endpoints.positions as v20Positions
import oandapyV20.contrib.requests as v20Requests
def ShowStop(variable):
print(variable)
sys.exit(0)
###
### Main code base
###
# Load credentials from file
Credentials={}
if os.path.exists('credentials.txt'):
FileHandle=open('credentials.txt','r')
Credentials['AccountID']=FileHandle.readline().strip()
Credentials['BearerToken']=FileHandle.readline().strip()
FileHandle.close()
else:
print("You must save your OANDA account crdentials to: credentials.txt")
sys.exit(1)
# Try to connect to the OANDA broker
try:
oanda=oandapyV20.API(access_token=Credentials['BearerToken'])
except Exception as err:
print("Error: ",str(err))
sys.exit(1)
# Load the market data
markets={}
req=v20Accounts.AccountInstruments(accountID=Credentials['AccountID'])
results=oanda.request(req)
# Collect Bid/Ask data from OANDA and calculate the spread.
# Use a loop to iterate (count) through all of the available assets.
for cur in results['instruments']:
asset=cur['name'].upper().replace('_','/')
# OANDA request to pull ticker data
symbol=cur['name']
params={"instruments":symbol }
req=v20Pricing.PricingInfo(accountID=Credentials['AccountID'],params=params)
ticker=oanda.request(req)
# Build the forex pair dictionary
ForexPair={}
ForexPair['Ask']=round(float(ticker['prices'][0]['asks'][0]['price']),5)
ForexPair['Bid']=round(float(ticker['prices'][0]['bids'][0]['price']),5)
ForexPair['Spread']=round(ForexPair['Ask']-ForexPair['Bid'],5)
# This lets use manipulate the asset as follows: markets['EUR/USD']
markets[asset]=ForexPair
# Sort the dictionary based upon the spread. Remember the KEY/VALUE paradigm.
# This is really the hardest line in the entire program.
#
# KEY VALUE: yes, it can be any object, including a dictionary
# USD/JPY {'Ask': 142.661, 'Bid': 142.561, 'Spread': 0.1}
#
# lambda is a shorthand approach for the VALUE of the primary KEY,
# USD/JPY in this example. x[1] is the VALUE of the USD/JPY or
# {'Ask': 142.661, 'Bid': 142.561, 'Spread': 0.1}
#
# We explicitly want the Spread VALUE to be hoe we sort the ENTIRE main
# dictionary of markets.
#
# There is a conversion process that occurs internally to get this done right.
# Its actually a very complicated process, so we need to explicitly tell python
# we want a dict(ionary) when all is said and done.
markets=dict(sorted(markets.items(), key=lambda x: (x[1]['Spread'])))
# Print some fancy headers to our table. We gott make it look nice.
print(f"{'Asset':12}|{'Buy':>12}|{'Spread':>12}|{'Sell':>12}")
print(f"{'-'*12}|{'-'*12}|{'-'*12}|{'-'*12}")
# Print the actual table in a formated style
for cur in markets:
print(f"{cur:12} {markets[cur]['Ask']:12.5f} {markets[cur]['Spread']:12.5f} {markets[cur]['Bid']:12.5f}")