Back

Setting Indicators on Universes

Hi All,

I'm trying to do the below & having some difficulties (all the prices, indicator values etc are showing as 0).

I want to:

- Filter by top 500 by market cap

- Filter by a valuation ratio

- Set an indicator (like ATR) on the resulting universe

My questions:

1) Is it not possible to set indicators in OnData?

2) How do I know what is inside the universes i.e. self._changes.AddedSecurities etc... ? Very confusing.

I've attached the back test which is showing no values in the logs.

Thanks

Lex

Update Backtest








I think a similar question was recently mentioned here with explanation by Alex:
https://www.quantconnect.com/forum/discussion/3018/simple-moving-average-filter/p1

But c# version of code discussed:
https://github.com/QuantConnect/Lean/blob/master/Algorithm.CSharp/EmaCrossUniverseSelectionAlgorithm.cs

This doesnt address all your questions, but might help get started.

0

Thanks @Derek.

I should have been more clearer on my 3rd point but im not sure if it makes a difference. In the examples provided in the link below, they are just using it 

I've attached my code so far but it is too complex at this stage for me to understand & debug. Changing the indicator to ATR or Bollinger Bands doesn't seem to want to work here. I'm worried that the logs arent showing the security symbol & description.

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/EmaCrossUniverseSelectionAlgorithm.py
0


Lexx7 here is my attempt to combine your algo and the example to successfully(really using that term loosely) get uniiverse based indicators to print(not zero) to the log.

I had to work with updating the indicator inside OnData vs in the universe selectors like in the crossover example linked above to get the IBaseDataBar for updating ATR(or any indicator that would need more than the current pice). I am worried about my implementation here as new items added to the universe will report a value, but doesn't it require n updates before the indicator's rolling window is achieved?

I am unsure about this; but it's easy enough to add a count to SymbolData.Update and a bool check to see if the indicator has been warmed up if that fixes the issue. I did add UniverseSettings.MinimumTimeInUniverse to begin possibly addressing this issue so we at least have the symbols around long enough. Please consider these things to ensure your intentions are being met with my slapped together code.

Sample of log output:
2014-01-03 00:00:00 TWTR VLE97YS7S57P : 3.04000000000
2014-01-03 00:00:00 PXD R735QTJ8XC9X : 7.93408489500
2014-01-03 00:00:00 X R735QTJ8XC9X : 1.19211095200

0


Here is the same algo, but I added a couple lines to show how to go about switching back to EMA; or an indicator that only required a value(vs bar) update.

0


Thanks Derek Tishler 

I've tried to play around with it by changing the EMA indicator to something which had 'sub-parameters' inside (like lower/upper bands for Bollinger Bands). Below is what I tried & ran into problems with:

Tried to reference the price for the symbol in the universe in OnData using the line below. It runs fine but doesnt seem to want to show anything in the logs. Does it need updating somehow as well? Does that mean setting universes doesn't actually store or accomodate for price data?

line 48:            self.Log("PRICE : ".format(self.Securities[symbol].Price))

Tried adding Bollinger Bands instead of EMA:

Returned 'no method matches arguements given' error

I tried the following lines of code:

line 103: self.indicator = context.BB(symbol, self.window)

line 117: return self.indicator.LowerBand.Current.Value, self.indicator.UpperBand.Current.Value

I would expect to use it in OnData in this manner: self.indicators[symbol].get_atr().LowerBand.CurrentValue etc or something similar. How would it work if I want to use Both ATR indicator and another one that uses values?

Many thanks,
Lex

0


Here's my latest backtest.

Having trouble with line 43 working to updating the indicators. 

Also it is taking a very long time to load, I have tried to set it to schedule 'coarse' & 'fine' functions at the end of each month but not sure if this is working correctly. Not sure if the multiple indicators are causing an issue or conflict somewhere. I created a new function per indicator attribute.

0


Attached latest backtest. Getting Key Error:

Runtime Error: Python.Runtime.PythonException: KeyError : (<QuantConnect.Symbol object at 0x7f5d5a9b9050>,)

0

KeyError refers to dictionary in this case so I knew from that error that the self.indicators dictionary was being asked for a symbol it did not have yet.

Also added a None check or two in case that Price issue came back. data can still contain that key but may have none for Price. Slightly adjusted log items to look like:

Coarse and Fine fire daily, ~4:50 am on weekdays except Friday I think its around ~1am Saturday.( Be sure your universe algos are live before these times). I added a datetime check to Fine to only allow universe updates on a new month & save resources. This is pretty hacked together so you might want to check over the usage.

To get any sort of trades going I added a slow use of History to calc a moving average to check for what I guess are breakouts? I didn't really think about it or check the leverage so I would ignore/review the results and really review what the algo is doing.

0


Thanks Tishler ! I hope QC are rewarding you for all the help you are providing :)

A few questions here:

1) I am trying to set it to do quarterly rebalancing. You can see the code I tried at the start of 'coarse' & 'fine' functions.

2) How would I get multiple indicators in here? I think that was what was causing the issue previously but as I understand, it was conflicting with the dictionary. Can I make a dictionary PER indicator or is that not wise?

I changed the indicator to be 'BollingerBands' upon suggestion from Alex who says that 'BB' is used for auto updating signals but then updating manually, should be using 'BollingerBands' instead,

Latest Backtest attached showing no trades.

0


self.universe was no longer being updated along with new asset list in Fine. Multiple indicators is just a quick addition to the object that handles the indicators per symbol, added one for example. I still worry about when the indicators have reached their target window after n updates. Could count the number of updates and ensure each indicator has been prepared after it's added to the universe, otherwise you might be using signals that are not what you intended. Something to think on.

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