Multiple RSI Signals followed by Moving Avg Crossover


0
0

Good morning Hahn,

Hope all is well. I was wondering if you could help me out or lead me in the right direction.  In ToS Scan, I would like to have a scan alert that I could set to my phone for the following situation.

For example,

If RSI signals Oversold 2 or more times within the following 5 bars, then the Moving Average (9) crosses above the Moving Average (18), send an alert text/plot arrow up

I would like to be able to adjust the following inputs above:

RSI Oversold

RSI Overbought

min number of times = 2 (shown in example)

length of time for the signal = 5 (shown in example)

moving avg. #1 = 9 (shown in example)

moving avg. #2 =  (shown in example)

 

 

Thanks,

Robin Hood

Marked as spam
Posted by (Questions: 2, Answers: 3)
Asked on April 5, 2017 5:33 am
888 views
0
Private answer

I think this will do the trick. I’m logged into PaperMoney side today and the scans are only available on the real money side. So I could not test it.

input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input oversoldHits = 2;
input signalLength = 5;
input maLength1 = 9;
input maLength2 = 18;
def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def ma1 = Average(close, maLength1);
def ma2 = Average(close, maLength2);
def rsiOversold = RSI < over_Sold; def sumOfOversold = Sum(rsiOversold, signalLength); def trigger = ma1[1] < ma2[1] and ma1 > ma2 and sumOfOversold >= oversoldHits;
plot scan = trigger;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on April 5, 2017 9:07 am
0

It worked but I need to make a slight change & not sure how to do it. RSI oversold hits needs to be changed to ”rsiOneBarCrossAbove” hits as shown in the code below.

=============
declare lower;

input length = 14;
input over_Bought = 80;
input over_Sold = 20;
input price = close;
input averageType = AverageType.WILDERS;

def NetChgAvg = MovingAverage(averageType, price – price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price – price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
OverSold.SetDefaultColor(Color.CYAN);
OverSold.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

plot OverBought = over_Bought;
OverBought.SetDefaultColor(Color.MAGENTA);
OverBought.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

RSI.DefineColor(”OverBought”, GetColor(5));
RSI.DefineColor(”Normal”, GetColor(7));
RSI.DefineColor(”OverSold”, GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.Color(”OverBought”) else if RSI < over_Sold then RSI.Color("OverSold") else RSI.Color("Normal")); OverSold.SetDefaultColor(GetColor(8)); OverBought.SetDefaultColor(GetColor(8)); def rsiOneBarCrossAbove = RSI[1] > 20 and RSI < 20; plot scanUp = rsiOneBarCrossAbove; scanUp.SetDefaultColor(Color.CYAN); scanUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); def rsiOneBarCrossBelow = RSI[1] < 80 and RSI > 80;

plot scanDown = rsiOneBarCrossBelow;
scanDown.SetDefaultColor(Color.MAGENTA);
scanDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

Alert(rsiOneBarCrossAbove, ”RSI Cross Above 80”, Alert.BAR, Sound.Ring);
Alert(rsiOneBarCrossBelow, ”RSI Cross Below 20”, Alert.BAR, Sound.Ring);

( at April 9, 2017 11:15 pm)
0

Couple things about your original request and the code you have included in the comment above.

First is that you are asking for this to send alerts to your phone. The code you have posted here is for a chart study. Not a scan. If you want to send alerts to your phone you must build this as a scan and not a custom chart study.

Second is that your original specification states the following:

”If RSI signals Oversold 2 or more times within the following 5 bars, then the Moving Average (9) crosses above the Moving Average (18), send an alert text/plot arrow up”

In order to modify the code to achieve some different specification you need to clearly state what that new specification is. So please explain what has changed from your original specification that I have quoted here.

( at April 10, 2017 7:30 am)
0

Hi Hahn,

Correct me if I’m wrong, I believe there is a difference between the current RSI oversold hits & (RSIcrossAbove>20 in 1bar) scan shown in my scan pasted in the previous comment. Instead of ”oversold hits”, I want the hits to be the number of times RSI crosses from below 20 to above 20. Here’s my example showing 2 hits within 4 ticks (i.e. If RSI is at 18 then is at 22 at the next bar = 1 hit, next bar RSI goes from 22 to 21=0 hits, next bar RSI goes from 21 to 19=0 hits, next bar RSI goes from 19 to 23= 1 hit (2nd hit).

If this code seems familiar, you helped make it.
==========
def rsiOneBarCrossAbove = RSI[1] > 20 and RSI < 20; plot scanUp = rsiOneBarCrossAbove; scanUp.SetDefaultColor(Color.CYAN); scanUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); def rsiOneBarCrossBelow = RSI[1] < 80 and RSI > 80;
plot scanDown = rsiOneBarCrossBelow;
scanDown.SetDefaultColor(Color.MAGENTA);
scanDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Alert(rsiOneBarCrossAbove, ”RSI Cross Above 20”, Alert.BAR, Sound.Ring);
Alert(rsiOneBarCrossBelow, ”RSI Cross Below 80”, Alert.BAR, Sound.Ring);

( at April 10, 2017 9:39 pm)
0
Private answer

Ok, given the clarified specification I have an updated version of the code I provided previously. The original version merely counted how many bars the RSI was in Oversold state. The new version counts how many time the RSI crosses from above Oversold to below Oversold

Once again, this is setup as a scan and not a lower study to keep things within the context of the original request to send text alert to a phone. A chart study is not able to send text alerts. You must use this code in a Study Alert or a Scan in order to enable text alerts for this code.

iinput length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input oversoldHits = 2;
input signalLength = 5;
input maLength1 = 9;
input maLength2 = 18;
def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def ma1 = Average(close, maLength1);
def ma2 = Average(close, maLength2);
#def rsiOversold = RSI < over_Sold; def rsiCrossesOversold = RSI[1] > over_Sold and RSI < over_Sold;
#def sumOfOversold = Sum(rsiOversold, signalLength);
def sumOfOversold = Sum(rsiCrossesOversold, signalLength);
def trigger = ma1[1] < ma2[1] and ma1 > ma2 and sumOfOversold >= oversoldHits;
plot scan = trigger;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on April 11, 2017 7:30 am
0

Hey Hahn,

I added the following code below to the end of the scan in order to see where the price is when the trigger hit, but my charts aren’t showing anything. Any help?

scan.SetDefaultColor(Color.ORANGE);
scan.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

( at April 13, 2017 12:48 am)
0

The two conditions you have specified for a signal should be extremely rare. You have two RSI crosses of the oversold inside of 5 bars, combined with a crossing of two moving averages. I am not surprised you cannot find any signals. If you have an example (ticker symbol and time frame) I will take a look. But I think the most helpful method would be for you to debug your conditions by separating them and plotting them individually:

plot condition1 = sumOfOversold >= oversoldHits ;
plot condition2 = ma1[1] < ma2[1] and ma1 > ma2;

For example (AAPL / Daily / 11-28-16), using this technique I found both conditions align for a combined signal ONLY AFTER reducing oversoldHits to 1 and increasing signalLength to 10.

( at April 13, 2017 7:34 am)
0

Hey Hahn,

If I wanted to add an autobuy for when my ”scan > value:1” (shown above: plot scan = trigger) in my strategy, how would it look like?

Thanks again!

( at May 17, 2017 4:22 am)
0

in general, the code required in the scan engine is identical to the code required to create Study Alerts and Conditional Orders. So as long as it works in the scan you should give it a try in the Study Alert, to test it first. There are some limitations that may prevent the code from working but I have not taken the time to test it or look for potential issues.

( at May 17, 2017 8:07 am)
0

Hi Pete,

I’ve tried multiple variations of the autobuy below, but there isn’t activity showing in my P/L strategy.
AddOrder(OrderType.BUY_AUTO, scan, open, tradeSize, Color.BLUE, Color.LIGHT_GREEN);
AddOrder(OrderType.BUY_AUTO, trigger, open, tradeSize, Color.BLUE, Color.LIGHT_GREEN);

For your reference, the scan works in SCAN tab when I put ”SCAN is greater than (VALUE): 0”

( at May 17, 2017 9:51 am)
0

A strategy requires both buys and sells. You only have one side of the strategy there. Both AddOrder statements are set to BUY_AUTO. You need an opposing signal and use that with an AddOrder statement set to SELL_AUTO. You may want to go back and watch our strategy videos again. We also have a video demonstrating how to convert between scans and strategies. https://www.hahn-tech.com/thinkorswim-scan-to-strategy/

( at May 17, 2017 9:59 am)
0

Thanks for the tidbit. Must have missed that key detail that a strategy needs a buy & sell auto. Enjoy the donations for your troubles.

( at May 17, 2017 1:58 pm)
0

Thank you, that was very generous. Very much appreciated.

( at May 17, 2017 2:04 pm)