Back

Compile using REST API

Hello,

I try to use REST API to compile projects, however, the compilation is always failed and the error message is as follows. The same project can be compiled successfully from Web IDE. Does any one have the same issue? Thanks,

[0]: "Timeout on build request: x{"iUserID":"16503","iTimestamp":1455771522,"sSessionID":"0.48976300 1455770939","iProjectID":"208146","sActionResponse":"compiler.build","sResponseCode":"200 OK","sState":"busy","sCompileID":"bbc2576e3f500142accbd74a8f26ac55"}"

Update Backtest








I'm done this a bit and may be able to help. Can you post your request code?

0

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.


Thanks Ray. Here is the code. I am using RestSharp. I can read project files using the similar code, but just cannot compile successfully.


private class Item
{
public int projectId;
}

var client = new RestClient("https://www.quantconnect.com");
client.Authenticator = new HttpBasicAuthenticator("myaccount", "mypassword");

var request = new RestRequest("api/v1/compiler/create", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new Item
{
projectId = 208146
});

var response = client.Execute(request);
0

Can you try adding this to your body?

versionId = 241

Also Ive noticed that if its been a while since the call was made you actually have to call it a couple of times for it to be successful

0

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.


Here is my function that is working against a beta server. I'm not set up right now to test it against the production endpoint, but if I recall thats the missing component. If not I can continue to help troubleshoot

private static Boolean PostApiCompilerCreate(int projectId, int leanVersionId, out string compileId)
{
compileId = "";

var client = new RestClient("https://www.quantconnect.com/api/v1/" )
{
Authenticator = new HttpBasicAuthenticator(Config.Get("user"), Config.Get("pass"))
};

var request = new RestRequest("compiler/create", Method.POST);
var requestStr = JsonConvert.SerializeObject(new {projectId = projectId, versionId = leanVersionId});
request.AddParameter("application/json", requestStr, ParameterType.RequestBody);

Console.WriteLine("Posting compiler/create "+requestStr);
IRestResponse response = client.Execute(request);
var json = response.Content;

JObject o = JObject.Parse(json);
Boolean success = false;


try
{
success = o["success"].ToString().ToLower() != "false";
compileId = o["compileId"].ToString();
}
catch (Exception err)
{
JSONError(o,
"Invalid JSON Response Received. Missing expected values 'success' or 'compileId'. "+err.Message);
return false;

}

if (!success)
{
JSONError(o, "Server response success = false");
return false;
}


Console.WriteLine("Success! compileId = "+ compileId);
// Full JSON Debug
Console.WriteLine(o.ToString());
return true;
}

2

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.


Thanks for your help, Ray! I tried and the compilation works now.

0

Hi,
I'm facing the same problem,
In my case the response content i receive is missing the compileID:
"{\"success\":true,\"errors\":[],\"ip\":\"46.116.30.78\"}"

Thanks,
Eran

0

Can you post your code?

0

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.


i've attached a link
https://www.dropbox.com/sh/lgmip4rkrqx4m64/AAC3tWfVQfZ4RAI-cH4z_9xta?dl=0
see what you can do

0

Ray, did you succeed to get the backtest results with REST API?
i always get empty results, even tough the algo is running fine in the WEB IDE.

0

Hi,

I am having the same issue. Any REST API call returns only the following results when successful:

Common response for compile/create, backtests/create, backtests/read:
{"success":true,"errors":[],"ip":"144.76.73.133"}

Expected responses:
compile/create: {"success":true,"errors":[],"compileId": "...","log" :"..."}
backtests/create: {"success":true,"errors":[],"backtestId": "..."}
backtests/read: {"success":true,"errors":[],"progress" : "...","processingTime" : ..., "results" : ...}

Code below, based on Ray's code in the comments above:

Boolean wSuccess = false;
wSuccess = PostApiCompilerCreate(228116, 241);
//wSuccess = PostApiBacktestCreate(228116, 241, "30024ef45ecd03f3e994b05cf59a5188", "REST Backtest");
//wSuccess = PostApiBacktestRead("20b44532add14add4212914c0a54cdd6", 241);


private Boolean PostApiCompilerCreate(int projectId, int leanVersionId)
{
return PostApi("compiler/create", new {projectId = projectId, versionId = leanVersionId});
}

private Boolean PostApiBacktestCreate(int iProjectId, int leanVersionId, string iCompileId, string iBacktestName)
{
return PostApi("Backtests/create", new {projectId = iProjectId, versionId = leanVersionId, compileId = iCompileId, backtestName = iBacktestName});
}

private Boolean PostApiBacktestRead(string iBacktestId, int leanVersionId)
{
return PostApi("Backtests/read", new {backtestId = iBacktestId, versionId = leanVersionId});
}

private Boolean PostApi(string requestString, System.Object mParameters)
{
var client = new RestClient("https://www.quantconnect.com/api/v1" )
{
Authenticator = new HttpBasicAuthenticator("", "")
};

var request = new RestRequest(requestString, Method.POST);
var requestStr = JsonConvert.SerializeObject(mParameters);
request.AddParameter("application/json", requestStr, ParameterType.RequestBody);

Log("Posting Backtests/create "+requestStr);
IRestResponse response = client.Execute(request);
var json = response.Content;

JObject o = JObject.Parse(json);
Boolean success = false;

Log("RequestStr = " + requestStr);
Log("Response content = " + response.Content);
Log("Parsed Response = " + o);

try
{
success = o["success"].ToString().ToLower() != "false";
//compileId = o["compileId"].ToString();
}
catch (Exception err)
{
Log("Invalid JSON Response Received. Missing expected values 'success'. "+err.Message);
return false;

}

if (!success)
{
Log("Server response success = false");
return false;
}

Log("Success!");
// Full JSON Debug
Log(o.ToString());
return true;
}

0

Please post bug reports to https://www.quantconnect.com/support to keep the forums focused on algorithm development. The REST API is in beta and is subject to change.

We released a CI deployment system so it now needs a version id to be included in the request specifying which version of the API you'd like to build. Now that we've open sourced LEAN I recommend you use this for algorithm development.

0

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.


Hi Mathieu,
try to change this line:
var client = new RestClient("https://www.quantconnect.com/api/v1" )
to this:
var client = new RestClient("https://www.quantconnect.com/api/v1" )
In the compilation you might get a timeout error, but loop till success, it should not take more than 30 sec.

0

Sorry,
try to remove the & qout

0

Hi Jared,

You mention to keep this forum algo-related. Is there a separate forum where we could continue this discussion where other members could participate?

Eran: Your suggestion fixed the compilation, but the backtest execution still returns nothing.

wSuccess = PostApiBacktestCreate(228116, 241, "30024ef45ecd03f3e994b05cf59a5188", "REST Backtest");

Resonse:
{"success":true,"errors":[],"ip":"144.76.73.133"}

Instead of:
{"success":true,"errors":[],"backtestId": "..."}

0

try to use versionID 357 instead 241,
and before this line:
IRestResponse response = client.Execute(request);
add these lines:
RestClient restClient = new RestClient(this._endPoint);
RestClientExtensions.AddDefaultHeader((IRestClient)client, "Accept", "application/json");
RestClientExtensions.AddDefaultHeader((IRestClient)client, "Content-Type", "application/json");
RestClientExtensions.AddDefaultHeader((IRestClient)client, "Authorization", "Basic " + this._accessToken);

you should have the _accessToken if you use the original RESTAPI

0

Thank you Eran for your help!

Hmm I tried to implement this but I am having issues.

RestClient restClient = new RestClient(this._endPoint);
- I am not sure of where the _endPoint comes from. This code is added within the class "QCAlgorithm", which does not contain an _endPoint variable.
- Same issue with _accessToken

Below is my understanding of your recommendation:

My old code (using version 357):
var client = new RestClient('https://www.quantconnect.com/api/v1')
{
Authenticator = new HttpBasicAuthenticator("myEmail", "myPassword")
};

var request = new RestRequest(requestString, Method.POST);
var requestStr = JsonConvert.SerializeObject(mParameters);
request.AddParameter("application/json", requestStr, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);



Are you proposing the following? (using version 357)
var client = new RestClient('https://www.quantconnect.com/api/v1');

RestClientExtensions.AddDefaultHeader((IRestClient)client, "Accept", "application/json");
RestClientExtensions.AddDefaultHeader((IRestClient)client, "Content-Type", "application/json");
RestClientExtensions.AddDefaultHeader((IRestClient)client, "Authorization", "Basic " + "Shjdy63GjADGJgy3dggdgdSaa"); // Shjdy63GjADGJgy3dggdgdSaa to be replaced with the real access token from my QC account?

IRestResponse response = client.Execute(request);


The above does not authenticate properly.

As for my old code, the behavior with version 357 is the same as with version 241.

Let me know if any other thoughts come to mind on this.

Thanks again!

0

the end point in my code is 'https://www.quantconnect.com/api/v1'
the accessToken is calculate in initialization like this:

this._accessToken = this.Base64Encode(email + ":" + password);

private string Base64Encode(string plainText)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(plainText));
}

0

Thanks Eran,

I refactored my code as per your recommendation above. Compilation through REST works.

I still get no result from the backtest however...

string wAccessToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(":"));

var client = new RestClient("https://www.quantconnect.com/api/v1" );

RestClientExtensions.AddDefaultHeader((IRestClient)client, "Accept", "application/json");
RestClientExtensions.AddDefaultHeader((IRestClient)client, "Content-Type", "application/json");
RestClientExtensions.AddDefaultHeader((IRestClient)client, "Authorization", "Basic " + wAccessToken);

var request = new RestRequest(requestString, Method.POST);
var requestStr = JsonConvert.SerializeObject(mParameters);
request.AddParameter("application/json", requestStr, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);


Were you successful in running a backtest? I have the feeling we have met the limitations of the beta Rest API here...

0

you didn't remove the & quot from the endpoint,
if its not working, the last thing you can try (i don't know if its related) is to check on the IDE the LEAN version of the project that should be master.

0

Update Backtest





0

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.


Loading...

This discussion is closed