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:15 am #48083
I cut and pasted the script and the last line shows as an error. The rest was all valid. I have
IF(r2 == 1,r1,r3)
Is that correct? Thank you,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)September 7, 2018 at 6:25 am #48111
Yes that’s correct – not sure why you are getting the error. I’ve attached a workbook that contains the scripts. Let us know how you go.
You must be logged in to access attached files.September 7, 2018 at 10:22 pm #48143
Darren, the workbook is working. The script manager page is attached. I cannot understand it and maybe it is just my version of Windows creating a problem?
You must be logged in to access attached files.September 8, 2018 at 12:29 am #48155
Thanks Michael. Strange – when you copied the text of the script there was a formatting issue which replaced the ‘>’ with ‘>’ in variable r2, which I think is causing the issue. What version of Windows are you using?
If you paste to Notepad first to remove formatting, and then copy from Notepad to the Optuma script editor window does it then work?September 8, 2018 at 12:37 am #48159
that fixed it
Thank you. It is Windows Home 10, 64-bit machine. I ran it through notepad and that fixed it. Will do that in the future.September 8, 2018 at 12:43 am #48163
Hmm that’s the same operating system as I’m using, so will investigate what else it could be.
You must be logged in to reply to this topic.