def delete_price_sub(context_id, *, ref_id=None): token = get_token() client = API(access_token=token) if ref_id is None: # delete whole thing req = trading.prices.PriceSubscriptionRemoveByTag(context_id) else: req = trading.prices.PriceSubscriptionRemove(context_id, ref_id) client.request(req) status = "succesful" if req.status_code == req.expected_status else "failed" print(f"price sub delete: {context_id} {ref_id} {status}")
def test__requests_params(self): """request parameters.""" request_params = {"timeout": 10} api = API(environment=environment, access_token=self.access_token, request_params=request_params) self.assertTrue(api.request_params == request_params)
def create_price_sub(context_id, *instruments): """fetch instrument data by the name of the instrument and extract the Uic (Identifier) and use that to subscribe for prices. Use the name of the instrument as reference. """ token = get_token() client = API(access_token=token) account_info = session.account_info(client=client) # body template for price subscription body = { "Arguments": { "Uic": "", "AssetType": "FxSpot" }, "ContextId": "", "ReferenceId": "" } body['ContextId'] = context_id for instrument in instruments: params = { 'AccountKey': account_info.AccountKey, 'AssetTypes': 'FxSpot', 'Keywords': instrument } # create the request to fetch Instrument info req = referencedata.instruments.Instruments(params=params) rv = client.request(req) print(rv) rv = [x for x in rv['Data'] if x['Symbol'] == instrument] assert len(rv) == 1 rv = rv[0] body['Arguments'].update({'Uic': rv['Identifier']}) body.update({"ReferenceId": instrument}) print(json.dumps(body, indent=2)) # create the request to fetch Instrument info req = trading.prices.CreatePriceSubscription(data=body) client.request(req) status = "succesful" if req.status_code == req.expected_status else "failed" print(f"Subscription for instrument: {instrument} {status}")
def test__requests_exception(self): """force a requests exception.""" from requests.exceptions import RequestException import saxo_openapi.endpoints.portfolio as pf setattr( sys.modules["saxo_openapi.saxo_openapi"], "TRADING_ENVIRONMENTS", { "simulation": { "stream": "ttps://test.com", "api": "ttps://test.com", "prefix": "sim" } }) api = API(environment=environment, access_token=self.access_token) text = "No connection " \ "adapters were found for " \ "'ttps://test.com/sim/openapi/port/v1/accounts/me'" r = pf.accounts.AccountsMe() with self.assertRaises(RequestException) as oErr: api.request(r) self.assertEqual("{}".format(oErr.exception), text)
def setUp(self): """Set up test fixtures, if any.""" self.access_token = None self.api = None try: self.access_token = auth() setattr(sys.modules["saxo_openapi.saxo_openapi"], "TRADING_ENVIRONMENTS", mock_env) self.api = API(environment=environment, access_token=self.access_token) self.api.api_url = 'https://test.com' except Exception as e: print(e) exit(0)
from saxo_openapi import API import saxo_openapi.endpoints.rootservices as rs import saxo_openapi.endpoints.chart as chart from pprint import pprint import json token = "eyJhbGciOiJFUzI1NiIsIng1dCI6IjhGQzE5Qjc0MzFCNjNFNTVCNjc0M0QwQTc5MjMzNjZCREZGOEI4NTAifQ.eyJvYWEiOiI3Nzc3NSIsImlzcyI6Im9hIiwiYWlkIjoiMTA5IiwidWlkIjoiVnFRemJCSGVvTjQ4UXNPalNTZ1Bldz09IiwiY2lkIjoiVnFRemJCSGVvTjQ4UXNPalNTZ1Bldz09IiwiaXNhIjoiRmFsc2UiLCJ0aWQiOiIyMDAyIiwic2lkIjoiNTk4ZjUyNjAxZDRiNGU4OTgwODk2ZWFjNjA0NWM3YTAiLCJkZ2kiOiI4NCIsImV4cCI6IjE2MTE1NTY5ODEifQ.3IKHbaLnF1I-PirYcuKN3qB00wYeBAb4SAKuQhvGUPZf6eTBmAGSFi_rruiUfPStg90m87cKF-j71h6OWJ47oQ" client = API(access_token=token) # lets make a diagnostics request, it should return '' with a state 200 r = rs.diagnostics.Get() print("request is: ", r) rv = client.request(r) assert rv is None and r.status_code == 200 print('diagnostics passed') # request available rootservices-features r = rs.features.Availability() rv = client.request(r) print("request is: ", r) print("response: ") pprint(rv, indent=2) print(r.status_code) params = {"AssetType": "FxSpot", "Horizon": 1, "Count": 1000, "Uic": 23} r = chart.charts.GetChartData(params=params) rv = client.request(r) print(json.dumps(rv, indent=2))
def test__saxo_environment(self): """test the exception on a faulty environment.""" with self.assertRaises(KeyError) as envErr: API(environment="faulty", access_token=self.access_token) self.assertTrue("Unknown environment" in "{}".format(envErr.exception))