ayanami27
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: April 16th, 2022
Contact: TradingView Profile

Back test strategy

New to pine script in general and before i go down and learn this rabbit hole i want to know if something is possible. Can a strategy be written to test around events like earnings, dividend date or splits. Can a strat be written to lets say buy the close the day before an earnings date and sell the open the day of earnings?

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Back test strategy

The answer is yes, you can use the pinescript request.earnings, request.dividends and request.splits to extract information from stocks. I'm not exactly sure what data is returned but I would expect all the necessary information about earnings, dividends and splits would be provided and therefore you could code your strategy accordingly

These are the list of fundamental fields available in pinescript. I couldn't find the earnings date but it may be in the list. Otherwise you may have to get a list of earnings dates from another source and import them

General Fields = Financial, Available periods, financial_id
Accounts payable FQ, FY ACCOUNTS_PAYABLE
Accounts receivable - trade, net FQ, FY ACCOUNTS_RECEIVABLES_NET
Accrued payroll FQ, FY ACCRUED_PAYROLL
Accumulated depreciation, total FQ, FY ACCUM_DEPREC_TOTAL
Additional paid-in capital/Capital surplus FQ, FY ADDITIONAL_PAID_IN_CAPITAL
Book value per share FQ, FY BOOK_VALUE_PER_SHARE
Capital and operating lease obligations FQ, FY CAPITAL_OPERATING_LEASE_OBLIGATIONS
Capitalized lease obligations FQ, FY CAPITAL_LEASE_OBLIGATIONS
Cash & equivalents FQ, FY CASH_N_EQUIVALENTS
Cash and short term investments FQ, FY CASH_N_SHORT_TERM_INVEST
Common equity, total FQ, FY COMMON_EQUITY_TOTAL
Common stock par/Carrying value FQ, FY COMMON_STOCK_PAR
Current portion of LT debt and capital leases FQ, FY CURRENT_PORT_DEBT_CAPITAL_LEASES
Deferred income, current FQ, FY DEFERRED_INCOME_CURRENT
Deferred income, non-current FQ, FY DEFERRED_INCOME_NON_CURRENT
Deferred tax assets FQ, FY DEFERRED_TAX_ASSESTS
Deferred tax liabilities FQ, FY DEFERRED_TAX_LIABILITIES
Dividends payable FY DIVIDENDS_PAYABLE
Goodwill, net FQ, FY GOODWILL
Gross property/plant/equipment FQ, FY PPE_TOTAL_GROSS
Income tax payable FQ, FY INCOME_TAX_PAYABLE
Inventories - finished goods FQ, FY INVENTORY_FINISHED_GOODS
Inventories - progress payments & other FQ, FY INVENTORY_PROGRESS_PAYMENTS
Inventories - raw materials FQ, FY INVENTORY_RAW_MATERIALS
Inventories - work in progress FQ, FY INVENTORY_WORK_IN_PROGRESS
Investments in unconsolidated subsidiaries FQ, FY INVESTMENTS_IN_UNCONCSOLIDATE
Long term debt FQ, FY LONG_TERM_DEBT
Long term debt excl. lease liabilities FQ, FY LONG_TERM_DEBT_EXCL_CAPITAL_LEASE
Long term investments FQ, FY LONG_TERM_INVESTMENTS
Minority interest FQ, FY MINORITY_INTEREST
Net debt FQ, FY NET_DEBT
Net intangible assets FQ, FY INTANGIBLES_NET
Net property/plant/equipment FQ, FY PPE_TOTAL_NET
Note receivable - long term FQ, FY LONG_TERM_NOTE_RECEIVABLE
Notes payable FY NOTES_PAYABLE_SHORT_TERM_DEBT
Operating lease liabilities FQ, FY OPERATING_LEASE_LIABILITIES
Other common equity FQ, FY OTHER_COMMON_EQUITY
Other current assets, total FQ, FY OTHER_CURRENT_ASSETS_TOTAL
Other current liabilities FQ, FY OTHER_CURRENT_LIABILITIES
Other intangibles, net FQ, FY OTHER_INTANGIBLES_NET
Other investments FQ, FY OTHER_INVESTMENTS
Other long term assets, total FQ, FY LONG_TERM_OTHER_ASSETS_TOTAL
Other non-current liabilities, total FQ, FY OTHER_LIABILITIES_TOTAL
Other receivables FQ, FY OTHER_RECEIVABLES
Other short term debt FY OTHER_SHORT_TERM_DEBT
Paid in capital FQ, FY PAID_IN_CAPITAL
Preferred stock, carrying value FQ, FY PREFERRED_STOCK_CARRYING_VALUE
Prepaid expenses FQ, FY PREPAID_EXPENSES
Provision for risks & charge FQ, FY PROVISION_F_RISKS
Retained earnings FQ, FY RETAINED_EARNINGS
Shareholders' equity FQ, FY SHRHLDRS_EQUITY
Short term debt FQ, FY SHORT_TERM_DEBT
Short term debt excl. current portion of LT debt FQ, FY SHORT_TERM_DEBT_EXCL_CURRENT_PORT
Short term investments FQ, FY SHORT_TERM_INVEST
Tangible book value per share FQ, FY BOOK_TANGIBLE_PER_SHARE
Total assets FQ, FY TOTAL_ASSETS
Total current assets FQ, FY TOTAL_CURRENT_ASSETS
Total current liabilities FQ, FY TOTAL_CURRENT_LIABILITIES
Total debt FQ, FY TOTAL_DEBT
Total equity FQ, FY TOTAL_EQUITY
Total inventory FQ, FY TOTAL_INVENTORY
Total liabilities FQ, FY TOTAL_LIABILITIES
Total liabilities & shareholders' equities FQ, FY TOTAL_LIABILITIES_SHRHLDRS_EQUITY
Total non-current assets FQ, FY TOTAL_NON_CURRENT_ASSETS
Total non-current liabilities FQ, FY TOTAL_NON_CURRENT_LIABILITIES
Total receivables, net FQ, FY TOTAL_RECEIVABLES_NET
Treasury stock - common FQ, FY TREASURY_STOCK_COMMON

CASH FLOW Fields = Financial, Available periods, financial_id
Amortization FQ, FY AMORTIZATION
Capital expenditures FQ, FY CAPITAL_EXPENDITURES
Capital expenditures - fixed assets FQ, FY CAPITAL_EXPENDITURES_FIXED_ASSETS
Capital expenditures - other assets FQ, FY CAPITAL_EXPENDITURES_OTHER_ASSETS
Cash from financing activities FQ, FY, TTM CASH_F_FINANCING_ACTIVITIES
Cash from investing activities FQ, FY, TTM CASH_F_INVESTING_ACTIVITIES
Cash from operating activities FQ, FY, TTM CASH_F_OPERATING_ACTIVITIES
Change in accounts payable FQ, FY CHANGE_IN_ACCOUNTS_PAYABLE
Change in accounts receivable FQ, FY CHANGE_IN_ACCOUNTS_RECEIVABLE
Change in accrued expenses FQ, FY CHANGE_IN_ACCRUED_EXPENSES
Change in inventories FQ, FY CHANGE_IN_INVENTORIES
Change in other assets/liabilities FQ, FY CHANGE_IN_OTHER_ASSETS
Change in taxes payable FQ, FY CHANGE_IN_TAXES_PAYABLE
Changes in working capital FQ, FY CHANGES_IN_WORKING_CAPITAL
Common dividends paid FQ, FY COMMON_DIVIDENDS_CASH_FLOW
Deferred taxes (cash flow) FQ, FY CASH_FLOW_DEFERRED_TAXES
Depreciation & amortization (cash flow) FQ, FY CASH_FLOW_DEPRECATION_N_AMORTIZATION
Depreciation/depletion FQ, FY DEPRECIATION_DEPLETION
Financing activities – other sources FQ, FY OTHER_FINANCING_CASH_FLOW_SOURCES
Financing activities – other uses FQ, FY OTHER_FINANCING_CASH_FLOW_USES
Free cash flow FQ, FY, TTM FREE_CASH_FLOW
Funds from operations FQ, FY FUNDS_F_OPERATIONS
Investing activities – other sources FQ, FY OTHER_INVESTING_CASH_FLOW_SOURCES
Investing activities – other uses FQ, FY OTHER_INVESTING_CASH_FLOW_USES
Issuance of long term debt FQ, FY SUPPLYING_OF_LONG_TERM_DEBT
Issuance/retirement of debt, net FQ, FY ISSUANCE_OF_DEBT_NET
Issuance/retirement of long term debt FQ, FY ISSUANCE_OF_LONG_TERM_DEBT
Issuance/retirement of other debt FQ, FY ISSUANCE_OF_OTHER_DEBT
Issuance/retirement of short term debt FQ, FY ISSUANCE_OF_SHORT_TERM_DEBT
Issuance/retirement of stock, net FQ, FY ISSUANCE_OF_STOCK_NET
Net income (cash flow) FQ, FY NET_INCOME_STARTING_LINE
Non-cash items FQ, FY NON_CASH_ITEMS
Other financing cash flow items, total FQ, FY OTHER_FINANCING_CASH_FLOW_ITEMS_TOTAL
Other investing cash flow items, total FQ, FY OTHER_INVESTING_CASH_FLOW_ITEMS_TOTAL
Preferred dividends paid FQ, FY PREFERRED_DIVIDENDS_CASH_FLOW
Purchase of investments FQ, FY PURCHASE_OF_INVESTMENTS
Purchase/acquisition of business FQ, FY PURCHASE_OF_BUSINESS
Purchase/sale of business, net FQ, FY PURCHASE_SALE_BUSINESS
Purchase/sale of investments, net FQ, FY PURCHASE_SALE_INVESTMENTS
Reduction of long term debt FQ, FY REDUCTION_OF_LONG_TERM_DEBT
Repurchase of common & preferred stock FQ, FY PURCHASE_OF_STOCK
Sale of common & preferred stock FQ, FY SALE_OF_STOCK
Sale of fixed assets & businesses FQ, FY SALES_OF_BUSINESS
Sale/maturity of investments FQ, FY SALES_OF_INVESTMENTS
Total cash dividends paid FQ, FY TOTAL_CASH_DIVIDENDS_PAID

Income statements Fields = Financial, Available periods, financial_id
After tax other income/expense FQ, FY AFTER_TAX_OTHER_INCOME
Average basic shares outstanding FQ, FY BASIC_SHARES_OUTSTANDING
Basic earnings per share (Basic EPS) FQ, FY, TTM EARNINGS_PER_SHARE_BASIC
Cost of goods sold FQ, FY, TTM COST_OF_GOODS
Deprecation and amortization FQ, FY, TTM DEP_AMORT_EXP_INCOME_S
Diluted earnings per share (Diluted EPS) FQ, FY, TTM EARNINGS_PER_SHARE_DILUTED
Diluted net income available to common stockholders FQ, FY DILUTED_NET_INCOME
Diluted shares outstanding FQ, FY DILUTED_SHARES_OUTSTANDING
Dilution adjustment FQ, FY DILUTION_ADJUSTMENT
Discontinued operations FQ, FY DISCONTINUED_OPERATIONS
EBIT FQ, FY, TTM EBIT
EBITDA FQ, FY, TTM EBITDA
Equity in earnings FQ, FY EQUITY_IN_EARNINGS
Gross profit FQ, FY, TTM GROSS_PROFIT
Interest capitalized FQ, FY INTEREST_CAPITALIZED
Interest expense on debt FQ, FY INTEREST_EXPENSE_ON_DEBT
Interest expense, net of interest capitalized FQ, FY NON_OPER_INTEREST_EXP
Miscellaneous non-operating expense FQ, FY OTHER_INCOME
Net income FQ, FY, TTM NET_INCOME
Net income before discontinued operations FQ, FY NET_INCOME_BEF_DISC_OPER
Non-controlling/minority interest FQ, FY MINORITY_INTEREST_EXP
Non-operating income, excl. interest expenses FQ, FY NON_OPER_INCOME
Non-operating income, total FQ, FY, TTM TOTAL_NON_OPER_INCOME
Non-operating interest income FQ, FY NON_OPER_INTEREST_INCOME
Operating expenses (excl. COGS) FQ, FY OPERATING_EXPENSES
Operating income FQ, FY, TTM OPER_INCOME
Other cost of goods sold FQ, FY COST_OF_GOODS_EXCL_DEP_AMORT
Other operating expenses, total FQ, FY OTHER_OPER_EXPENSE_TOTAL
Preferred dividends FQ, FY PREFERRED_DIVIDENDS
Pretax equity in earnings FQ, FY PRETAX_EQUITY_IN_EARNINGS
Pretax income FQ, FY, TTM PRETAX_INCOME
Research & development FQ, FY RESEARCH_AND_DEV
Selling/general/admin expenses, other FQ, FY SELL_GEN_ADMIN_EXP_OTHER
Selling/general/admin expenses, total FQ, FY SELL_GEN_ADMIN_EXP_TOTAL
Taxes FQ, FY, TTM INCOME_TAX
Total operating expenses FQ, FY, TTM TOTAL_OPER_EXPENSE
Total revenue FQ, FY, TTM TOTAL_REVENUE
Unusual income/expense FQ, FY UNUSUAL_EXPENSE_INC

Statistics Fields = Financial, Available periods, financial_id
Accruals FQ, FY ACCRUALS_RATIO
Altman Z-score FQ, FY ALTMAN_Z_SCORE
Asset turnover FQ, FY ASSET_TURNOVER
Beneish M-score FQ, FY BENEISH_M_SCORE
Buyback yield % FQ, FY BUYBACK_YIELD
COGS to revenue ratio FQ, FY COGS_TO_REVENUE
Cash conversion cycle FQ, FY CASH_CONVERSION_CYCLE
Cash to debt ratio FQ, FY CASH_TO_DEBT
Current ratio FQ, FY CURRENT_RATIO
Days inventory FQ, FY DAYS_INVENT
Days payable FQ, FY DAYS_PAY
Days sales outstanding FQ, FY DAY_SALES_OUT
Debt to EBITDA ratio FQ, FY DEBT_TO_EBITDA
Debt to assets ratio FQ, FY DEBT_TO_ASSET
Debt to equity ratio FQ, FY DEBT_TO_EQUITY
Debt to revenue ratio FQ, FY DEBT_TO_REVENUE
Dividend payout ratio % FQ, FY DIVIDEND_PAYOUT_RATIO
Dividend yield % FQ, FY DIVIDENDS_YIELD
Dividends per share - common stock primary issue FQ, FY DPS_COMMON_STOCK_PRIM_ISSUE
EBITDA margin % FQ, FY, TTM EBITDA_MARGIN
EPS basic one year growth FQ, FY EARNINGS_PER_SHARE_BASIC_ONE_YEAR_GROWTH
EPS diluted one year growth FQ, FY EARNINGS_PER_SHARE_DILUTED_ONE_YEAR_GROWTH
EPS estimates FQ, FY EARNINGS_ESTIMATE
Effective interest rate on debt % FQ, FY EFFECTIVE_INTEREST_RATE_ON_DEBT
Enterprise value FQ, FY ENTERPRISE_VALUE
Enterprise value to EBIT ratio FQ, FY EV_EBIT
Enterprise value to EBITDA ratio FQ, FY ENTERPRISE_VALUE_EBITDA
Enterprise value to revenue ratio FQ, FY EV_REVENUE
Equity to assets ratio FQ, FY EQUITY_TO_ASSET
Float shares outstanding FY FLOAT_SHARES_OUTSTANDING
Free cash flow margin % FQ, FY FREE_CASH_FLOW_MARGIN
Fulmer H factor FQ, FY FULMER_H_FACTOR
Goodwill to assets ratio FQ, FY GOODWILL_TO_ASSET
Graham's number FQ, FY GRAHAM_NUMBERS
Gross margin % FQ, FY, TTM GROSS_MARGIN
Gross profit to assets ratio FQ, FY GROSS_PROFIT_TO_ASSET
Interest coverage FQ, FY INTERST_COVER
Inventory to revenue ratio FQ, FY INVENT_TO_REVENUE
Inventory turnover FQ, FY INVENT_TURNOVER
KZ index FY KZ_INDEX
Long term debt to total assets ratio FQ, FY LONG_TERM_DEBT_TO_ASSETS
Net current asset value per share FQ, FY NCAVPS_RATIO
Net income per employee FY NET_INCOME_PER_EMPLOYEE
Net margin % FQ, FY, TTM NET_MARGIN
Number of employees FY NUMBER_OF_EMPLOYEES
Operating earnings yield % FQ, FY OPERATING_EARNINGS_YIELD
Operating margin % FQ, FY OPERATING_MARGIN
PEG ratio FQ, FY PEG_RATIO
Piotroski F-score FQ, FY PIOTROSKI_F_SCORE
Price earnings ratio forward FQ, FY PRICE_EARNINGS_FORWARD
Price sales ratio forward FQ, FY PRICE_SALES_FORWARD
Quality ratio FQ, FY QUALITY_RATIO
Quick ratio FQ, FY QUICK_RATIO
Research & development to revenue ratio FQ, FY RESEARCH_AND_DEVELOP_TO_REVENUE
Return on assets % FQ, FY RETURN_ON_ASSETS
Return on common equity % FQ, FY RETURN_ON_COMMON_EQUITY
Return on equity % FQ, FY RETURN_ON_EQUITY
Return on equity adjusted to book value % FQ, FY RETURN_ON_EQUITY_ADJUST_TO_BOOK
Return on invested capital % FQ, FY RETURN_ON_INVESTED_CAPITAL
Return on tangible assets % FQ, FY RETURN_ON_TANG_ASSETS
Return on tangible equity % FQ, FY RETURN_ON_TANG_EQUITY
Revenue estimates FQ, FY SALES_ESTIMATES
Revenue one year growth FQ, FY REVENUE_ONE_YEAR_GROWTH
Revenue per employee FY REVENUE_PER_EMPLOYEE
Shares buyback ratio % FQ, FY SHARE_BUYBACK_RATIO
Sloan ratio % FQ, FY SLOAN_RATIO
Springate score FQ, FY SPRINGATE_SCORE
Sustainable growth rate FQ, FY SUSTAINABLE_GROWTH_RATE
Tangible common equity ratio FQ, FY TANGIBLE_COMMON_EQUITY_RATIO
Tobin's Q (approximate) FQ, FY TOBIN_Q_RATIO
Total common shares outstanding FQ, FY TOTAL_SHARES_OUTSTANDING
Zmijewski score FQ, FY ZMIJEWSKI_SCORE

Return to “Pine Script Q&A”