This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

Trading Index (TRIN): Formula, Calculation & Strategy in Python – Part III


Visit: QuantInsti

See Part I for a definition of TRIN and Part II for instructions on how to calculate TRIN in Python.

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
prices[[‘advancing_stocks’, ‘declining_stocks’]].head()


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
mergedDf[[‘advancing_stocks’, ‘declining_stocks’,
‘advancing_volume’, ‘declining_volume’]].head()


# 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’]



01-04-2020         NaN

02-04-2020    1.289376

03-04-2020    0.686072

06-04-2020    3.285942

07-04-2020    0.693863

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:

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.

trading top