-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoanda_api.py
68 lines (56 loc) · 2.42 KB
/
oanda_api.py
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
from rest_api import RestAPI
import security
from logger import Logger
from sqlite_conn import SQLiteConn
from psql_conn import PSQLConn
from security import oanda_auth_keys
from datetime import datetime
import pytz
log = Logger(__name__)
TRADE_ENV = {
"practice": {
"stream": 'https://stream-fxpractice.oanda.com/v3/',
"api": 'https://api-fxpractice.oanda.com/v3/'
},
"live": {
"stream": 'https://stream-fxtrade.oanda.com/v3/',
"api": 'https://api-fxtrade.oanda.com/v3/'
}
}
class OandaAPI(RestAPI):
db_path = "DB/oanda.sqlite"
EDT = pytz.timezone('America/New_York')
ALA = pytz.timezone('America/Los_Angeles')
def __init__(self, auth):
super().__init__()
self.conn = SQLiteConn(OandaAPI.db_path)
self.conn = PSQLConn('oanda')
if TRADE_ENV[auth['env']]:
self.url_api = TRADE_ENV[auth['env']]['api']
self.url_stream = TRADE_ENV[auth['env']]['stream']
else:
log_rest.log_err(f"Unknown trade environment: {auth['env']}")
self.session.headers = {'Accept - Encoding': 'gzip, deflate',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + auth['key'],
'Accept-Datetime-Format': 'UNIX',
}
self.headers = self.session.headers
self.url_accounts = f"{self.url_api}accounts"
self.accounts_ids = [account['id'] for account in self.get_accounts()['accounts']] # accounts ids list
log.log_info('Oanda_API Init')
def get_accounts(self):
return self.get(self.url_accounts)
@staticmethod
def from_ts(timestamp):
return datetime.fromtimestamp(float(timestamp))
if __name__ == "__main__":
oanda = OandaAPI(oanda_auth_keys[1])
# print(oanda.get('https://api-fxpractice.oanda.com/v3/instruments/EUR_USD/candles?count=6&price=M&granularity=S5').text)
# print(oanda.get('https://api-fxpractice.oanda.com/v3/instruments/EUR_USD/candles?count=6&price=M&granularity=S5&alignmentTimezone=GMT').text)
# print(oanda.get('https://api-fxpractice.oanda.com/v3/accounts').text)
print(oanda.get('https://api-fxpractice.oanda.com/v3/accounts'))
print(oanda.get_accounts())
print(oanda.accounts_ids)
# print(oanda.get_account_details('101-001-15249313-002'))
# print(oanda.get_account_summary('101-001-15249313-001'))