How to fill in NaN when using np.append witb a condition?

51 views Asked by At

Sorry for another noob question!

I have a function which is taking the opening price of a bar, and increasing it by 100%, to return my target entry price:

def prices(open, index):
    gap_amount = 100
    prices_array = np.array([])
    index = index.vbt.to_ns()
    day = 0
    target_price = 0
    first_bar_of_day = 0

    for i in range(open.shape[0]):
        first_bar_of_day = 0

        day_changed = vbt.utils.datetime_nb.day_changed_nb(index[i - 1], index[i])
        
        # if we have a new day
        if (day_changed):
            day = day + 1
            # print(f'day {day}')
            first_bar_of_day = i
            fist_open_price_of_day = open[first_bar_of_day]
            target_price = increaseByPercentage(fist_open_price_of_day, gap_amount)

            prices_array = np.append(prices_array, target_price)

    return prices_array

Used like so:

prices_array = prices(df['open'], df.index)
print(prices_array, 'prices_array')

It returns:

[ 1.953  12.14    2.4     2.36    6.04    6.6     2.62    2.8     3.94
  2.      5.16    3.28    5.74    3.6     2.48    4.2     4.02    2.72
  5.52    3.34    3.84    2.02    2.58    4.76    2.28    3.54    2.54
  3.7     3.38    3.4     6.68    2.48    7.2     4.5     5.66    4.48
  5.92    5.26    4.06    3.96    4.      4.42    2.62    1.76    3.66
  5.5     3.82    1.8002  3.02    7.78    2.32    4.6     3.34    0.899
  1.52    5.28    5.1     2.88  ] prices_array

But how can I append the numpy array, to fill in NaN for when the condition hasn't been met? So when my condition isn't met, there should be NaN for each bar / index of the loop.

The data is simply OHLC data of different days, in chronological order.

I hope this makes sense, please let me know if I can improve the question!

I've tried inserting at the specific index using insert:

def prices(close, open, index):
    gap_amount = 100
    prices_array = np.array([])
    index = index.vbt.to_ns()
    day = 0
    target_price = 10000
    first_bar_of_day = 0

    for i in range(open.shape[0]):
        first_bar_of_day = 0

        day_changed = vbt.utils.datetime_nb.day_changed_nb(index[i - 1], index[i])
        
        # if we have a new day
        if (day_changed):
            day = day + 1
            # print(f'day {day}')
            first_bar_of_day = i
            fist_open_price_of_day = open[first_bar_of_day]
            target_price = increaseByPercentage(fist_open_price_of_day, gap_amount)

        # if close goes above or equal to target price
        if (close[i] >= target_price):
            prices_array = np.insert(prices_array, i, target_price)
        else:
            prices_array = np.insert(prices_array, i, np.nan)

    return prices_array

Which gives:

[nan nan nan ... nan nan nan] prices_array
1

There are 1 answers

1
a7dc On

Solved it thanks to RoganJosh:

def prices(close, open, index):
    gap_amount = 100
    prices_array = []
    index = index.vbt.to_ns()
    day = 0
    target_price = 10000
    first_bar_of_day = 0

    for i in range(open.shape[0]):
        first_bar_of_day = 0

        day_changed = vbt.utils.datetime_nb.day_changed_nb(index[i - 1], index[i])
        
        # if we have a new day
        if (day_changed):
            day = day + 1
            # print(f'day {day}')
            first_bar_of_day = i
            fist_open_price_of_day = open[first_bar_of_day]
            target_price = increaseByPercentage(fist_open_price_of_day, gap_amount)

        # if close goes above or equal to target price
        if (close[i] >= target_price):
            prices_array.insert(i, target_price)
        else:
            prices_array.insert(i, np.NaN)

    return prices_array