Data variable and trading universe will be the topic we will start with.
A trading universe is any stock that we might be interested in trading in a given bar in our algorithm.
The trading universe is represented by the data variable, which is a dictionary key'd by securities.
def handle_data(context, data);
log.info(get_datetime())
for stock in data;
for stock in data;
log.info('Close price for %s: .2f' % (stock.symbol, data[stock].close_price))
log.info('/n')
So what we did here is logged all this so we can take a look at the minute/lead/close price of each our securities ('Close price for (stock.symbol, data[stock].close_price)), and then we added in some formatting so that it all prints out in a more readable form (%s: .2f' %). And then we added the print time so we can see which minute we are on ( log.info(get_datetime()) ) And then we printed a new line for spacing ( log.info('/n') ).
Hit the 'Build Algorithm' key and what you will find is the pricing data for AAPL and SPY in each minute.
The 'stock universe' includes any stocks that we currently hold position in, as well as any stocks that we have explicitly referenced in the context variable. Another way to get a stock in our universe is to use the 'update universe function'.
Now let's turn our attention to the 'history()' function! The history function is used to us historical data for each stock in our stock universe.
#this function is run once per bar, REQUIRED
def handle_data(context, data);
def handle_data(context, data);
print history(5, '1m', 'close_price')
What this is doing is grabbing the history for our SPY and APPL data for the last 5 minute bars, and then we put '1m' for our frequency, and then we put close_price as the field that we are interested in. This returns as a pandas data frame with each column being a different stock and each row being a different date-time.
If we wanted a days frequency we would simply put '1d' instead of '1m'. Additionally, we can also change our field, so that we can find the volume traded rather than the closing price. We do this by placing 'volume' where our 'closing_price' is.
If we want to find the ten day mean in the closing price of our stocks, we can change the number of bars to '11' and keep the frequency at '1d' and then I am actually going to save this function which, we can do through the following function: hist = history(11, '1d', 'close_price')
Now we want to make a 'for' loop going through each stock in data:
#this function is run once per bar, REQUIRED
def handle_data(context, data);
def handle_data(context, data);
hist = history(11, '1d', 'close_price')
for stock in data:
print '10-day mean close price for stock %s: %2f' % (stock.symbol, np.mean(hist[stock][:-1]))
So, what we did here was print a line of text '10-day mean close price for stock' that references our stock %s: %2f' % and then we retrieve the stock symbol, and then we import the numpy library denoted as np (**NOTE: when importing a library you must also type a line of a code at the very beginning of your algorithm (above the def initialize(context) function); so above that function we would have the following line of code: import numpy as np. )
followed by the use of the statistical .mean variable of the (hist <--- our historical data for our stock. And then we type [stock] for the column that corresponds to the specific stock in our loop.
The [:-1] portion of our code excludes the last row of our column.
And the result will give us our ten day mean for AAPL and SPY.
We can change the our days to minutes like this: '10-minute mean...'
Thank you for reading part 3 of our tutorial, and as always if you have any questions or comments please send me an e-mail or comment!
The [:-1] portion of our code excludes the last row of our column.
And the result will give us our ten day mean for AAPL and SPY.
We can change the our days to minutes like this: '10-minute mean...'
Thank you for reading part 3 of our tutorial, and as always if you have any questions or comments please send me an e-mail or comment!
No comments:
Post a Comment