Back

[REQUEST] Example Algorithm With A List of Symbols

Hi Team,

I was wondering if we could get an example algorithm using a List of ticker symbols. All of the current examples only have a single symbol. I think this will help a lot of the non-professional programming guys here on QC.

I always learn best from reading other code. It took me a little bit of thinking to get TradeBar data for a list of symbols.
Update Backtest








Here ya go. It only back tests for 1 day and the returns suck

The structure of the algorithm is something JJ and I have been using for a while.

- There is a Dictionary of Strategies, one for each symbol
- Each Strategy has a couple of indicators which are automatically updated by the framework.
- After the indicators are updated, the CheckSignal() method is called which sets the OrderSignal for the strategy.
- In OnData, after checking for unfilled orders and end of day, ExecuteStrategy is called in each bar for each symbol.
- ExecuteStrategy gets the symbol and the OrderSignal (actualOrder) and if it is not doNothing, it does something.

We have found that the advantages to this structure is that we can mess around with the signal generator without having to change the rest of the algorithm. You could even have different strategies for each symbol.

Have fun,

Nick
1


Let me try adding that algorithm again with a couple more days in it.
2


Thanks Nick. Hopefully anyone new coming into QC can find this easily. I was mostly curious about how to pass Tradebar data to a List of symbols. I created a few object classes that house all of my appropriate multidimensional lists. But I still can't seem to get TradeBar data for all of them.
0

Hey Jack! QuantConnect doesn't require any special coding to use multiple symbols. Perhaps I am misunderstanding what you need to do?

// In Initialize
List _symbols = new List { "IBM", "SPY", "AAPL" };
foreach (var symbol in _symbols) {
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
}

public override void OnData(Slice data)
{
foreach (var bar in data.Bars)
{
//Every _symbols trade bar will appear here every minute: bar.Close
}
//Also can access via Securities:
var price = Securities["IBM"].Close;
}
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 case its useful to others I use this approach, which makes it easier for me to paste bigger symbol lists into code, in comma seperated format.

public override void Initialize()
{
public string symbolsCSV = "A,AA,AAPL,ABC,ABI,ABT,ACAS,ACE,ACT,ADBE,ADI,ADM,ADP,ADSK,AEE";
List symbols = symbolsCSV.Split(',').ToList();
foreach (string stock in symbols)
{
AddSecurity(SecurityType.Equity, stock , Resolution.Minute);
}
}
0

Nicholas - that is a really elegant template that you use, it seperates functionality very cleanly. Thanks for sharing
0

@Rob Thanks! I hope you find it useful. If you make any changes that are worthwhile, I would love to hear about it. nicholasstein@cox.net
0

Hi All,

I'm taking a look at @Nicholas Stein's great template above. I'm getting an error at the following line: 

foreach (string t in symbolarray)
{
Symbols.Add(new Symbol(t));
}

The error is: There is no argument given that corresponds to the required formal parameter 'value' of 'Symbol.Symbol(SecurityIdentifier, string)'

How should I modify this line to get it working please?

Thanks,

Gavin.

1

Hi Gavin,

I wrote the algorithm back in December 2015.  The platform has evolved quite a bit since then.  Make sure you are using the latest build of the QC Engine.  Here is what I used to run the attached back test.  Older versions of the engine used a different Symbol class.

I just compiled and ran a back test of the attached algorithm and it ran fine, except for the horrible returns. :0)

 

 

2


Thanks so much Nicholas, love your work!

G

0

@Nicholas Stein and anyone else with insight:

I'm using Nicholas' framework above. I'm trying to modify it to trade off custom minute tradebars in Lean. While it uses minute tradebars as input, it seems to consolidate the data to daily frequency, and then trades at the following open.

The problem is I can't see any consolidator code. Which part of the code is doing the consolidation, and what needs to change to pass the minute data through?

Any help would be appreciated.

Thanks,

Gavin.

 

 

0

Hi Gavin,

After checking the code and the generated trades, it doesn't seem to me that data is consolidated. Yes, some trades happen on the following open/day, but most of the happen on the same day and several minutes after the open. Minute data is being passed through.

Here is a code snippet where we set custom minute bars and set up indicators with automatic updates:

class Example : QCAlgorithm
{
private SimpleMovingAverage fast;
private SimpleMovingAverage slow;

public override void Initialize()
{
SetEndDate(DateTime.Today.AddDays(-1));
SetStartDate(EndDate.AddYears(-1));
SetCash(100000);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);

// TradeBar consolidator for a 10 minutes bar
var minConsolidator =
new TradeBarConsolidator(TimeSpan.FromMinutes(10));

// Attach the event handler (function that will be called each time we produce a new consolidated piece of data).
minConsolidator.DataConsolidated += OnTenMinutes;

// Adds our consolidator to the manager in order to receive updates from the engine
SubscriptionManager.AddConsolidator("SPY", minConsolidator);

fast = new SimpleMovingAverage(2);
slow = new SimpleMovingAverage(4);

// Manually register these indicators for automatic updates
RegisterIndicator("SPY", fast, minConsolidator);
RegisterIndicator("SPY", slow, minConsolidator);
}

private void OnTenMinutes(object sender, TradeBar bar)
{
if(fast > slow)
{
SetHoldings("SPY", 1);
}
else
{
SetHoldings("SPY", -1);
}
}

public void OnData(TradeBar data)
{
//
}
}


 

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,

Thanks for taking a look man, I thought I had posted yesterday that I'd solved it (or at least know what the issue is), but no post is present. I wrote some custom code to input IB data, but I think the index must be off, hence the behaviour of 1 value in any day and fillforward data for the remainder of the day. Default data inputs properly.

Cheers,

Gavin.

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