Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.0-flash-001· GoogleHugging Face LMArena Human Preference 140K

adjust this thinkscript line to be the current 60-candle average instead of the current…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Googlegemma-3-27b-it’s answer.
  1. User

    adjust this thinkscript line to be the current 60-candle average instead of the current price. It will be used to draw a horizontal line so it must NOT fluctuate across the chart:

    def lastBarPrice = if IsNaN(close[-1]) then close else Double.NaN;

  2. Gemini Assistant
    def sixtyCandleAverage = Average(close, 60);
    def lastBarAverage = if IsNaN(close[-1]) then sixtyCandleAverage else Double.NaN;
    

    Explanation:

    1. def sixtyCandleAverage = Average(close, 60);: This line calculates the simple moving average of the closing price (close) over the last 60 bars. This is the average we want to "freeze" and use for our horizontal line.

    2. def lastBarAverage = if IsNaN(close[-1]) then sixtyCandleAverage else Double.NaN;: This line checks if the closing price of the previous bar (close[-1]) is NaN (Not a Number). IsNaN(close[-1]) is typically true on the last historical bar of the chart (before the live data starts), and false on all other bars.

      • If IsNaN(close[-1]) is true (we are on the last historical bar), then lastBarAverage is set to sixtyCandleAverage which contains the 60-period average we calculated. This "freezes" the value.

      • If IsNaN(close[-1]) is false (we are not on the last historical bar), then lastBarAverage is set to Double.NaN. This makes the plot only draw on the last historical bar, thus producing a horizontal line.

    How it Works and Why it's Correct:

    • Fixed Value: The key is that sixtyCandleAverage is calculated on every bar. However, we ONLY assign that calculated value to lastBarAverage on the last historical bar. Once assigned, the value never changes because the condition IsNaN(close[-1]) will be false for all subsequent bars.
    • No Fluctuation: Because lastBarAverage only takes on the sixtyCandleAverage's value once, and then becomes Double.NaN everywhere else, the horizontal line will be plotted only on the last bar, and it won't fluctuate as the chart updates.
    • Horizontal Line: By setting the output to Double.NaN everywhere except the last historical bar, thinkscript interprets this as meaning there is nothing to plot except for that one point, thus creating a horizontal line.

    To Use This in Your Code:

    You would use lastBarAverage in a Plot statement, for example:

    def sixtyCandleAverage = Average(close, 60);
    def lastBarAverage = if IsNaN(close[-1]) then sixtyCandleAverage else Double.NaN;
    
    plot HorizontalLine = lastBarAverage;
    
    HorizontalLine.SetDefaultColor(Color.Green); // Example: Set the color
    HorizontalLine.SetStyle(Curve.SHORT_DASH);  // Example: Set the style
    

    Important Considerations:

    • Starting Point: The horizontal line will appear starting from the last historical bar loaded on your chart. If you only have a few bars of historical data, the line will be short. The more history you load, the further back the line will extend.
    • Data Availability: The 60-candle average requires 60 bars of data before it can be calculated. If your chart doesn't have at least 60 bars of historical data, the average will be incomplete or inaccurate.
    • Alternative Approach (Static Variable): You could also use a static variable. However, using IsNaN(close[-1]) is generally preferred because it's more reliable across different chart setups and doesn't rely on the user manually resetting the static variable if needed.
    #Alternate using static var. May be better for speed/calculation
    #static averageCalculated = no;
    #static averageValue = Double.NaN;
    
    #if !averageCalculated {
    #    averageValue = Average(close, 60);
    #    averageCalculated = yes;
    #}
    
    #plot StaticHorizontalLine = if averageCalculated then averageValue else Double.NaN;
    

    This alternative method calculates the average once, and only once. It's potentially more efficient, but the IsNaN(close[-1]) approach is generally more robust and easier to understand. Choose the method that best suits your needs and coding style.

  3. User

    In Thinkscript, how do I add a comment in the middle of a line

  4. Gemini Assistant

    In ThinkScript, you can add a comment in the middle of a line using either:

    1. // - Single-line comment: Everything after // on that line is ignored.
    2. /* ... */ - Multi-line comment: Anything between /* and */ is ignored, even if it spans multiple lines.

    Example using // (Single-line):

    def priceDifference = close - open; // Calculate the difference between close and open
    plot PriceChange = priceDifference; //Plot the data.
    
    

    Example using /* ... */ (Multi-line):

    def priceDifference = close /* get end of bar
    price versus beginning of bar */- open;
    plot PriceChange = priceDifference; /*and display in chart*/
    

    Important Notes:

    • //: This is the most common and generally preferred method for short comments. It's cleaner and easier to read.
    • /* ... */: Use this for longer comments that span multiple lines, or for commenting out blocks of code. It's also handy for placing a comment within an expression. However, be careful that the /* and */ are properly balanced.
    • Placement: Comments should be placed in a way that doesn't break the syntax of the ThinkScript code. For example, you can't put a comment inside a keyword or variable name. Also, placing them after then or else statements may result in undesired consequences.
    • Readability: Use comments liberally to explain what your code is doing, especially for complex calculations or logic. Well-commented code is much easier to understand and maintain.
    • Avoid Over-Commenting: Don't comment the obvious. For instance, a line saying def x = 5; // Assign 5 to x is redundant. Comments should explain why you're doing something, not just what you're doing.

    Example demonstrating an incorrect way to insert a comment

    #Incorrect example:
    #def closePrice = clos/*price */e; #Syntax error. Cannot insert comment in middle of variable or keyword.
    
    #incorrect example
    #def closePrice = close the/*price */n High; #invalid 'then' statement
    

    Use these commenting styles to make your ThinkScript code more understandable and maintainable.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.