Enter a Position Overview:
In this lesson, you will learn to use data collected using bars to enter a position.
Using Bars to Create Entry Logic
The following logic looks for increases in the highs of the past three minute bars. Each minute's high must be greater than the last.
if bar_1.high[0] < bar_1.high[1] < bar_1.high[2] and self.in_position:
If this condition is met, shares are purchased.
Working Example
This example shows how you can use bar data to determine how you enter positions.
# Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy
class increasingHighs(Strategy):
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
return symbol =='AAL'
# 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))
# variable to track position
self.in_position = False
# create a variable to control when the model starts to collect data/ enter positions
# 3 minutes after so 3 bars are always returned.
self.model_start = md.market_open_time + service.time_interval(minutes=3)
# called when time and sales message is received
def on_minute_bar(self, event, md, order, service, account, bar):
if md.L1.timestamp > self.model_start:
bar_1 = bar.minute(start=-3)
# if the high price is increasing over the past three minutes
# check that you are not in a position
if bar_1.high[0] < bar_1.high[1] < bar_1.high[2] and self.in_position == False:
print ("\n---------- in on_minute_bar " + service.time_to_string(event.timestamp)[11:19])
print("\nEntry Condition Met!\n\nThe highs for the past 3 minutes were:")
# print the timestamp and high for each bar
print("\t%s: %s\n\t%s: %s\n\t%s: %s" % (
service.time_to_string(bar_1.timestamp[0])[11:19], str(bar_1.high[0]),
service.time_to_string(bar_1.timestamp[1])[11:19], str(bar_1.high[1]),
service.time_to_string(bar_1.timestamp[2])[11:19], str(bar_1.high[2])))
# send order for 100 shares
order.algo_buy(self.symbol, "market", intent="init", order_quantity=100)
print("\nPosition entered: " + service.time_to_string(event.timestamp)[11:19])
# change position variable
self.in_position = True
# sell shares if a position is held
elif self.in_position:
print ("\n---------- in on_minute_bar " + service.time_to_string(event.timestamp)[11:19])
print '\nsell order sent'
order.algo_sell(self.symbol, "market", intent='exit')
service.terminate()
Console
AAL ---------- in on_start 2016-09-07 09:30:00.000000 ---------- in on_minute_bar 09:37:00 Entry Condition Met! The highs for the past 3 minutes were: 09:34:00: 38.0099983215 09:35:00: 38.0499992371 09:36:00: 38.1850013733 Position entered: 09:37:00 ---------- in on_minute_bar 09:38:00 sell order sent
This is a basic example of how bar data can influence how you trade. In the lessons to come, you will learn how you use multiple bar attributes to create a more advanced and more profitable strategy.