Display number of days since cross of two moving averages



How do I modify the following code to add a custom watchlist column indicating

  1. A 2-8 DMA crossover indicated in Green if 2DMA is above 8DMA and the number of days since the cross indicated by the identifier in the cell as A1-A9 (basically number of days the price has been above the crossover point).
  2. Also in reverse 2-8 DMA crossover indicated in Red if 2DMA is below 8DMA and the number of days since the crossover indicated by the identifier in the cell as B1-B9

input lengthOne = 2;
input lengthTwo = 8;

def maOne = Average(close, lengthOne);
def maTwo = Average(close, lengthTwo);

def condup = if maone < matwo
then double.nan
else if maone crosses above
matwo then 1
else if maone > matwo
then condup[1] + 1
else condup[1];

def conddown = if maone > matwo
then double.nan
else if maone crosses below
matwo then 1
else if maone < matwo
then conddown[1] + 1
else conddown[1];

if condup == 1 then “A1” else if conddown == 1 then “B1” else
if condup == 2 then “A2” else if conddown == 2 then “B2” else
if condup == 3 then “A3” else if conddown == 3 then “B3” else
if condup == 4 then “A4” else if conddown == 4 then “B4” else
if condup == 5 then “A5” else if conddown == 5 then “B5” else
if condup == 6 then “A6” else if conddown == 6 then “B6” else
if condup == 7 then “A7” else if conddown == 7 then “B7” else
if condup == 8 then “A8” else if conddown == 8 then “B8” else
if condup == 9 then “A9” else if conddown == 9 then “B9” else “<->”);


Marked as spam
Posted by (Questions: 5, Answers: 7)
Asked on April 13, 2020 12:14 pm
Private answer

Rather than try to fix your code I will start from scratch. When I say I am going to start from scratch what I actually mean is I am going to steal some code from a previous post that is almost identical to your yours. Link to that previous post:


I only had to add a second moving average and replace the close with one of the moving averages. User inputs are included so you can apply any of the moving averages included with Thinkorswim. The lengths of each can also be adjusted. This is a one size fits all solution.

input priceOne = close;
input lengthOne = 2;
input typeOne = AverageType.SIMPLE;
input priceTwo = close;
input lengthTwo = 8;
input typeTwo = AverageType.SIMPLE;
def maOne = MovingAverage(typeOne, priceOne, lengthOne);
def maTwo = MovingAverage(typeTwo, priceTwo, lengthTwo);
def crossAbove = maOne[1] < maTwo[1] and maOne > maTwo;
def crossBelow = maOne[1] > maTwo[1] and maOne < maTwo; rec countAbove = if crossAbove then 1 else if maOne > maTwo then countAbove[1] + 1 else 0;
rec countBelow = if crossBelow then -1 else if maOne < maTwo then countBelow[1] - 1 else 0; plot data = if countAbove > AbsValue(countBelow) then countAbove else countBelow;
data.AssignValueColor(if data == 0 then Color.CURRENT else Color.BLACK);
AssignBackgroundColor(if data > 0 then Color.GREEN else if data < 0 then Color.RED else Color.CURRENT);

Notice that in this solution we display positive values for cross above and negative values for cross below. This is an improvement over the method your code attempts to implement because with this solution you will be able to sort this column numerically. (sorting does not work properly when you try to display text in the column). Another bonus from using this method is there is no limit to the number of bars this code will count. Actually it's only limited by the amount of historical data available to the custom quote.

Marked as spam
Posted by (Questions: 37, Answers: 3222)
Answered on April 13, 2020 1:56 pm
This is awesome! I watched your conditional wizard tutorial and created a scan to look for moving averages about to cross: SimpleMovingAvg(”length” = 8).”SMA” is less than SimpleMovingAvg(”length” = 21).”SMA” and close is greater than SimpleMovingAvg(”length” = 21).”SMA” (I am a newbie and there is probably a better way to do this but this is what i got) then added the code above to the watchlist – it seems to work awesome! How could I add the code above to the scan to only get stocks that the SMA’s have not crossed in 30 (or so) bars on an hourly chart and the close price is above the 21 showing that the 8 is about to cross above?
( at August 11, 2020 9:12 am)
Well first thing to note is this code is for a custom watchlist column. So it produces a value and not a true/false signal required to build a scan. The best you can do with this code to build a scan is to remove the last statement, change "plot data" to "def data" and then add a new line for the scan: "plot scan = data > 30". This would return a list of stocks that have a value greater than 30 currently displayed on the watchlist column.
( at August 11, 2020 10:46 am)
That works, thank you! For Bullish 30. Once again thank you for your tutorials!
( at August 11, 2020 3:52 pm)