从pine到python的翻译

2024-06-16 09:24:30 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的脚本是我从tradingview.com复制的。我试图理解这种语言,我想把脚本翻译成Python。链接页面为https://www.tradingview.com/chart/0AZVjf79/该指标称为埃勒斯瞬时趋势线,由Alex Orekhov(everget)创建

有几个句子使我困惑。1.函数“computeComponent(src,periodMult)”中的“periodMult”是单个值还是系列?2.在“_eit(src)”的函数中,所有的东西都是按级数计算的吗?因此,要翻译成Python,它们应该经过多次迭代?3.平滑周期有迭代吗?4.dcPeriod是单个值还是一个系列?5.“i=0到dcPeriod-1”的交互作用如何?方向和python不一样吗?谢谢你

 //@version=3
// Copyright (c) 2018-present, Alex Orekhov (everget)
// Ehlers Instantaneous Trendline script may be freely distributed under the MIT license.
study("Ehlers Instantaneous Trendline", shorttitle="EIT", overlay=true)

src = input(title="Source", type=source, defval=close)
applyFilling = input(title="Apply Ribbon Filling ?", type=bool, defval=true)

// Truncated Hilbert transform
hilbertTransform(src) =>
    0.0962 * src + 0.5769 * nz(src[2]) - 0.5769 * nz(src[4]) - 0.0962 * nz(src[6])

computeComponent(src, periodMult) =>
    hilbertTransform(src) * periodMult

computePart(src) =>
    0.2 * src + 0.8 * nz(src[1])

// FIR Filter
_fir(src) =>
    (4 * src + 3 * nz(src[1]) + 2 * nz(src[2]) + nz(src[3])) / 10

fir = _fir(src)

_eit(src) =>
    PI = 2 * asin(1)

    mesaPeriod = 0.0
    mesaPeriodMult = 0.075 * nz(mesaPeriod[1]) + 0.54

    detrender = 0.0
    detrender := computeComponent(fir, mesaPeriodMult)

    // Compute InPhase and Quadrature components
    I1 = nz(detrender[3])
    Q1 = computeComponent(detrender, mesaPeriodMult)

    // Advance the phase of I1 and Q1 by 90 degrees
    jI = computeComponent(I1, mesaPeriodMult)
    jQ = computeComponent(Q1, mesaPeriodMult)

    I2 = 0.0
    Q2 = 0.0

    // Phasor addition for 3 bar averaging
    I2 := I1 - jQ
    Q2 := Q1 + jI

    // Smooth the I and Q components before applying the discriminator
    I2 := computePart(I2)
    Q2 := computePart(Q2)

    // Homodyne Discriminator
    Re = I2 * nz(I2[1]) + Q2 * nz(Q2[1])
    Im = I2 * nz(Q2[1]) - Q2 * nz(I2[1])

    Re := computePart(Re)
    Im := computePart(Im)

    if Re != 0 and Im != 0
        mesaPeriod := 2 * PI / atan(Im / Re)

    if mesaPeriod > 1.5 * nz(mesaPeriod[1])
        mesaPeriod := 1.5 * nz(mesaPeriod[1])

    if mesaPeriod < 0.67 * nz(mesaPeriod[1])
        mesaPeriod := 0.67 * nz(mesaPeriod[1])

    if mesaPeriod < 6
        mesaPeriod := 6

    if mesaPeriod > 50
        mesaPeriod := 50

    mesaPeriod := 0.2 * mesaPeriod + 0.8 * nz(mesaPeriod[1])

    smoothPeriod = 0.0
    smoothPeriod := 0.33 * mesaPeriod + 0.67 * nz(smoothPeriod[1])

    // Compute Trendline as a SMA over the measured dominant cycle period
    dcPeriod = floor(smoothPeriod + 0.5)

    if dcPeriod < 1
        dcPeriod := 1

    itrend = 0.0

    for i = 0 to dcPeriod - 1
        itrend := itrend + src[i]

    if dcPeriod > 0
        itrend := itrend / dcPeriod

    eit = _fir(itrend)

    if n < 12
        eit := src
    eit

eit = _eit(src)

Tags: theresrcifnzq2firi2