• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

Plot a box for average daily price movement?

martinflds

New member
Good Day,

This is my first post as well as "attempt" at scripting for TOS or any kind for that matter. Most of my coding is from looking at other peoples code and lines from the TOS code and trying to plug and play. For those of you whom know more than I, (probably all of you)would you be so kind as to take a look at this and give me some feedback. Additionally once all is good I can repost the completed code and the link so others can use.

What I am trying to do here is place a box on my chart that displays the average daily price movement of a stock over 30 (or X# )days. Additionally I would like another with the % of the current day's movement in compared to that 30 Day Avg. Ultimately I would like to be able to look as I am scanning stocks to determine if the daily average price movement would interest further investigation. Say I am looking at great gapping stock but it is 200% over the daily price movement I may want to note that but look for something that has a little more room on the daily average for profit, until I become a little more competent with trading.

Issues:
The box does show up with avg well as a % box before the market is open, however once in premarket/ open it will only work with some stocks. Additionally no % of the daily avg shows in the box.

I would like to make it adjustable by time frame. Below is set for 30 days specifically and in order to use a higher period of time I would needs to manually enter the days. Initially I tried "def avg= average (price, length/ length) ;" "(then using input length = 30;" however it came up with a wildly different number I believe higher by $3 on the QQQ's for todays date. With my basic lvl math equation writing in script (which is basically none) I couldn't figure it out.

Code:
# Name Daily_$_Movement_Indicator
# Version 1.0
# Created by: Joey
# Created: 04/26/20

#Inputs
input DayAvg = yes;
input PercentOfDay = yes;

#Price Data
def price = Open("day")-close("day");
def avg = (price[1] + price[2] + price[3] + price[4] + price[5] + price[6] + price[7] + price[8] + price[9] + price[10] + price[11] + price[12] + price[13] + price[14] + price[15] + price[16] + price[17] + price[18] + price[19] + price[20] + price[21] + price[22] + price[23] + price[24] + price[25] + price[26] + price[27] + price[28] + price[29] + price[30]) / 30;
def PercentOfDay = Round((price / avg) * 100);

# Labels
AddLabel(DayAvg, "Avg Price Mvt:" + round(avg, 2), Color.LIGHT_GRAY);
AddLabel(PercentOfDay, "% of Avg:" + ("%"), Color.YELLOW);


Long Post but Thanks!
 
Last edited by a moderator:

martinflds

New member
I believe I have corrected the price issue as I had not assigned a period for the price to be biased off. This seem to work well, however I am still unable to get make it adjustable to a greater length w/o creating more "price[#]" boxes in the "def avg" formula. I also made an adjustment to the color for the % label so that once the stock rose above its daily price movement it would turn red otherwise yellow (may change to gray).

Thoughts?

Code:
# Box Daily_$_Movement Indicator
# Version 1.4
# Creator: Joey
# Updated: 04/28/2020

# Note: Color change of the % of daily avg is redundant. If then % is > than 100% then the stock has already moved more than the daily avg.

declare upper;
#Inputs
input ShowDayAvg= yes;
input ShowPercentDay= yes;

#Price Data
def open = open(period = AggregationPeriod.DAY);
def close = close(period = AggregationPeriod.DAY);
def price = AbsValue(open - close);#exclude current day
def lastclose = close(pricetype.last);
def secprice = (open - lastclose);
def avg = (price[1] + price[2] + price[3] + price[4] + price[5] + price[6] + price[7] + price[8] + price[9] + price[10] + price[11] + price[12] + price[13] + price[14] + price[15] + price[16] + price[17] + price[18] + price[19] + price[20] + price[21] + price[22] + price[23] + price[24] + price[25] + price[26] + price[27] + price[28] + price[29] + price[30]) / 30;
def PercentOfDay = Round((secprice / avg) * 100, 0);

# Labels
AddLabel(ShowDayAvg, "Avg Price Mvt:" + "$" + Round(avg, 2), Color.LIGHT_GRAY);
AddLabel(ShowPercentDay,"% of Avg:" + (PercentOfDay)+("%"), color.yellow);
 
Last edited:

martinflds

New member
I finally figured out all the issues and completed the Code, link below, code at bottom.

https://tos.mx/dhKicNX

This will populate three labels on your chart.
1) This Label shows the calculated daily average movement in price of a stock. (user define period of time)
2) This Label shows compares today's price movement to the average movement and is displayed as a percent.
3) This label shows the average amount of a single bar (user defined period) over a (user defined length).

Note labels will not show up on higher chart periods if it is set to a lower time frame.

Let me know your thought please, this is my first time taking it up.

Thanks

Code:
# Name Daily_Price_Indicators
# Version 2.0
# Created by: Joey
# Created: 04/29/20

#Inputs
input showDayAvg = yes;
input showPercentOfDay = yes;
input showBarAvg = yes;
input prilength = 30;
input seclength = 100;
input aggregationperiod = AggregationPeriod.TWO_MIN;
#aggregation period =current chart/ desired period; lower periods may not show up on higher period charts

# Name Daily_Price_Indicators
# Version 2.0
# Created by: Joey
# Created: 04/29/20

#Inputs
input showDayAvg = yes;
input showPercentOfDay = yes;
input showBarAvg = yes;
input prilength = 30;
input seclength = 100;
input aggregationperiod = AggregationPeriod.TWO_MIN;
#aggregation period =current chart/ desired period; lower periods may not show up on higher period charts

#Define Avg price: Calculates the "range" or total movement in price of a stock in one day over a "user defined" period of time. (Then range is diffrence between the high and low of the day.)
def a = high(period = AggregationPeriod.DAY);
def b = low(period = AggregationPeriod.DAY);
def price = round(absvalue((a - b)),3);
def avg = round(Average(price, prilength),2);

#Define PercentofDay: This compares the amount the stock has currently moved in the day to the avg range of a stock in a day. This is displayed as a % of the average range of the stock.
def priprice = open(period = aggregationperiod.day);
def lastprice = close(PriceType.MARK);
Def thirdprice= absvalue(open-close);
def percentofday = roundup(thirdprice / avg);

#Define BarAvg: This Box displays the average amount a single "user defined period" bar moves over a "user defined length of time" Warning: This label will not show up on higher frame charts. If you define a two_min period you need to be on that chart to see the label. 
def c = high(period = aggregationperiod);
def d = low(period = aggregationperiod);
def fourthprice = round(absvalue((c -d)),3);
def bar = round(Average(fourthprice, seclength),2);

# Labels: Shuffel these as you would like to see them on the chart
AddLabel(showDayAvg, "Avg Mvt:" + asdollars(avg), Color.LIGHT_GRAY);
AddLabel (showPercentOfDay, "% AVG:" + aspercent(percentofday), (if percentofday >= 100 then Color.YELLOW else if percentofday <= 0 then Color.LIGHT_GRAY else Color.LIGHT_GRAY) );
AddLabel (showBarAvg, "BarAvg:" + asdollars(bar), Color.LIGHT_GRAY);
 

Emanuel5162

New member
What does input sec length = 100; mean in your code?

Code:
input prilength = 30;
input seclength = 100;

What's the difference? when you change them in thinkscript?

This label shows the average amount of a single bar (user defined period) over a (user defined length).?
 
Last edited by a moderator:

amirfutures

New member
Hello, this is my first post here so would like to thank you in advance for sharing your knowledge.

Im trying to write a code that will plot the average daily price, starting at 9:30 am ET on the /ES 5 min chart for example.

The logic is as follows:

After the second bar is closed (at 9:40 am), the average will be AVG_1=(close+close[1])/2.
As the 3rd bar closes (at 9:45), the average will be AVG_2=(close+close[1]+close[1])/3 or AVG2=(AVG_1+close)/2...and so on
until the market closes. Restart the calculation on the next day at 9:30.

It's the same logic as VWAP, but no volume only price.

Thanks again for your assistance.

Amir
 

cswu1211

Member
VIP
@martinflds Could you show a Graph of this study? I didn't get anything on my chart, tks

ZxpR3Qc.png
 

ToddinMT

New member
Hello eveyone!

I am new to programming and thinkscript in general.

I am looking for a way to calculate the average daily price movement of a particular stock. I want to display in $.

I will put this into a label in the chart, which I know how to do.

Thanks!
 

dxk

New member
VIP
I found a study for "Plot a box for average daily price movement?"

https://usethinkscript.com/threads/plot-a-box-for-average-daily-price-movement.2297/

it works great, any one know how to do a scan in TOS for the "average daily price movement" similar to this study

I already tried a ATR scan for a $1 or more and it still shows stocks price moving less than a $1 and misses almost all the stocks the Plot Box is showing
including stocks I hold positions that move more than a dollar daily, can anyone show me a scan that will spot stocks that move more than a $1 daily or any price movement I'm looking for

Thanks ahead great group here, I've learn much wish I was a better coder
 

MerryDay

Well-known member
VIP
@dxk The script is just a label. It doesn't plot anything. There is not anything to scan for.
If you want to create a scan, you need to be able to state your request in mathematical terms.
IE: percentofday>100
By the way, if that is what you are looking for just change the beginning of the percentofday statement from 'def' to 'plot' and you will be able to run your scan.
 
  • Like
Reactions: dxk

dxk

New member
VIP
@dxk The script is just a label. It doesn't plot anything. There is not anything to scan for.
If you want to create a scan, you need to be able to state your request in mathematical terms.
IE: percentofday>100
By the way, if that is what you are looking for just change the beginning of the percentofday statement from 'def' to 'plot' and you will be able to run your scan.
thanks for your reply
I'm not looking for the percent but for the actual price movement, I suspect I need to do the same thing will give it a try
 

dxk

New member
VIP
Ok I finally converted the label scrip into a scan, thanks @MerryDay on how to do it, armed with a little knowledge is sometimes a little dangerous but I finally did it for a guy totally clueless about this code.

Also thanks to @martinflds for the code it works slick as a label on the chart and also finds the price movements better than the ATR

In case anyone wants to scan stocks for specific average price movements the code is below, I've come to the conclusion if I want see larger gains I needed to pick stocks that make decent size moves in greater than a dollar daily rather than in pennies daily.

To set the scan up pick your aggregation period
set it greater than or equal to
pick value and how much of a price movement I picked 1 I like to see my stocks make a dollar in movement or more daily
that's it

Code:
# Name Daily_Price_Indicators
# Version 2.0
# Created by: Joey
# Created: 04/29/20


#Inputs
input showDayAvg = yes;
#input showPercentOfDay = yes;
#input showBarAvg = yes;
input prilength = 30;
input seclength = 100;
input aggregationperiod = AggregationPeriod.TWO_MIN;
#aggregation period =current chart/ desired period; lower periods may not show up on higher period charts

#Def dayopen = 0800;

#Define Avg price: Calculates the "range" or total movement in price of a stock in one day over a "user defined" period of time. (Then range is diffrence between the high and low of the day.)
def a = high(period = AggregationPeriod.DAY);
def b = low(period = AggregationPeriod.DAY);
avg price = round(absvalue((a - b)),3);
plot avg = round(Average(price, prilength),2);

#Define PercentofDay: This compares the amount the stock has currently moved in the day to the avg range of a stock in a day. This is displayed as a % of the average range of the stock.
#def priprice = open(period = aggregationperiod.day);
#def lastprice = close(PriceType.MARK);
#Def thirdprice= absvalue(open-close);
#def percentofday = roundup(thirdprice / avg);

#Define BarAvg: This Box displays the average amount a single "user defined period" bar moves over a "user defined length of time" Warning: This label will not show up on higher frame charts. If you define a two_min period you need to be on that chart to see the label.
#def c = high(period = aggregationperiod);
#def d = low(period = aggregationperiod);
#def fourthprice = round(absvalue((c -d)),3);
#def bar = round(Average(fourthprice, seclength),2);

# Labels: Shuffel these as you would like to see them on the chart
#AddLabel(showDayAvg, "Avg Mvt:" + asdollars(avg), Color.LIGHT_GRAY);
#AddLabel (showPercentOfDay, "% AVG:" + aspercent(percentofday), (if percentofday >= 100 then Color.YELLOW else if percentofday <= 0 then Color.LIGHT_GRAY else Color.LIGHT_GRAY) );
#AddLabel (showBarAvg, "BarAvg:" + asdollars(bar), Color.LIGHT_GRAY);
 

Similar threads

Top