Plot a box for average daily price movement?

martinflds

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

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

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);
 
E

Emanuel5162

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

input prilength = 30;
input seclength = 100;

Whats the difference? when you change them in thinkscript?

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

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
 
C

cswu1211

New member
VIP
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);
could you show a Graph of this study? I didn't get anything on my chart, tks
 
C

cswu1211

New member
VIP
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);
Could you show an example Graph. I didn't get anything on the chart, anything wrong? tks.
 

Similar threads

Top