Stock Market Days Up vs. Days Down Indicator for ThinkorSwim



Active member
Don't try to reverse engineer what this image shows...I'll post the code/description later...

Just wondering if anyone sees a way to play this indicator?

Well, not a lot of input here, but really I wrote this because someone said there are about the same number of up days and down days in a given year. So of course I set out to measure this...who knows, maybe mean reversion or something could be some odd indicator.

As promised, here is the code for this Up Down Ratio. The plots above are year/month/week on a 1-Day chart.

Enjoy :)

# Stock Market Days Up vs. Days Down Indicator for ThinkorSwim
# Shows number of up days versus down days.
# Author: Kory Gill, @korygill

declare lower;
declare once_per_bar;
declare hide_on_intraday;

input RollOverPeriod = {default year, month, week};

def vClose = close;
def year = GetYear();
def month = GetMonth();
def week = GetWeek();
def isRollOver;
def lookback;

switch (RollOverPeriod) {
    case year:
        isRollOver = if year != year[1] then 1 else 0;
        lookback = 252;
    case month:
        isRollOver = if month != month[1] then 1 else 0;
        lookback = 20;
    case week:
        lookback = 5;
        isRollOver = if week != week[1] then 1 else 0;

def bn = BarNumber();

def upCount = if bn == 1 then 0
              else if isRollOver then if vClose >= vClose[1] then 1 else 0
              else if vClose >= vClose[1] then upCount[1]+1 else upCount[1];

def dnCount = if bn == 1 then 0
              else if isRollOver then if vClose < vClose[1] then -1 else 0
              else if vClose < vClose[1] then dnCount[1]-1 else dnCount[1];

# used for debugging
#plot pUpCount = upCount;
#plot pDnCount = dnCount;
#plot pTotal = upCount - dnCount;

plot zeroLine = 0;

def upSum = Sum(upCount, lookback);
def dnSum = Sum(dnCount, lookback);
plot pSumUpDnRatio = if dnSum != 0 then (upSum+dnSum)/(upSum-dnSum)*100 else 0;
pSumUpDnRatio.AssignValueColor(if pSumUpDnRatio >= 0 then Color.Green else Color.Red);

plot pUpVsDn = upCount + dnCount;
pUpVsDn.AssignValueColor(if pUpVsDn >= 0 then Color.Yellow else Color.Magenta);

AddLabel(yes, ">= 0", Color.Green);
AddLabel(yes, ">= 0", Color.Yellow);
AddLabel(yes, "< 0", Color.Red);
AddLabel(yes, "< 0", Color.Magenta);
Last edited by a moderator:


Well-known member
Well the obvious setup is 0 line cross on fastest momentum(bottom) when in confluence with slower momentum (mid).


Active member
As a side note, I used to track number of down days and if we go to around 8-9 down days on the XAU, you could do a pretty low risk trade the next day was up. If it was not, double down. If not again, double down again. Kind of like the ultimate winning roulette strategy if you had infinite funds. /lol That said, there are better ways to play XAU then counting down days, but events outside the norm do mean revert, so it's not all that crazy. Check out my posts on FGMR for a working example of this in action.


New member
Hi TOS experts,

I am trying to write a script that selects stocks that have 3 consecutive days of rising prices and with rising volume, once they cross SMA 20 for a specific market cap.

I have the preset market cap and SMA filters in the scanner.

How do i capture current close, previous-day close and previous-day-1 closing price and compare; and repeat the same for volume.