Hi DL,
Yes I used those same volume conditions to construct the algo above. I'll use the example you mentioned above of trading on the Friday open to explain the steps in my code:
vol_hist = history(4, '1d', 'volume') :
This will return the return the daily volume history for Tuesday, Wednesday, Thursday and Friday (which is the current day). Vol_hist is a dataframe with 4 values.
prior_VolAvg = sum(vol_hist.ix[0:2])/len(vol_hist.iloc[0:2]):
This calculates the average volume of Tuesday-Wednesday. It is using the formula that average = sum of the terms/ # of terms. This snippet of code uses pandas for indexing into dataframes. The notation [0:2] indicates the start and end values that we want. This means we're grabbing everything from 0 until (but not including) 2. Thus, we are getting the 0 and 1 position in the dataframe, which corresponds to the "Tuesday" and"Wednesday" volume respectively.
yest_vol = vol_hist.iloc[2:] :
This is the volume for the 2nd position, which is Thursday (yseterday) in our example.
Now we get to the meat of the algo!
if exchange_time.hour== 9 and exchange_time.minute == 31 :
Only enter the position at 9:31AM, which is the closest you can trade in Quantopian to the market open.
if yest_vol > prior_VolAvg: order_target_value(context.tsla, 10000) :
If Thursday's volume is greater than the average of Tuesday-Wednesday volume, then order $10,000 worth of Tesla stock.
elif exchange_time.hour==15 and exchange_time.minute == 59: if context.portfolio.positions[context.tsla].amount != 0: order_target_percent(context.tsla,0)
If we have any positions in the algo, sell them at 3:59PM, which is the market close.
Hope this helped break down the steps of the algo. If something is amiss or unexpected, let me know and I'll take a another look!
Alisa