Conversion indicador Tradingview

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #228566 quote
    nautilus23nautilus23
    Participant
    Average

    Solicito, si es posible, convertir el indicador adjunto.

    Dicho indicador procede de Tradingview y muestra en la esquina inferior derecha del gráfico la tasa de crecimiento y la tasa de crecimiento anual compuesta (CAGR) entre la apertura de la primera barra visible del gráfico y el cierre de su última barra. A medida que hace zoom o se desplaza por el gráfico, los cálculos se ajustarán a las barras del gráfico.

    Adjunto gráfico y código:

    https://www.tradingview.com/script/xbwxRNY6-Chart-CAGR/

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

    // © TradingView

     

    //@version=5

    indicator(“Chart CAGR”, overlay = true)

     

    // Chart CAGR

    // v1, 2022.10.09

     

    // This code was written using the recommendations from the Pine Script™ User Manual’s Style Guide:

    //   https://www.tradingview.com/pine-script-docs/en/v5/writing/Style_guide.html

     

    import PineCoders/VisibleChart/1 as chart

    import TradingView/ta/2 as ta

     

    //#region ———————————————————— Constants and Inputs

     

    // ————— Constants

    color   GRAY    = color.silver

    color   NOCOLOR = #FFFFFF00

     

    // ————— Inputs

    string  infoBoxSizeInput     = input.string(“normal”, “Size ”, inline = “display”, options = [“tiny”, “small”, “normal”, “large”, “huge”, “auto”])

    string  infoBoxYPosInput     = input.string(“bottom”, “↕”,     inline = “display”, options = [“top”, “middle”, “bottom”])

    string  infoBoxXPosInput     = input.string(“right”,  “↔”,     inline = “display”, options = [“left”, “center”, “right”])

    color   infoBoxColorInput    = input.color(NOCOLOR,   “”,      inline = “display”)

    color   infoBoxTxtColorInput = input.color(GRAY,      “T”,     inline = “display”)

    //#endregion

     

    //#region ———————————————————— Functions

     

    //@function         Calculates the growth rate as a percent from the exitPrice relative to the entryPrice.

    //@param entryPrice (series float) The entry price.

    //@param exitPrice  (series float) The exit price.

    //@returns          (float) The percent growth rate.

    growthRate(series float entryPrice, series float exitPrice) =>

    float result = (exitPrice / entryPrice – 1) * 100

    //#endregion

     

    //#region ———————————————————— Calculations

     

    float firstChartBarOpen = chart.open()

    float lastChartBarClose = chart.close()

    int   firstChartBarTime = chart.left_visible_bar_time

    int   lastChartBarTime  = chart.right_visible_bar_time

    float cagr              = ta.cagr(firstChartBarTime, firstChartBarOpen, lastChartBarTime, lastChartBarClose)

    float gr                = growthRate(firstChartBarOpen, lastChartBarClose)

    //#endregion

     

    //#region ———————————————————— Visuals

     

    var table infoBox = table.new(infoBoxYPosInput + “_” + infoBoxXPosInput, 2, 3)

    if barstate.isfirst

    table.cell(infoBox, 0, 0, “Growth rate:”, text_color = infoBoxTxtColorInput, text_size = infoBoxSizeInput, bgcolor = infoBoxColorInput)

    table.cell(infoBox, 0, 1, “CAGR:”,        text_color = infoBoxTxtColorInput, text_size = infoBoxSizeInput, bgcolor = infoBoxColorInput)

    table.cell(infoBox, 1, 0, “”,             text_color = infoBoxTxtColorInput, text_size = infoBoxSizeInput, bgcolor = infoBoxColorInput)

    table.cell(infoBox, 1, 1, “”,             text_color = infoBoxTxtColorInput, text_size = infoBoxSizeInput, bgcolor = infoBoxColorInput)

    else if barstate.islast

    table.cell_set_text(infoBox, 1, 0, str.tostring(gr,   format.percent))

    table.cell_set_text(infoBox, 1, 1, str.tostring(cagr, format.percent))

    //#endregion

    xxx22.02.2024_12.55.25_REC.png xxx22.02.2024_12.55.25_REC.png
    #228775 quote
    Iván GonzálezIván González
    Moderator
    Legend

    Hola
    Aquí tienes un código que calcula el rendimiento según las barras cargadas en el gráfico para gráficos diarios, semanales o anuales.
    https://www.prorealcode.com/prorealtime-indicators/cagr-compound-annual-growth-rate/Si quieres que haga el cálculo para un timeframe menor habría que seguir el mismo procedimiento que he hecho pero introduciendo nuevas líneas para calcular n.

    defparam drawonlastbaronly = true
    
    tm = gettimeframe
    //month = 2592000
    //weekly = 604800
    //daily = 86400
    
    if tm = 2592000 then
    n = 12
    elsif tm = 604800 then
    n = 52
    elsif tm = 86400 then
    n = 252
    endif
    
    period = barindex 
    FinValue = close
    IniValue = close[period]
    if barindex < n then
    CAGR = undefined
    else
    CAGR = (POW((FinValue/IniValue),(n/period))-1)*100
    GR = (FinValue / IniValue - 1) * 100
    endif
    
    drawtext("CAGR: #CAGR# %",-100,+80)anchor(BOTTOMRIGHT ,XSHIFT ,YSHIFT )
    drawtext("Growth rate: #GR# %",-100,100)anchor(BOTTOMRIGHT ,XSHIFT ,YSHIFT )
    
    return
    nautilus23 thanked this post
    #228930 quote
    nautilus23nautilus23
    Participant
    Average
    Muchas gracias Iván.
    El indicador funciona perfectamente en las temporalidades mensual semanal y diaria. Lo único que he observado respecto del indicador de la pagina https://www.tradingview.com/script/xbwxRNY6-Chart-CAGR/  es que allí se dice que A medida que hace zoom o se desplaza por el gráfico, los cálculos se ajustarán a las barras del gráfico.
    Lo he probado y a mi no me funciona, si se puede corregir aumentaría mucho su funcionalidad (Desconozco si seria posible hacerlo).
    De todas formas a mi me sirve tal y como está.
    Extracto de la pagina de antes mencionada:
    This simple script displays in the lower-right corner of the chart the Growth Rate and the Compound Annual Growth Rate (​CAGR) between the open of the chart’s first visible bar and the close of its last bar. As you zoom/scroll the chart, calculations will adjust to the chart’s bars

    Este sencillo script muestra en la esquina inferior derecha del gráfico la tasa de crecimiento y la tasa de crecimiento anual compuesta (​CAGR) entre la apertura de la primera barra visible del gráfico y el cierre de su última barra. A medida que hace zoom o se desplaza por el gráfico, los cálculos se ajustarán a las barras del gráfico.

     

    Muy agradecido por tu aportación.

    Un saludo.

     

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Conversion indicador Tradingview


ProBuilder: Indicadores y Herramientas

New Reply
Author
author-avatar
nautilus23 @nautilus23 Participant
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by nautilus23nautilus23
2 years, 7 months ago.

Topic Details
Forum: ProBuilder: Indicadores y Herramientas
Language: Spanish
Started: 02/22/2024
Status: Active
Attachments: 1 files
ProRealCode ProRealCode
Loading...