Back

Using the QuantConnect BasicTemplateLibrary

I am new to C# and was wondering how to using the Libraries in the QuantConnect Platform. I looked at the BasicTemplateLibrary but am having problems with the "using" statement.

I tried these which didn't work:

using QuantConnect.Data.BasicTemplateLibrary;

using QuantConnect.BasicTemplateLibrary;

using QuantConnect.API.BasicTemplateLibrary;
namespace QuantConnect
{

.............

 var btl = new BasicTemplateLibrary();
        
btl.Add(1,2)

 

Thanks

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.


In the Algorithm Lab try the bootcamp (tab at the top)

Jared Broad 

new Members should maybe start with few steps in the bootcamp.........

0

As shown in the backtest below, 'using [namespace]' allows us to create libraries and then access certain functions. These libraries, or namespaces, can be written in separate files and then accessed via 'using.'

As Michael mentioned, try the bootcamp. It will help in learning how write and compile (another useful case for 'using')  the C# code.

0


Thanks Michael I have already completed the BootCamp and have written a few projects

Gurumeher I am trying to pull code into my project similar to the way that #include is used in C.  I thought that "using" might accomplish this but it doesn't seem to work for me.

How does Quantconnect  include libraries in a program?  My goal is to store a  group of functions in the QuantConnect libary and be able to call them inside my project. 

 

 

 

0

LIbrary project files are automatically added to the current project's compile. So you should add the using statement for the namespace of that project file. If it's the same namespace you don't need to do anything - just create an instance of the class. 

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 All,

I figrued out what I was doing wrong.

First:  I didn't include  "namespace TestLibrary"  inside the file TestLibrary.cs 

Second:  I didn't add the file TestLibrary.cs into the project. I incorectly thought if it was in the Library folder that it would be accessible by  main.cs

 

Here is my Library.cs file:

namespace TestLibrary
{
/// <summary>
/// Basic Template Library Class
/// Library classes are snippets of code you can reuse between projects. They are added to projects on compile. This can be useful for reusing
/// indicators, math components, risk modules etc. If you use a custom namespace make sure you add the correct using statement to the
/// algorithm-user.
/// </summary>
public class BasicTemplateLibrary
{
/*
* To use this library; add its namespace at the top of the page:
* using QuantConnect
*
* Then instantiate the class:
* var btl = new BasicTemplateLibrary();
* btl.Add(1,2)
*/

public int Add(int a, int b)
{
return a + b;
}

public int Subtract(int a, int b)
{
return a - b;
}
}
}

 

And here is how I included it in my main.cs 

 

using System;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using TestLibrary;



namespace QuantConnect
{
/*
Designed as a "first use" algorithm by a trader new to robotic trading, with a cash
trading account of minimal size. Does not use leverage or margin and waits 4 days from
sell date to next purchase date to allow for funds settlement by the broker. Does not
make a lot of money, but does not lose. It may however leave you holding stock for a long
time if you choose a stock that is falling, never to come back (oops!).

Happy trading!
*/
public class BeginnerAlgo : QCAlgorithm
{
private string symbol = "SPY";



.........................................

// Setup the Libraries here

var btl = new BasicTemplateLibrary();

 

For those that learn visually like myself, here it is :

 

Fixed:

 

Original:

0

It should automatically be in your project, I'll confirm tomorrow
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.


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

 
";