简体   繁体   中英

Conver a loop from Pinescript to Python

Im tryng to convert this formula (WMA Moving Average) for loop in Python from Pinescript but for i to x not exist. I tried for i in range(x) but seems dont return same result. What exactly means to ? Documentation of Pinescript said means from i to x but i dont find the equivalent in Python

pine_wma(x, y) =>
    norm = 0.0
    sum  = 0.0
    for i = 0 to y - 1
       weight = (y - i) * y
       norm := norm + weight
       sum := sum + x[i] * weight
    sum / norm

plot(pine_wma(close, 15))

Python Code:

import pandas as pd

dataframe = pd.read_csv('dataframe.csv')
  
def formula_wma(x, y):
    list = []
    norm = 0.0
    sum = 0.0
    i = 0
    for i in range(y - 1):
        weight = (y - i) * y
        norm   = norm + weight
        sum    = sum + x[i] * weight
        _wma = sum / norm
        list.append(_wma)
        i += 1
    return list  

wma_slow   = formula_wma(dataframe['close'],45)
dataframe['wma_slow']   = pd.Series(wma_slow, index=dataframe.index[:len(wma_slow)])

print(dataframe['wma_slow'].to_string())

Output:

0       317.328133
[Skipping lines]
39      317.589010
40      317.449259
41      317.421662
42      317.378052
43      317.328133
44             NaN
45             NaN
[Skipping Lines]
2999           NaN
3000           NaN

First of all, don't reassign built-in names!

sum is a built-in function that calculates the summation of a sequence of numbers. So is list , it is a class constructor.

For example:

sum(range(10)) returns 45.

The above is equivalent to:

numbers = (0,1,2,3,4,5,6,7,8,9)
s = 0
for i in numbers: s += i

Second, don't increment the variable you use for looping inside the loop, unless you have a good reason for it.

That i += 1 at the end of the loop has no effect whatsoever, for loop automatically reassigns the name to the next item in the sequence, in this case the next item is incremented by one, so i automatically gets incremented.

Further, if there is anything using i after that line, they will break.

Lastly, the reason you are not getting the same result, is Python uses zero-based indexing and range excludes the stop.

I don't know about pine script, but from what you have written, from x to y must include y .

For example 0 to 10 in pine script will give you 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

But using range(10) :

print(list(range(10)))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Why? Because there are exactly ten numbers in the range you specified.

In the first example, there are actually eleven numbers. If you know your math, the number of terms in an arithmetic sequence is the difference between the maximum term and the minimum term divided by the increment plus one.

So how to solve your problem?

Remove - 1 after y in range!

Fixed code:

import pandas as pd

dataframe = pd.read_csv('dataframe.csv')
  
def formula_wma(x, y):
    lst = []
    norm = 0.0
    sum_ = 0.0
    i = 0
    for i in range(y):
        weight = (y - i) * y
        norm   = norm + weight
        sum_   = sum_ + x[i] * weight
        _wma = sum_ / norm
        lst.append(_wma)
    return lst  

wma_slow   = formula_wma(dataframe['close'],45)
dataframe['wma_slow']   = pd.Series(wma_slow, index=dataframe.index[:len(wma_slow)])

print(dataframe['wma_slow'].to_string())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM