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
Michael Manus
In the Algorithm Lab try the bootcamp (tab at the top)
Jared Broad
new Members should maybe start with few steps in the bootcamp.........
Gurumeher Sawhney
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.
John Bellas
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.
Jared Broad
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.
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.
John Bellas
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:
Jared Broad
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.
Tony Shacklock
Hi
I'm also struggling to understand how to make and use a library in the Algorithm lab. Here's my understanding (which doesn't work).
Create the library within a project by clicking "Add New Library". In the newly created library project create file(s) with your shared classes.
You need to name each library file with the module name, but the project doesn't build without a file "main.py". Should that be another file in the library?
Having done that, I expected see the library &/or library files under "Libraries" on the left when you have a project open, but I don't. I think you should just be able to import the the library class from the module, like...
from ExecutionModels import SpecialExecutionModel
What am I doing wrong?
A quick video on this would be great.
Tony Shacklock
I think I've figured it out. For those reading this...
After creating the library with its file(s), you need to add the library to the project explicitly by clicking "Add New Library" and selecting the one you want.
The files with the code to be 'imported' are added to the project. Beware! Don't delete the file from the project - it gets deleted from the library, as well. I don't know how to remove a file that isn't needed any more.
Â
John Bellas
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!