Year to date (YTD)
#def allows you to teach ThinkScript new "words" that you can reference later in your code
#GetYYYYMMDD() returns today's date
#there are 252 trading days each year
def yearstart = getyear() * 10000 + 101;
def tradedays = countTradingDays(yearstart, GetYYYYMMDD());
def closeEOY = getvalue(close, tradedays, 252);
def YTDnetchange = ((close - closeEOY)/closeEOY) * 100;
#YTDnetchange could have been the plot line, however to view results with a limited number of decimal places that line was another def or definition for ThinkScript. And a new "word" value was used for the plot line in this code. 2 means round to 2 decimal places
plot value = round(YTDnetchange, 0);
#AddLabel tells ThinkScript how you want the results of your code diplayed. Yes means do show the label. Words or sybols in " " will appear with your label.
#This label could have been created complicated using concat function ... or the simplified method of using a + sign between values and text you want to appear in the label
AddLabel(yes, value + "% YTD", color.BLACK);
#any watchlist column label can be quickly converted into a chart label via copy/paste/tweak into a new chart Study or Strategy. HOWEVER AssignBackgroundColor on a chart label will change entire background of the chart when condition is met
AssignBackgroundColor(if value < 0
then Color.RED
else if value > 0
then Color.GREEN
else color.LIGHT_GRAY);
Month to date (MTD)
#def allows you to teach ThinkScript new "words" that you can reference later in your code
def MonthClose = if GetLastMonth() == GetMonth() then close(period = aggregationPeriod.Month)[1] else Double.NaN;
def MTDnetchange = ((close - MonthClose) / MonthClose) * 100;
#MTDnetchange could have been the plot line, however to view results with a limited number of decimal places that line was another def or definition for ThinkScript. And a new "word" value was used for the plot line in this code. 2 means round to 2 decimal places
plot valueMTD = round(MTDnetchange, 0);
#AddLabel tells ThinkScript how you want the results of your code diplayed. Yes means do show the label. Words or sybols in " " will appear with your label.
#This label could have been created complicated using concat function ... or the simplified method of using a + sign between values and text you want to appear in the label
AddLabel(yes, valueMTD + "% MTD", color.BLACK);
#any watchlist column label can be quickly converted into a chart label via copy/paste/tweak into a new chart Study or Strategy. HOWEVER AssignBackgroundColor on a chart label will change entire background of the chart when condition is met
AssignBackgroundColor(if valueMTD == 0
then Color.LIGHT_GRAY
else if valueMTD < 0
then Color.RED
else color.GREEN);
Week to date (WTD)
#def allows you to teach ThinkScript new "words" that you can reference later in your code
def WeekClose = if GetLastWeek() == GetWeek() then close(period = aggregationPeriod.Week)[1] else Double.NaN;
def WTDnetchange = ((close - WeekClose) / WeekClose) * 100;
#WTDnetchange could have been the plot line, however to view results with a limited number of decimal places that line was another def or definition for ThinkScript. And a new "word" value was used for the plot line in this code. 2 means round to 2 decimal places
plot valueWTD = round(WTDnetchange, 0);
#AddLabel tells ThinkScript how you want the results of your code diplayed. Yes means do show the label. Words or sybols in " " will appear with your label.
#This label could have been created complicated using concat function ... or the simplified method of using a + sign between values and text you want to appear in the label
AddLabel(yes, valueWTD + "% WTD", color.BLACK);
#any watchlist column label can be quickly converted into a chart label via copy/paste/tweak into a new chart Study or Strategy. HOWEVER AssignBackgroundColor on a chart label will change entire background of the chart when condition is met
AssignBackgroundColor(if valueWTD == 0
then Color.LIGHT_GRAY
else if valueWTD < 0
then Color.RED
else color.GREEN);
0 comments:
Post a Comment