Profile Picture

Finding the last bar

Posted By andreas_clenow 5 Years Ago
Posted Tuesday December 09 2014
Is there a simple way to know in Newbar() whether the currently processed bar is the final one available?

Currently, I've got a very ugly workaround in place for this purpose. My workaround involves invoking my custom database class, and asking the database directly for the last date. Very messy, and shouldn't be necessary.

Why would one need to know if it's the last bar? For example, to color the chart based on open positions (See attached images as example) or to run some custom reporting or other code that should only run once the simulation is complete, but still doesn't belong in a SystemResults plugin. Even more important could be to make sure a position is properly closed, if for some reason data  ceases to exist. This could be a delisted stock for instance.

JRU.png (279 views, 54.00 KB)
FC.png (266 views, 55.00 KB)
Posted Tuesday December 09 2014
You can override the Shutdown method of the system class.  Will that work for you?

Posted Wednesday December 10 2014
Thanks, Daniel! This solves the most important issue. Overriding Shutdown makes it easy to do the chart cosmetics based on open positions, so that issue is solved.

The other scenario would be if a symbol would for some reason cease to exist. A stock getting delisted for instance. If you happen to have an open position in that stock, you would need to exit on the last bar, to avoid being stuck in this position for the rest of the simulation period. If you do a CloseAtMarket on the last bar, you're left with an open order to close on the next bar which will never get executed. 

Issues like that often come up with large scale stock simulations. If you run a stock picking model on a thousand stocks over a few decades, you're sure to run into this. To solve this one, you'd either need to know one bar in advance, or be able to force position shutdown during the current bar.

Is there a neat way to solve that too? 
Posted Thursday December 11 2014
The only way I was able to do this was by accessing a list of stocks that got delisted and then referencing that list.

Not to clean but does the job.

Similar Topics

Reading This Topic

2005-2019 © RightEdge Systems