bar information from outside market hours
Bar Include Extended Overview:In this lesson, we will learn how to obtain bar information from outside market hours by setting the parameter to True.
Basic use case:Pre-market data acquired through bars can be used to estimate the open for a symbol.
Interface: bar.minute(bar_size = 1, include_extended = True)
The effects of include_extended Backtest time: from 9:25-16:00
# Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy
class BarIncludeExtended(Strategy):
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
# only run the script on AAL
return symbol == "SPY"
def on_start(self, md, order, service, account):
# symbol and timestamp
print(self.symbol + "\n---------- in on_start " + service.time_to_string(service.system_time))
# initialize variable to count the number of bars
self.bar_count = 0
def on_minute_bar(self, event, md, order, service, account, bar):
print ("\n---------- in on_minute_bar " + service.time_to_string(event.timestamp)[11:19])
# request the bar data for the most recent minute
# include_extended is False
barry = bar.minute(bar_size=1, include_extended=False)
# same bar as above, but include_extended is True
barry_include_extended = bar.minute(bar_size=1, include_extended=True)
# # print bar information; include_extended = False
print("\nregular bar data for the last 1 minutes:")
print barry
print("The low prices: " + str(barry.low))
print("The high prices: " + str(barry.high))
# # print bar information; include_extended = True
print("\ninclude_extended bar data for the last 1 minutes:")
print barry_include_extended
print("The low prices: " + str(barry_include_extended.low))
print("The high prices: " + str(barry_include_extended.high))
# # increment variable
self.bar_count += 1
# after printing the bar information 2 times, terminate
if self.bar_count == 2:
service.terminate()
Console
SPY ---------- in on_start 2016-09-07 09:25:00.000000 ---------- in on_minute_bar 09:25:00 regular bar data for the last 1 minutes: BarView(symbol='SPY', length=60, timestamp=array([], dtype=float64), open=array([], dtype=float64), high=array([], dtype=float64), low=array([], dtype=float64), close=array([], dtype=float64), volume=array([], dtype=float64), vwap=array([], dtype=float64), spread=array([], dtype=float64), bidvol=array([], dtype=float64), askvol=array([], dtype=float64), count=array([], dtype=float64), valid=array([], dtype=float64), closing_trade=array([], dtype=float64), opening_trade=array([], dtype=float64)) The low prices: [] The high prices: [] include_extended bar data for the last 1 minutes: BarView(symbol='SPY', length=array([60]), timestamp=array([1473254640000000], dtype=uint64), open=array([ 218.86999512]), high=array([ 218.88000488]), low=array([ 218.86000061]), close=array([ 218.86000061]), volume=array([2050], dtype=int32), vwap=array([ 218.8692627]), spread=array([ 0.00999641]), bidvol=array([700], dtype=int32), askvol=array([1350], dtype=int32), count=array([8], dtype=int32), valid=array([ True], dtype=bool), closing_trade=array([ 0.]), opening_trade=array([ 0.])) The low prices: [ 218.86000061] The high prices: [ 218.88000488] ---------- in on_minute_bar 09:26:05 regular bar data for the last 1 minutes: BarView(symbol='SPY', length=60, timestamp=array([], dtype=float64), open=array([], dtype=float64), high=array([], dtype=float64), low=array([], dtype=float64), close=array([], dtype=float64), volume=array([], dtype=float64), vwap=array([], dtype=float64), spread=array([], dtype=float64), bidvol=array([], dtype=float64), askvol=array([], dtype=float64), count=array([], dtype=float64), valid=array([], dtype=float64), closing_trade=array([], dtype=float64), opening_trade=array([], dtype=float64)) The low prices: [] The high prices: [] include_extended bar data for the last 1 minutes: BarView(symbol='SPY', length=array([60]), timestamp=array([1473254700000000], dtype=uint64), open=array([ 218.86000061]), high=array([ 218.86000061]), low=array([ 218.83999634]), close=array([ 218.86000061]), volume=array([5900], dtype=int32), vwap=array([ 218.85797119]), spread=array([ 0.01293766]), bidvol=array([1500], dtype=int32), askvol=array([4400], dtype=int32), count=array([17], dtype=int32), valid=array([ True], dtype=bool), closing_trade=array([ 0.]), opening_trade=array([ 0.])) The low prices: [ 218.83999634] The high prices: [ 218.86000061]