End Overview:
In this lesson, you will learn to use the bar parameter "" to limit the amount of data you receive when you call a bar method.
What does end do? md.bar.minute(start=-5, end-3)
- When passed a timestamp, all bars that start forming after the indicated end time are ignored.
- unlike start, end only cares about when the bars begin forming.
- When passed a negative index, such as -5, the bar ignores the most recent 5 bars.
- End is used too keep information for set times in the past (5 minutes from the present time).
At 9:40 I request the following minute bars. One with an end time of 9:38.
regular_bar = bar.minute(start=-4)
bar_with_end = bar.minute(start=-4, end=service.time(9,38))
regular_bar will contain the data for the past 4 minutes in single minute bars. However, bar_with_end will only contain minute bars that start forming before 9:38.
Regular Bar: 09:36:00 09:37:00 09:38:00 09:39:00 Bar with End: 09:36:00 09:37:00
However, if the end time is increased by 1 second, the bar from 9:38-9:39 will be included.
bar_with_end = bar.minute(start=-4, end=service.time(9,38,1))
All bars that start forming before 9:38:01 are included.
Regular Bar: 09:36:00 09:37:00 09:38:00 09:39:00 Bar with End: 09:36:00 09:37:00 09:38:00Negative Integers:
The more common way of using the "end" parameter is to pass it a negative integer. Negative integers make dealing with bigger bars (multiple minutes long) easier in comparison to passing a timestamp.
At 9:40 I request the following.
bar_with_end = bar.minute(start=-4, end=-2)
regular_bar = bar.minute(start=-4)
The bar_with_end will ignore the final 2 minute bars that regular_bar will include.
Regular Bar: 09:36:00 09:37:00 09:38:00 09:39:00 Bar with End: 09:36:00 09:37:00Working Example:
Use the "end" parameter to request data on the open.
# Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy
class openData(Strategy):
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
return symbol =='AMZN'
# called at the beginning of each instance
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))
# create timer at 11:40
service.add_time_trigger(service.time(11,40))
def on_minute_bar(self, event, md, order, service, account, bar):
if event.timestamp > service.time(11,40):
print ("\n---------- in on_minute_bar " + service.time_to_string(event.timestamp)[11:19])
# Request 1 minute bars from 9:30 to 9:35
first_five = bar.minute(start=service.time(9,30,1), end=service.time(9,35))
# print the highs for the opening 5 minutes
print("\nThe opening 5 minute highs: ")
for time, high in zip(first_five.timestamp, first_five.high):
print '\t', service.time_to_string(time)[11:19] + ':', high
# print the volume for the opening 5 minutes
print("\nThe opening 5 minute volumes: ")
for time, volume in zip(first_five.timestamp, first_five.volume):
print '\t', service.time_to_string(time)[11:19] + ':', volume
service.terminate()
Console
AMZN ---------- in on_start() 2016-09-07 09:30:00.000000 ---------- in on_minute_bar 11:40:01 The opening 5 minute highs: 09:30:00: 789.75 09:31:00: 789.260009766 09:32:00: 788.020019531 09:33:00: 789.150024414 09:34:00: 788.780029297 The opening 5 minute volumes: 09:30:00: 49455 09:31:00: 19058 09:32:00: 14920 09:33:00: 18852 09:34:00: 11473