-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First pass an insider trading tracker * clean up routes * Add support for insider trades and recent insider trades * Refactor adding rows to table body in insider trades controller * Refactor insider trades filtering and API call handling * Update insider trades limit to 250 & start date to 180 days ago, plus some UI tweaks * Update insider trading limits and cache expiration times * Update cached content to be HTML safe and add action_name attribute * Add Cross-Site Scripting warning to brakeman.ignore
- Loading branch information
Showing
15 changed files
with
623 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = ["input"] | ||
|
||
connect() { | ||
} | ||
|
||
submit(event) { | ||
const symbol = event.target.value.split(' ')[0] | ||
|
||
if (symbol) { | ||
const url = `/tools/inside-trading-tracker/${symbol.toUpperCase()}` | ||
Turbo.visit(url) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = ["tableBody", "loading"] | ||
|
||
connect() { | ||
this.sortDirection = {} | ||
} | ||
|
||
sort(event) { | ||
const column = event.currentTarget.dataset.column | ||
this.sortDirection[column] = !this.sortDirection[column] | ||
|
||
const rows = Array.from(this.tableBodyTarget.children) | ||
const sortedRows = this.sortRows(rows, column) | ||
|
||
this.showLoading() | ||
|
||
setTimeout(() => { | ||
this.tableBodyTarget.innerHTML = '' | ||
for (const row of sortedRows) { | ||
this.tableBodyTarget.appendChild(row) | ||
} | ||
this.hideLoading() | ||
}, 200) | ||
} | ||
|
||
sortRows(rows, column) { | ||
return rows.sort((a, b) => { | ||
let aVal = this.getCellValue(a, column) | ||
let bVal = this.getCellValue(b, column) | ||
|
||
if (column === 'date') { | ||
aVal = new Date(aVal) | ||
bVal = new Date(bVal) | ||
} else if (column === 'shares' || column === 'value') { | ||
aVal = this.parseNumericValue(aVal) | ||
bVal = this.parseNumericValue(bVal) | ||
} | ||
|
||
return this.sortDirection[column] ? | ||
this.compareValues(aVal, bVal) : | ||
this.compareValues(bVal, aVal) | ||
}) | ||
} | ||
|
||
getCellValue(row, column) { | ||
const index = { | ||
name: 0, | ||
position: 1, | ||
date: 2, | ||
shares: 3, | ||
value: 4, | ||
holdings: 5, | ||
type: 6 | ||
}[column] | ||
|
||
return row.children[index].textContent.trim() | ||
} | ||
|
||
parseNumericValue(value) { | ||
return Number.parseFloat(value.replace(/[^-\d.]/g, '')) | ||
} | ||
|
||
compareValues(a, b) { | ||
return a > b ? 1 : a < b ? -1 : 0 | ||
} | ||
|
||
showLoading() { | ||
this.loadingTarget.classList.remove('hidden') | ||
this.tableBodyTarget.classList.add('opacity-50') | ||
} | ||
|
||
hideLoading() { | ||
this.loadingTarget.classList.add('hidden') | ||
this.tableBodyTarget.classList.remove('opacity-50') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
app/presenters/tool/presenter/inside_trading_tracker.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
class Tool::Presenter::InsideTradingTracker < Tool::Presenter | ||
attribute :symbol, :string | ||
attribute :action_name, :string | ||
attribute :filter, :string | ||
|
||
def blank? | ||
insider_trades.empty? | ||
end | ||
|
||
def company_name | ||
return "Recent Insider Trading Activity" if symbol.blank? | ||
insider_data.dig(:meta, :name) || symbol&.upcase | ||
end | ||
|
||
def insider_trades(filter = nil) | ||
if symbol.present? | ||
return [] unless insider_data[:trades]&.any? | ||
format_trades(insider_data[:trades]) | ||
else | ||
case filter | ||
when "top-owners" | ||
fetch_filtered_trades(ten_percent_owner: true, min_value: 100_000, sort: "value") | ||
when "biggest-trades" | ||
fetch_filtered_trades(min_value: 1_000_000, sort: "value") | ||
when "top-officers" | ||
fetch_filtered_trades(officer: true, min_value: 100_000, sort: "value") | ||
else | ||
recent_insider_trades | ||
end | ||
end | ||
end | ||
|
||
def total_value | ||
insider_trades.sum { |trade| trade[:value] } | ||
end | ||
|
||
def total_shares | ||
insider_trades.sum { |trade| trade[:shares] } | ||
end | ||
|
||
def top_trader | ||
insider_trades | ||
.group_by { |trade| trade[:full_name] } | ||
.transform_values { |trades| trades.sum { |t| t[:value].abs } } | ||
.max_by { |_, value| value } | ||
&.then { |name, value| { name: name, value: value } } | ||
end | ||
|
||
def largest_transaction | ||
insider_trades.max_by { |trade| trade[:value].abs } | ||
end | ||
|
||
def recent_trend | ||
last_month_trades = insider_trades | ||
.select { |t| Date.parse(t[:date_reported]) >= 30.days.ago } | ||
|
||
total_value = last_month_trades.sum { |t| t[:value] } | ||
total_volume = last_month_trades.sum { |t| t[:shares] } | ||
|
||
{ | ||
value: total_value, | ||
volume: total_volume, | ||
count: last_month_trades.size | ||
} | ||
end | ||
|
||
private | ||
def active_record | ||
@active_record ||= Tool.find_by! slug: "inside-trading-tracker" | ||
end | ||
|
||
def insider_data | ||
@insider_data ||= Rails.cache.fetch("insider_trades/#{symbol}/#{180.days.ago.to_date}/#{Date.today}/250", expires_in: 6.hours) do | ||
response = Provider::Synth.new.insider_trades( | ||
ticker: symbol, | ||
start_date: 180.days.ago, | ||
end_date: Date.today, | ||
limit: 250 | ||
) | ||
|
||
if response.success? | ||
{ trades: response.trades } | ||
else | ||
{ trades: [] } | ||
end | ||
end | ||
end | ||
|
||
def recent_insider_trades | ||
Rails.cache.fetch("recent_insider_trades/#{Date.today}", expires_in: 6.hours) do | ||
response = Provider::Synth.new.recent_insider_trades(limit: 250) | ||
return [] unless response[:trades]&.any? | ||
format_trades(response[:trades]) | ||
end | ||
end | ||
|
||
def format_trades(trades) | ||
trades.map do |trade| | ||
next unless trade["transaction_type"].present? | ||
|
||
transaction_type = trade["transaction_type"] | ||
is_positive = [ "Purchase", "Grant", "Exercise/Conversion" ].include?(transaction_type) | ||
is_negative = [ "Sale", "Sale to Issuer", "Payment of Exercise Price" ].include?(transaction_type) | ||
next unless is_positive || is_negative || transaction_type == "Discretionary Transaction" | ||
|
||
shares = trade["shares"].to_i.abs | ||
value = trade["value"].to_f.abs | ||
|
||
{ | ||
full_name: trade["full_name"], | ||
title: trade["position"], | ||
date_reported: trade["transaction_date"], | ||
description: trade["formatted_transaction_code"], | ||
shares: is_positive ? shares : -shares, | ||
value: is_positive ? value : -value, | ||
price: trade["price"], | ||
roles: trade["formatted_roles"], | ||
ownership_type: trade["formatted_ownership_type"], | ||
post_transaction_shares: trade["post_transaction_shares"], | ||
filing_link: trade["filing_link"], | ||
summary: trade["summary"], | ||
company: trade.dig("company", "name") || trade["company_name"] || trade["ticker"], | ||
company_description: trade.dig("company", "description"), | ||
company_industry: trade.dig("company", "industry"), | ||
company_sector: trade.dig("company", "sector"), | ||
company_employees: trade.dig("company", "total_employees"), | ||
ticker: trade["ticker"], | ||
position: trade["position"], | ||
exchange: trade.dig("exchange", "acronym"), | ||
exchange_country: trade.dig("exchange", "country_code"), | ||
footnotes: trade["footnotes"], | ||
transaction_type: transaction_type | ||
} | ||
end.compact | ||
end | ||
|
||
def fetch_filtered_trades(filters = {}) | ||
cache_key = "filtered_insider_trades/#{filters.to_json}/#{90.days.ago.to_date}/#{Date.today}" | ||
|
||
Rails.cache.fetch(cache_key, expires_in: 30.minutes) do | ||
Rails.logger.warn "Fetching filtered trades with filters: #{filters}" | ||
response = Provider::Synth.new.recent_insider_trades( | ||
start_date: 90.days.ago, | ||
end_date: Date.today, | ||
limit: 250, | ||
**filters | ||
) | ||
|
||
return [] unless response.success? && response.trades&.any? | ||
format_trades(response.trades) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.