Accessing Daily Bars
New users are often confused when it comes to accessing daily bars and that is understandable, it's hard to wrap your head around.
So I thought I would drop my video in here and a little CloudQuant code that hopefully will make it clear how it all works, also the code shows you how to get a fast backtest when all you need is basic data.
https://youtube.com/watch?v=cOQjobA7Nj8
from cloudquant.interfaces import Strategy, Event
class AAPL_Close_Check(Strategy):
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
if symbol == 'AAPL':
bars=md.bar.daily(start=-5)
print md.stat.prev_close,md.stat.dividend_amount ,md.stat.dividend_comment, md.stat.dividend_percent
print " 0",round(bars.close[0],2),service.time_to_string(bars.timestamp[0], '%Y-%m-%d') # Note that this is actually the oldest bar, the same as -5
print "-1",round(bars.close[-1],2),service.time_to_string(bars.timestamp[-1], '%Y-%m-%d') # yesterday... move the pointer back 1 and read to the right
print "-2",round(bars.close[-2],2),service.time_to_string(bars.timestamp[-2], '%Y-%m-%d') # close 2 days ago
print "-3",round(bars.close[-3],2),service.time_to_string(bars.timestamp[-3], '%Y-%m-%d') # close 3 days ago
print "-4",round(bars.close[-4],2),service.time_to_string(bars.timestamp[-4], '%Y-%m-%d') # close 4 days ago
print "-5",round(bars.close[-5],2),service.time_to_string(bars.timestamp[-5], '%Y-%m-%d') # close 5 days ago
return False
I pull all the bar data into a varialbe called bars so I dont keep re-reading it, this is more efficient. Reading bars can be costly. Often reading a block and then pulling the data from that block is much more efficient. Try to think in terms of self.
and class.
as local data, md.
, service.
and account.
as calls to other programs thus consider them distant data, they _will _slow your code down. (Remember - you can always use the profiler tool to find out where your script is slow).
I use round function to get around (no pun intended) Python's problem with "loss of precision in binary floating point numbers".
And yes, I know there are better ways to format the print statments, I like to stick with what I am comfortable with!
SUPER QUICK
Using a script that sits in is_symbol_qualified, does its work and returns false every time (terminating that symbol) results in some very fast backtests and is very useful for checking data or collecting small amounts of data (remember - the console has fixed limits).
Paul
Best Answers
-
lf263820 Posts: 17
Paul,
it is confusing because you said index 0 and -5 give the same value for daily bars. Why is that case?
Leon? -
ptunney Posts: 246
Because you are pulling only 5 bars into a list and the list can be referenced/read forwards or backwards.
More info...
You pull 5 minute bars at 9:35
The bars you get back in order are [ 9:30 , 9:31 , 9:32 , 9:33 , 9:34 ]
Referencing the list FORWARDS 0 1 2 3 4
Referencing the list BACKWARDS -4 -3 -3 -2 -1
So the pointer in the list starts at 0.. and you read to the right..
So if you read from 0 (reading right) you get the oldest, 9:30 data
If you read -1 you are moving backwards and you get the newest 9:34 data.
It is up to you how you read it.