也就是说,我正在寻找true_row
这样的功能:
true_row([False, True, True, True, True, False, False])
返回False
但
true_row([True, True, True, True, True, False, False])
返回True
。
编辑:如果有帮助,我已将完整代码附在下面:
position_open = False
def initialize(context):
context.stock = sid(26578)
context.open_hours = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
context.is_bullish = [False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False]
context.first_check_minute = 1
context.second_check_minute = 57
def handle_data(context, data):
event_hour = data[context.stock].datetime.hour
event_minute = data[context.stock].datetime.minute
hour_open_price = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
hour_close_price = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
global position_open
# Hour 1 market direction checks
if event_hour == context.open_hours[0] and event_minute == context.first_check_minute:
hour_open_price[0] = data[context.stock].close_price
if event_hour == context.open_hours[0] and event_minute == context.second_check_minute:
hour_close_price[0] = data[context.stock].close_price
if hour_open_price[0] < hour_close_price[0]:
context.is_bullish[0] = True
if hour_open_price[0] > hour_close_price[0]:
context.is_bullish[0] = False
# Hour 2 market direction checks
if event_hour == context.open_hours[1] and event_minute == context.first_check_minute:
hour_open_price[1] = data[context.stock].close_price
if event_hour == context.open_hours[1] and event_minute == context.second_check_minute:
hour_close_price[1] = data[context.stock].close_price
if hour_open_price[1] < hour_close_price[1]:
context.is_bullish[1] = True
if hour_open_price[1] > hour_close_price[1]:
context.is_bullish[1] = False
# Same block repeated with different numbers x24 (edited out to reduce size)
# Make Trades? - I want to edit this to detect if context.is_bullish has 5 trues in a row without needing to manually make more if statements like the one already below
if event_hour in context.open_hours and context.is_bullish[0] == True and context.is_bullish[1] == True and context.is_bullish[2] == True and context.is_bullish[3] == True and context.is_bullish[4] == True and position_open == False:
order(context.stock,+1000)
log.info('Buy Order Placed')
position_open = True
if event_hour in context.open_hours and context.is_bullish[0] == False and position_open == True:
order(context.stock,-1000)
log.info('Buy Position Closed')
position_open = False