Back

Open Range Breakout for Multiple Stocks Not Working

Basically looking to expand on the boot camp using the output of a consolidator to detect an open range breakout for multiple stocks instead of just one, but it doesn't seem to ever invest

// I have these objects, the Dictionary is used to hold the consolidated tradebar
private Dictionary<string, TradeBar> MyStocks = new Dictionary<string, TradeBar>();
private string[] _stocks = new string[] { "TSLA", "AMD", "BAC", "SYMC", "HP" };

//then in Initialize() I add the equities with 1 minute resolution, and add it to my dictionary as well
//as set the consolidation
for(int i = 0; i < _stocks.Length; i++)
{
AddEquity(_stocks[i], Resolution.Minute);
MyStocks.Add(_stocks[i], null);
Consolidate(_stocks[i], TimeSpan.FromMinutes(30), OnDataConsolidation);
}

//With my OnDataConsolidation method being as follows -
private void OnDataConsolidation(TradeBar bar)
{
if(bar.Time.Hour == 9 && bar.Time.Minute == 30)
{
MyStocks[bar.Symbol] = bar;
}
}

//And my OnData function looking as follows to check each equity of the proper parameters
public override void OnData(Slice data)
{
for(int i = 0; i < _stocks.Length; i++)
{
if(!Portfolio[_stocks[i]].Invested && MyStocks[_stocks[i]] != null)
{
if(data[_stocks[i]].Close > MyStocks[_stocks[i]].High)
{
SetHoldings(_stocks[i], 1/_stocks.Length);
}

if(data[_stocks[i]].Close < MyStocks[_stocks[i]].Low)
{
SetHoldings(_stocks[i], -1/_stocks.Length);
}
}
}
}

 

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.


So I believe it is coming from how I am arranging the holdings... when I hardcoded the leverage it seems to have worked fine.

0

Hi Aaron,

Great work! I admire you learn the boot camp tutorial and try to extend it. Following your idea, I rewrite the code for you. I've attached the backtest below, and all trades make sense to me. Please focus on the OnData() method to see how I arrange multiple holdings. Hope it helps!

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 Daniel. That definitely cleans up my code a bit and is an interesting structure.

How can I tell if I implemented MyRiskManagement properly? I was following the lead of some API samples, but I don't know if it is implemented properly.

0

Hi Aaron, 

In this algorithm, we don't use the MyRiskManagement.cs actually. Basically, there are two ways to build an algorithm: with or without using Algorithm Framework. This is a component from Algorithm Framework, but this algorithm is built in the way not using Algo Framework. If you would like to use the Algorithm Framework, please follow the pattern of using it. For more details, please check out here.

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.


Got it - thanks Daniel!

I set up AddRiskManagement - interestingly adding risk management decreases the profitablility.

0


Daniel Chen Hey Daniel, I am trying to move this over to using Universe selection to guide the stock picks, but I keep getting errors because sometimes symbols come in with a value of "AMZN R735QTJ8XC9X" and I get object errors because no symbol like that exists. It is more difficult to implement this than I think it should be... what am I doing wrong here?

0


Hi Aaron - casting a symbol to a string can lead to unexpected names like the one you wrote. The symbol object itself is correct and should be used instead of its string representation. Can you give an example of an error you're seeing?

0

  Douglas Stridsberg getting a Runtime Error of Object reference not set and the stack trace leads back to the OnData function, when I go to access data[symbol] because technically it is trying to access data["AMZN R735QTJ8XC9X"] instead of data["AMZN"]

 

That long string is coming from my OnSecuritiesChanged function where I add the security that was added to my universe to MyStocks via a foreach looping through AddedSecurities and using the symbol from that. But that was giving me issues, so I changed it to Symbol.Value which worked for a few things, but it still always get some erroneous value.

0

Yeah but that's because your MyStocks dictionary is keyed by a string. If you key that by a Symbol instead and you stop using strings altogether, you should not run into any issues. In the definition of Symbol, you see that:

  • The SID stays constant over the life of the security.
  • Symbol equality is checked using the SID (lines 308-332).
So by using the Symbol object rather than its string representation/ticker, you ensure that you're always referencing the security you intend to reference.
0

Douglas Stridsberg Thanks! Definitely helping me out. Appreciate it.

However, still running into that issue. (ignore the continue in the code, that wasj ust so I could post the project here)

So I changed it to Symbol, and now I am getting a runtime error because the Data Slice object doesn't contain a symbol - 

Runtime Error: ' ' wasn't found in the Slice object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey(" ")

0


Hi Aaron,

Couple of changes:

  1. The MyStocks.ContainsKey(symbol) check is unnecessary - you're inside a foreach of MyStocks, so it will by definition contain a key with that symbol...
  2. You should always check whether data.ContainsKey(symbol) and do it first. It's even in the error message.
  3. Stop using symbol.Value, it means the string representation (ticker), and will get you into trouble for the reasons I outlined earlier.
  4. Your issue is coming from that you are trying to access .High and .Low from a tradebar that doesn't exist. You cannot assume your OnDataConsolidation function has populated the bar before your OnData is run. You must check this. I leave it up to you to implement this check.
0


Douglas Stridsberg Thanks Douglas! Seem to have it up and running now. The only thing I am having trouble with now is Linq and filtering out stocks - I just can't find what values for Coarse Fundamentals are available for the Markets property. I would like to only work with stocks and not any indecies

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