0

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
a7dc
  • 3,323
  • 7
  • 32
  • 50
  • 1
    Why are you building a numpy array with append? – roganjosh Dec 21 '22 at 15:15
  • 1
    There is a lot in this that isn't pythonic at all, but I'm sure you'll still be aware of `else` conditions, so why don't you just add that and append `np.nan`? – roganjosh Dec 21 '22 at 15:18
  • Hey mate, I've tried that and it's giving me an error (updated the OP) – a7dc Dec 21 '22 at 15:32
  • Sorry, had an error. Updated again – a7dc Dec 21 '22 at 15:33
  • I was using append previously but I have since changed it to `insert` to use the index – a7dc Dec 21 '22 at 15:34
  • 1
    Just drop the array altogether. _Every single append_ is forcing a full copy of the array. You should be using lists for a start - don't dynamically grow a numpy array because the performance of that is _terrible_. Your logic has changed now in terms of the actual output – roganjosh Dec 21 '22 at 15:52

1 Answers1

0

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
a7dc
  • 3,323
  • 7
  • 32
  • 50
  • 1
    You might want to rename `prices_array` to `prices_list` - to clarify to yourself, and your readers that you aren't "growing" arrays. – hpaulj Dec 21 '22 at 16:30