Hi, 

I am trying to read my live portfolio's log via API but am unable to get it to work.

Have verified that the API call's authentication is working, and tested with getting project's detail which also work. 

For /live/read/log  there is an algorithmId, I have set it in main.py→ Initialize using SetAlgorithmId. Is this the correct way to do it?

  1. def Initialize(self):
  2. self.SetAlgorithmId("algoId123")
  3. self.Debug(f"AlgorithmId is {self.AlgorithmId}")

 

Attached my API code below..

  1. #-- authentication
  2. import base64
  3. import hashlib
  4. import time
  5. #-- http call
  6. import requests
  7. #-- json formatting
  8. import json
  9. #-- CONFIGURATION
  10. api_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  11. base_url = "https://www.quantconnect.com/api/v2"
  12. #-- GET API TOKEN
  13. timestamp = str(int(time.time()))
  14. time_stamped_token = api_token + ':' + timestamp
  15. hashed_token = hashlib.sha256(time_stamped_token.encode('utf-8')).hexdigest()
  16. authentication = "{}:{}".format(user_ID, hashed_token)
  17. api_token = base64.b64encode(authentication.encode('utf-8')).decode('ascii')
  18. # print(f"api_token: {api_token}")
  19. # Create headers dictionary.
  20. headers = {
  21. 'Authorization': 'Basic %s' % api_token,
  22. 'Timestamp': timestamp
  23. }
  24. #-- 1) AUTHENTICATION (successful)
  25. request_url = base_url + "/authenticate"
  26. response = requests.post(request_url,
  27. data = {},
  28. headers = headers)
  29. content = response.text
  30. print(f"Authentication is {content}")
  31. #-- 2) READ PROJECT (successful)
  32. request_url = base_url + "/projects/read"
  33. dataDict = {}
  34. dataDict["projectId"] = "13299999"
  35. response = requests.post(request_url,
  36. data = dataDict,
  37. headers = headers)
  38. content = response.json()
  39. pretty_content = json.dumps(content, indent=4)
  40. print(f"project's content is {pretty_content}")
  41. #-- 3) READ LIVE PORTFOLIO (not working)
  42. request_url = base_url + "/live/read/portfolio"
  43. dataDict = {}
  44. dataDict["projectId"] = "13299999"
  45. response = requests.post(request_url,
  46. data = dataDict,
  47. headers = headers)
  48. content = response.json()
  49. pretty_content = json.dumps(content, indent=4)
  50. print(f"project's live portfolio is {pretty_content}")
  51. #-- 4) READ LIVE LOG (not working)
  52. request_url = base_url + "/live/read/log"
  53. dataDict = {}
  54. dataDict["format"] = "json"
  55. dataDict["projectId"] = "13299999"
  56. dataDict["algorithmId"] = "algoId123"
  57. dataDict["start"] = 0
  58. dataDict["end"] = 99999999999
  59. response = requests.post(request_url,
  60. data = dataDict,
  61. headers = headers)
  62. content = response.json()
  63. pretty_content = json.dumps(content, indent=4)
  64. print(f"project's log is {pretty_content}")
+ Expand

 

 

Author

Pranava

November 2022