I am doing the Capstone project in the course Swing Trading Strategies. The error here indicates to the 'strategy_signal', which a column in the data frame where the signal is stored. Refered the model solution given, but couldn't debug the issue.
Hi Arbaaz,
The error indicates that the 'strategy_signal' column is not found in the 'price_data' DataFrame.
Maybe you have not executed the code to find the strategy signal yet? Or you didn't save the data frame in the dict which you are reading at the time of calculating the trade book?
This can be possibly due to a missing line of code where you calculate the strategy signal and then save that data frame into the 'resampled_asset_data' dictionary.
This line of code is available in the capstone solution as the last line of the "Strategy Logic" code block.
resampled_asset_data[asset] = asset_data
Hope this helps!
Thanks,
Gaurav
Hi Gaurav,
Thank you for the help. I had made the strategy signal, and save that in the resampled_asset_data dictionary. I got this error in spite of this.
Code Snippet
Thank you
Hi Arbaaz,
The link which you have shared seems private. Also, request you to upload your code to help debug the issue better.
Thanks,
Gaurav
Hi Gaurav,
It was a miss from my end. I have uploaded the code.
‘’’
Generating Signal
‘’’
for asset in Filtered_stocks:
price_data = resampled_asset_data[asset]
# William Fractals
def bullish_fractal(five_bars):
if five_bars[2] == min(five_bars):
return 1
else:
return 0
# Function to find the bearish fractal
def bearish_fractal(five_bars):
if five_bars[2] == max(five_bars):
return -1
else:
return 0
# Finding the fractal signals
price_data['bull_fractal'] = price_data['Low'].rolling(5).apply(bullish_fractal)
price_data['bear_fractal'] = price_data['High'].rolling(5).apply(bearish_fractal)
price_data['fractal_signal'] = (price_data['bear_fractal'] + price_data['bull_fractal']).shift(1)
# Williams Alligator
price_data['williams_jaw'] = ta.EMA(price_data['Close'], 13).shift(8)
price_data['williams_teeth'] = ta.EMA(price_data['Close'], 8).shift(5)
price_data['williams_lips'] = ta.EMA(price_data['Close'], 5).shift(3)
# Get the entry signal using Williams Alligator
price_data['alligator_entry_signal'] = \
np.where(
(price_data['williams_lips'] > price_data['williams_teeth']) &
(price_data['williams_teeth'] > price_data['williams_jaw']), 1, 0)
# Get strategy signal
price_data.loc[:,'strategy_signal'] = np.where(
(price_data['alligator_entry_signal']==1) &
(price_data['fractal_signal']==1), 1, 0)
resampled_asset_data[asset] = price_data
"""
Trade book Code
"""
asset_trade_book = {}
take_profit = 0.04
stop_loss = 0.02
for asset in tickers:
price_data = resampled_asset_data[asset]
trade_book = get_trade_book(price_data,'strategy_signal',take_profit, stop_loss)
asset_trade_book[asset] = trade_book
Hi Arbaaz,
The code seems to be incorrectly indented.
for asset in Filtered_stocks:
price_data = resampled_asset_data[asset]
All the code after the for loop, right until
resampled_asset_data[asset] = price_data
should be inside the for-loop. At the moment it is outside the for-loop because of the incorrect indentation.
You can refer to the capstone solution for the correct code.
Hope this helps!
Thanks,
Gaurav