Next, we will find out the number of advancing stocks (ones which ended in green) and the number of declining stocks (ones which ended in red) from the previous day.
# Add the number of stocks that ended in green from previous day
# and number of stocks that ended in red from previous day
direction_cols = [col + ‘_direction’ for col in stocks]
prices[‘advancing_stocks’] = prices[direction_cols].sum(axis=1)
prices[‘declining_stocks’] = len(direction_cols) – prices[‘advancing_stocks’]
# Print the “pos_direction” and “neg_direction” columns from dataframe
Now, we will generate the sum of the volume of the stocks ended in positive and negative from the previous day since this is required to calculate AD volume.
# Rename volume dataframe columns by adding ‘_volume’ , to help merge prices and volume dataframes
volume_cols = [col + ‘_volume’ for col in stocks]
volume.rename(columns=dict(zip(stocks, volume_cols)), inplace=True)
# Print the first two rows of the volume dataframe
Further, we will find out advancing and declining stocks and advancing and declining volume of stocks. Then, finally, we will calculate the TRIN value.
# Merge both prices, volume data-frames
mergedDf = prices.merge(volume, left_index=True, right_on=’Date’)
# Create vol_direction column for every stock, to sum positive ended and negative ended stocks volume
for col in stocks:
volume_col = col + ‘_volume’
direction_col = col + ‘_direction’
vol_direction = col + ‘_vol_direction’
mergedDf[vol_direction] = mergedDf[volume_col] * mergedDf[direction_col]
# Add the volume of all positive ended stocks to ‘total_pos_volume’
# And sum of the volume of negative ended stocks to ‘total_neg_volume’
vol_direction_cols = [col + ‘_vol_direction’ for col in stocks]
mergedDf[‘total_volume’] = mergedDf[volume_cols].sum(axis=1)
mergedDf[‘advancing_volume’] = mergedDf[vol_direction_cols].sum(axis=1)
mergedDf[‘declining_volume’] = mergedDf[‘total_volume’] – \
# Print ‘pos_direction’,’neg_direction’, ‘total_pos_volume’ and ‘total_neg_volume’ columns from dataframe
# Calculate TRIN ratio
mergedDf[‘AD_ratio’] = mergedDf[‘advancing_stocks’]/ mergedDf[‘declining_stocks’]
mergedDf[‘AD_volume’] = mergedDf[‘advancing_volume’]/ mergedDf[‘declining_volume’]
mergedDf[‘TRIN’] = mergedDf[‘AD_ratio’]/ mergedDf[‘AD_volume’]
Name: TRIN, dtype: float64
Stay tuned for the next installment in which the author will plot a graph to find out the TRIN value for S&P500.
Visit QuantInsti for additional ready-to-use code: https://blog.quantinsti.com/trin/
Disclosure: Interactive Brokers
Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.