Back

Dictionary problem

Can somebody explain why the following algorithm blows up on weekends? It runs fine until midnight Sunday (00:00:00 Monday), then throws an error:

"2016-06-13 00:00:00 Runtime Error: An item with the same key has already been added."

I think it must have something to do with the dictionary that stores the day's opening price values. I'm lost because the algo deletes all of the entries in the dictionary at the end of each day and rebuilds it with new opening price values the next day, so it should be empty at midnight Sunday. Why is it even doing anything at all at midnight Sunday?

This was all an exercise to learn how to trade an algo with multiple stocks, so the strategy is pretty basic. Comments, criticism and suggestions are welcome, but the primary goal of this thread is to fix whatever problem is keeping it from continuing past the first weekend.   Thanks.

Update Backtest








Hey Oran! The first place I check is the stack trace as it tells you exactly where the error happened -- in this case:

at System.ThrowHelper.ThrowArgumentException (ExceptionResource resource) [0x00000] in :0
at System.Collections.Generic.Dictionary`2[System.String,System.Decimal].Insert (System.String key, Decimal value, Boolean add) [0x00000] in :0
at System.Collections.Generic.Dictionary`2[System.String,System.Decimal].Add (System.String key, Decimal value) [0x00000] in :0
at QuantConnect.dictionaryPractice.b__8_0 () [0x00000] in :0
at QuantConnect.Scheduling.ScheduleManager+c__AnonStorey1.<>m__0 (System.String name, DateTime time) [0x00000] in :0
at QuantConnect.Scheduling.ScheduledEvent.OnEventFired (DateTime triggerTime) [0x00000] in :0

Reading from the bottom up -> it tells us the scheduler fired, then it tried to add to a dictionary which already had the key.

1 - So why is it firing on a Sunday? Because the schedule function says "EveryDay()". You can use EveryDay("AAPL") to make it only fire on trading days.

2 - Why does the error happen - because your clear logic only happens at 3:58pm in OnData (when there is market data) - so there is no market data on Sunday.

 

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 Jared, I'll try it with Everyday("AAPL").  I assume that the "AAPL" could be any of the symbols in the list, correct? You say the error happens because "your clear logic only happens at 3:58pm in OnData (when there is market data) - so there is no market data on Sunday."  If the dictionary cleared at 3:58 pm on Friday and won't rebuild until 9:30am Monday, then it should still be empty all day Sunday, right? Oh, wait - back to the Everyday thing. Is it rebuilding the dictionary at 9:30 on Saturday and again Sunday using whatever the last price was in the afterhour market? Not what I intended, but I guess that would make sense.

0

@Jared, I added "AAPL" to the Everyday expression as you suggesed and it worked.  I answered my own question above by changing to other symbols than AAPL and it still worked. I extended the time frame and a couple of other bugs bit me and I had to fix those. What's new, right :).

I am wondering though, is  there a practical limit to how many symbols I can add to the list without bogging down the system? It has to be a resource hog looking at multiple symbols with minute resolution,  Would 10 symbols be ok?  20?  Where would the practical limit be? Thanks.

0

There's only a practical memory and speed limit in backtesting; in live we limit it to 10 second resolution, 100 minute resolution because the machines only have 512MB RAM and we don't want people's algorithm's crashing.

Universe selection is the preferred way to select those symbols (or using some other way of avoiding selection bias). But you can just add them manually if you like.

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