Price within X% of SMA


Category:
0
0

Hey Pete, hope all is well. I found some code (see below) and tried making it into an MTF lower study that would spike when price is within 5% of a 50 SMA. It seems to only show a fraction of the 50 SMA interactions. I think the “def = status” line is goofed up — like it’s saying that price has to actually be below the SMA?? Maybe there’s a better way of scripting “price within 5%?” Any assistance is greatly appreciated.

-Dixon

declare lower;

input Period = AggregationPeriod.FIFTEEN_MIN;
input percent = 0.05;
input length = 50;

def SMA = Average(close(period = period), length);
def Status = absvalue(close – SMA) is less than or equal to percent;

plot Proximity = if Status then 1 else 0;

Marked as spam
Posted by (Questions: 2, Answers: 4)
Asked on April 13, 2020 7:21 pm
208 views
0
Private answer

Here is how I would compute that:

declare lower;
input timeFrame = AggregationPeriod.FIFTEEN_MIN;
input xPercent = 5.0;
input length = 50;
def maOne = Average(close(Period = timeFrame), length);
plot signal = close > maOne * (1 - xPercent * 0.01) and close < maOne * (1 + xPercent * 0.01);

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on April 13, 2020 8:25 pm
0
Yeah, I see what you mean. I thought the below, revised script was going to work (it seemed to work via calculator), but it's not producing any results. Maybe after another cup of coffee... Ideas? declare lower; input Period = AggregationPeriod.FIFTEEN_MIN; input Percent = 0.05; input Length = 50; def SMA = Average(close(Period = Period), Length); def Value = ((close - SMA) / SMA); def Status = Value
( at April 14, 2020 7:01 am)
0
I updated my answer to include my solution to the request.
( at April 14, 2020 8:15 am)
0
Awesome Pete, thanks! As a heads-up, I wasn't getting good results with xPercent set to 5.0 (500%??), but it works like a charm at .05. Thank you, Sir.
( at April 14, 2020 8:44 am)
0
The input value of 5.0 is indeed 5%, and is converted to the decimal equivalent later on in the code by multiplying it by 0.01 (5.0 * 0.01 = 0.05). You did say you wanted 5%. But if you enter a value of 0.05 you are actually applying 0.05% to the moving average.
( at April 14, 2020 8:50 am)
0
Gotcha. Another impact to my results was the price of the stock/index. 5% of a $100 stock vs. a $2700 index will produce very different results and the /ES was always spiked with a 5% setting. I'll just adjust the % based on the value of stock/index. Thanks for explaining, Pete!
( at April 14, 2020 9:20 am)
0
Fixed percentages and other values are always problematic. I suggest you explore the concept of applying the ATR value to your computation so that it can automatically adjust to stocks of various price levels.
( at April 14, 2020 10:37 am)
0
That's a good idea, I'll have to look into that. For now I just knocked the indices down to xPercent .04 - 1.0 and am in the process of figuring out the equities values. Thanks again.
( at April 14, 2020 11:29 am)