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?
def Initialize(self):
self.SetAlgorithmId("algoId123")
self.Debug(f"AlgorithmId is {self.AlgorithmId}")
Attached my API code below..
#-- authentication
import base64
import hashlib
import time
#-- http call
import requests
#-- json formatting
import json
#-- CONFIGURATION
api_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
base_url = "https://www.quantconnect.com/api/v2"
#-- GET API TOKEN
timestamp = str(int(time.time()))
time_stamped_token = api_token + ':' + timestamp
hashed_token = hashlib.sha256(time_stamped_token.encode('utf-8')).hexdigest()
authentication = "{}:{}".format(user_ID, hashed_token)
api_token = base64.b64encode(authentication.encode('utf-8')).decode('ascii')
# print(f"api_token: {api_token}")
# Create headers dictionary.
headers = {
'Authorization': 'Basic %s' % api_token,
'Timestamp': timestamp
}
#-- 1) AUTHENTICATION (successful)
request_url = base_url + "/authenticate"
response = requests.post(request_url,
data = {},
headers = headers)
content = response.text
print(f"Authentication is {content}")
#-- 2) READ PROJECT (successful)
request_url = base_url + "/projects/read"
dataDict = {}
dataDict["projectId"] = "13299999"
response = requests.post(request_url,
data = dataDict,
headers = headers)
content = response.json()
pretty_content = json.dumps(content, indent=4)
print(f"project's content is {pretty_content}")
#-- 3) READ LIVE PORTFOLIO (not working)
request_url = base_url + "/live/read/portfolio"
dataDict = {}
dataDict["projectId"] = "13299999"
response = requests.post(request_url,
data = dataDict,
headers = headers)
content = response.json()
pretty_content = json.dumps(content, indent=4)
print(f"project's live portfolio is {pretty_content}")
#-- 4) READ LIVE LOG (not working)
request_url = base_url + "/live/read/log"
dataDict = {}
dataDict["format"] = "json"
dataDict["projectId"] = "13299999"
dataDict["algorithmId"] = "algoId123"
dataDict["start"] = 0
dataDict["end"] = 99999999999
response = requests.post(request_url,
data = dataDict,
headers = headers)
content = response.json()
pretty_content = json.dumps(content, indent=4)
print(f"project's log is {pretty_content}")
Pranava
This is the output I have received for the live portfolio's portion.
Pranava
Hi, I manage to get it working.
The AlgorithmId apparently must use the one returned when the algorithm is launched live - Launching analysis for L-bxxxxxxxxxxxxxxxxxxx with LEAN Engine v2.5.0.0.14854
The live portfolio somehow also worked now… It could be because I retrigger a compilation or 1st backtest of the code.
Pranava
If I have stop and restarted the algorithm multiple times, then I would not be able to get the logs in 1 go. Not crucial but would be a useful feature.
Alternatively would there be a way for me to obtain the all AlgorithmIds or for BackTests, all the backtestIDs without looking at the logs?
Pranava
Hi,
Please ignore the last comment. think there is just 1 algorithmId.
But is there a limit to the size of the log I can download? Seems like capped around 1MB. no matter the date range I set.
Louis Szeto
Hi Pranava
You can call /backtests/read with only the projectId as payload (do not specify the backtestId). It will return a BacktestList Model object containing all backtest instances.
The size was still subjected to the cloud log limit (1Mb per backtest for Team tier).
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Pranava
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!