-3

つまり、次の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
4

4 に答える 4

2

itertools.groupby行内の同一要素のセットをグループ化する を使用します。

import itertools
any(len(list(g)) >= 5 and k == True for k, g in itertools.groupby(lst))
于 2013-01-06T17:08:01.207 に答える
1

リストがあればl、使用できます

('True' * 5) in ''.join(map(str, l))

言い換えれば、あなたの機能は

def true_row(row):
    return ('True' * 5) in ''.join(map(str, row))

>>> def true_row(row):
...     return ('True' * 5) in ''.join(map(str, row))
... 
>>> true_row([False, True, True, True, True, False, False])
False
>>> true_row([True, True, True, True, True, False, False])
True
于 2013-01-06T17:07:43.733 に答える
0

あなたの質問を正しく読んだ場合、連続する True 値ではなく、ブール値の数にのみ関心があります。Listまた、イテラブルではなくa に取り組んでいます。その場合は、単純に count を使用できます。シーケンスをリストに変換して、反復可能なすべての結果が一貫していることを確認することをお勧めします

def true_row(row):
    return list(row).count(True) >= 5

OPで説明されているように、彼は5つの連続したブール値の肯定を必要とします. 私がテストした1000個のデータのランダムサンプルでは、​​10倍高速です。かなりの期間にわたって何千もの株式データを反復処理する必要があると思われるので、これは役に立ちます。

def true_row(row, length = 5):
    count = - length
    for e in row:
        if e:
            count += 1
        else:
            count = -length
        if not count:
            return True
    return False

今スピードテスト

>>> seq = (choice([True, False]) for _ in xrange(1000))
>>> def David(seq):
    return any(len(list(g)) >= 5 and k == True for k, g in itertools.groupby(lst))

>>> def ARS(seq):
    return ('True' * 5) in ''.join(map(str, row))

>>> t_ab = timeit.Timer(stmt = "true_row(seq)", setup = "from __main__ import true_row, seq")
>>> t_david = timeit.Timer(stmt = "David(seq)", setup = "from __main__ import David, seq, itertools")
>>> t_ars = timeit.Timer(stmt = "ARS(seq)", setup = "from __main__ import ARS, seq")
>>> t_ab.timeit(number=1000000)
0.3180467774861455
>>> t_david.timeit(number=1000000)
10.293826538349393
>>> t_ars.timeit(number=1000000)
4.2967059784193395

シーケンス全体を反復する必要がある小さなシーケンスの場合でも、これはより高速です

>>> seq = (choice([True, False]) for _ in xrange(10))
>>> true_row(seq)
False
>>> t_ab = timeit.Timer(stmt = "true_row(seq)", setup = "from __main__ import true_row, seq")
>>> t_david = timeit.Timer(stmt = "David(seq)", setup = "from __main__ import David, seq, itertools")
>>> t_ars = timeit.Timer(stmt = "ARS(seq)", setup = "from __main__ import ARS, seq")
>>> t_ab.timeit(number=1000000)
0.32354575678039055
>>> t_david.timeit(number=1000000)
10.270037445319304
>>> t_ars.timeit(number=1000000)
3.7353719451734833
于 2013-01-06T17:41:59.383 に答える
0

簡単にするために設計された答えは次のとおりです

def five_true(L):
    for i in range(len(L) - 5):
        if L[i:i + 5] == [True] * 5:
            return True
    return False
于 2013-01-06T17:46:37.237 に答える