# Post-Election Backtester for ThinkOrSwim (Free Download)

Backtester Free Beginner 10 min video 33 lines of code Nov 17, 2024 Updated Oct 21, 2026

Free election backtester for ThinkOrSwim. It buys the open the day after each U.S. presidential election from 2000 to 2020 and holds 30, 60 or 90 days.

## Overview

In this tutorial I build an election backtester for ThinkOrSwim to see whether post-election volatility has offered an edge. The strategy buys at the open on the first trading day after each U.S. presidential election since 2000, holds for a set number of days, and sells at the close. I test holds of 30, 60 and 90 days and look at the P/L and which trades lost.

The idea came from a Reddit post about using TQQQ, the triple-leveraged QQQ ETF, and post-election volatility to grow a small account. The backtester takes about 10 minutes to build from 6 election dates, and it suits anyone who wants to check a calendar idea against history before trading it.

## Key Takeaways

- The election backtester is a free ThinkOrSwim strategy that buys at the open of the first bar after each U.S. presidential election day and sells at the close of the bar 30 bars after election day.

- It covers 6 elections, from November 7, 2000 to November 3, 2020, stored as YYYYMMDD numbers. The postElectionDays input sets the hold: 30 by default, and I also test 60 and 90.

- In the video, on SPY with the maximum data and a 30-day hold, the 2000 and 2008 trades were losers, and both were bear market years. The video reads out no dollar totals and no win rate.

- The 2020 election drove most of the P/L on SPY and QQQ, where I called the overall result negligible at best. On the Dow, the strategy was profitable before 2020, but marginally.

- A 60-day hold left the 2000 and 2008 trades as losers, and at 90 days the P/L dropped on both QQQ and the S&P.

## What the Election Backtester Tests

The election backtester tests one simple strategy: buy once the election results are in and hold for a fixed number of days. I want to know whether holding for 30 days after election day is a good idea, whether 60 days is better, or whether 90 days is better still, measured by the P/L and by how often the trade succeeds.

The question came from a Reddit post. Its author suggested using TQQQ and the volatility after an election as a way to grow a small account. I wanted to see if the idea had any merit, and a backtest across every election since 2000 is a quick way to check.

## The Election Rule

The election rule in this backtester has one event, one entry and one exit, and the hold length is an input. The table lists each part as the code sets it.

Part of the rule Setting in the code

Event U.S. presidential election day, for the 6 elections from 2000 to 2020

Entry Buy to open at the open of the first bar after election day, when the results are in

Exit Sell to close at the close of the bar postElectionDays bars after election day

Hold 30 bars by default. I also test 60 and 90.

Size Not set in the code, so the strategy uses the default quantity

Direction Long only

The hold counts bars, so on a daily chart it counts trading days, which run behind calendar days:

postElectionDays Trading sessions held Roughly on the calendar

30 30 About 6 weeks

60 60 About 3 months

90 90 About 4 months

## The 6 Election Dates

The election backtester stores each election day as a number in year, month, day order, the same format getYYYYMMDD() returns for every bar. I used Perplexity to list the election dates going back to 2000, which sped up the research. The Code tab version also stores which party won each election, with 1 for Democratic and 2 for Republican.

Variable Election day Value in the code Party variable

election1 November 3, 2020 20201103 party1 = 1

election2 November 8, 2016 20161108 party2 = 2

election3 November 6, 2012 20121106 party3 = 1

election4 November 4, 2008 20081104 party4 = 1

election5 November 2, 2004 20041102 party5 = 2

election6 November 7, 2000 20001107 party6 = 2

The party variables are not part of the video build, and the orders don't use them yet. They are there if you want to split the results by the party that won.

## How to Build the Backtester in ThinkOrSwim

The election backtester takes one input and 6 dates, and you set it up as a new strategy. The download button on this page currently delivers the Santa Claus rally strategy file instead of this one, so build it from the Code tab:

- Click the Studies icon on your chart and open the Strategies tab.

- Click Create, delete the starter code, and paste in the code from the Code tab on this page.

- Give the strategy a name. My first choice was already in use, so mine is called TI election analysis.

- Click OK and Apply.

- Load SPY on a daily chart with the maximum available data, so all 6 elections are on the chart.

- Right-click and choose Show report to list the trades.

If no P/L bars show under the chart, turn on the Floating P/L study in Global Strategy Settings, the same fix I show in the [SPY meltdown backtester](https://tosindicators.com/backtesters/spy-meltdown) video. For importing .ts files in general, see our guide on [how to import a study or strategy into ThinkOrSwim](https://tosindicators.com/thinkscript/how-to-import-thinkorswim).

## How the Code Works

The election backtester code has 4 parts: the hold input, the election dates, an election day check, and the two orders. The snippets below come from the Code tab version.

### Step 1: The Hold Input

(This step's code is free with an account.)

I added this input at the top so the hold can change from the strategy's settings. Its default is 30.

### Step 2: The Election Dates

(This step's code is free with an account.)

Each date is a plain number, year first, then month, then day. The other 4 elections follow the same pattern, each with its party line, as listed in the table above.

### Step 3: The Election Day Check

getYYYYMMDD() is the thinkScript function that returns the date of any candle. The electionDay line compares it with each of the 6 dates, joined with or , so it is true on any of the 6 election days and false on every other bar:

(This step's code is free with an account.)

### Step 4: The Orders

(This step's code is free with an account.)

The buy order checks electionDay[1] : was the previous bar an election day? If so, the results are in and the strategy buys at the open of the current bar. No quantity is given, so ThinkOrSwim uses its default.

The sell order checks electionDay[postElectionDays] : was election day 30 bars ago? If so, it sells to close at that bar's close. Change the input to 60 or 90 and the exit moves with it, with no other edits.

## Reading the Strategy Report

The election backtester's report appears to list each entry one day after the bar the strategy entered on, which I call a quirk of ThinkOrSwim in the video. As I describe it there, a trade that entered the day after the election shows a date 2 days after it, while the closing prices in the report are correct.

Our [Santa Claus rally backtester](https://tosindicators.com/backtesters/santa-claus-rally) video shows the same offset with exact prices: the report listed a December 21, 2021 entry at $454.48, the open of the December 20 bar. When a report date looks a day late, check its price against the bar before it.

## Results From the Video

The election backtester produced these results with the maximum available data in the video. They are historical results from the 6 elections in the code, 2000 to 2020. The video describes each run, reads out no dollar totals and no win rate, and doesn't name the Dow symbol it used.

Symbol Hold Elections What the video shows

SPY 30 days 6 2000 and 2008 were losers, both bear market years. The P/L showed a positive number, which it reached only with the 2020 election.

QQQ 30 days Not stated, includes 2000 and 2008 The same 2 election cycles lost. P/L negligible at best.

The Dow (symbol not named) 30 days 6 Profitable even before the 2020 election, but marginally. Mediocre profits.

SPY 60 days 6 2000 and 2008 were still losers. The 2020 election drove most of the P/L.

QQQ 60 days Not stated Similar to SPY

QQQ 90 days Not stated The P/L dropped

The S&P 90 days Not stated The P/L dropped as well

The two losing years matter most. In 2000 and 2008 the market didn't spike higher after the election volatility ended, and the bearish activity continued. Both were bear market years.

Adding time didn't help. At 60 days, the 2000 and 2008 trades were still losers. Logic might say the longer you wait, the better the trade gets, but at 90 days the QQQ P/L dropped, and the S&P showed the same thing.

## How I Read the Results

The election backtester's results look mediocre to me. Across SPY, QQQ and the Dow, the profits were small, and on SPY and QQQ most of the P/L came from one election, 2020. A positive total that depends on the most recent trade should give you pause. Our research on [bear market relief rallies in SPY, QQQ, DIA and IWM](https://tosindicators.com/research/bear-market-relief-rally-a-deep-dive-in-spy-qqq-dia-and-iwm) looks at the same markets in bear market conditions.

The bigger question is what to do with the two losing years that lined up with bear markets. Six elections is not a lot of data points, so one or two trades can swing the result either way. For the Redditor thinking about TQQQ, I'd treat this as a starting point for research.

A post-election trade is a calendar idea, and the backtest is a read on how it has gone, not an entry signal by itself. If you trade around an election, decide where your stop goes before you enter, and size the position so a stop-out costs a small, fixed share of your account.

## Ways to Extend the Backtester

The election backtester is a clean starting point for the question of what happens 30, 60 and 90 days after an election. At the end of the video I suggest a few ways to dig deeper:

- Split by party. Look for patterns around which party won. The party variables on the Code tab hold that information for each election.

- Add market structure. Instead of a fixed number of days, add conditions based on what the market looked like heading into the election.

- Test the Nasdaq with more history. TQQQ doesn't have many elections in its data. Switch to QQQ or to the Nasdaq futures to cover more of them.

- Add newer elections. This edit is not in the video. Add a seventh date in the same format, such as def election7 = 20241105; for the November 5, 2024 election, and add or getYYYYMMDD() == election7 to the end of the electionDay line, before the semicolon.

Our [Super Bowl indicator](https://tosindicators.com/indicators/superbowl) uses the same approach: an outside list of dates and winners, turned into thinkScript so ThinkOrSwim can test a theory against the chart.

## Other Ways to Test Market Events

The election backtester works from a fixed list of dates. Our other free event tools find their bars in different ways:

- The Santa Claus rally backtester finds its dates with getDayOfMonth and getMonth, so it covers every December on the chart with no list to update.

- The SPY meltdown backtester triggers on price instead of the calendar, buying the session after SPY closes more than 3% below its open.

- The Seasonal Analysis indicator shows how often each month has closed higher and its average change.

The rest of our strategies are on the [ThinkOrSwim backtesters page](https://tosindicators.com/backtesters).

## The backtester on real data

Drawn from real daily bars with the code on this page, through Oct 20, 2026. The backtester buys at the open of the first bar after election day and sells at the close of the bar 30 bars after it (60 and 90 as inputs). The post's code lists the 6 elections from 2000 to 2020; our daily bars start in 2015, so this covers 2016 and 2020, plus November 5, 2024, which the code does not include. To add it, put def election7 = 20241105; in the list and add it to electionDay.

### SPY after the 2024 election

SPY: bought at the open on November 6, 2024. Each red or green arrow is where one hold length sold, with its result on 100 shares.

### Every election in our data

SPY, election 30-day hold 60-day hold 90-day hold

Nov 8, 2016 +6.3% $1,340 on 100 shares +7.8% $1,656 on 100 shares +10.1% $2,136 on 100 shares

Nov 3, 2020 +8.6% $2,931 on 100 shares +10.4% $3,537 on 100 shares +16.2% $5,505 on 100 shares

Nov 5, 2024 -0.5% -$292 on 100 shares +2.1% $1,258 on 100 shares -3.7% -$2,207 on 100 shares

Fund, 3 elections 30-day hold 60-day hold 90-day hold

SPY 2 of 3 average +4.8% 3 of 3 average +6.8% 2 of 3 average +7.5%

QQQ 3 of 3 average +5.4% 3 of 3 average +9.1% 2 of 3 average +7.1%

DIA 2 of 3 average +5.5% 3 of 3 average +7.4% 2 of 3 average +9.8%

IWM 2 of 3 average +10.8% 2 of 3 average +14.8% 2 of 3 average +15.6%

Results from the open of the day after the election to the close of the exit day. For comparison, any 30-day hold on SPY since 2016 was higher 70% of the time (average +1.7%), any 60-day hold 78% and any 90-day hold 79%. Three elections are three trades: enough to see what happened, not enough to call an edge.

The full thinkScript code is free with an account: [create one](https://tosindicators.com/register) or [sign in](https://tosindicators.com/login) on this page.

## Download

Election Backtester Download the Election Backtester for ThinkOrSwim. The download link on this page currently delivers TI_SantaClausRallyBacktesterSTRATEGY.ts, the strategy from our Santa Claus Rally Backtester, and not the election backtester. Until the file is replaced, copy the election code from the Code tab into a new strategy: click the Studies icon, open the Strategies tab, click Create and paste it in. If you haven't added a strategy before, here's [how to import into your ThinkOrSwim platform](https://tosindicators.com/thinkscript/how-to-import-thinkorswim).

[Download free](https://tosindicators.com/backtesters/election)

## Video: Post-Election Backtester, built line by line

10 minutes on YouTube.

[Watch on YouTube](https://www.youtube.com/watch?v=33HvUEB4WnE)

## Questions

What is the election backtester?

It's a free ThinkOrSwim strategy that tests buying after a U.S. presidential election. It buys at the open of the first bar after election day and sells at the close 30 bars later, for each of the 6 elections from 2000 to 2020. An input changes the hold, so you can test 60 or 90 days as well.

How do I backtest the stock market after an election in ThinkOrSwim?

Create a strategy on the Strategies tab and paste in the code from the Code tab. It stores each election day as a YYYYMMDD number, checks for those dates with getYYYYMMDD(), and places a buy to open order the day after. Load a daily chart with the maximum data and right-click Show report to see each trade.

Has the market gone up after elections in the backtest?

Not reliably. In the video, on SPY with a 30-day hold, the trades after the 2000 and 2008 elections were losers, both in bear market years. The SPY total turned positive only with the 2020 election. On the Dow the strategy was profitable before 2020, but marginally. These are historical results from 6 elections.

Is it better to hold 30, 60 or 90 days after an election?

Longer holds didn't help in the video. At 60 days on SPY, the 2000 and 2008 trades were still losers, and at 90 days the P/L dropped on both QQQ and the S&P. The video reads out no dollar totals, and with only 6 elections, one trade can swing the result.

What does the postElectionDays input do?

It sets how long the strategy holds after election day, 30 by default. The exit checks electionDay with that number of bars back, so on a daily chart it counts trading days. A setting of 30 holds for 30 sessions, about 6 weeks, and 90 holds for about 4 months.

Why does the entry date in the report look a day late?

The report appears to list each entry one day after the bar the strategy entered on, which I call a quirk of ThinkOrSwim in the video. Our Santa Claus rally video shows the same offset with prices: a December 21, 2021 entry at $454.48, the open of the December 20 bar. The closing prices are correct, so check a listed entry against the bar before it.

Can I run the election backtester on TQQQ?

You can, but TQQQ doesn't have many elections in its data, so the test has very few trades. In the video I suggest switching to QQQ or to the Nasdaq futures instead. They carry the same Nasdaq idea with more election cycles in their history.

Which elections does the backtester cover?

It covers the 6 U.S. presidential elections from 2000 to 2020: November 7, 2000, November 2, 2004, November 4, 2008, November 6, 2012, November 8, 2016 and November 3, 2020. Each one is stored as a YYYYMMDD number, such as 20201103. To add a newer election, add another date variable and include it in the electionDay check.

## Related tools

[Seasonal Analysis: Indicator · Free](https://tosindicators.com/indicators/seasonal-analysis)[SPY Meltdown Backtester: Backtester · Free](https://tosindicators.com/backtesters/spy-meltdown)[Santa Claus Rally Backtester: Backtester · Free](https://tosindicators.com/backtesters/santa-claus-rally)[Super Bowl Indicator: Indicator · Free](https://tosindicators.com/indicators/superbowl)[TTM Squeeze Histogram Backtester: Backtester · Free](https://tosindicators.com/backtesters/ttm-squeeze-histogram)[Moving Average Pullback Backtester: Backtester · Free](https://tosindicators.com/backtesters/moving-average-pullback)

## Get each new indicator by email

One email on Sundays with the week's new tool and its code. A free account also unlocks every .txt download.

No credit card. Unsubscribe anytime.
