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:September 7, 2018 at 6:16 am #48087
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
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())-D2May 29, 2020 at 10:54 am #58335Dean
- Topics: 1
- Replies: 2
- Posts: 3
Thanks everyone at Optuma for all of the help with the scripting. These forums really are incredibly useful.
I have a basic script where i am looking for ADX > 14 and DM+ > DM-. I am trying to create a TimeSinceSignal script to return the number of days since these conditions were met. Strangely the script is returning 0 for all stocks where the signal is currently live, and a value for stocks where it is not.
I see the logic in that the script is returning the number of days since the signal ended, how would I correct this to return the number of days since it was triggered?
DeanMay 29, 2020 at 9:56 pm #58337
I think the issue is that you may be counting since the condition was last true – not how long it has been true, hence the zeroes for those where currently true. It’s a bit confusing, but instead you need to count how long it’s been since the condition was last false. Remember that a value of 1 is true and 0 is false, so this will show how long it has been true by counting the days since it was false:12345V1=ADX()>14 and (ADX(DEFAULT=DMPlus) > ADX(DEFAULT=DMMinus));//How long since V1 was last false?;TIMESINCESIGNAL(V1==0)
This list of FTSE100 stocks shows when the V1 condition is true in blue on the chart, with the green showing the time counts. So OCDO has been positive for 51 days, whereas the ADX False column shows how long it has been false (in other words how long since it’s been true) i.e. 28 days for NG. Hope that makes sense!
- This reply was modified 10 months ago by Darren.
1 user thanked author for this post.
- You must be logged in to reply to this topic.