Remove loops and use vectorised functions to improve the time complexity

I’m trying to capture profits and set a stop loss in my trading strategy. I want the stop loss to be set daily based on the past data, from the date of calculation into the past.

For calculating the stop-loss, I first measure the expected returns that I aim at securing, the volatility of the stock and based on these informations, I then calculate stop loss level.

The data that I have is, a dataframe which contain information on when to enter the trade for which stock? It is a boolean dataframe where the row index are dates and columns are stock names. If it is a true, I will enter the trade, false will ensure I stay out.

For the first instance, I calculate the stop loss level, and save it, then I apply loops to calculate the stop loss in a similar fashion for the stock but in the context of the next day, using the data for the proceeding day.

This is taking very very long because of the loops and I am afraid this code doesn’t work correctly either.

Below is the implementation for the same:

def drawdown(result_df, dict_dfs):
    last_year_df = pd.DataFrame(data=np.nan, index=result_df.index, columns=result_df.columns)

    for idx in range(len(result_df)):
        for stock in result_df.columns:
            if result_df.iloc[idx][stock]:
                past_date_idx = max(0, idx - 250)
                past_date = result_df.index[past_date_idx]
                current_date = result_df.index[idx]

                last_year = dict_dfs['close'].loc[past_date:current_date, stock]

                drawdowns = []
                for i in range(len(last_year)):
                    rolling_min = last_year.iloc[:i + 1].min()
                    rolling_max = last_year.iloc[i:].max()
                    if rolling_min != 0:
                        drawdown = (rolling_max - rolling_min) / rolling_min
                        drawdowns.append(drawdown)

                last_year_df.iloc[idx][stock] = np.median(drawdowns)
    return last_year_df

def stop_loss(expectations, volatility):
    stop_loss_levels = pd.DataFrame(
        np.minimum(20,
                   np.minimum(0.4 * expectations, (1 / volatility))),
        index=expectations.index,
        columns=expectations.columns
    )

    stop_loss_levels = stop_loss_levels.where(expectations.notna() & volatility.notna())

    return stop_loss_levels

def volatility_cal(result_df, dict_dfs):
    volatility_year = pd.DataFrame(data=np.nan, index=result_df.index, columns=result_df.columns)

    for idx in range(len(result_df)):
        for stock in result_df.columns:
            if result_df.iloc[idx][stock]:
                past_date_idx = max(0, idx - 250)
                past_date = result_df.index[past_date_idx]
                current_date = result_df.index[idx]

                last_year = dict_dfs['close'].loc[past_date:current_date, stock]
                standard_dev = last_year.std()
                volatility_year.iloc[idx][stock] = standard_dev
    return volatility_year


def logging_stop_loss(dict_dfs, result_df):
    trades = []
    positions = {}
    expectations = drawdown(result_df, dict_dfs)
    volatility = volatility_cal(result_df, dict_dfs)
    stop_loss_levels = stop_loss(expectations, volatility)

    for date_idx in range(len(result_df.index)):
        date = result_df.index[date_idx]
        for stock_idx, stock in enumerate(result_df.columns):
            if result_df.iloc[date_idx, stock_idx]:
                if stock not in positions:
                    entry_price = dict_dfs['close'].iloc[date_idx, stock_idx]
                    positions[(stock, date)] = {
                        'entry_date': date,
                        'entry_price': entry_price,
                        'stop_loss': entry_price * (1 - stop_loss_levels.iloc[date_idx, stock_idx])
                    }

        closed_positions = []
        count = 0
        for (stock, entry_date) in list(positions.keys()):
            count=count+1
            pos = positions[(stock, entry_date)]
            date = result_df.index[date_idx+count]
            current_price = dict_dfs['close'].loc[date, stock]

            current_expectations = drawdown(result_df.shift(count), dict_dfs)
            current_volatility = volatility_cal(result_df.shift(count), dict_dfs)
            current_stop_loss_levels = stop_loss(current_expectations, current_volatility)

            pos['stop_loss'] = current_price * (1 - current_stop_loss_levels.loc[date, stock])

            if current_price < pos['stop_loss']:
                trades.append({
                    'stock': stock,
                    'entry_date': pos['entry_date'],
                    'entry_price': pos['entry_price'],
                    'exit_date': date,
                    'exit_price': current_price,
                    'return': (current_price - pos['entry_price']) / pos['entry_price']
                })
                closed_positions.append((stock, pos['entry_date']))

        for stock, entry_date in closed_positions:
            del positions[(stock, entry_date)]
    return pd.DataFrame(trades)

Here drawdown is the first function to be called and it gives me the expected returns. The logic for the same is absolutely correct and no changes should happen there. Next is volatility_cal function and it gives me standard deviation. Next is then Stop Loss calculator, which lets me know the stop loss percent that I can incur the following percent loss.

Post this, I then aim at logging the information of when to exit the trades based on continuous stop loss calculations.
I first calculate the entry price and stop loss associated with it. With this I maintain a counter which say how many days have passed after entering the trade and helps in moving the boolean dataframe into the future. By moving the boolean dataframe into the future, I get an idea that yes, now I enter the trade on this day again and accordingly gives me the stop loss levels. If the price falls below stop loss percentages, I exit the trade and marks the observations in the logger.

Once all the TRUE values from original boolean dataframe is satisfied, it returns the logger.

The code is getting stuck in an infinite loop or it is just inefficient, as I have not been able to observe the results for the above function even after waiting a couple of hours.

Therefore please help me improve the time complexity by using dataframes and matrix, instead of loops.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật