Scan with OR conditions

N

nervo88

New member
Hello All !

I'm new for scripts in the scanner.

1. I'm trying to create a scan with two conditions but with the OR option:

EMA 9 crosses 50 EMA , price closed

OR

EMA 9 cross 200 EMA , price closed

2. Is there an option to add conditions like: EMA9 very close to 50 EMA (like in % maybe? ) so it will almost indicate a cross. ofcorse not a 100% , but still, is this possible?


Thank you for your help !
 
BenTen

BenTen

Administrative
Staff
VIP
Here you go:

Code:
MovAvgExponential()."AvgExp" crosses MovAvgExponential("length" = 50)."AvgExp" or MovAvgExponential()."AvgExp" crosses MovAvgExponential("length" = 200)."AvgExp"
 
N

nervo88

New member
Thank you BenTen !!!
One little question:

MovAvgExponential()."AvgExp" crosses...

Which AvgExp does it reffers to ? how does it know which AvgExp i want to cross the 50?

because after that I see you wrote length=50

And: Is there an option so the cross will be only above the 50 and 200 ?

Thanks again !!!
 
Last edited:
J

JabronLames

Member
VIP
Hi Nervo,

I wrote something similar to what you are looking for the for ATR Trailing Stop. It's not too complicated, but it gets the job done in my watchlist and helps me enter trades with minimal losses. It merely gets the distance in a % between the price and the trailing stop.

*Just thought I would share

Code:
# By Dan Dude
# Get distance to ATR Trailing Stop
# Copyright forever
input price = close;

# 1 Reference Trailing Spot in TOS
def ATR = reference ATRTrailingStop();

# 2 Get the distance from PRICE to trailing stop => Sq the number and get the Sqrt to avoid pesky negatives
def distanceToStop = (Sqrt(Sqr(price / ATR - 1))) * 100;

# 3 assign a background color based on my tinkering
AssignBackgroundColor(if distanceToStop >= .35 then color.dark_red else if distanceToStop < .35 then color.dark_green else color.current);

# 4 add a label
AddLabel(yes, asPercent(round(distanceToStop,2)));

I'll see what I can do to modify this into a EMA expression for you

Let me know if this is a step in the right direction.

**All this will do is add a label for the distance between the moving averages on your chart. Is there a specific distance between the averages you are targeting? If so, I can set the color of the label so that it changes when it meets the criteria.

Hope this helps!

Code:
# Dan Dude

# By Dan Dude
# Get distance Between Moving Averages
# Copyright forever


input price = close;
input EMALength50 = 50;
input EMALength200 = 200;

def EMASlow = MovingAverage(AverageType.Exponential, price, EMALength200);

def EMAFast = MovingAverage(AverageType.Exponential, price, EMALength50);


def DistanceBetween = (Sqrt(Sqr(EMASlow / EMAFast))) / 100;


#AssignBackgroundColor(if DistanceBetween >= 2 then color.dark_red else if DistanceBetween < 2 then color.dark_green else color.current);

AddLabel(yes, "Distance Between Averages =  " + asPercent(round(DistanceBetween,4)));
 
Last edited by a moderator:
L

Learnbot

Member
VIP
@JabronLames, quick question on the code for ATR if I may please. I am trying to test my strategy, so lets say my strategy generated a BUY signal, what I want to do is (if possible) is TOS to get the ATR value at that BUY signal and multiply that number by 4 and that would be my EXIT signal. is this possible? I am trying to figure out with "getvalue" function in TOS but i dont understand that function that well
 
J

JabronLames

Member
VIP
@Learnbot,

The line def ATR = reference ATRTrailingStop(); assigns the current price value(y-axis on chart) of the ATR to the variable(def).

You can certainly create a new variable that multiples this by 4, def ATRx4 = ATR * 4; but this would cause your exit to be 400% of the current price of the trailing stop. I don't think this is what you are trying to accomplish.

Can you show an example of a setup on a chart? It may help me understand what you are trying to do.

*PS - If you are setting your limit sells at 400% gain we should be best friends =)

I'm here to help
 
Last edited:
L

Learnbot

Member
VIP
@Learnbot lol dude 400% gains, I would quit my day job tomorrow! lol I am new with TOS so it was my error. So here is what I am trying to do is let's say I bought 1 stock is of COHN at $18.59 and at that time ATR is 1.31. So what I am trying to do is have a stop loss to be Stock price - (ATR*2), so in this case, it would be 18.59-2.62 = 15.97 and Exit price would be Stock price + (ATR*4) = 18.59+5.42 = 23.83.


Hope this makes sense, the goal basically is to test my strategy. I have entry signals just not exit so I am trying to exit based on % profit.

thanks again for your time.
 
J

JabronLames

Member
VIP
Ah, got it. You want the distance represented in $ instead of a percent.

If you're looking for the label on the chart to change color (i.e. GREEN to RED), that's easy peazy. But the ATR is not a static value, it follows the price, so the x4 would zig-zag around and your exit signal would change every candle...

One thing I'm not sure of how to do is reference the market price of the most recent filled order?... Perhaps @BenTen would be able to better answer that question for you, as it is a bit out of my league.

I'm happy to help with this more later, but the market is opening and I got moves to make 💪💪

The script below will change the label to a $.

Code:
input price = close;
input Labels = "Yes";
def ATR = reference ATRTrailingStop();
def distanceToStopInDollars=  (Sqrt(Sqr(price - ATR)));

def ATRx4 = distanceToStopInDollars * 4; # <--- This will multiple the spot by 4

AddLabel(distanceToStopInDollars and yes, "ATR Dist. = $" + asText(round(distanceToStopInDollars,2)), color.light_green);
 
L

Learnbot

Member
VIP
@JabronLames dude much appreciated, I will try and figure this out as well Using your provided code. Thank you for your time man
 
N

nervo88

New member
Here you go:

Code:
MovAvgExponential()."AvgExp" crosses MovAvgExponential("length" = 50)."AvgExp" or MovAvgExponential()."AvgExp" crosses MovAvgExponential("length" = 200)."AvgExp"
@JabronLames dude much appreciated, I will try and figure this out as well Using your provided code. Thank you for your time man
so i noticed that when i search for a cross it shows me also:
1. crosses that happened in pre market
2. cross below and not above

i tried also to add a limit of 5 bars ago but still the same
it happened also with the TOS built in script: MovingAvgCrossover

what you think about that ? can it be solved ?
 

Similar threads

Top