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 and if the current price, i.e., price for the date falls below the stop_loss levels, sell the stock.
Below is the implementation for the same:
def logging_stop_loss(dict_dfs, result_df, stop_loss_levels):
trades = []
positions = {}
for date_idx, date in enumerate(result_df.index):
for stock in result_df.columns[result_df.iloc[date_idx]]:
if (stock, date) not in positions:
entry_price = dict_dfs['close'].at[date, stock]
stop_loss_level = stop_loss_levels.at[date, stock]
positions[(stock, date)] = {
'entry_date': date,
'entry_price': entry_price,
'stop_loss': entry_price * (1 - stop_loss_level),
'count': 0 # Initialize count to track days since entry
}
closed_positions = []
for (stock, entry_date), pos in list(positions.items()):
pos['count'] += 1
future_date = result_df.index[min(date_idx + pos['count'], len(result_df) - 1)]
current_price = dict_dfs['close'].at[future_date, stock]
stop_loss_level = stop_loss_levels.at[future_date, stock]
pos['stop_loss'] = current_price * (1 - stop_loss_level)
if current_price < pos['stop_loss']:
trades.append({
'stock': stock,
'entry_date': pos['entry_date'],
'entry_price': pos['entry_price'],
'exit_date': future_date,
'exit_price': current_price,
'return': (current_price - pos['entry_price']) / pos['entry_price']
})
closed_positions.append((stock, entry_date))
for stock_entry in closed_positions:
del positions[stock_entry]
return pd.DataFrame(trades)
I have included my code which takes in stop loss calculated using certain logic for all the days. Another dataframe tells me about the trading dates, with TRUE value indicating I enter the trade, and FALSE value indicating I exit the trade. With this logic, I enter the trade and see if stop loss is met. If not, get going to the next date, if it is met, then sell else continue the loop till the end of the closing price dataframe is met. I will then log the entry and exit prices in a dataframe and save it.
This code returns empty logging_stop_loss dataframe, and it has no entry in the dataframe. This being the case, I want to understand what is causing the issue and rectify it. Can someone lead me to the direction which is causing this error and I can modify my logic accordingly.