September 7, 2018 at 4:24 am #48071
Here’s a variation which counts the number of trading days since two moving averages crossed, calculating a positive number when crossing above and a negative number when crossing below. The example is for the 20EMA crossing the 50SMA:12345678910111213// Calculate the MAs20EMA = MA(BARS=20, STYLE=Exponential, CALC=Close);50SMA = MA(BARS=50, CALC=Close);// Calculate time since the MA crossr1 = TIMESINCESIGNAL(20EMA crosses 50SMA);// Is it a cross above?r2 = 20EMA > 50SMA;//Multiply by -1 to get negative numbersr3 = r1 * -1;//If cross is above use positive r1 value, otherwise use the negative r3 valueIF(r2 == 1,r1,r3)
The script can be added to a watchlist column and also a Show View to display the results (note the Show View properties have been set to a Dot plotstyle, with a positive/negative colour scheme):
So NKE crossed above 105 days ago, and CVX crossed below 51 days ago.
Also note that you can count in calendar days or weeks by changing the Units property in the TimeSinceSignal function:
1 user thanked author for this post.September 7, 2018 at 6:16 am #48087
Time Since the Signal Before Last
To calculate the time since the signal before last use the following (note: this will return calendar days), in this case RSI crossing above 70:12345678910//Get the signal;S = RSI() CrossesAbove 70;//Get the bar date of the signal before last - increase the OFFSET for earlier signalsP1 = BARDATE();P2 = LAST(BARDATE(NONZERO(S), OFFSET=1));Signal = P1 == P2;//Count the bars since the signalTIMESINCESIGNAL(signal, UNIT=Days)March 19, 2019 at 9:29 pm #51863
Highest/Lowest close since
To calculate the time since the current closing price was that high (in trading days):1234D1=LAST(CLOSE());TIMESINCESIGNAL(CLOSE()>D1)
To calculate the time since the current closing price was that low (in calendar days):1234D1=LAST(CLOSE());TIMESINCESIGNAL(CLOSE()<D1, UNIT=Days)
To show the date use the following BARDATE() function, and set the watchlist Column Type to Date:12345D1=LAST(CLOSE());D2=TIMESINCESIGNAL(CLOSE()<D1, UNIT=Days);LAST(BARDATE())-D2
You must be logged in to reply to this topic.