Conversion indicador Tradingview

Forums ProRealTime foro Español Soporte ProBuilder Conversion indicador Tradingview

Viewing 3 posts - 1 through 3 (of 3 total)
  • #228566

    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

    #228775

    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.
    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.

    1 user thanked author for this post.
    #228930
    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)

Create your free account now and post your request to benefit from the help of the community
Register or Login