Nhờ giúp đỡ về coding pinescript tradingview

Thảo luận trong 'Thư giãn' bắt đầu bởi namchum2006, 30/3/22.

  1. namchum2006

    namchum2006 Sonic the Hedgehog Lão Làng GVN

    Tham gia ngày:
    6/1/07
    Bài viết:
    4,781
    Nơi ở:
    Somewhere i belong
    Tình hình mình đang muốn combine 1 script như bên dưới nhưng lại không biết nó lỗi ở đâu. Anh em nào rành về vụ này xin giúp đỡ hứa có hậu tạ :)

    Mã:
    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    // © ekrem6nel
    
    //@version=5
    indicator('DCF Valuation')
    
    expectedGrowthRate = input(0.10)
    marginOfSafety = input(0.25)
    growthDeclineRate = input(0.05)
    year10FCFMultiplier = input(12)
    
    length = input.int(title='Beta Window', defval=300, minval=1)
    Rm = input.float(title='Rm', defval=0.06, minval=-100000, maxval=100000, step=0.01)
    src = input(title='Source', defval=close)
    financial_id = input(title='Rd Financial ID', defval='EF_INTEREST_DEBT')
    
    //WACC
    
    var float q1_preferedDiv = 0.0
    var float q2_preferedDiv = 0.0
    var float q3_preferedDiv = 0.0
    var float q4_preferedDiv = 0.0
    var float q1_taxes = 0.0
    var float q2_taxes = 0.0
    var float q3_taxes = 0.0
    var float q4_taxes = 0.0
    var float q1_preTaxIncome = 0.0
    var float q2_preTaxIncome = 0.0
    var float q3_preTaxIncome = 0.0
    var float q4_preTaxIncome = 0.0
    
    preferedDiv = request.financial(syminfo.tickerid, 'PREFERRED_DIVIDENDS', 'FY')
    taxes = request.financial(syminfo.tickerid, 'INCOME_TAX', 'FY')
    preTaxIncome = request.financial(syminfo.tickerid, 'PRETAX_INCOME', 'FY')
    TSO = request.financial(syminfo.tickerid, 'TOTAL_SHARES_OUTSTANDING', 'FY')
    totalLiabiliteis = request.financial(syminfo.tickerid, 'TOTAL_LIABILITIES', 'FY')
    preferedStock = request.financial(syminfo.tickerid, 'PREFERRED_STOCK_CARRYING_VALUE', 'FY')
    Rd = request.financial(syminfo.tickerid, 'EF_INTEREST_DEBT', 'FY')
    
    if preferedDiv != q1_preferedDiv
        q4_preferedDiv := q3_preferedDiv
        q3_preferedDiv := q2_preferedDiv
        q2_preferedDiv := q1_preferedDiv
        q1_preferedDiv := preferedDiv
        q1_preferedDiv
    
    if taxes != q1_taxes
        q4_taxes := q3_taxes
        q3_taxes := q2_taxes
        q2_taxes := q1_taxes
        q1_taxes := taxes
        q1_taxes
    
    if preTaxIncome != q1_preTaxIncome
        q4_preTaxIncome := q3_preTaxIncome
        q3_preTaxIncome := q2_preTaxIncome
        q2_preTaxIncome := q1_preTaxIncome
        q1_preTaxIncome := preTaxIncome
        q1_preTaxIncome
        
    preferedDiv_TTM = (q1_preferedDiv + q2_preferedDiv + q3_preferedDiv + q4_preferedDiv) * -1
    taxes_TTM = (q1_taxes + q2_taxes + q3_taxes + q4_taxes) * -1
    preTaxIncome_TTM = q1_preTaxIncome + q2_preTaxIncome + q3_preTaxIncome + q4_preTaxIncome
    
    mktCap = TSO * close
    E = mktCap
    D = totalLiabiliteis
    P = preferedStock
    
    // V - total value of capital
    V = E + D + P
    
    res = timeframe.period
    sym_2 = 'VN10Y'
    Rf = request.security(sym_2, res, src) / 100.0
    sym_1 = 'VNINDEX'
    ovr = request.security(sym_1, res, src)
    ret = (src - src[1]) / src
    retb = (ovr - ovr[1]) / ovr
    secd = ta.stdev(ret, length)
    mktd = ta.stdev(retb, length)
    Beta = ta.correlation(ret, retb, length) * secd / mktd
    B = Beta
    Re = Rf + B * (Rm - Rf)
    
    Rp = preferedDiv_TTM / preferedStock
    Rp := Rp ? Rp : 0
    T = taxes_TTM / preTaxIncome_TTM
    
    WACC = E / V * Re + D / V * Rd * (1 - T) + P / V * Rp
    //WACC
    
    discountRate = WACC
    
    npv(value, discountRate, numberOfPeriods) =>
        value / math.pow(1 + discountRate, numberOfPeriods)
    
    fcf(baseValue, growthRate, growthDeclineRate, numberOfPeriods) =>
        baseValue * (1 + growthRate * math.pow(1 - growthDeclineRate, numberOfPeriods - 1))
    
    freeCashFlow = request.financial(syminfo.tickerid, 'FREE_CASH_FLOW', 'FY')
    //expectedGrowthRate = security(syminfo.tickerid, "1W", ema(roc(ema(freeCashFlow, 200), 12), 50)/100)
    //expectedGrowthRate=financial(syminfo.tickerid, "SUSTAINABLE_GROWTH_RATE", "FQ")/100
    //plot(expectedGrowthRate)
    conservativeGrowthRate = expectedGrowthRate * (1 - marginOfSafety)
    
    year1FCF = freeCashFlow * (1 + conservativeGrowthRate)
    year2FCF = fcf(year1FCF, conservativeGrowthRate, growthDeclineRate, 2)
    year3FCF = fcf(year2FCF, conservativeGrowthRate, growthDeclineRate, 3)
    year4FCF = fcf(year3FCF, conservativeGrowthRate, growthDeclineRate, 4)
    year5FCF = fcf(year4FCF, conservativeGrowthRate, growthDeclineRate, 5)
    year6FCF = fcf(year5FCF, conservativeGrowthRate, growthDeclineRate, 6)
    year7FCF = fcf(year6FCF, conservativeGrowthRate, growthDeclineRate, 7)
    year8FCF = fcf(year7FCF, conservativeGrowthRate, growthDeclineRate, 8)
    year9FCF = fcf(year8FCF, conservativeGrowthRate, growthDeclineRate, 9)
    year10FCF = fcf(year9FCF, conservativeGrowthRate, growthDeclineRate, 10)
    
    year1NPVFCF = npv(year1FCF, discountRate, 1)
    year2NPVFCF = npv(year2FCF, discountRate, 2)
    year3NPVFCF = npv(year3FCF, discountRate, 3)
    year4NPVFCF = npv(year4FCF, discountRate, 4)
    year5NPVFCF = npv(year5FCF, discountRate, 5)
    year6NPVFCF = npv(year6FCF, discountRate, 6)
    year7NPVFCF = npv(year7FCF, discountRate, 7)
    year8NPVFCF = npv(year8FCF, discountRate, 8)
    year9NPVFCF = npv(year9FCF, discountRate, 9)
    year10NPVFCF = npv(year10FCF, discountRate, 10)
    
    totalNPVFCF = year1NPVFCF + year2NPVFCF + year3NPVFCF + year4NPVFCF + year5NPVFCF + year6NPVFCF + year7NPVFCF + year8NPVFCF + year9NPVFCF + year10NPVFCF
    year10FCFValue = year10FCFMultiplier * year10NPVFCF
    cashAndEquivalents = request.financial(syminfo.tickerid, 'CASH_N_EQUIVALENTS', 'FY')
    totalLiabilities = request.financial(syminfo.tickerid, 'TOTAL_LIABILITIES', 'FY')
    companyValue = totalNPVFCF + year10FCFValue + cashAndEquivalents - totalLiabilities
    
    numberOfShares = request.financial(syminfo.tickerid, 'TOTAL_SHARES_OUTSTANDING', 'FY')
    valuePerShare = companyValue / numberOfShares
    c = valuePerShare > 0 ? valuePerShare > close ? color.green : color.red : color.gray
    plot(valuePerShare > 0 ? valuePerShare : 0, color=c)
    
    
     
  2. 934944

    934944 Baldur's Gate Lão Làng GVN

    Tham gia ngày:
    13/8/06
    Bài viết:
    31,326
    Nơi ở:
    đà nẵng
  3. mashimuro

    mashimuro SEKIRO「隻腕の狼」 Lão Làng GVN

    Tham gia ngày:
    16/11/04
    Bài viết:
    22,158
    Compile?
     
  4. namchum2006

    namchum2006 Sonic the Hedgehog Lão Làng GVN

    Tham gia ngày:
    6/1/07
    Bài viết:
    4,781
    Nơi ở:
    Somewhere i belong
    2 script mình combine lại làm 1. Nhưng xong rồi, mình đã tìm ra lỗi :))
     
    mashimuro thích bài này.
  5. haiduong87

    haiduong87 Knee before Eden Lord Lão Làng GVN

    Tham gia ngày:
    20/5/04
    Bài viết:
    24,556
    Nơi ở:
    TP HCM
  6. T1nhLaG1

    T1nhLaG1 Star swallower ♞ Blade Knight ♞ Lão Làng GVN

    Tham gia ngày:
    2/11/09
    Bài viết:
    12,893
    Inbox cho mình 50k mobile
     

Chia sẻ trang này