Getting empty dataframe for hs_trade_details in H and S pattern

I have taken Nifty 50 data from Angel One.

Please check the code as below

 

 

 

 

 

Hello Somdutt,



There could be various reasons for getting an empty dataframe. 

You can try printing the data frame at various stages of the code to check if the code is running properly, these stages include

  1. When you retrieve the asset data from Angel One 
  2. When you are running the backtester function and generating the trading signals
  3. When you are calculating trade level analytics



    You might have to check if there are any trades taking place in the backtesting period and if yes, are there any gaps which is leading the code to skip the data rows.



    Hope this helps.

Hi Sir,



I have tried and made some changes. I am sharing google collab notebook for better understanding of yours about the code.



Request you to please check it. I am getting an unusual error. In fact, I am not getting hs_trade_details



Notebook: https://colab.research.google.com/drive/1CGRyJhDImysGIv5h86mAtaspiePXI6FZ#scrollTo=b412b396



Notebook

Hi Somdutt,



Please share access to the notebook.

Hi Sir,



Any update??

Hi Somdutt,



Please be assured that we are looking into your query and will respond as well. 



Thank you for your patience. 

Hi Somdutt,



With respect to the error, it seems that there is a type mismatch. Specifically, data.index likely contains datetime.date object, while sh2_date_ts is a Timestamp. Pandas does not allow direct comparison between these types.



You can try to use the modified function "get_confirmation_date_hs" and see if it works



def get_confirmation_date_hs(hs_patterns, data=data):

    # Convert hs_patterns.sh2_date to match the data index type

    sh2_date_ts = pd.Timestamp(hs_patterns['sh2_date'])



    # Ensure data.index is also in Timestamp format for comparison

    if not isinstance(data.index, pd.DatetimeIndex):

        data.index = pd.to_datetime(data.index)



    # Filter data starting from the Shoulder 2 date

    data_after_sh2 = data.loc[sh2_date_ts:].Close



    try:

        # Capture the date if prices close below the neckline

        return data_after_sh2[data_after_sh2 < hs_patterns['neck2_price']].index[0]

    except IndexError:

        # Assign NaN if no such date is found

        return np.nan





With respect to your question about not getting trades, you will have to dig deeper and check if there are any patterns identified or not. There could be a chance that there were no patterns identified in the backtesting period. See if you can use a longer backtesting period or any other asset to check if there are any patterns identified.



Hope this helps