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.