Opportunistic vs routine insiders: how to classify them in Python

Tutorial
Python
How to label an insider as routine or opportunistic in Python, from the calendar months of their trades over the three previous years.
Published

April 20, 2026

Updated

November 26, 2026

Illustration of an insider choosing between trading on information and trading for other reasons, such as taxes.

I wrote this post because insiders can trade for a variety of reasons, and the main question is how you can separate the trades that suggest an insider is trading on information. That is when they are not trading in a routine fashion.

Routine trading in itself is non-informative, because you can predict those trades. If you can predict a trade, it is already priced. And if you are trading on a routine basis, then you are probably not trading on information. Cohen, Malloy and Pomorski call the non-routine trades opportunistic, and they find that all the return prediction in insider trades comes from those. So if you are going to follow insider trading, you should probably focus on the opportunistic trades.

My code is a simpler version of their rule. It labels each insider each year based on the three years before, and it does not keep the Routine label in later years as the paper does.

Defining the rule

Each year, I look at the insider’s trades in the three years before:

  • Routine, traded in all three prior years, and at least one calendar month appears in all three.
  • Opportunistic, traded in all three prior years, and no calendar month appears in all three.
  • NonClassified, no trade in at least one of the three prior years.

\[M_{Y-3} \cap M_{Y-2} \cap M_{Y-1},\]

where \(M_y\) is the set of calendar months in which the insider traded in year \(y\), \(\cap\) keeps the months that are in every set, and the Routine label needs this set to hold at least one month.

Every trade the insider makes that year gets the label. The label pools the insider’s trades across firms, as in the paper.

Step 1. Simulating the data

I use made-up data to make this a simple and intuitive methodology. It is similar to the data you would get from a data vendor. I am not saying that LSEG has this format, but this is what you normally get. I make up trades by four insiders at three firms over ten years, so I know the label each one should get.

The first insider is me. I buy 1,000 shares of Company A every March, and I trade Company B once a year in a different month. The rule pools my trades across firms, so March appears every year, and I am Routine. My Company B trades get the same label.

Beatrice (Company B, different month every year) → Opportunistic. Cecilia (Company C, only odd years) → NonClassified. Diana (one trade in 2015, then no further trades) → NonClassified too.

import pandas as pd

# One row is one trade: the firm, the insider, the date, and the size in shares.
trade_rows = []

# Alexander, me, buys at Company A in March of every year. Same month every year, so the
# rule should call me Routine.
for year in range(2015, 2025):
    trade_rows.append(('Company A', 'Alexander', f'{year}-03-15', 1000))

# I am an insider at Company B as well, where I trade once a year in a scattered month.
# These trades have no pattern of their own. zip walks the years and the months side by
# side, one pair per trade.
alexander_company_b_months = [5, 8, 11, 2, 6, 10, 1, 4, 7, 12]
for year, month in zip(range(2015, 2025), alexander_company_b_months):
    trade_rows.append(('Company B', 'Alexander', f'{year}-{month:02d}-10', 200))

# Beatrice trades once a year at Company B and never repeats a calendar month, so no month
# can be shared across three years and the rule should label her Opportunistic.
beatrice_months = [6, 8, 11, 2, 9, 4, 7, 10, 1, 5]
for year, month in zip(range(2015, 2025), beatrice_months):
    trade_rows.append(('Company B', 'Beatrice', f'{year}-{month:02d}-10', 800))

# Cecilia always trades in June, but only in odd years, so she never has three years of
# history in a row and the rule labels her NonClassified.
for year in range(2015, 2025, 2):
    trade_rows.append(('Company C', 'Cecilia', f'{year}-06-20', 500))

# Diana trades once and then stops, so she never has three years of history.
trade_rows.append(('Company A', 'Diana', '2015-04-10', 300))

trades = pd.DataFrame(trade_rows, columns=['Firm', 'Insider', 'Date', 'Shares'])

# The dates arrive as text, and to_datetime turns them into real dates, so the .dt
# accessor below can read them. The rule reads calendar years and calendar months, so each
# one gets its own column here, and every later step reads them directly.
trades['Date'] = pd.to_datetime(trades['Date'])
trades['Year'] = trades['Date'].dt.year
trades['Month'] = trades['Date'].dt.month

# to_string() writes the whole table as text, so print shows it in a script as well
print(trades.head(12).to_string())
         Firm    Insider       Date  Shares  Year  Month
0   Company A  Alexander 2015-03-15    1000  2015      3
1   Company A  Alexander 2016-03-15    1000  2016      3
2   Company A  Alexander 2017-03-15    1000  2017      3
3   Company A  Alexander 2018-03-15    1000  2018      3
4   Company A  Alexander 2019-03-15    1000  2019      3
5   Company A  Alexander 2020-03-15    1000  2020      3
6   Company A  Alexander 2021-03-15    1000  2021      3
7   Company A  Alexander 2022-03-15    1000  2022      3
8   Company A  Alexander 2023-03-15    1000  2023      3
9   Company A  Alexander 2024-03-15    1000  2024      3
10  Company B  Alexander 2015-05-10     200  2015      5
11  Company B  Alexander 2016-08-10     200  2016      8

Step 2. Classifying each insider-year

For me in 2018, March is in all of 2015, 2016 and 2017, so I am Routine.

def months_traded_by_year(insider, year):
    """The calendar months this insider traded in, one set of months per year, over the
    three calendar years before the given year.

    Asking about Alexander in 2018 looks at 2015, 2016 and 2017, and answers March and
    May for 2015, March and August for 2016, and March and November for 2017.
    """
    is_this_insider = trades['Insider'] == insider
    is_in_lookback = trades['Year'].between(year - 3, year - 1)   # both ends included

    # The paper classifies at the insider level, so trades at every firm count together.
    # .loc keeps the rows where the True/False condition is True.
    lookback_trades = trades.loc[is_this_insider & is_in_lookback]

    # groupby splits the trades into one group per year, and agg(set) collapses each group
    # down to the months it contains. A month traded twice in one year still counts once.
    return lookback_trades.groupby('Year')['Month'].agg(set)


def months_traded_in_every_year(months_by_year):
    """The calendar months that appear in every year of a months-by-year table.

    Given March and May, then March and August, then March and November, the answer is
    March on its own. An empty answer means no calendar month was repeated every year.
    """
    one_set_per_year = list(months_by_year)

    shared_months = one_set_per_year[0]
    for months_of_one_year in one_set_per_year[1:]:
        shared_months = shared_months & months_of_one_year   # & keeps months in both sets
    return shared_months
def classify_insider_year(insider, year):
    """A simpler version of the paper's rule, applied to one insider in one year.

    Routine if some calendar month appears in all three prior years, Opportunistic if
    three years of history share no month, NonClassified if a year of history is missing.
    """
    months_by_year = months_traded_by_year(insider, year)

    # The rule returns NonClassified for an insider who skipped one of the three prior years.
    if len(months_by_year) < 3:
        return 'NonClassified'

    shared_months = months_traded_in_every_year(months_by_year)
    if shared_months:
        return 'Routine'
    return 'Opportunistic'


# The first three years of the sample cannot be classified, because each classification
# year needs three earlier years of history before it.
classification_years = range(trades['Year'].min() + 3, trades['Year'].max() + 1)

label_rows = []
for insider in trades['Insider'].unique():
    for year in classification_years:
        label = classify_insider_year(insider, year)
        label_rows.append((insider, year, label))

insider_year_class = pd.DataFrame(label_rows, columns=['Insider', 'Year', 'Class'])

# pivot reshapes the long table into one row per insider and one column per year.
print(insider_year_class.pivot(index='Insider', columns='Year', values='Class').to_string())
Year                2018           2019           2020           2021           2022           2023           2024
Insider                                                                                                           
Alexander        Routine        Routine        Routine        Routine        Routine        Routine        Routine
Beatrice   Opportunistic  Opportunistic  Opportunistic  Opportunistic  Opportunistic  Opportunistic  Opportunistic
Cecilia    NonClassified  NonClassified  NonClassified  NonClassified  NonClassified  NonClassified  NonClassified
Diana      NonClassified  NonClassified  NonClassified  NonClassified  NonClassified  NonClassified  NonClassified

The labels match the ones I built the data to produce.

Step 3. Labeling every trade

Then I attach each insider’s year label to every trade that year.

# A left merge keeps every trade and attaches the label its insider had that year.
labelled_trades = trades.merge(insider_year_class, on=['Insider', 'Year'], how='left')

# Trades in the first three years matched no label, so the merge left Class empty for them.
# Empty here means too little history, and NonClassified is the label for that case.
labelled_trades = labelled_trades.fillna({'Class': 'NonClassified'})

is_alexander = labelled_trades['Insider'] == 'Alexander'
# .loc[rows, columns] keeps the rows marked True and the columns listed
print(labelled_trades.loc[is_alexander, ['Firm', 'Date', 'Shares', 'Class']].to_string())
         Firm       Date  Shares          Class
0   Company A 2015-03-15    1000  NonClassified
1   Company A 2016-03-15    1000  NonClassified
2   Company A 2017-03-15    1000  NonClassified
3   Company A 2018-03-15    1000        Routine
4   Company A 2019-03-15    1000        Routine
5   Company A 2020-03-15    1000        Routine
6   Company A 2021-03-15    1000        Routine
7   Company A 2022-03-15    1000        Routine
8   Company A 2023-03-15    1000        Routine
9   Company A 2024-03-15    1000        Routine
10  Company B 2015-05-10     200  NonClassified
11  Company B 2016-08-10     200  NonClassified
12  Company B 2017-11-10     200  NonClassified
13  Company B 2018-02-10     200        Routine
14  Company B 2019-06-10     200        Routine
15  Company B 2020-10-10     200        Routine
16  Company B 2021-01-10     200        Routine
17  Company B 2022-04-10     200        Routine
18  Company B 2023-07-10     200        Routine
19  Company B 2024-12-10     200        Routine

My trades from 2015 to 2017 are NonClassified, because the rule needs three earlier years. The chart shows every trade at its year and calendar month, in the color of its label.

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

RED, TEAL, GREY = '#C0392B', '#17868A', '#888888'
# The site's chart colours, in order: the cream background, the text, the light grid lines,
# the pale frame and the axis labels, the same on every chart of the site.
BG, INK, GRID, SPINE, AXIS_TEXT = '#FCEFE3', '#1f1f1f', '#EADCCC', '#D5C6B4', '#4a4a4a'
# One colour per label
CLASS_COLOURS = {'Routine': TEAL, 'Opportunistic': RED, 'NonClassified': GREY}


def style_chart(figure, axis):
    """Give a finished chart the site's cream background, light grid and pale frame."""
    figure.patch.set_facecolor(BG)
    axis.set_facecolor(BG)
    axis.grid(True, axis='y', color=GRID, lw=1.0)
    axis.set_axisbelow(True)
    for side in ('top', 'right'):
        axis.spines[side].set_visible(False)
    for side in ('left', 'bottom'):
        axis.spines[side].set_color(SPINE)
    axis.tick_params(axis='both', length=0, colors=INK, pad=6)
    axis.xaxis.label.set_color(AXIS_TEXT)
    axis.yaxis.label.set_color(AXIS_TEXT)
    axis.title.set_color(INK)


# plt.subplots(2, 2) creates the figure with four charts in two rows, one per insider. figsize
# is the figure's width and height in inches, and sharex and sharey give the four charts the
# same axes. axes.flat goes through the four charts row by row.
figure, axes = plt.subplots(2, 2, figsize=(10, 6.4), sharex=True, sharey=True)
for axis, insider in zip(axes.flat, trades['Insider'].unique()):
    insider_trades = labelled_trades.loc[labelled_trades['Insider'] == insider]
    # One scatter per label, so each trade takes the colour of its label; s is the dot's size
    for label, colour in CLASS_COLOURS.items():
        trades_with_label = insider_trades.loc[insider_trades['Class'] == label]
        axis.scatter(trades_with_label['Year'], trades_with_label['Month'], s=55, color=colour)
    axis.set_title(insider)
    style_chart(figure, axis)

# The axes are shared, so setting the ticks on one chart sets them on all four: a month every
# quarter, and every third year from 2015
axes[0, 0].set_yticks([3, 6, 9, 12], ['Mar', 'Jun', 'Sep', 'Dec'])
axes[0, 0].set_xticks([2015, 2018, 2021, 2024])
for axis in axes[1]:              # axes[1] is the bottom row
    axis.set_xlabel('Year')
for axis in axes[:, 0]:           # axes[:, 0] is the left column
    axis.set_ylabel('Calendar month')

# One legend for the whole figure. Line2D([], []) draws nothing on a chart, so it only gives the
# legend a dot in each label's colour; ls='' leaves out the line through the dot.
legend_dots = []
for label, colour in CLASS_COLOURS.items():
    legend_dots.append(Line2D([], [], marker='o', ls='', color=colour, label=label))
# frameon=False draws the legend without a box, ncol=3 puts the three labels in one row, and
# bbox_to_anchor places the legend's top centre at the middle of the figure's top edge
legend = figure.legend(handles=legend_dots, frameon=False, ncol=3, loc='upper center',
                       bbox_to_anchor=(0.5, 1.0))
for text, colour in zip(legend.get_texts(), CLASS_COLOURS.values()):
    text.set_color(colour)
figure.suptitle('Every trade by year and calendar month', color=INK, y=1.04)
# tight_layout() fits the labels inside the figure. savefig writes the chart to a PNG file:
# dpi=140 sets its resolution, bbox_inches='tight' trims the empty margin, and facecolor=BG
# keeps the cream background. show() displays the chart.
plt.tight_layout()
plt.savefig('insider_trade_calendar.png', dpi=140, bbox_inches='tight', facecolor=BG)
plt.show()

Using the classifier on real data

With real data, you need to focus on the open-market trades by themselves. Option exercises, tax payments and anything else that is not an insider trade must go. You also need to match the insider name across firms. And you need three years of history before the first label, so data that starts in 2000 gives labels from 2003.

Conclusion

So the conclusion is that this is a methodology for how you can separate insiders when they are trading on information from when they are probably not trading on information. Everything is suggestive, of course, but this is the way to proxy for it.

References

Cohen, L., Malloy, C., & Pomorski, L. (2012). Decoding inside information. The Journal of Finance, 67(3), 1009–1043. https://doi.org/10.1111/j.1540-6261.2012.01740.x

Disclaimer: a teaching example on made-up trades. Not investment advice.