根据参考点变量查找指数的下一个高点/低点

问题描述 投票:0回答:1

我正在尝试创建几种方法,这些方法使用给定股票的标记价格,然后根据该参考点引用下一个最高点或最低点(四个柱中)。

示例:

Min Array - [2, 3.5, 5, 8.5, 9]
Max Array - [20, 17, 9, 7, 5]

Sample input
Mark: 3.6

Expected output
Min - 3.5
Max - 5

那么如果Mark穿过

$5
上方或
$3.5
下方,结果将如下:

Mark: 5.1

Expected Output
Min - 5
Max - 7

或者

Mark: 3.4

Expected Output
Min - 2
Max - 5

我的方法似乎能够增加索引,但只能在一个方向上。例如,如果标记跌破 2.50 美元,指数将参考下一个值,但当标记跌破 2.50 美元后再次突破 2.50 美元时,指数不会返回到之前的值。

请看一下,让我知道我错过了什么。 (仅供参考:ohlcv 拉动所选代码的开盘价、最高价、最低价和收盘价。蜡烛 [2] 是当前时间范围的最高价,蜡烛 [3] 是当前时间范围的最低价。每当标记价格穿过指数高点时,指数递减并参考接下来的前四个柱来查找下一个高点,它会递减,因为指数是从高到低排序的,当标记价格穿过指数低点时也是如此。)

def compare_hl(ohlcv, mark, symbol):
    minarray = []
    maxarray = []

    low_index = 0
    high_index = 0
    # Extract the lows of all candles
    lows = [candle[3] for candle in ohlcv]
    highs = [candle[2] for candle in ohlcv]

    # Initialize variables to track the starting index of the previous four bars

    while low_index < len(lows):
        # Extract the lows of the previous four bars
        previous_lows = lows[low_index:low_index + 4]

        # Check if the mark price falls below the lows of the previous four bars
        if mark < min(previous_lows):
            # Update the starting index for the next set of previous four bars
            low_index += 4        
        elif mark > min(previous_lows) and low_index < -4:
            print("\nMark Price above Minimum of Previous Lows. Reverting to Previous Set.")
            low_index -= 4
            break
        else:
            # If mark price is not below the lows, move to the next set of four bars
            low_index += 4
        minarray.append(min(previous_lows))

    while high_index < len(highs):
        # Extract the lows of the previous four bars
        previous_highs = highs[high_index:high_index + 4]

        # Check if the mark price falls below the lows of the previous four bars
        if mark > max(previous_highs):
            # Update the starting index for the next set of previous four bars
            high_index += 4       
        elif mark < max(previous_highs) and high_index < -4:      
            high_index -= 4
            break
        else:
            # If mark price is not below the lows, move to the next set of four bars
            high_index += 4
        maxarray.append(max(previous_highs))

    return minarray, maxarray

def find_reference(mark_price, minarray, maxarray, lo_index, hi_index, lowref, hiref, crossbelow, crossabove):

    if mark_price > maxarray[hi_index]:
        crossabove = True
    elif mark_price < minarray[lo_index]:
        crossbelow = True

    if mark_price > maxarray[hi_index]:
        print(colors.CYAN + "Mark Max: ", str(mark_price), str(maxarray[hi_index]) + colors.END)
        print(colors.CYAN + "Mark has crossed above previous 4-bar Max.\nMark: ", str(mark_price),"\nMin: ", str(minarray[lo_index]) + colors.END)
        hi_index -= 1
        highreference = maxarray[hi_index]
        crossabove = True
        crossbelow = False
        print(colors.CYAN + "New High Reference Set: ", str(maxarray[hi_index]) + colors.END)
    elif hi_index <= -2 and mark_price < maxarray[hi_index + 1]:
        if not crossabove:
            print(colors.CYAN + "Mark has crossed below last high. Adjusting reference." + colors.END)
            hi_index += 1
            highreference = maxarray[hi_index]
            print(colors.CYAN + "New High Reference Set: ", str(maxarray[hi_index]) + colors.END)
            crossabove = False
            crossbelow = True

    if lo_index >= -len(minarray) and mark_price < minarray[lo_index]:
        print(colors.CYAN + "Mark has crossed below Min: ", str(mark_price), str(minarray[lo_index]) + colors.END)
        lo_index -= 1
        lowreference = minarray[lo_index]
        crossabove = False
        crossbelow = True
        print(colors.CYAN + "New Low Reference Set: ", str(minarray[lo_index]) + colors.END)
    elif lo_index <= -2 and mark_price > minarray[lo_index + 1]:
        if not crossbelow:
            print(colors.CYAN + "Mark has crossed above last min. Adjusting reference." + colors.END)
            lo_index += 1
            lowreference = minarray[lo_index]
            print(colors.CYAN + "New Low Reference Set: ", str(minarray[lo_index]) + colors.END)
            crossabove = True
            crossbelow = False

    lowreference = minarray[lo_index]
    highreference = maxarray[hi_index]

    return lowreference, highreference, lo_index, hi_index
python arrays indexing methods
1个回答
0
投票

您的代码有很多问题,并且包含一堆您从未使用过的参数和分配。要么你从更大的代码库中删除它,但仍然留下比问题所需的更多的内容,要么你这里有很多死代码。

但是,您的问题似乎是关于

find_reference
,这可以很简单:

def find_reference(mark, mins, maxs):
    return max(x for x in mins if x < mark), min(x for x in maxs if x > mark)


def main():
    mins, maxs = [2, 3.5, 5, 8.5, 9], [20, 17, 9, 7, 5]

    print(find_reference(5.1, mins, maxs))
    print(find_reference(3.4, mins, maxs))


main()

输出:

(5, 7)
(2, 5)

如果这不能回答您的问题,您应该提供一些实际的示例数据,并展示您期望如何调用这些函数、使用什么参数以及您期望的结果。

© www.soinside.com 2019 - 2024. All rights reserved.